forked from haproxytech/kubernetes-ingress
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package zone | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
"strings" | ||
) | ||
|
||
var controllerIP string | ||
var controllerZone string | ||
var zonesInfo string | ||
var zonesCrossEnabled bool = true | ||
|
||
func init() { | ||
controllerIP = os.Getenv("POD_IP") | ||
zonesInfo = os.Getenv("ZONES_INFO") | ||
zonesCrossEnabled = !(strings.ToLower(os.Getenv("ZONES_CROSS_TRAFFIC_ENABLED")) == "false") | ||
|
||
if controllerIP == "" || zonesInfo == "" { | ||
zonesCrossEnabled = true | ||
return | ||
} | ||
|
||
controllerZone = getZoneFromIP(controllerIP, zonesInfo) | ||
} | ||
|
||
// This function is used when haproxy configure a server in backend | ||
func IsBackupEnabledForThisIP(address string) bool { | ||
if zonesCrossEnabled || address == "" || address == "127.0.0.1" { | ||
return false | ||
} | ||
srvZone := getZoneFromIP(address, zonesInfo) | ||
return srvZone != controllerZone | ||
} | ||
|
||
// Private; Auxiliary functions | ||
func getZoneFromIP(address string, zoneSubnets string) string { | ||
input := strings.ReplaceAll(zoneSubnets, " ", "") | ||
subnetGroups := strings.Split(input, ";") | ||
|
||
for _, group := range subnetGroups { | ||
parts := strings.Split(group, ":") | ||
if len(parts) != 2 || parts[1] == "" { | ||
return "unknown" | ||
} | ||
|
||
zoneName := parts[0] | ||
subnets := strings.Split(parts[1], ",") | ||
|
||
for _, subnet := range subnets { | ||
if ok, _ := isIPInSubnet(address, subnet); ok { | ||
return zoneName | ||
} | ||
} | ||
} | ||
|
||
return "unknown" | ||
} | ||
|
||
func isIPInSubnet(ipStr, subnetStr string) (bool, error) { | ||
// Parseamos la IP | ||
ip := net.ParseIP(ipStr) | ||
if ip == nil { | ||
return false, fmt.Errorf("IP inválida: %s", ipStr) | ||
} | ||
|
||
// Parseamos la subnet (dirección IP + máscara) | ||
_, subnet, err := net.ParseCIDR(subnetStr) | ||
if err != nil { | ||
return false, fmt.Errorf("subred inválida: %s", subnetStr) | ||
} | ||
|
||
// Comprobamos si la IP está dentro de la subred | ||
return subnet.Contains(ip), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package zone | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGetZoneFromIp(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
address string | ||
zoneSubnets string | ||
expected string | ||
}{ | ||
{ | ||
name: "IP belongs to eu-south-2a", | ||
address: "192.168.2.1", | ||
zoneSubnets: "eu-south-2a: 192.168.1.0/24, 192.168.2.0/24; eu-south-2b: 192.168.3.0/24", | ||
expected: "eu-south-2a", | ||
}, | ||
{ | ||
name: "IP belongs to eu-south-2b", | ||
address: "192.168.3.10", | ||
zoneSubnets: "eu-south-2a: 192.168.1.0/24, 192.168.2.0/24; eu-south-2b: 192.168.3.0/24", | ||
expected: "eu-south-2b", | ||
}, | ||
{ | ||
name: "IP not in any subnet", | ||
address: "10.0.0.1", | ||
zoneSubnets: "eu-south-2a: 192.168.1.0/24, 192.168.2.0/24; eu-south-2b: 192.168.3.0/24", | ||
expected: "unknown", | ||
}, | ||
{ | ||
name: "Malformed input - no subnet", | ||
address: "192.168.2.1", | ||
zoneSubnets: "eu-south-2a:;eu-south-2b:192.168.2.0/24", | ||
expected: "unknown", | ||
}, | ||
{ | ||
name: "Malformed input - no subnet", | ||
address: "192.168.2.1", | ||
zoneSubnets: "eu-south-2a;eu-south-2b:192.168.2.0/24", | ||
expected: "unknown", | ||
}, | ||
{ | ||
name: "IP belongs to overlapping subnets, first match", | ||
address: "192.167.4.10", | ||
zoneSubnets: "eu-south-2a:192.167.4.0/24;eu-south-2b:192.167.4.0/16", | ||
expected: "eu-south-2a", | ||
}, | ||
{ | ||
name: "Empty Data", | ||
address: "192.167.4.10", | ||
zoneSubnets: " ", | ||
expected: "unknown", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := getZoneFromIP(tt.address, tt.zoneSubnets) | ||
if result != tt.expected { | ||
t.Errorf("expected %s, got %s", tt.expected, result) | ||
} | ||
}) | ||
} | ||
} |