|
4 | 4 | "encoding/base64"
|
5 | 5 | "encoding/json"
|
6 | 6 | "fmt"
|
| 7 | + "net" |
| 8 | + "strings" |
7 | 9 |
|
8 | 10 | "sigs.k8s.io/yaml"
|
9 | 11 |
|
@@ -176,9 +178,12 @@ func buildTemplateMapCP(
|
176 | 178 |
|
177 | 179 | failureDomains := generateNutanixFailureDomains(datacenterSpec.FailureDomains)
|
178 | 180 |
|
| 181 | + ccmIgnoredNodeIPs := generateCcmIgnoredNodeIPsList(clusterSpec) |
| 182 | + |
179 | 183 | values := map[string]interface{}{
|
180 | 184 | "auditPolicy": auditPolicy,
|
181 | 185 | "apiServerExtraArgs": apiServerExtraArgs.ToPartialYaml(),
|
| 186 | + "ccmIgnoredNodeIPs": ccmIgnoredNodeIPs, |
182 | 187 | "cloudProviderImage": versionsBundle.Nutanix.CloudProvider.VersionedImage(),
|
183 | 188 | "clusterName": clusterSpec.Cluster.Name,
|
184 | 189 | "controlPlaneEndpointIp": clusterSpec.Cluster.Spec.ControlPlaneConfiguration.Endpoint.Host,
|
@@ -568,3 +573,89 @@ func generateNutanixFailureDomains(eksNutanixFailureDomains []v1alpha1.NutanixDa
|
568 | 573 | }
|
569 | 574 | return failureDomains
|
570 | 575 | }
|
| 576 | + |
| 577 | +func incrementIP(ip net.IP) { |
| 578 | + for i := len(ip) - 1; i >= 0; i-- { |
| 579 | + ip[i]++ |
| 580 | + if ip[i] > 0 { |
| 581 | + break |
| 582 | + } |
| 583 | + } |
| 584 | +} |
| 585 | + |
| 586 | +func compareIP(ip1, ip2 net.IP) (int, error) { |
| 587 | + if len(ip1) != len(ip2) { |
| 588 | + return -1, fmt.Errorf("IP addresses are not the same protocol") |
| 589 | + } |
| 590 | + |
| 591 | + for i := 0; i < len(ip1); i++ { |
| 592 | + if ip1[i] < ip2[i] { |
| 593 | + return -1, nil |
| 594 | + } |
| 595 | + if ip1[i] > ip2[i] { |
| 596 | + return 1, nil |
| 597 | + } |
| 598 | + } |
| 599 | + |
| 600 | + return 0, nil |
| 601 | +} |
| 602 | + |
| 603 | +func addCIDRToIgnoredNodeIPsList(cidr string, result []string) []string { |
| 604 | + ip, ipNet, _ := net.ParseCIDR(cidr) |
| 605 | + |
| 606 | + // Add all ip addresses in the range to the list |
| 607 | + for ip := ip.Mask(ipNet.Mask); ipNet.Contains(ip); incrementIP(ip) { |
| 608 | + if ip != nil { |
| 609 | + result = append(result, ip.String()) |
| 610 | + } |
| 611 | + } |
| 612 | + |
| 613 | + return result |
| 614 | +} |
| 615 | + |
| 616 | +func addIPRangeToIgnoredNodeIPsList(ipRangeStr string, result []string) []string { |
| 617 | + // Parse the range |
| 618 | + ipRange := strings.Split(ipRangeStr, "-") |
| 619 | + |
| 620 | + // Parse the start and end of the range |
| 621 | + start := net.ParseIP(strings.TrimSpace(ipRange[0])) |
| 622 | + end := net.ParseIP(strings.TrimSpace(ipRange[1])) |
| 623 | + |
| 624 | + cmp, _ := compareIP(start, end) |
| 625 | + if cmp >= 0 { |
| 626 | + // swap start and end if start is greater than end |
| 627 | + start, end = end, start |
| 628 | + } |
| 629 | + |
| 630 | + // Add all ip addresses in the range to the list |
| 631 | + for ip := start; !ip.Equal(end); incrementIP(ip) { |
| 632 | + result = append(result, ip.String()) |
| 633 | + } |
| 634 | + |
| 635 | + result = append(result, end.String()) |
| 636 | + |
| 637 | + return result |
| 638 | +} |
| 639 | + |
| 640 | +func addIPAddressToIgnoredNodeIPsList(ipAddrStr string, result []string) []string { |
| 641 | + result = append(result, ipAddrStr) |
| 642 | + return result |
| 643 | +} |
| 644 | + |
| 645 | +func generateCcmIgnoredNodeIPsList(clusterSpec *cluster.Spec) []string { |
| 646 | + // Add the kube-vip IP address to the list |
| 647 | + result := []string{clusterSpec.Cluster.Spec.ControlPlaneConfiguration.Endpoint.Host} |
| 648 | + |
| 649 | + for _, IPAddrOrRange := range clusterSpec.NutanixDatacenter.Spec.CcmExcludeNodeIPs { |
| 650 | + addrOrRange := strings.TrimSpace(IPAddrOrRange) |
| 651 | + if strings.Contains(addrOrRange, "/") { |
| 652 | + result = addCIDRToIgnoredNodeIPsList(addrOrRange, result) |
| 653 | + } else if strings.Contains(addrOrRange, "-") { |
| 654 | + result = addIPRangeToIgnoredNodeIPsList(addrOrRange, result) |
| 655 | + } else { |
| 656 | + result = addIPAddressToIgnoredNodeIPsList(addrOrRange, result) |
| 657 | + } |
| 658 | + } |
| 659 | + |
| 660 | + return result |
| 661 | +} |
0 commit comments