default_servicecidr_controller: use new ServiceCIDR API
Change-Id: I2c53815136e0d985959420911339aacd62e01e78
This commit is contained in:
		@@ -19,6 +19,7 @@ package defaultservicecidr
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"net"
 | 
						"net"
 | 
				
			||||||
 | 
						"reflect"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	v1 "k8s.io/api/core/v1"
 | 
						v1 "k8s.io/api/core/v1"
 | 
				
			||||||
@@ -38,7 +39,6 @@ import (
 | 
				
			|||||||
	"k8s.io/client-go/tools/cache"
 | 
						"k8s.io/client-go/tools/cache"
 | 
				
			||||||
	"k8s.io/client-go/tools/record"
 | 
						"k8s.io/client-go/tools/record"
 | 
				
			||||||
	"k8s.io/klog/v2"
 | 
						"k8s.io/klog/v2"
 | 
				
			||||||
	netutils "k8s.io/utils/net"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
@@ -64,15 +64,9 @@ func NewController(
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// obtain configuration from flags
 | 
						// obtain configuration from flags
 | 
				
			||||||
	if netutils.IsIPv4CIDR(&primaryRange) {
 | 
						c.cidrs = append(c.cidrs, primaryRange.String())
 | 
				
			||||||
		c.ipv4CIDR = primaryRange.String()
 | 
						if secondaryRange.IP != nil {
 | 
				
			||||||
	} else if netutils.IsIPv4CIDR(&secondaryRange) {
 | 
							c.cidrs = append(c.cidrs, secondaryRange.String())
 | 
				
			||||||
		c.ipv4CIDR = secondaryRange.String()
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if netutils.IsIPv6CIDR(&primaryRange) {
 | 
					 | 
				
			||||||
		c.ipv6CIDR = primaryRange.String()
 | 
					 | 
				
			||||||
	} else if netutils.IsIPv6CIDR(&secondaryRange) {
 | 
					 | 
				
			||||||
		c.ipv6CIDR = secondaryRange.String()
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// instead of using the shared informers from the controlplane instance, we construct our own informer
 | 
						// instead of using the shared informers from the controlplane instance, we construct our own informer
 | 
				
			||||||
	// because we need such a small subset of the information available, only the kubernetes.default ServiceCIDR
 | 
						// because we need such a small subset of the information available, only the kubernetes.default ServiceCIDR
 | 
				
			||||||
@@ -95,8 +89,7 @@ func NewController(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Controller manages selector-based service ipAddress.
 | 
					// Controller manages selector-based service ipAddress.
 | 
				
			||||||
type Controller struct {
 | 
					type Controller struct {
 | 
				
			||||||
	ipv4CIDR string
 | 
						cidrs []string // order matters, first cidr defines the default IP family
 | 
				
			||||||
	ipv6CIDR string
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	client           clientset.Interface
 | 
						client           clientset.Interface
 | 
				
			||||||
	eventBroadcaster record.EventBroadcaster
 | 
						eventBroadcaster record.EventBroadcaster
 | 
				
			||||||
@@ -152,14 +145,13 @@ func (c *Controller) sync() {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// default ServiceCIDR does not exist
 | 
						// default ServiceCIDR does not exist
 | 
				
			||||||
	klog.Infof("Creating default ServiceCIDR, ipv4: %q ipv6: %q", c.ipv4CIDR, c.ipv6CIDR)
 | 
						klog.Infof("Creating default ServiceCIDR with CIDRs: %v", c.cidrs)
 | 
				
			||||||
	serviceCIDR = &networkingapiv1alpha1.ServiceCIDR{
 | 
						serviceCIDR = &networkingapiv1alpha1.ServiceCIDR{
 | 
				
			||||||
		ObjectMeta: metav1.ObjectMeta{
 | 
							ObjectMeta: metav1.ObjectMeta{
 | 
				
			||||||
			Name: DefaultServiceCIDRName,
 | 
								Name: DefaultServiceCIDRName,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		Spec: networkingapiv1alpha1.ServiceCIDRSpec{
 | 
							Spec: networkingapiv1alpha1.ServiceCIDRSpec{
 | 
				
			||||||
			IPv4: c.ipv4CIDR,
 | 
								CIDRs: c.cidrs,
 | 
				
			||||||
			IPv6: c.ipv6CIDR,
 | 
					 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	serviceCIDR, err = c.client.NetworkingV1alpha1().ServiceCIDRs().Create(context.Background(), serviceCIDR, metav1.CreateOptions{})
 | 
						serviceCIDR, err = c.client.NetworkingV1alpha1().ServiceCIDRs().Create(context.Background(), serviceCIDR, metav1.CreateOptions{})
 | 
				
			||||||
@@ -201,8 +193,7 @@ func (c *Controller) syncStatus(serviceCIDR *networkingapiv1alpha1.ServiceCIDR)
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// set status to ready if the ServiceCIDR matches this configuration
 | 
						// set status to ready if the ServiceCIDR matches this configuration
 | 
				
			||||||
	if c.ipv4CIDR == serviceCIDR.Spec.IPv4 &&
 | 
						if reflect.DeepEqual(c.cidrs, serviceCIDR.Spec.CIDRs) {
 | 
				
			||||||
		c.ipv6CIDR == serviceCIDR.Spec.IPv6 {
 | 
					 | 
				
			||||||
		klog.Infof("Setting default ServiceCIDR condition Ready to True")
 | 
							klog.Infof("Setting default ServiceCIDR condition Ready to True")
 | 
				
			||||||
		svcApplyStatus := networkingapiv1alpha1apply.ServiceCIDRStatus().WithConditions(
 | 
							svcApplyStatus := networkingapiv1alpha1apply.ServiceCIDRStatus().WithConditions(
 | 
				
			||||||
			metav1apply.Condition().
 | 
								metav1apply.Condition().
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -52,8 +52,7 @@ func newController(t *testing.T, objects []*networkingapiv1alpha1.ServiceCIDR) (
 | 
				
			|||||||
	c := &Controller{
 | 
						c := &Controller{
 | 
				
			||||||
		client:             client,
 | 
							client:             client,
 | 
				
			||||||
		interval:           time.Second,
 | 
							interval:           time.Second,
 | 
				
			||||||
		ipv4CIDR:           defaultIPv4CIDR,
 | 
							cidrs:              []string{defaultIPv4CIDR, defaultIPv6CIDR},
 | 
				
			||||||
		ipv6CIDR:           defaultIPv6CIDR,
 | 
					 | 
				
			||||||
		eventRecorder:      record.NewFakeRecorder(100),
 | 
							eventRecorder:      record.NewFakeRecorder(100),
 | 
				
			||||||
		serviceCIDRLister:  serviceCIDRInformer.Lister(),
 | 
							serviceCIDRLister:  serviceCIDRInformer.Lister(),
 | 
				
			||||||
		serviceCIDRsSynced: func() bool { return true },
 | 
							serviceCIDRsSynced: func() bool { return true },
 | 
				
			||||||
@@ -81,8 +80,7 @@ func TestControllerSync(t *testing.T) {
 | 
				
			|||||||
						Name: DefaultServiceCIDRName,
 | 
											Name: DefaultServiceCIDRName,
 | 
				
			||||||
					},
 | 
										},
 | 
				
			||||||
					Spec: networkingapiv1alpha1.ServiceCIDRSpec{
 | 
										Spec: networkingapiv1alpha1.ServiceCIDRSpec{
 | 
				
			||||||
						IPv4: defaultIPv4CIDR,
 | 
											CIDRs: []string{defaultIPv4CIDR, defaultIPv6CIDR},
 | 
				
			||||||
						IPv6: defaultIPv6CIDR,
 | 
					 | 
				
			||||||
					},
 | 
										},
 | 
				
			||||||
				},
 | 
									},
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
@@ -96,8 +94,7 @@ func TestControllerSync(t *testing.T) {
 | 
				
			|||||||
						Name: DefaultServiceCIDRName,
 | 
											Name: DefaultServiceCIDRName,
 | 
				
			||||||
					},
 | 
										},
 | 
				
			||||||
					Spec: networkingapiv1alpha1.ServiceCIDRSpec{
 | 
										Spec: networkingapiv1alpha1.ServiceCIDRSpec{
 | 
				
			||||||
						IPv4: "",
 | 
											CIDRs: []string{"fd00::/112"},
 | 
				
			||||||
						IPv6: "fd00::/112",
 | 
					 | 
				
			||||||
					},
 | 
										},
 | 
				
			||||||
				},
 | 
									},
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
@@ -110,8 +107,7 @@ func TestControllerSync(t *testing.T) {
 | 
				
			|||||||
						Name: DefaultServiceCIDRName,
 | 
											Name: DefaultServiceCIDRName,
 | 
				
			||||||
					},
 | 
										},
 | 
				
			||||||
					Spec: networkingapiv1alpha1.ServiceCIDRSpec{
 | 
										Spec: networkingapiv1alpha1.ServiceCIDRSpec{
 | 
				
			||||||
						IPv4: defaultIPv4CIDR,
 | 
											CIDRs: []string{defaultIPv4CIDR, defaultIPv6CIDR},
 | 
				
			||||||
						IPv6: defaultIPv6CIDR,
 | 
					 | 
				
			||||||
					},
 | 
										},
 | 
				
			||||||
					Status: networkingapiv1alpha1.ServiceCIDRStatus{
 | 
										Status: networkingapiv1alpha1.ServiceCIDRStatus{
 | 
				
			||||||
						Conditions: []metav1.Condition{
 | 
											Conditions: []metav1.Condition{
 | 
				
			||||||
@@ -133,8 +129,7 @@ func TestControllerSync(t *testing.T) {
 | 
				
			|||||||
						DeletionTimestamp: ptr.To(metav1.Now()),
 | 
											DeletionTimestamp: ptr.To(metav1.Now()),
 | 
				
			||||||
					},
 | 
										},
 | 
				
			||||||
					Spec: networkingapiv1alpha1.ServiceCIDRSpec{
 | 
										Spec: networkingapiv1alpha1.ServiceCIDRSpec{
 | 
				
			||||||
						IPv4: defaultIPv4CIDR,
 | 
											CIDRs: []string{defaultIPv4CIDR, defaultIPv6CIDR},
 | 
				
			||||||
						IPv6: defaultIPv6CIDR,
 | 
					 | 
				
			||||||
					},
 | 
										},
 | 
				
			||||||
				},
 | 
									},
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
@@ -147,8 +142,7 @@ func TestControllerSync(t *testing.T) {
 | 
				
			|||||||
						Name: "non-default-cidr",
 | 
											Name: "non-default-cidr",
 | 
				
			||||||
					},
 | 
										},
 | 
				
			||||||
					Spec: networkingapiv1alpha1.ServiceCIDRSpec{
 | 
										Spec: networkingapiv1alpha1.ServiceCIDRSpec{
 | 
				
			||||||
						IPv4: defaultIPv4CIDR,
 | 
											CIDRs: []string{defaultIPv4CIDR, defaultIPv6CIDR},
 | 
				
			||||||
						IPv6: defaultIPv6CIDR,
 | 
					 | 
				
			||||||
					},
 | 
										},
 | 
				
			||||||
				},
 | 
									},
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user