diff --git a/pkg/loxinet/cluster.go b/pkg/loxinet/cluster.go index a0d3bfcb..dec6c46c 100644 --- a/pkg/loxinet/cluster.go +++ b/pkg/loxinet/cluster.go @@ -21,6 +21,8 @@ import ( "fmt" "net" "os" + "regexp" + "strconv" "time" nlp "github.com/loxilb-io/loxilb/api/loxinlp" @@ -144,6 +146,23 @@ func (ci *CIStateH) CISpawn() { } } +func parseInstance(input string) (int, error) { + // Define a regex pattern to match "-inst" + re := regexp.MustCompile(`^[a-zA-Z0-9_-]+-inst(\d+)$`) + + matches := re.FindStringSubmatch(input) + if matches == nil || len(matches) < 2 { + return 0, fmt.Errorf("no match found in input: %s", input) + } + + number, err := strconv.Atoi(matches[1]) + if err != nil { + return 0, fmt.Errorf("failed to parse number: %v", err) + } + + return number, nil +} + // CIStateGetInst - routine to get HA state func (h *CIStateH) CIStateGetInst(inst string) (string, error) { @@ -151,6 +170,18 @@ func (h *CIStateH) CIStateGetInst(inst string) (string, error) { return ci.StateStr, nil } + if inst == cmn.CIDefault { + for ciStr, ci := range h.ClusterMap { + instNum, err := parseInstance(ciStr) + if err != nil { + continue + } + if instNum == 0 { + return ci.StateStr, nil + } + } + } + return "NOT_DEFINED", errors.New("not found") }