mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 23:15:14 +00:00
repairip controller: use new ServiceCIDR API
This commit is contained in:
parent
881cf4d54f
commit
016c3c9e36
@ -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
|
||||
}
|
||||
@ -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()
|
||||
|
@ -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",
|
||||
@ -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",
|
||||
@ -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",
|
||||
@ -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",
|
||||
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user