From 68803c7c8965946bcf46c757d385f0857e055643 Mon Sep 17 00:00:00 2001 From: wangxye Date: Tue, 6 Aug 2024 22:00:21 +0800 Subject: [PATCH] feat: update normal sync pool service in vip loadbalancer Signed-off-by: wangxye --- .../viploadbalancer/ip_utils.go | 74 +++++++-------- .../viploadbalancer/ip_utils_test.go | 5 +- .../viploadbalancer_controller.go | 94 +++++++++++-------- .../viploadbalancer_controller_test.go | 40 ++++---- .../v1alpha1/poolservice_handler.go | 18 +--- 5 files changed, 120 insertions(+), 111 deletions(-) diff --git a/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer/ip_utils.go b/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer/ip_utils.go index 0e7dd87bce2..5033de990c0 100644 --- a/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer/ip_utils.go +++ b/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer/ip_utils.go @@ -33,10 +33,11 @@ type IPVRID struct { } type IPManager struct { + IPRanges []string // ipPools indicates if ip is assign - ipPools map[string]int + IPPools map[string]int // ipVRIDs indicates which IPs are assigned to vrid - ipVRIDs map[int][]string + IPVRIDs map[int][]string } func NewIPVRID(ips []string, vrid int) IPVRID { @@ -46,15 +47,11 @@ func NewIPVRID(ips []string, vrid int) IPVRID { } } -func NewIPManager(ipRanges string) (*IPManager, error) { +func NewIPManager(ipr []string) (*IPManager, error) { manager := &IPManager{ - ipPools: map[string]int{}, - ipVRIDs: make(map[int][]string), - } - - iprs := ParseIP(ipRanges) - for _, ipr := range iprs { - manager.ipPools[ipr] = VRIDEVICTED + IPRanges: ipr, + IPPools: make(map[string]int), + IPVRIDs: make(map[int][]string), } return manager, nil @@ -101,11 +98,11 @@ func incrementIP(ip net.IP) net.IP { // Get return a IPVRID with a available IP and VRID combination func (m *IPManager) Get() (IPVRID, error) { for vrid := 0; vrid < VRIDMAXVALUE; vrid++ { - if ips, ok := m.ipVRIDs[vrid]; !ok || len(ips) == 0 { - for ip, used := range m.ipPools { + if ips, ok := m.IPVRIDs[vrid]; !ok || len(ips) == 0 { + for ip, used := range m.IPPools { if used == VRIDEVICTED { - m.ipPools[ip] = vrid - m.ipVRIDs[vrid] = []string{ip} + m.IPPools[ip] = vrid + m.IPVRIDs[vrid] = []string{ip} return IPVRID{IPs: []string{ip}, VRID: vrid}, nil } } @@ -119,45 +116,46 @@ func (m *IPManager) Get() (IPVRID, error) { func (m *IPManager) Assign(ips []string) (IPVRID, error) { var noConflictIPs []string for _, ip := range ips { - // if conflict, just use no conflict - if m.ipPools[ip] != VRIDEVICTED { + // if conflicted, just use no conflict + if _, ok := m.IPPools[ip]; ok { continue } noConflictIPs = append(noConflictIPs, ip) } - // if no avalible ip, get a new ipvrid + // if no available ip, get a new ipvrid if len(noConflictIPs) == 0 { return m.Get() } var vrid int for ; vrid < VRIDMAXVALUE; vrid++ { - if _, ok := m.ipVRIDs[vrid]; !ok { - m.ipVRIDs[vrid] = append(m.ipVRIDs[vrid], noConflictIPs...) + if _, ok := m.IPVRIDs[vrid]; ok { + continue + } - for _, ip := range noConflictIPs { - m.ipPools[ip] = vrid - } - break + m.IPVRIDs[vrid] = append(m.IPVRIDs[vrid], noConflictIPs...) + for _, ip := range noConflictIPs { + m.IPPools[ip] = vrid } + break } // Get fully vrid-ips pair - return IPVRID{VRID: vrid, IPs: m.ipVRIDs[vrid]}, nil + return IPVRID{VRID: vrid, IPs: m.IPVRIDs[vrid]}, nil } -// Release release ips from vrid, if vrid is not assigned, return error +// Release ips from vrid, if vrid is not assigned, return error func (m *IPManager) Release(ipVRID IPVRID) error { if err := m.IsValid(ipVRID); err != nil { return err } - if _, ok := m.ipVRIDs[ipVRID.VRID]; !ok { + if _, ok := m.IPVRIDs[ipVRID.VRID]; !ok { return fmt.Errorf("VRID %d does not assign ips", ipVRID.VRID) } - remain := make([]string, len(m.ipVRIDs[ipVRID.VRID])-len(ipVRID.IPs)) + remain := make([]string, len(m.IPVRIDs[ipVRID.VRID])-len(ipVRID.IPs)) - for _, ip := range m.ipVRIDs[ipVRID.VRID] { + for _, ip := range m.IPVRIDs[ipVRID.VRID] { if m.isIPPresent(ip, ipVRID.IPs) { continue } @@ -165,25 +163,25 @@ func (m *IPManager) Release(ipVRID IPVRID) error { remain = append(remain, ip) } - if len(remain) == len(m.ipVRIDs[ipVRID.VRID]) { + if len(remain) == len(m.IPVRIDs[ipVRID.VRID]) { return fmt.Errorf("IP %v is not assigned", ipVRID.IPs) } for _, ip := range remain { - m.ipPools[ip] = VRIDEVICTED + m.IPPools[ip] = VRIDEVICTED } return nil } -// check if ip and vrid is valid in this ip-pools, if not return error +// IsValid check if ip and vrid is valid in this ip-pools, if not return error func (m *IPManager) IsValid(ipvrid IPVRID) error { if len(ipvrid.IPs) == 0 { return fmt.Errorf("IPs is empty") } for _, ip := range ipvrid.IPs { - if _, ok := m.ipPools[ip]; !ok { + if _, ok := m.IPPools[ip]; !ok { return fmt.Errorf("IP: %s is not found in IP-Pools", ip) } } @@ -203,15 +201,15 @@ func (m *IPManager) Sync(ipVRIDs []IPVRID) error { ips := ipVRID.IPs vrid := ipVRID.VRID - app, del := m.findDiffIPs(ips, m.ipVRIDs[vrid]) + app, del := m.findDiffIPs(ips, m.IPVRIDs[vrid]) for _, ip := range del { - m.ipPools[ip] = VRIDEVICTED + m.IPPools[ip] = VRIDEVICTED } - m.ipVRIDs[vrid] = ips + m.IPVRIDs[vrid] = ips for _, ip := range app { - m.ipPools[ip] = vrid + m.IPPools[ip] = vrid } } @@ -222,13 +220,13 @@ func (m *IPManager) Sync(ipVRIDs []IPVRID) error { // findDiffIPs find the difference between des and cur, return the difference between des and cur func (m *IPManager) findDiffIPs(des, cur []string) (app, del []string) { for _, dip := range des { - if exsit := m.isIPPresent(dip, cur); !exsit { + if exist := m.isIPPresent(dip, cur); !exist { app = append(app, dip) } } for _, cip := range cur { - if exsit := m.isIPPresent(cip, des); !exsit { + if exist := m.isIPPresent(cip, des); !exist { del = append(del, cip) } } diff --git a/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer/ip_utils_test.go b/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer/ip_utils_test.go index 62b4696c842..23a443aa6c4 100644 --- a/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer/ip_utils_test.go +++ b/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer/ip_utils_test.go @@ -24,7 +24,7 @@ import ( func TestIPMAnager(t *testing.T) { ipRanges := "192.168.0.1-192.168.1.5, 10.0.0.1-10.0.0.3" - manager, err := vip.NewIPManager(ipRanges) + manager, err := vip.NewIPManager(vip.ParseIP(ipRanges)) if err != nil { t.Fatalf("Failed to create IPManager: %v", err) } @@ -54,7 +54,8 @@ func TestIPMAnager(t *testing.T) { t.Run("get ip when none are available", func(t *testing.T) { // Test getting IPVRID when none are available - m, err := vip.NewIPManager("192.168.0.1") + ipr := "192.168.0.1" + m, err := vip.NewIPManager(vip.ParseIP(ipr)) if err != nil { t.Errorf("Failed to create IPManager: %v", err) } diff --git a/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer/viploadbalancer_controller.go b/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer/viploadbalancer_controller.go index 467610ceeb8..a63a0b3d7f0 100644 --- a/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer/viploadbalancer_controller.go +++ b/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer/viploadbalancer_controller.go @@ -100,19 +100,19 @@ type ReconcileVipLoadBalancer struct { recorder record.EventRecorder mapper meta.RESTMapper - Configration config.VipLoadBalancerControllerConfiguration - IPManagers map[string]*IPManager + Configuration config.VipLoadBalancerControllerConfiguration + IPManagers map[string]*IPManager } // newReconciler returns a new reconcile.Reconciler func newReconciler(c *appconfig.CompletedConfig, mgr manager.Manager) *ReconcileVipLoadBalancer { return &ReconcileVipLoadBalancer{ - Client: mgr.GetClient(), - scheme: mgr.GetScheme(), - mapper: mgr.GetRESTMapper(), - recorder: mgr.GetEventRecorderFor(names.VipLoadBalancerController), - Configration: c.ComponentConfig.VipLoadBalancerController, - IPManagers: make(map[string]*IPManager), + Client: mgr.GetClient(), + scheme: mgr.GetScheme(), + mapper: mgr.GetRESTMapper(), + recorder: mgr.GetEventRecorderFor(names.VipLoadBalancerController), + Configuration: c.ComponentConfig.VipLoadBalancerController, + IPManagers: make(map[string]*IPManager), } } @@ -154,6 +154,7 @@ func (r *ReconcileVipLoadBalancer) Reconcile(ctx context.Context, request reconc copyPoolService := poolService.DeepCopy() + // Check if the PoolService instance is deleted if poolService.DeletionTimestamp != nil { return r.reconcileDelete(ctx, copyPoolService) } @@ -185,7 +186,7 @@ func (r *ReconcileVipLoadBalancer) reconcilePoolService(ctx context.Context, poo func (r *ReconcileVipLoadBalancer) syncPoolService(ctx context.Context, poolService *netv1alpha1.PoolService) error { klog.V(4).Infof(Format("SyncPoolServices VipLoadBalancer %s/%s", poolService.Namespace, poolService.Name)) - // sync VRID from the poolservice + // sync IP-VRID pairs from the recently poolservice if err := r.syncIPVRIDs(ctx, poolService); err != nil { klog.Errorf(Format("Failed to sync VRID on Pool Service %s/%s: %v", poolService.Namespace, poolService.Name, err)) return err @@ -206,12 +207,28 @@ func (r *ReconcileVipLoadBalancer) syncPoolService(ctx context.Context, poolServ func (r *ReconcileVipLoadBalancer) syncIPVRIDs(ctx context.Context, poolService *netv1alpha1.PoolService) error { poolName := poolService.Labels[network.LabelNodePoolName] - poolAddress, err := r.getCurrentPoolAddress(ctx, poolService) + // Get the pool address from nodepool label + if err := r.syncIPAddressPools(ctx, poolName); err != nil { + return fmt.Errorf("failed to sync ip addresses from nodepool: %v", err) + } + + currentIPVRIDs, err := r.getCurrentAssignedIPVRIDs(ctx, poolService) if err != nil { - return fmt.Errorf("failed to get avalible Pool address of nodepool: %v", err) + return fmt.Errorf("failed to get current PoolServices: %v", err) } - // if nodepool has not address-pools + // Sync the IPVRIDs + r.IPManagers[poolName].Sync(currentIPVRIDs) + return nil +} + +func (r *ReconcileVipLoadBalancer) syncIPAddressPools(ctx context.Context, poolName string) error { + poolAddress, err := r.getCurrentPoolAddress(ctx, poolName) + if err != nil { + return fmt.Errorf("failed to get available Pool address of nodepool: %v", err) + } + + // check if nodepool has not address-pools if _, ok := r.IPManagers[poolName]; !ok { r.IPManagers[poolName], err = NewIPManager(poolAddress) if err != nil { @@ -219,32 +236,32 @@ func (r *ReconcileVipLoadBalancer) syncIPVRIDs(ctx context.Context, poolService } } - currentIPVRIDs, err := r.getCurrentIPVRIDs(ctx, poolService) - if err != nil { - return fmt.Errorf("failed to get current PoolServices: %v", err) + // TODO: if user update poolAddress label in the nodepool + if poolAddress != nil && strings.Join(poolAddress, ",") != strings.Join(r.IPManagers[poolName].IPRanges, ",") { + klog.Infof(Format("NodePool: %s 's IP address pool has been updated, please delete it and reconfigure it", poolName)) } - // Sync the IPVRIDs - r.IPManagers[poolService.Labels[network.LabelNodePoolName]].Sync(currentIPVRIDs) - return nil } -func (r *ReconcileVipLoadBalancer) getCurrentPoolAddress(ctx context.Context, poolService *netv1alpha1.PoolService) (string, error) { +func (r *ReconcileVipLoadBalancer) getCurrentPoolAddress(ctx context.Context, poolName string) ([]string, error) { np := &v1beta1.NodePool{} - if err := r.Get(ctx, client.ObjectKey{Name: poolService.Labels[network.LabelNodePoolName]}, np); err != nil { - return "", err + + // Get All NodePools from the cluster by nodepool name + if err := r.Get(ctx, client.ObjectKey{Name: poolName}, np); err != nil { + return nil, err } + // Check if the NodePool has address pools if np.Annotations == nil { - return "", fmt.Errorf("NodePool %s doesn't have annotations", np.Name) + return nil, fmt.Errorf("NodePool %s doesn't have not available annotations", np.Name) } - if _, ok := np.Annotations[AnnotationNodePoolAddressPools]; !ok { - return "", fmt.Errorf("NodePool %s doesn't have address pools", np.Name) + if poolAddress, ok := np.Annotations[AnnotationNodePoolAddressPools]; ok { + return ParseIP(poolAddress), nil } - return np.Annotations[AnnotationNodePoolAddressPools], nil + return nil, fmt.Errorf("NodePool %s is not assigned address pools", np.Name) } func (r *ReconcileVipLoadBalancer) syncPoolServiceStatus(ctx context.Context, poolService *netv1alpha1.PoolService) error { @@ -257,7 +274,7 @@ func (r *ReconcileVipLoadBalancer) syncPoolServiceStatus(ctx context.Context, po desiredLbStatus, err := r.desiredLbStatus(poolService) if err != nil { - return fmt.Errorf("failed to calculate desire lb stattus for poolservice %s/%s: %v", poolService.Namespace, poolService.Name, err) + return fmt.Errorf("failed to calculate desire lb status for poolservice %s/%s: %v", poolService.Namespace, poolService.Name, err) } poolService.Status.LoadBalancer = desiredLbStatus @@ -291,7 +308,7 @@ func (r *ReconcileVipLoadBalancer) desiredLbStatus(poolService *netv1alpha1.Pool }, nil } -func (r *ReconcileVipLoadBalancer) getCurrentIPVRIDs(ctx context.Context, poolService *netv1alpha1.PoolService) ([]IPVRID, error) { +func (r *ReconcileVipLoadBalancer) getCurrentAssignedIPVRIDs(ctx context.Context, poolService *netv1alpha1.PoolService) ([]IPVRID, error) { // Get the poolservice list listSelector := &client.ListOptions{ LabelSelector: labels.SelectorFromSet(map[string]string{ @@ -334,11 +351,7 @@ func filterInvalidPoolService(poolServices []netv1alpha1.PoolService) []IPVRID { } func (r *ReconcileVipLoadBalancer) checkIPVRIDs(poolService netv1alpha1.PoolService) (*IPVRID, error) { - if !r.containsVRID(&poolService) { - // not have VRID - return nil, fmt.Errorf("PoolService %s/%s doesn't have VRID", poolService.Namespace, poolService.Name) - } - + // check if the poolservice has vrid-ips ipvrid, err := r.isValidIPVRID(&poolService) if err != nil { // ip-vrid is invalid @@ -362,10 +375,15 @@ func (r *ReconcileVipLoadBalancer) containsVRID(poolService *netv1alpha1.PoolSer } func (r *ReconcileVipLoadBalancer) isValidIPVRID(poolService *netv1alpha1.PoolService) (*IPVRID, error) { - poolName := poolService.Labels[network.LabelNodePoolName] + // check if the poolservice has vrid + if !r.containsVRID(poolService) { + // not have VRID + return nil, fmt.Errorf("PoolService %s/%s doesn't have VRID", poolService.Namespace, poolService.Name) + } + vrid, err := strconv.Atoi(poolService.Annotations[AnnotationVipLoadBalancerVRID]) if err != nil { - return nil, fmt.Errorf("invalid VRID: %v", err) + return nil, fmt.Errorf("PoolService %s/%s has invalid VRID: %v", poolService.Namespace, poolService.Name, err) } ips := []string{} @@ -376,7 +394,7 @@ func (r *ReconcileVipLoadBalancer) isValidIPVRID(poolService *netv1alpha1.PoolSe } ipvrid := NewIPVRID(ips, vrid) - if err := r.IPManagers[poolName].IsValid(ipvrid); err != nil { + if err := r.IPManagers[poolService.Labels[network.LabelNodePoolName]].IsValid(ipvrid); err != nil { return nil, fmt.Errorf("VRID: %d is not valid: %v", vrid, err) } @@ -384,9 +402,9 @@ func (r *ReconcileVipLoadBalancer) isValidIPVRID(poolService *netv1alpha1.PoolSe } func (r *ReconcileVipLoadBalancer) handleIPVRIDs(ctx context.Context, poolService *netv1alpha1.PoolService) error { - // Check if the PoolService has a VRID - _, err := r.checkIPVRIDs(*poolService) - if err == nil { + // Check if the PoolService has a available VRID + if _, err := r.checkIPVRIDs(*poolService); err == nil { + // If yes, use the user-specified vrid return nil } diff --git a/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer/viploadbalancer_controller_test.go b/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer/viploadbalancer_controller_test.go index 438cc47697b..336e72f4768 100644 --- a/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer/viploadbalancer_controller_test.go +++ b/pkg/yurtmanager/controller/loadbalancerset/viploadbalancer/viploadbalancer_controller_test.go @@ -50,7 +50,7 @@ const ( mockServiceUid = "c0af506a-7096-4ef9-b39a-eac2feb5c07g" mockNodePoolUid = "f47dd9db-d3bc-40f3-8d03-7409930b6289" mockEndpointUid = "f7a5a351-3e33-47a9-bb00-685b357a62e5" - vridLable = "service.openyurt.io/vrid" + vridLabel = "service.openyurt.io/vrid" ) var ( @@ -74,8 +74,8 @@ func TestReconcileVipLoadBalancer_Reconcile(t *testing.T) { svc := newService(v1.NamespaceDefault, mockServiceName) poolsvc := newPoolService(v1.NamespaceDefault, "np123", nil, nil) np1 := newNodepool("np123", "name=np123,app=deploy") - adressPool := "192.168.0.1-192.168.1.1" - np1.Annotations = map[string]string{viploadbalancer.AnnotationNodePoolAddressPools: adressPool} + addressPool := "192.168.0.1-192.168.1.1" + np1.Annotations = map[string]string{viploadbalancer.AnnotationNodePoolAddressPools: addressPool} c := fakeclient.NewClientBuilder().WithScheme(scheme).WithObjects(svc).WithObjects(np1).WithObjects(poolsvc).Build() rc := viploadbalancer.ReconcileVipLoadBalancer{ @@ -100,8 +100,8 @@ func TestReconcileVipLoadBalancer_Reconcile(t *testing.T) { svc := newService(v1.NamespaceDefault, mockServiceName) poolsvc := newPoolService(v1.NamespaceDefault, "np123", nil, nil) np1 := newNodepool("np123", "name=np123,app=deploy") - adressPool := "192.168.0.1-192.168.1.1" - np1.Annotations = map[string]string{viploadbalancer.AnnotationNodePoolAddressPools: adressPool} + addressPool := "192.168.0.1-192.168.1.1" + np1.Annotations = map[string]string{viploadbalancer.AnnotationNodePoolAddressPools: addressPool} poolsvc.Annotations = map[string]string{viploadbalancer.AnnotationVipLoadBalancerVRID: "6"} @@ -130,8 +130,8 @@ func TestReconcileVipLoadBalancer_Reconcile(t *testing.T) { svc := newService(v1.NamespaceDefault, mockServiceName) poolsvc := newPoolService(v1.NamespaceDefault, "np123", nil, nil) np1 := newNodepool("np123", "name=np123,app=deploy") - adressPool := "192.168.0.1-192.168.1.1" - np1.Annotations = map[string]string{viploadbalancer.AnnotationNodePoolAddressPools: adressPool} + addressPool := "192.168.0.1-192.168.1.1" + np1.Annotations = map[string]string{viploadbalancer.AnnotationNodePoolAddressPools: addressPool} poolsvc.Annotations = map[string]string{viploadbalancer.AnnotationVipLoadBalancerVRID: "256"} @@ -159,8 +159,8 @@ func TestReconcileVipLoadBalancer_Reconcile(t *testing.T) { svc.Annotations = map[string]string{viploadbalancer.AnnotationServiceVIPAddress: "192.168.1.1"} poolsvc := newPoolService(v1.NamespaceDefault, "np123", nil, nil) np1 := newNodepool("np123", "name=np123,app=deploy") - adressPool := "192.168.0.1-192.168.2.2" - np1.Annotations = map[string]string{viploadbalancer.AnnotationNodePoolAddressPools: adressPool} + addressPool := "192.168.0.1-192.168.2.2" + np1.Annotations = map[string]string{viploadbalancer.AnnotationNodePoolAddressPools: addressPool} endpoint := newEndpoint(v1.NamespaceDefault, mockEndpointName) endpoint.Annotations = map[string]string{ viploadbalancer.AnnotationServiceVIPStatus: viploadbalancer.AnnotationServiceVIPStatusOnline, @@ -189,8 +189,8 @@ func TestReconcileVipLoadBalancer_Reconcile(t *testing.T) { svc.Annotations = map[string]string{viploadbalancer.AnnotationServiceVIPAddress: svcIPRange} poolsvc := newPoolService(v1.NamespaceDefault, "np123", nil, nil) np1 := newNodepool("np123", "name=np123,app=deploy") - adressPool := "192.168.0.1-192.168.2.2" - np1.Annotations = map[string]string{viploadbalancer.AnnotationNodePoolAddressPools: adressPool} + addressPool := "192.168.0.1-192.168.2.2" + np1.Annotations = map[string]string{viploadbalancer.AnnotationNodePoolAddressPools: addressPool} endpoint := newEndpoint(v1.NamespaceDefault, mockEndpointName) endpoint.Annotations = map[string]string{ viploadbalancer.AnnotationServiceVIPStatus: viploadbalancer.AnnotationServiceVIPStatusOnline, @@ -219,8 +219,8 @@ func TestReconcileVipLoadBalancer_Reconcile(t *testing.T) { svc.Annotations = map[string]string{viploadbalancer.AnnotationServiceVIPAddress: svcIPRange} poolsvc := newPoolService(v1.NamespaceDefault, "np123", nil, nil) np1 := newNodepool("np123", "name=np123,app=deploy") - adressPool := "192.168.0.1-192.168.2.2, 10.1.0.1-10.1.0.2" - np1.Annotations = map[string]string{viploadbalancer.AnnotationNodePoolAddressPools: adressPool} + addressPool := "192.168.0.1-192.168.2.2, 10.1.0.1-10.1.0.2" + np1.Annotations = map[string]string{viploadbalancer.AnnotationNodePoolAddressPools: addressPool} endpoint := newEndpoint(v1.NamespaceDefault, mockEndpointName) endpoint.Annotations = map[string]string{ viploadbalancer.AnnotationServiceVIPStatus: viploadbalancer.AnnotationServiceVIPStatusOnline, @@ -248,7 +248,7 @@ func TestReconcileVipLoadBalancer_Reconcile(t *testing.T) { ps1 := newPoolService(v1.NamespaceDefault, "np123", nil, nil) ps1.Finalizers = []string{viploadbalancer.VipLoadBalancerFinalizer} - ipManage, err := viploadbalancer.NewIPManager("192.168.0.1-192.168.1.1") + ipManage, err := viploadbalancer.NewIPManager(viploadbalancer.ParseIP("192.168.0.1-192.168.1.1")) if err != nil { t.Fatalf("Failed to create IPManager: %v", err) } @@ -281,7 +281,7 @@ func TestReconcileVipLoadBalancer_Reconcile(t *testing.T) { ps1 := newPoolService(v1.NamespaceDefault, "np123", nil, nil) ps1.Finalizers = []string{viploadbalancer.VipLoadBalancerFinalizer} - ipManage, err := viploadbalancer.NewIPManager("192.168.0.1-192.168.1.1") + ipManage, err := viploadbalancer.NewIPManager(viploadbalancer.ParseIP("192.168.0.1-192.168.1.1")) if err != nil { t.Fatalf("Failed to create IPManager: %v", err) } @@ -314,7 +314,7 @@ func TestReconcileVipLoadBalancer_Reconcile(t *testing.T) { ps1.Finalizers = []string{viploadbalancer.VipLoadBalancerFinalizer} ps1.Status.LoadBalancer.Ingress = []corev1.LoadBalancerIngress{{IP: "192.168.0.1"}} - ipManage, err := viploadbalancer.NewIPManager("192.168.0.1-192.168.1.1") + ipManage, err := viploadbalancer.NewIPManager(viploadbalancer.ParseIP("192.168.0.1-192.168.1.1")) if err != nil { t.Fatalf("Failed to create IPManager: %v", err) } @@ -351,7 +351,7 @@ func TestReconcileVipLoadBalancer_Reconcile(t *testing.T) { ps1.Finalizers = []string{viploadbalancer.VipLoadBalancerFinalizer} ps1.Status.LoadBalancer.Ingress = []corev1.LoadBalancerIngress{{IP: "192.168.0.1"}} - ipManage, err := viploadbalancer.NewIPManager("192.168.0.1-192.168.1.1") + ipManage, err := viploadbalancer.NewIPManager(viploadbalancer.ParseIP("192.168.0.1-192.168.1.1")) if err != nil { t.Fatalf("Failed to create IPManager: %v", err) } @@ -388,7 +388,7 @@ func TestReconcileVipLoadBalancer_Reconcile(t *testing.T) { ps1.Finalizers = []string{viploadbalancer.VipLoadBalancerFinalizer} ps1.Status.LoadBalancer.Ingress = []corev1.LoadBalancerIngress{{IP: "192.168.0.1"}} - ipManage, err := viploadbalancer.NewIPManager("192.168.0.1-192.168.1.1") + ipManage, err := viploadbalancer.NewIPManager(viploadbalancer.ParseIP("192.168.0.1-192.168.1.1")) if err != nil { t.Fatalf("Failed to create IPManager: %v", err) } @@ -624,8 +624,8 @@ func assertPoolServiceVRIDLabels(t testing.TB, psl *v1alpha1.PoolServiceList, vr return } - if ps.Annotations[vridLable] != vrid { - t.Errorf("expected pool service vird is %s, but got %s", vrid, ps.Annotations[vridLable]) + if ps.Annotations[vridLabel] != vrid { + t.Errorf("expected pool service vird is %s, but got %s", vrid, ps.Annotations[vridLabel]) } } } diff --git a/pkg/yurtmanager/webhook/poolservice/v1alpha1/poolservice_handler.go b/pkg/yurtmanager/webhook/poolservice/v1alpha1/poolservice_handler.go index b97dc9a6649..e70c55aeea0 100644 --- a/pkg/yurtmanager/webhook/poolservice/v1alpha1/poolservice_handler.go +++ b/pkg/yurtmanager/webhook/poolservice/v1alpha1/poolservice_handler.go @@ -29,9 +29,10 @@ package v1alpha1 import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/client/apiutil" "sigs.k8s.io/controller-runtime/pkg/webhook" + yurtClient "github.com/openyurtio/openyurt/cmd/yurt-manager/app/client" + "github.com/openyurtio/openyurt/cmd/yurt-manager/names" "github.com/openyurtio/openyurt/pkg/apis/network/v1alpha1" "github.com/openyurtio/openyurt/pkg/yurtmanager/webhook/util" ) @@ -39,18 +40,9 @@ import ( // SetupWebhookWithManager sets up Cluster webhooks. mutate path, validatepath, error func (webhook *PoolServiceHandler) SetupWebhookWithManager(mgr ctrl.Manager) (string, string, error) { // init - webhook.Client = mgr.GetClient() - - gvk, err := apiutil.GVKForObject(&v1alpha1.PoolService{}, mgr.GetScheme()) - if err != nil { - return "", "", err - } - return util.GenerateMutatePath(gvk), - util.GenerateValidatePath(gvk), - ctrl.NewWebhookManagedBy(mgr). - For(&v1alpha1.PoolService{}). - WithDefaulter(webhook). - Complete() + webhook.Client = yurtClient.GetClientByControllerNameOrDie(mgr, names.VipLoadBalancerController) + + return util.RegisterWebhook(mgr, &v1alpha1.PoolService{}, webhook) } // +kubebuilder:webhook:path=/mutate-network-openyurt-io-poolservice,mutating=true,failurePolicy=fail,sideEffects=None,admissionReviewVersions=v1;v1beta1,groups=network.openyurt.io,resources=poolservices,verbs=create;update,versions=v1alpha1,name=mutate.network.v1alpha1.poolservice.openyurt.io