Skip to content

Commit

Permalink
fix: handle the case when the gateway suffix (in HA mode) ends up bei…
Browse files Browse the repository at this point in the history
…ng a number (#16)
  • Loading branch information
raman2805 committed Apr 11, 2023
1 parent 6d3d4f5 commit f6ee7d4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
6 changes: 6 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,10 @@ func Test_Misc(t *testing.T) {

t.Setenv("INSTANCE_ID", "prousmtusmt-v0-rs")
require.Equal(t, "", GetInstanceID())

t.Setenv("INSTANCE_ID", "prousmtusmt-v0-rs-gw-10")
require.Equal(t, "10", GetInstanceID())

t.Setenv("INSTANCE_ID", "prousmtusmt-v0-rs-gw-ha-12-234234-10")
require.Equal(t, "12", GetInstanceID())
}
21 changes: 10 additions & 11 deletions config/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package config

import (
"os"
"strconv"
"regexp"
"strings"
)

var (
regexGwHa = regexp.MustCompile(`^.*-gw-ha-\d+-\w+-\w+$`)
regexGwNonHaOrProcessor = regexp.MustCompile(`^.*-\d+$`)
)

// GetWorkspaceToken returns the workspace token provided in the environment variables
// Env variable CONFIG_BACKEND_TOKEN is deprecating soon
// WORKSPACE_TOKEN is newly introduced. This will override CONFIG_BACKEND_TOKEN
Expand Down Expand Up @@ -38,16 +43,10 @@ func GetInstanceID() string {
// This handles 2 kinds of server instances
// a) Processor OR Gateway running in non HA mod where the instance name ends with the index
// b) Gateway running in HA mode, where the instance name is of the form *-gw-ha-<index>-<statefulset-id>-<pod-id>
potentialServerIndexIndices := []int{length - 1, length - 3}
for _, i := range potentialServerIndexIndices {
if i < 0 {
continue
}
serverIndex := instanceArr[i]
_, err := strconv.Atoi(serverIndex)
if err == nil {
return serverIndex
}
if (regexGwHa.MatchString(instance)) && (length > 3) {
return instanceArr[length-3]
} else if (regexGwNonHaOrProcessor.MatchString(instance)) && (length > 1) {
return instanceArr[length-1]
}
return ""
}
Expand Down

0 comments on commit f6ee7d4

Please sign in to comment.