Skip to content

Commit

Permalink
repairip controller: use new ServiceCIDR API
Browse files Browse the repository at this point in the history
  • Loading branch information
aojea committed Oct 31, 2023
1 parent 881cf4d commit 016c3c9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
15 changes: 7 additions & 8 deletions pkg/registry/core/service/ipallocator/controller/repairip.go
Expand Up @@ -367,7 +367,7 @@ func (r *RepairIPAddress) syncService(key string) error {
r.muTree.Unlock()
if len(prefixes) == 0 {
// ClusterIP is out of range
r.recorder.Eventf(svc, nil, v1.EventTypeWarning, "ClusterIPOutOfRange", "ClusterIPAllocation", "Cluster IP [%v]: %s is not within the configured Service CIDR; please recreate service", family, ip)
r.recorder.Eventf(svc, nil, v1.EventTypeWarning, "ClusterIPOutOfRange", "ClusterIPAllocation", "Cluster IP [%v]: %s is not within any configured Service CIDR; please recreate service", family, ip)
runtime.HandleError(fmt.Errorf("the ClusterIP [%v]: %s for Service %s/%s is not within any service CIDR; please recreate", family, ip, svc.Namespace, svc.Name))
continue
}
Expand Down Expand Up @@ -585,18 +585,17 @@ func (r *RepairIPAddress) handleCIDRErr(err error, key interface{}) {

// syncCIDRs rebuilds the radix tree based from the informers cache
func (r *RepairIPAddress) syncCIDRs() error {
cidrList, err := r.serviceCIDRLister.List(labels.Everything())
serviceCIDRList, err := r.serviceCIDRLister.List(labels.Everything())
if err != nil {
return err
}

tree := iptree.New[string]()
for _, cidr := range cidrList {
if prefix, err := netip.ParsePrefix(cidr.Spec.IPv4); err == nil { // if is empty err will not be nil
tree.InsertPrefix(prefix, cidr.Name)
}
if prefix, err := netip.ParsePrefix(cidr.Spec.IPv6); err == nil { // if is empty err will not be nil
tree.InsertPrefix(prefix, cidr.Name)
for _, serviceCIDR := range serviceCIDRList {
for _, cidr := range serviceCIDR.Spec.CIDRs {
if prefix, err := netip.ParsePrefix(cidr); err == nil { // it can not fail since is already validated
tree.InsertPrefix(prefix, serviceCIDR.Name)
}
}
}
r.muTree.Lock()
Expand Down
19 changes: 10 additions & 9 deletions pkg/registry/core/service/ipallocator/controller/repairip_test.go
Expand Up @@ -213,7 +213,7 @@ func TestRepairServiceIP(t *testing.T) {
},
expectedIPs: []string{"2001:db8::10"},
actions: [][]string{},
events: []string{"Warning ClusterIPOutOfRange Cluster IP [IPv4]: 192.168.1.1 is not within the configured Service CIDR; please recreate service"},
events: []string{"Warning ClusterIPOutOfRange Cluster IP [IPv4]: 192.168.1.1 is not within any configured Service CIDR; please recreate service"},
},
{
name: "one IP orphan",
Expand All @@ -237,7 +237,7 @@ func TestRepairServiceIP(t *testing.T) {
},
expectedIPs: []string{"10.0.0.0"},
actions: [][]string{},
events: []string{"Warning ClusterIPOutOfRange Cluster IP [IPv4]: 10.0.0.0 is not within the configured Service CIDR; please recreate service"},
events: []string{"Warning ClusterIPOutOfRange Cluster IP [IPv4]: 10.0.0.0 is not within any configured Service CIDR; please recreate service"},
},
{
name: "one IP out of range matching the broadcast address",
Expand All @@ -250,7 +250,7 @@ func TestRepairServiceIP(t *testing.T) {
},
expectedIPs: []string{"10.0.255.255"},
actions: [][]string{},
events: []string{"Warning ClusterIPOutOfRange Cluster IP [IPv4]: 10.0.255.255 is not within the configured Service CIDR; please recreate service"},
events: []string{"Warning ClusterIPOutOfRange Cluster IP [IPv4]: 10.0.255.255 is not within any configured Service CIDR; please recreate service"},
},
{
name: "one IPv6 out of range matching the subnet address",
Expand All @@ -263,7 +263,7 @@ func TestRepairServiceIP(t *testing.T) {
},
expectedIPs: []string{"2001:db8::"},
actions: [][]string{},
events: []string{"Warning ClusterIPOutOfRange Cluster IP [IPv6]: 2001:db8:: is not within the configured Service CIDR; please recreate service"},
events: []string{"Warning ClusterIPOutOfRange Cluster IP [IPv6]: 2001:db8:: is not within any configured Service CIDR; please recreate service"},
},
{
name: "one IPv6 matching the broadcast address",
Expand Down Expand Up @@ -526,15 +526,16 @@ func newService(name string, ips []string) *v1.Service {
return svc
}

func newServiceCIDR(name, ipv4, ipv6 string) *networkingv1alpha1.ServiceCIDR {
func newServiceCIDR(name, primary, secondary string) *networkingv1alpha1.ServiceCIDR {
serviceCIDR := &networkingv1alpha1.ServiceCIDR{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: networkingv1alpha1.ServiceCIDRSpec{
IPv4: ipv4,
IPv6: ipv6,
},
Spec: networkingv1alpha1.ServiceCIDRSpec{},
}
serviceCIDR.Spec.CIDRs = append(serviceCIDR.Spec.CIDRs, primary)
if secondary != "" {
serviceCIDR.Spec.CIDRs = append(serviceCIDR.Spec.CIDRs, secondary)
}
return serviceCIDR
}
Expand Down

0 comments on commit 016c3c9

Please sign in to comment.