-
Notifications
You must be signed in to change notification settings - Fork 288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Nutanix CCM ignore node IPs list #9072
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ | |
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net" | ||
"strings" | ||
|
||
"sigs.k8s.io/yaml" | ||
|
||
|
@@ -176,9 +179,12 @@ | |
|
||
failureDomains := generateNutanixFailureDomains(datacenterSpec.FailureDomains) | ||
|
||
ccmIgnoredNodeIPs := generateCcmIgnoredNodeIPsList(clusterSpec) | ||
|
||
values := map[string]interface{}{ | ||
"auditPolicy": auditPolicy, | ||
"apiServerExtraArgs": apiServerExtraArgs.ToPartialYaml(), | ||
"ccmIgnoredNodeIPs": ccmIgnoredNodeIPs, | ||
"cloudProviderImage": versionsBundle.Nutanix.CloudProvider.VersionedImage(), | ||
"clusterName": clusterSpec.Cluster.Name, | ||
"controlPlaneEndpointIp": clusterSpec.Cluster.Spec.ControlPlaneConfiguration.Endpoint.Host, | ||
|
@@ -568,3 +574,118 @@ | |
} | ||
return failureDomains | ||
} | ||
|
||
func incrementIP(ip net.IP) { | ||
for i := len(ip) - 1; i >= 0; i-- { | ||
ip[i]++ | ||
if ip[i] > 0 { | ||
break | ||
} | ||
} | ||
} | ||
|
||
func compareIP(ip1, ip2 net.IP) (int, error) { | ||
if len(ip1) != len(ip2) { | ||
return -1, fmt.Errorf("IP addresses are not the same protocol") | ||
} | ||
|
||
for i := 0; i < len(ip1); i++ { | ||
if ip1[i] < ip2[i] { | ||
return -1, nil | ||
} | ||
if ip1[i] > ip2[i] { | ||
return 1, nil | ||
} | ||
} | ||
|
||
return 0, nil | ||
} | ||
|
||
func addCIDRToIgnoredNodeIPsList(cidr string, result []string) []string { | ||
ip, ipNet, err := net.ParseCIDR(cidr) | ||
if err != nil { | ||
// log error and continue | ||
log.Printf("error parsing CIDR %s: %v", cidr, err) | ||
return result | ||
} | ||
|
||
// Add all ip addresses in the range to the list | ||
for ip := ip.Mask(ipNet.Mask); ipNet.Contains(ip); incrementIP(ip) { | ||
if ip != nil { | ||
result = append(result, ip.String()) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
func addIPRangeToIgnoredNodeIPsList(ipRangeStr string, result []string) []string { | ||
// Parse the range | ||
ipRange := strings.Split(ipRangeStr, "-") | ||
if len(ipRange) != 2 { | ||
// log error and return | ||
log.Printf("error parsing range %s: expected 2 values, got %d", ipRangeStr, len(ipRange)) | ||
return result | ||
} | ||
|
||
// Parse the start and end of the range | ||
start := net.ParseIP(strings.TrimSpace(ipRange[0])) | ||
end := net.ParseIP(strings.TrimSpace(ipRange[1])) | ||
if start == nil || end == nil { | ||
// log error and return | ||
log.Printf("error parsing range %s: invalid IP address", ipRangeStr) | ||
return result | ||
} | ||
|
||
cmp, err := compareIP(start, end) | ||
if err != nil { | ||
// log error and return | ||
log.Printf("error comparing IP addresses %s and %s: %v", start.String(), end.String(), err) | ||
return result | ||
} | ||
|
||
if cmp >= 0 { | ||
// swap start and end if start is greater than end | ||
start, end = end, start | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above, already validated in validator, can be removed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
|
||
// Add all ip addresses in the range to the list | ||
for ip := start; !ip.Equal(end); incrementIP(ip) { | ||
result = append(result, ip.String()) | ||
} | ||
|
||
result = append(result, end.String()) | ||
|
||
return result | ||
} | ||
|
||
func addIPAddressToIgnoredNodeIPsList(ipAddrStr string, result []string) []string { | ||
ip := net.ParseIP(ipAddrStr) | ||
if ip == nil { | ||
// log error and return | ||
log.Printf("error parsing IP address %s", ipAddrStr) | ||
return result | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here as well, already validated, can be removed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
|
||
result = append(result, ip.String()) | ||
return result | ||
} | ||
|
||
func generateCcmIgnoredNodeIPsList(clusterSpec *cluster.Spec) []string { | ||
// Add the kube-vip IP address to the list | ||
result := []string{clusterSpec.Cluster.Spec.ControlPlaneConfiguration.Endpoint.Host} | ||
|
||
// Add the IP addresses, IP ranges and CIDRs to the list from the NutanixDatacenter spec CcmExcludeNodeIPs | ||
for _, IPAddrOrRange := range clusterSpec.NutanixDatacenter.Spec.CcmExcludeNodeIPs { | ||
addrOrRange := strings.TrimSpace(IPAddrOrRange) | ||
if strings.Contains(addrOrRange, "/") { | ||
result = addCIDRToIgnoredNodeIPsList(addrOrRange, result) | ||
} else if strings.Contains(addrOrRange, "-") { | ||
result = addIPRangeToIgnoredNodeIPsList(addrOrRange, result) | ||
} else { | ||
result = addIPAddressToIgnoredNodeIPsList(addrOrRange, result) | ||
} | ||
} | ||
|
||
return result | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
apiVersion: anywhere.eks.amazonaws.com/v1alpha1 | ||
kind: NutanixDatacenterConfig | ||
metadata: | ||
name: eksa-unit-test | ||
namespace: default | ||
spec: | ||
endpoint: "prism.nutanix.com" | ||
port: 9440 | ||
credentialRef: | ||
kind: Secret | ||
name: "nutanix-credentials" | ||
ccmExcludeNodeIPs: | ||
- 10.0.0.1 | ||
- 10.0.0.0/24 | ||
- 10.0.0.10-10.0.0.30 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are already validating this in the validator. Let's remove it from here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed