repairip controller: use new ServiceCIDR API

This commit is contained in:
Antonio Ojea 2023-10-29 18:30:27 +00:00
parent 881cf4d54f
commit 016c3c9e36
2 changed files with 17 additions and 17 deletions

View File

@ -367,7 +367,7 @@ func (r *RepairIPAddress) syncService(key string) error {
r.muTree.Unlock() r.muTree.Unlock()
if len(prefixes) == 0 { if len(prefixes) == 0 {
// ClusterIP is out of range // 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)) 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 continue
} }
@ -585,18 +585,17 @@ func (r *RepairIPAddress) handleCIDRErr(err error, key interface{}) {
// syncCIDRs rebuilds the radix tree based from the informers cache // syncCIDRs rebuilds the radix tree based from the informers cache
func (r *RepairIPAddress) syncCIDRs() error { func (r *RepairIPAddress) syncCIDRs() error {
cidrList, err := r.serviceCIDRLister.List(labels.Everything()) serviceCIDRList, err := r.serviceCIDRLister.List(labels.Everything())
if err != nil { if err != nil {
return err return err
} }
tree := iptree.New[string]() tree := iptree.New[string]()
for _, cidr := range cidrList { for _, serviceCIDR := range serviceCIDRList {
if prefix, err := netip.ParsePrefix(cidr.Spec.IPv4); err == nil { // if is empty err will not be nil for _, cidr := range serviceCIDR.Spec.CIDRs {
tree.InsertPrefix(prefix, cidr.Name) if prefix, err := netip.ParsePrefix(cidr); err == nil { // it can not fail since is already validated
} tree.InsertPrefix(prefix, serviceCIDR.Name)
if prefix, err := netip.ParsePrefix(cidr.Spec.IPv6); err == nil { // if is empty err will not be nil }
tree.InsertPrefix(prefix, cidr.Name)
} }
} }
r.muTree.Lock() r.muTree.Lock()

View File

@ -213,7 +213,7 @@ func TestRepairServiceIP(t *testing.T) {
}, },
expectedIPs: []string{"2001:db8::10"}, expectedIPs: []string{"2001:db8::10"},
actions: [][]string{}, 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", name: "one IP orphan",
@ -237,7 +237,7 @@ func TestRepairServiceIP(t *testing.T) {
}, },
expectedIPs: []string{"10.0.0.0"}, expectedIPs: []string{"10.0.0.0"},
actions: [][]string{}, 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", name: "one IP out of range matching the broadcast address",
@ -250,7 +250,7 @@ func TestRepairServiceIP(t *testing.T) {
}, },
expectedIPs: []string{"10.0.255.255"}, expectedIPs: []string{"10.0.255.255"},
actions: [][]string{}, 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", name: "one IPv6 out of range matching the subnet address",
@ -263,7 +263,7 @@ func TestRepairServiceIP(t *testing.T) {
}, },
expectedIPs: []string{"2001:db8::"}, expectedIPs: []string{"2001:db8::"},
actions: [][]string{}, 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", name: "one IPv6 matching the broadcast address",
@ -526,15 +526,16 @@ func newService(name string, ips []string) *v1.Service {
return svc return svc
} }
func newServiceCIDR(name, ipv4, ipv6 string) *networkingv1alpha1.ServiceCIDR { func newServiceCIDR(name, primary, secondary string) *networkingv1alpha1.ServiceCIDR {
serviceCIDR := &networkingv1alpha1.ServiceCIDR{ serviceCIDR := &networkingv1alpha1.ServiceCIDR{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: name, Name: name,
}, },
Spec: networkingv1alpha1.ServiceCIDRSpec{ Spec: networkingv1alpha1.ServiceCIDRSpec{},
IPv4: ipv4, }
IPv6: ipv6, serviceCIDR.Spec.CIDRs = append(serviceCIDR.Spec.CIDRs, primary)
}, if secondary != "" {
serviceCIDR.Spec.CIDRs = append(serviceCIDR.Spec.CIDRs, secondary)
} }
return serviceCIDR return serviceCIDR
} }