From 4c2b5ece8a5128cede02239e11f63b97595434ce Mon Sep 17 00:00:00 2001 From: bobz965 Date: Wed, 21 Feb 2024 11:34:29 +0800 Subject: [PATCH] optimize Signed-off-by: bobz965 --- pkg/controller/init.go | 19 ++++++- pkg/controller/ip.go | 26 ++++----- pkg/controller/ovn_dnat.go | 23 ++++---- pkg/controller/ovn_eip.go | 23 ++++---- pkg/controller/ovn_fip.go | 26 ++++----- pkg/controller/ovn_snat.go | 26 ++++----- pkg/controller/qos_policy.go | 26 ++++----- pkg/controller/subnet.go | 27 ++++------ pkg/controller/vip.go | 26 ++++----- pkg/controller/vpc_nat_gw_eip.go | 28 ++++------ pkg/controller/vpc_nat_gw_nat.go | 84 ++++++++++++------------------ pkg/util/const.go | 4 +- test/e2e/kube-ovn/subnet/subnet.go | 16 +++--- 13 files changed, 152 insertions(+), 202 deletions(-) diff --git a/pkg/controller/init.go b/pkg/controller/init.go index 69e687dd9bd0..6cc2f6d49e50 100644 --- a/pkg/controller/init.go +++ b/pkg/controller/init.go @@ -15,6 +15,8 @@ import ( "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/types" "k8s.io/klog/v2" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1" "github.com/kubeovn/kube-ovn/pkg/ovs" @@ -578,7 +580,7 @@ func (c *Controller) syncIPCR() error { for _, ipCR := range ips { ip := ipCR.DeepCopy() - if ip.DeletionTimestamp != nil && slices.Contains(ip.Finalizers, util.FinalizerName) { + if ip.DeletionTimestamp != nil && slices.Contains(ip.Finalizers, util.KubeOVNControllerFinalizer) { klog.Infof("enqueue update for deleting ip %s", ip.Name) c.updateIPQueue.Add(ip.Name) } @@ -882,3 +884,18 @@ func (c *Controller) syncFinalizers() error { klog.Info("sync finalizers done") return nil } + +func (c *Controller) ReplaceFinalizer(cachedObj client.Object) ([]byte, error) { + if slices.Contains(cachedObj.GetFinalizers(), util.DepreciatedFinalizerName) { + newObj := cachedObj.DeepCopyObject().(client.Object) + controllerutil.RemoveFinalizer(newObj, util.DepreciatedFinalizerName) + controllerutil.AddFinalizer(newObj, util.KubeOVNControllerFinalizer) + patch, err := util.GenerateMergePatchPayload(cachedObj, newObj) + if err != nil { + klog.Errorf("failed to generate patch payload for %s, %v", newObj.GetName(), err) + return nil, err + } + return patch, nil + } + return nil, nil +} diff --git a/pkg/controller/ip.go b/pkg/controller/ip.go index 8295b195993a..83003856098f 100644 --- a/pkg/controller/ip.go +++ b/pkg/controller/ip.go @@ -324,7 +324,7 @@ func (c *Controller) handleUpdateIP(key string) error { klog.Infof("ip cr %s release ipam pod key %s from subnet %s", cachedIP.Name, podKey, cachedIP.Spec.Subnet) c.ipam.ReleaseAddressByPod(podKey, cachedIP.Spec.Subnet) } - if err = c.handleDelIPFinalizer(cachedIP, util.FinalizerName); err != nil { + if err = c.handleDelIPFinalizer(cachedIP, util.KubeOVNControllerFinalizer); err != nil { klog.Errorf("failed to handle del ip finalizer %v", err) return err } @@ -353,24 +353,18 @@ func (c *Controller) syncIPFinalizer() error { return err } for _, cachedIP := range ips { - if len(cachedIP.Finalizers) == 0 { - continue - } - if slices.Contains(cachedIP.Finalizers, util.DepreciatedFinalizerName) { - newIP := cachedIP.DeepCopy() - controllerutil.RemoveFinalizer(newIP, util.DepreciatedFinalizerName) - controllerutil.AddFinalizer(newIP, util.FinalizerName) - patch, err := util.GenerateMergePatchPayload(cachedIP, newIP) - if err != nil { - klog.Errorf("failed to generate patch payload for ip %s, %v", newIP.Name, err) - return err - } - if _, err := c.config.KubeOvnClient.KubeovnV1().IPs().Patch(context.Background(), newIP.Name, + patch, err := c.ReplaceFinalizer(cachedIP) + if err != nil { + klog.Errorf("failed to sync finalizer for ip %s, %v", cachedIP.Name, err) + return err + } + if patch != nil { + if _, err := c.config.KubeOvnClient.KubeovnV1().IPs().Patch(context.Background(), cachedIP.Name, types.MergePatchType, patch, metav1.PatchOptions{}, ""); err != nil { if k8serrors.IsNotFound(err) { return nil } - klog.Errorf("failed to sync finalizer for ip %s, %v", newIP.Name, err) + klog.Errorf("failed to sync finalizer for ip %s, %v", cachedIP.Name, err) return err } } @@ -572,7 +566,7 @@ func (c *Controller) createOrUpdateIPCR(ipCRName, podName, ip, mac, subnetName, } } - if err := c.handleAddIPFinalizer(ipCR, util.FinalizerName); err != nil { + if err := c.handleAddIPFinalizer(ipCR, util.KubeOVNControllerFinalizer); err != nil { klog.Errorf("failed to handle add ip finalizer %v", err) return err } diff --git a/pkg/controller/ovn_dnat.go b/pkg/controller/ovn_dnat.go index 82b948af95de..1a788e7a8b53 100644 --- a/pkg/controller/ovn_dnat.go +++ b/pkg/controller/ovn_dnat.go @@ -301,7 +301,7 @@ func (c *Controller) handleAddOvnDnatRule(key string) error { return err } - if err := c.handleAddOvnDnatFinalizer(cachedDnat, util.FinalizerName); err != nil { + if err := c.handleAddOvnDnatFinalizer(cachedDnat, util.KubeOVNControllerFinalizer); err != nil { klog.Errorf("failed to add finalizer for ovn dnat %s, %v", cachedDnat.Name, err) return err } @@ -348,7 +348,7 @@ func (c *Controller) handleDelOvnDnatRule(key string) error { return err } } - if err = c.handleDelOvnDnatFinalizer(cachedDnat, util.FinalizerName); err != nil { + if err = c.handleDelOvnDnatFinalizer(cachedDnat, util.KubeOVNControllerFinalizer); err != nil { klog.Errorf("failed to remove finalizer for ovn dnat %s, %v", cachedDnat.Name, err) return err } @@ -674,21 +674,18 @@ func (c *Controller) syncOvnDnatFinalizer() error { if len(cachedDnat.Finalizers) == 0 { continue } - if slices.Contains(cachedDnat.Finalizers, util.DepreciatedFinalizerName) { - newDnat := cachedDnat.DeepCopy() - controllerutil.RemoveFinalizer(newDnat, util.DepreciatedFinalizerName) - controllerutil.AddFinalizer(newDnat, util.FinalizerName) - patch, err := util.GenerateMergePatchPayload(cachedDnat, newDnat) - if err != nil { - klog.Errorf("failed to generate patch payload for dnat %s, %v", newDnat.Name, err) - return err - } - if _, err := c.config.KubeOvnClient.KubeovnV1().OvnDnatRules().Patch(context.Background(), newDnat.Name, + patch, err := c.ReplaceFinalizer(cachedDnat) + if err != nil { + klog.Errorf("failed to sync finalizer for dnat %s, %v", cachedDnat.Name, err) + return err + } + if patch != nil { + if _, err := c.config.KubeOvnClient.KubeovnV1().OvnDnatRules().Patch(context.Background(), cachedDnat.Name, types.MergePatchType, patch, metav1.PatchOptions{}, ""); err != nil { if k8serrors.IsNotFound(err) { return nil } - klog.Errorf("failed to sync finalizer for dnat %s, %v", newDnat.Name, err) + klog.Errorf("failed to sync finalizer for dnat %s, %v", cachedDnat.Name, err) return err } } diff --git a/pkg/controller/ovn_eip.go b/pkg/controller/ovn_eip.go index ca20edc7a560..e0bfd060d661 100644 --- a/pkg/controller/ovn_eip.go +++ b/pkg/controller/ovn_eip.go @@ -268,7 +268,7 @@ func (c *Controller) handleAddOvnEip(key string) error { return err } } - if err = c.handleAddOvnEipFinalizer(cachedEip, util.FinalizerName); err != nil { + if err = c.handleAddOvnEipFinalizer(cachedEip, util.KubeOVNControllerFinalizer); err != nil { klog.Errorf("failed to add finalizer for ovn eip, %v", err) return err } @@ -343,7 +343,7 @@ func (c *Controller) handleDelOvnEip(key string) error { } } - if err = c.handleDelOvnEipFinalizer(eip, util.FinalizerName); err != nil { + if err = c.handleDelOvnEipFinalizer(eip, util.KubeOVNControllerFinalizer); err != nil { klog.Errorf("failed to handle remove ovn eip finalizer , %v", err) return err } @@ -598,21 +598,18 @@ func (c *Controller) syncOvnEipFinalizer() error { if len(cachedEip.Finalizers) == 0 { continue } - if slices.Contains(cachedEip.Finalizers, util.DepreciatedFinalizerName) { - newEip := cachedEip.DeepCopy() - controllerutil.RemoveFinalizer(newEip, util.DepreciatedFinalizerName) - controllerutil.AddFinalizer(newEip, util.FinalizerName) - patch, err := util.GenerateMergePatchPayload(cachedEip, newEip) - if err != nil { - klog.Errorf("failed to generate patch payload for eip %s, %v", newEip.Name, err) - return err - } - if _, err := c.config.KubeOvnClient.KubeovnV1().OvnEips().Patch(context.Background(), newEip.Name, + patch, err := c.ReplaceFinalizer(cachedEip) + if err != nil { + klog.Errorf("failed to sync finalizer for eip %s, %v", cachedEip.Name, err) + return err + } + if patch != nil { + if _, err := c.config.KubeOvnClient.KubeovnV1().OvnEips().Patch(context.Background(), cachedEip.Name, types.MergePatchType, patch, metav1.PatchOptions{}, ""); err != nil { if k8serrors.IsNotFound(err) { return nil } - klog.Errorf("failed to sync finalizer for eip %s, %v", newEip.Name, err) + klog.Errorf("failed to sync finalizer for eip %s, %v", cachedEip.Name, err) return err } } diff --git a/pkg/controller/ovn_fip.go b/pkg/controller/ovn_fip.go index d239a026fca2..2a867ec87ba2 100644 --- a/pkg/controller/ovn_fip.go +++ b/pkg/controller/ovn_fip.go @@ -296,7 +296,7 @@ func (c *Controller) handleAddOvnFip(key string) error { return err } - if err = c.handleAddOvnFipFinalizer(cachedFip, util.FinalizerName); err != nil { + if err = c.handleAddOvnFipFinalizer(cachedFip, util.KubeOVNControllerFinalizer); err != nil { klog.Errorf("failed to add finalizer for ovn fip, %v", err) return err } @@ -463,7 +463,7 @@ func (c *Controller) handleDelOvnFip(key string) error { return err } } - if err = c.handleDelOvnFipFinalizer(cachedFip, util.FinalizerName); err != nil { + if err = c.handleDelOvnFipFinalizer(cachedFip, util.KubeOVNControllerFinalizer); err != nil { klog.Errorf("failed to remove finalizer for ovn fip %s, %v", cachedFip.Name, err) return err } @@ -610,24 +610,18 @@ func (c *Controller) syncOvnFipFinalizer() error { return err } for _, cachedFip := range fips { - if len(cachedFip.Finalizers) == 0 { - continue - } - if slices.Contains(cachedFip.Finalizers, util.DepreciatedFinalizerName) { - newFip := cachedFip.DeepCopy() - controllerutil.RemoveFinalizer(newFip, util.DepreciatedFinalizerName) - controllerutil.AddFinalizer(newFip, util.FinalizerName) - patch, err := util.GenerateMergePatchPayload(cachedFip, newFip) - if err != nil { - klog.Errorf("failed to generate patch payload for fip %s, %v", newFip.Name, err) - return err - } - if _, err := c.config.KubeOvnClient.KubeovnV1().OvnFips().Patch(context.Background(), newFip.Name, + patch, err := c.ReplaceFinalizer(cachedFip) + if err != nil { + klog.Errorf("failed to sync finalizer for fip %s, %v", cachedFip.Name, err) + return err + } + if patch != nil { + if _, err := c.config.KubeOvnClient.KubeovnV1().OvnFips().Patch(context.Background(), cachedFip.Name, types.MergePatchType, patch, metav1.PatchOptions{}, ""); err != nil { if k8serrors.IsNotFound(err) { return nil } - klog.Errorf("failed to sync finalizer for fip %s, %v", newFip.Name, err) + klog.Errorf("failed to sync finalizer for fip %s, %v", cachedFip.Name, err) return err } } diff --git a/pkg/controller/ovn_snat.go b/pkg/controller/ovn_snat.go index a218a48b0a5b..1be94d1f31e5 100644 --- a/pkg/controller/ovn_snat.go +++ b/pkg/controller/ovn_snat.go @@ -258,7 +258,7 @@ func (c *Controller) handleAddOvnSnatRule(key string) error { klog.Errorf("failed to create snat, %v", err) return err } - if err := c.handleAddOvnSnatFinalizer(cachedSnat, util.FinalizerName); err != nil { + if err := c.handleAddOvnSnatFinalizer(cachedSnat, util.KubeOVNControllerFinalizer); err != nil { klog.Errorf("failed to add finalizer for ovn snat %s, %v", cachedSnat.Name, err) return err } @@ -420,7 +420,7 @@ func (c *Controller) handleDelOvnSnatRule(key string) error { return err } } - if err = c.handleDelOvnSnatFinalizer(cachedSnat, util.FinalizerName); err != nil { + if err = c.handleDelOvnSnatFinalizer(cachedSnat, util.KubeOVNControllerFinalizer); err != nil { klog.Errorf("failed to remove finalizer for ovn snat %s, %v", cachedSnat.Name, err) return err } @@ -554,24 +554,18 @@ func (c *Controller) syncOvnSnatFinalizer() error { return err } for _, cachedSnat := range snats { - if len(cachedSnat.Finalizers) == 0 { - continue - } - if slices.Contains(cachedSnat.Finalizers, util.DepreciatedFinalizerName) { - newSnat := cachedSnat.DeepCopy() - controllerutil.RemoveFinalizer(newSnat, util.DepreciatedFinalizerName) - controllerutil.AddFinalizer(newSnat, util.FinalizerName) - patch, err := util.GenerateMergePatchPayload(cachedSnat, newSnat) - if err != nil { - klog.Errorf("failed to generate patch payload for snat %s, %v", newSnat.Name, err) - return err - } - if _, err := c.config.KubeOvnClient.KubeovnV1().OvnSnatRules().Patch(context.Background(), newSnat.Name, + patch, err := c.ReplaceFinalizer(cachedSnat) + if err != nil { + klog.Errorf("failed to sync finalizer for snat %s, %v", cachedSnat.Name, err) + return err + } + if patch != nil { + if _, err := c.config.KubeOvnClient.KubeovnV1().OvnSnatRules().Patch(context.Background(), cachedSnat.Name, types.MergePatchType, patch, metav1.PatchOptions{}, ""); err != nil { if k8serrors.IsNotFound(err) { return nil } - klog.Errorf("failed to sync finalizer for snat %s, %v", newSnat.Name, err) + klog.Errorf("failed to sync finalizer for snat %s, %v", cachedSnat.Name, err) return err } } diff --git a/pkg/controller/qos_policy.go b/pkg/controller/qos_policy.go index 72f6e40f6f9a..86e46ad72cd5 100644 --- a/pkg/controller/qos_policy.go +++ b/pkg/controller/qos_policy.go @@ -265,7 +265,7 @@ func (c *Controller) handleDelQoSPoliciesFinalizer(key string) error { return nil } newQoSPolicies := cachedQoSPolicies.DeepCopy() - controllerutil.RemoveFinalizer(newQoSPolicies, util.FinalizerName) + controllerutil.RemoveFinalizer(newQoSPolicies, util.KubeOVNControllerFinalizer) patch, err := util.GenerateMergePatchPayload(cachedQoSPolicies, newQoSPolicies) if err != nil { klog.Errorf("failed to generate patch payload for qos '%s', %v", cachedQoSPolicies.Name, err) @@ -293,24 +293,18 @@ func (c *Controller) syncQoSPolicyFinalizer() error { return err } for _, cachedPolicy := range qosPolicies { - if len(cachedPolicy.Finalizers) == 0 { - continue + patch, err := c.ReplaceFinalizer(cachedPolicy) + if err != nil { + klog.Errorf("failed to sync finalizer for policy %s, %v", cachedPolicy.Name, err) + return err } - if slices.Contains(cachedPolicy.Finalizers, util.DepreciatedFinalizerName) { - newPolicy := cachedPolicy.DeepCopy() - controllerutil.RemoveFinalizer(newPolicy, util.DepreciatedFinalizerName) - controllerutil.AddFinalizer(newPolicy, util.FinalizerName) - patch, err := util.GenerateMergePatchPayload(cachedPolicy, newPolicy) - if err != nil { - klog.Errorf("failed to generate patch payload for policy %s, %v", newPolicy.Name, err) - return err - } - if _, err := c.config.KubeOvnClient.KubeovnV1().QoSPolicies().Patch(context.Background(), newPolicy.Name, + if patch != nil { + if _, err := c.config.KubeOvnClient.KubeovnV1().QoSPolicies().Patch(context.Background(), cachedPolicy.Name, types.MergePatchType, patch, metav1.PatchOptions{}, ""); err != nil { if k8serrors.IsNotFound(err) { return nil } - klog.Errorf("failed to sync finalizer for policy %s, %v", newPolicy.Name, err) + klog.Errorf("failed to sync finalizer for policy %s, %v", cachedPolicy.Name, err) return err } } @@ -557,12 +551,12 @@ func (c *Controller) handleAddQoSPolicyFinalizer(key string) error { return err } if cachedQoSPolicy.DeletionTimestamp.IsZero() { - if slices.Contains(cachedQoSPolicy.Finalizers, util.FinalizerName) { + if slices.Contains(cachedQoSPolicy.Finalizers, util.KubeOVNControllerFinalizer) { return nil } } newQoSPolicy := cachedQoSPolicy.DeepCopy() - controllerutil.AddFinalizer(newQoSPolicy, util.FinalizerName) + controllerutil.AddFinalizer(newQoSPolicy, util.KubeOVNControllerFinalizer) patch, err := util.GenerateMergePatchPayload(cachedQoSPolicy, newQoSPolicy) if err != nil { klog.Errorf("failed to generate patch payload for qos '%s', %v", cachedQoSPolicy.Name, err) diff --git a/pkg/controller/subnet.go b/pkg/controller/subnet.go index c2965414e543..a9567cd7e92d 100644 --- a/pkg/controller/subnet.go +++ b/pkg/controller/subnet.go @@ -20,7 +20,6 @@ import ( utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/tools/cache" "k8s.io/klog/v2" - "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1" "github.com/kubeovn/kube-ovn/pkg/ipam" @@ -491,24 +490,18 @@ func (c *Controller) syncSubnetFinalizer() error { return err } for _, cachedSubnet := range subnets { - if len(cachedSubnet.Finalizers) == 0 { - continue + patch, err := c.ReplaceFinalizer(cachedSubnet) + if err != nil { + klog.Errorf("failed to sync finalizer for subnet %s, %v", cachedSubnet.Name, err) + return err } - if slices.Contains(cachedSubnet.Finalizers, util.DepreciatedFinalizerName) { - newSubnet := cachedSubnet.DeepCopy() - controllerutil.RemoveFinalizer(newSubnet, util.DepreciatedFinalizerName) - controllerutil.AddFinalizer(newSubnet, util.FinalizerName) - patch, err := util.GenerateMergePatchPayload(cachedSubnet, newSubnet) - if err != nil { - klog.Errorf("failed to generate patch payload for subnet %s, %v", newSubnet.Name, err) - return err - } - if _, err := c.config.KubeOvnClient.KubeovnV1().Subnets().Patch(context.Background(), newSubnet.Name, + if patch != nil { + if _, err := c.config.KubeOvnClient.KubeovnV1().Subnets().Patch(context.Background(), cachedSubnet.Name, types.MergePatchType, patch, metav1.PatchOptions{}, ""); err != nil { if k8serrors.IsNotFound(err) { return nil } - klog.Errorf("failed to sync finalizer for subnet %s, %v", newSubnet.Name, err) + klog.Errorf("failed to sync finalizer for subnet %s, %v", cachedSubnet.Name, err) return err } } @@ -517,9 +510,9 @@ func (c *Controller) syncSubnetFinalizer() error { } func (c *Controller) handleSubnetFinalizer(subnet *kubeovnv1.Subnet) (bool, error) { - if subnet.DeletionTimestamp.IsZero() && !slices.Contains(subnet.Finalizers, util.FinalizerName) { + if subnet.DeletionTimestamp.IsZero() && !slices.Contains(subnet.Finalizers, util.KubeOVNControllerFinalizer) { newSubnet := subnet.DeepCopy() - newSubnet.Finalizers = append(newSubnet.Finalizers, util.FinalizerName) + newSubnet.Finalizers = append(newSubnet.Finalizers, util.KubeOVNControllerFinalizer) patch, err := util.GenerateMergePatchPayload(subnet, newSubnet) if err != nil { klog.Errorf("failed to generate patch payload for subnet '%s', %v", subnet.Name, err) @@ -543,7 +536,7 @@ func (c *Controller) handleSubnetFinalizer(subnet *kubeovnv1.Subnet) (bool, erro u2oInterconnIP := subnet.Status.U2OInterconnectionIP if !subnet.DeletionTimestamp.IsZero() && (usingIPs == 0 || (usingIPs == 1 && u2oInterconnIP != "")) { newSubnet := subnet.DeepCopy() - newSubnet.Finalizers = util.RemoveString(newSubnet.Finalizers, util.FinalizerName) + newSubnet.Finalizers = util.RemoveString(newSubnet.Finalizers, util.KubeOVNControllerFinalizer) patch, err := util.GenerateMergePatchPayload(subnet, newSubnet) if err != nil { klog.Errorf("failed to generate patch payload for subnet '%s', %v", subnet.Name, err) diff --git a/pkg/controller/vip.go b/pkg/controller/vip.go index d47572efb1f2..f2cff626f0f2 100644 --- a/pkg/controller/vip.go +++ b/pkg/controller/vip.go @@ -638,12 +638,12 @@ func (c *Controller) handleAddVipFinalizer(key string) error { return err } if cachedVip.DeletionTimestamp.IsZero() { - if slices.Contains(cachedVip.Finalizers, util.FinalizerName) { + if slices.Contains(cachedVip.Finalizers, util.KubeOVNControllerFinalizer) { return nil } } newVip := cachedVip.DeepCopy() - controllerutil.AddFinalizer(newVip, util.FinalizerName) + controllerutil.AddFinalizer(newVip, util.KubeOVNControllerFinalizer) patch, err := util.GenerateMergePatchPayload(cachedVip, newVip) if err != nil { klog.Errorf("failed to generate patch payload for ovn eip '%s', %v", cachedVip.Name, err) @@ -673,7 +673,7 @@ func (c *Controller) handleDelVipFinalizer(key string) error { return nil } newVip := cachedVip.DeepCopy() - controllerutil.RemoveFinalizer(newVip, util.FinalizerName) + controllerutil.RemoveFinalizer(newVip, util.KubeOVNControllerFinalizer) patch, err := util.GenerateMergePatchPayload(cachedVip, newVip) if err != nil { klog.Errorf("failed to generate patch payload for ovn eip '%s', %v", cachedVip.Name, err) @@ -701,24 +701,18 @@ func (c *Controller) syncVipFinalizer() error { return err } for _, cachedVip := range vips { - if len(cachedVip.Finalizers) == 0 { - continue + patch, err := c.ReplaceFinalizer(cachedVip) + if err != nil { + klog.Errorf("failed to sync finalizer for vip %s, %v", cachedVip.Name, err) + return err } - if slices.Contains(cachedVip.Finalizers, util.DepreciatedFinalizerName) { - newVip := cachedVip.DeepCopy() - controllerutil.RemoveFinalizer(newVip, util.DepreciatedFinalizerName) - controllerutil.AddFinalizer(newVip, util.FinalizerName) - patch, err := util.GenerateMergePatchPayload(cachedVip, newVip) - if err != nil { - klog.Errorf("failed to generate patch payload for vip %s, %v", newVip.Name, err) - return err - } - if _, err := c.config.KubeOvnClient.KubeovnV1().Vips().Patch(context.Background(), newVip.Name, + if patch != nil { + if _, err := c.config.KubeOvnClient.KubeovnV1().Vips().Patch(context.Background(), cachedVip.Name, types.MergePatchType, patch, metav1.PatchOptions{}, ""); err != nil { if k8serrors.IsNotFound(err) { return nil } - klog.Errorf("failed to sync finalizer for vip %s, %v", newVip.Name, err) + klog.Errorf("failed to sync finalizer for vip %s, %v", cachedVip.Name, err) return err } } diff --git a/pkg/controller/vpc_nat_gw_eip.go b/pkg/controller/vpc_nat_gw_eip.go index f47c55ea665d..dd8fbdd936ec 100644 --- a/pkg/controller/vpc_nat_gw_eip.go +++ b/pkg/controller/vpc_nat_gw_eip.go @@ -761,24 +761,18 @@ func (c *Controller) syncIptablesEipFinalizer() error { return err } for _, cachedEip := range eips { - if len(cachedEip.Finalizers) == 0 { - continue - } - if slices.Contains(cachedEip.Finalizers, util.DepreciatedFinalizerName) { - newEip := cachedEip.DeepCopy() - controllerutil.RemoveFinalizer(newEip, util.DepreciatedFinalizerName) - controllerutil.AddFinalizer(newEip, util.FinalizerName) - patch, err := util.GenerateMergePatchPayload(cachedEip, newEip) - if err != nil { - klog.Errorf("failed to generate patch payload for eip %s, %v", newEip.Name, err) - return err - } - if _, err := c.config.KubeOvnClient.KubeovnV1().IptablesEIPs().Patch(context.Background(), newEip.Name, + patch, err := c.ReplaceFinalizer(cachedEip) + if err != nil { + klog.Errorf("failed to sync finalizer for eip %s, %v", cachedEip.Name, err) + return err + } + if patch != nil { + if _, err := c.config.KubeOvnClient.KubeovnV1().IptablesEIPs().Patch(context.Background(), cachedEip.Name, types.MergePatchType, patch, metav1.PatchOptions{}, ""); err != nil { if k8serrors.IsNotFound(err) { return nil } - klog.Errorf("failed to sync finalizer for eip %s, %v", newEip.Name, err) + klog.Errorf("failed to sync finalizer for eip %s, %v", cachedEip.Name, err) return err } } @@ -796,12 +790,12 @@ func (c *Controller) handleAddIptablesEipFinalizer(key string) error { return err } if cachedIptablesEip.DeletionTimestamp.IsZero() { - if slices.Contains(cachedIptablesEip.Finalizers, util.FinalizerName) { + if slices.Contains(cachedIptablesEip.Finalizers, util.KubeOVNControllerFinalizer) { return nil } } newIptablesEip := cachedIptablesEip.DeepCopy() - controllerutil.AddFinalizer(newIptablesEip, util.FinalizerName) + controllerutil.AddFinalizer(newIptablesEip, util.KubeOVNControllerFinalizer) patch, err := util.GenerateMergePatchPayload(cachedIptablesEip, newIptablesEip) if err != nil { klog.Errorf("failed to generate patch payload for iptables eip '%s', %v", cachedIptablesEip.Name, err) @@ -831,7 +825,7 @@ func (c *Controller) handleDelIptablesEipFinalizer(key string) error { return nil } newIptablesEip := cachedIptablesEip.DeepCopy() - controllerutil.RemoveFinalizer(newIptablesEip, util.FinalizerName) + controllerutil.RemoveFinalizer(newIptablesEip, util.KubeOVNControllerFinalizer) patch, err := util.GenerateMergePatchPayload(cachedIptablesEip, newIptablesEip) if err != nil { klog.Errorf("failed to generate patch payload for iptables eip '%s', %v", cachedIptablesEip.Name, err) diff --git a/pkg/controller/vpc_nat_gw_nat.go b/pkg/controller/vpc_nat_gw_nat.go index b5895f725e78..1ac75aded9fb 100644 --- a/pkg/controller/vpc_nat_gw_nat.go +++ b/pkg/controller/vpc_nat_gw_nat.go @@ -1055,24 +1055,18 @@ func (c *Controller) syncIptablesFipFinalizer() error { return err } for _, cachedFip := range fips { - if len(cachedFip.Finalizers) == 0 { - continue - } - if slices.Contains(cachedFip.Finalizers, util.DepreciatedFinalizerName) { - newFip := cachedFip.DeepCopy() - controllerutil.RemoveFinalizer(newFip, util.DepreciatedFinalizerName) - controllerutil.AddFinalizer(newFip, util.FinalizerName) - patch, err := util.GenerateMergePatchPayload(cachedFip, newFip) - if err != nil { - klog.Errorf("failed to generate patch payload for fip %s, %v", newFip.Name, err) - return err - } - if _, err := c.config.KubeOvnClient.KubeovnV1().IptablesFIPRules().Patch(context.Background(), newFip.Name, + patch, err := c.ReplaceFinalizer(cachedFip) + if err != nil { + klog.Errorf("failed to sync finalizer for fip %s, %v", cachedFip.Name, err) + return err + } + if patch != nil { + if _, err := c.config.KubeOvnClient.KubeovnV1().IptablesFIPRules().Patch(context.Background(), cachedFip.Name, types.MergePatchType, patch, metav1.PatchOptions{}, ""); err != nil { if k8serrors.IsNotFound(err) { return nil } - klog.Errorf("failed to sync finalizer for fip %s, %v", newFip.Name, err) + klog.Errorf("failed to sync finalizer for fip %s, %v", cachedFip.Name, err) return err } } @@ -1090,12 +1084,12 @@ func (c *Controller) handleAddIptablesFipFinalizer(key string) error { return err } if cachedIptablesFip.DeletionTimestamp.IsZero() { - if slices.Contains(cachedIptablesFip.Finalizers, util.FinalizerName) { + if slices.Contains(cachedIptablesFip.Finalizers, util.KubeOVNControllerFinalizer) { return nil } } newIptablesFip := cachedIptablesFip.DeepCopy() - controllerutil.AddFinalizer(newIptablesFip, util.FinalizerName) + controllerutil.AddFinalizer(newIptablesFip, util.KubeOVNControllerFinalizer) patch, err := util.GenerateMergePatchPayload(cachedIptablesFip, newIptablesFip) if err != nil { klog.Errorf("failed to generate patch payload for iptables fip '%s', %v", cachedIptablesFip.Name, err) @@ -1125,7 +1119,7 @@ func (c *Controller) handleDelIptablesFipFinalizer(key string) error { return nil } newIptablesFip := cachedIptablesFip.DeepCopy() - controllerutil.RemoveFinalizer(newIptablesFip, util.FinalizerName) + controllerutil.RemoveFinalizer(newIptablesFip, util.KubeOVNControllerFinalizer) patch, err := util.GenerateMergePatchPayload(cachedIptablesFip, newIptablesFip) if err != nil { klog.Errorf("failed to generate patch payload for iptables fip '%s', %v", cachedIptablesFip.Name, err) @@ -1153,24 +1147,18 @@ func (c *Controller) syncIptablesDnatFinalizer() error { return err } for _, cachedDnat := range dnats { - if len(cachedDnat.Finalizers) == 0 { - continue - } - if slices.Contains(cachedDnat.Finalizers, util.DepreciatedFinalizerName) { - newDnat := cachedDnat.DeepCopy() - controllerutil.RemoveFinalizer(newDnat, util.DepreciatedFinalizerName) - controllerutil.AddFinalizer(newDnat, util.FinalizerName) - patch, err := util.GenerateMergePatchPayload(cachedDnat, newDnat) - if err != nil { - klog.Errorf("failed to generate patch payload for dnat %s, %v", newDnat.Name, err) - return err - } - if _, err := c.config.KubeOvnClient.KubeovnV1().IptablesDnatRules().Patch(context.Background(), newDnat.Name, + patch, err := c.ReplaceFinalizer(cachedDnat) + if err != nil { + klog.Errorf("failed to sync finalizer for dnat %s, %v", cachedDnat.Name, err) + return err + } + if patch != nil { + if _, err := c.config.KubeOvnClient.KubeovnV1().IptablesDnatRules().Patch(context.Background(), cachedDnat.Name, types.MergePatchType, patch, metav1.PatchOptions{}, ""); err != nil { if k8serrors.IsNotFound(err) { return nil } - klog.Errorf("failed to sync finalizer for dnat %s, %v", newDnat.Name, err) + klog.Errorf("failed to sync finalizer for dnat %s, %v", cachedDnat.Name, err) return err } } @@ -1188,12 +1176,12 @@ func (c *Controller) handleAddIptablesDnatFinalizer(key string) error { return err } if cachedIptablesDnat.DeletionTimestamp.IsZero() { - if slices.Contains(cachedIptablesDnat.Finalizers, util.FinalizerName) { + if slices.Contains(cachedIptablesDnat.Finalizers, util.KubeOVNControllerFinalizer) { return nil } } newIptablesDnat := cachedIptablesDnat.DeepCopy() - controllerutil.AddFinalizer(newIptablesDnat, util.FinalizerName) + controllerutil.AddFinalizer(newIptablesDnat, util.KubeOVNControllerFinalizer) patch, err := util.GenerateMergePatchPayload(cachedIptablesDnat, newIptablesDnat) if err != nil { klog.Errorf("failed to generate patch payload for iptables dnat '%s', %v", cachedIptablesDnat.Name, err) @@ -1223,7 +1211,7 @@ func (c *Controller) handleDelIptablesDnatFinalizer(key string) error { return nil } newIptablesDnat := cachedIptablesDnat.DeepCopy() - controllerutil.RemoveFinalizer(newIptablesDnat, util.FinalizerName) + controllerutil.RemoveFinalizer(newIptablesDnat, util.KubeOVNControllerFinalizer) patch, err := util.GenerateMergePatchPayload(cachedIptablesDnat, newIptablesDnat) if err != nil { klog.Errorf("failed to generate patch payload for iptables dnat '%s', %v", cachedIptablesDnat.Name, err) @@ -1302,24 +1290,18 @@ func (c *Controller) syncIptablesSnatFinalizer() error { return err } for _, cachedSnat := range snats { - if len(cachedSnat.Finalizers) == 0 { - continue - } - if slices.Contains(cachedSnat.Finalizers, util.DepreciatedFinalizerName) { - newSnat := cachedSnat.DeepCopy() - controllerutil.RemoveFinalizer(newSnat, util.DepreciatedFinalizerName) - controllerutil.AddFinalizer(newSnat, util.FinalizerName) - patch, err := util.GenerateMergePatchPayload(cachedSnat, newSnat) - if err != nil { - klog.Errorf("failed to generate patch payload for dnat %s, %v", newSnat.Name, err) - return err - } - if _, err := c.config.KubeOvnClient.KubeovnV1().IptablesSnatRules().Patch(context.Background(), newSnat.Name, + patch, err := c.ReplaceFinalizer(cachedSnat) + if err != nil { + klog.Errorf("failed to sync finalizer for snat %s, %v", cachedSnat.Name, err) + return err + } + if patch != nil { + if _, err := c.config.KubeOvnClient.KubeovnV1().IptablesSnatRules().Patch(context.Background(), cachedSnat.Name, types.MergePatchType, patch, metav1.PatchOptions{}, ""); err != nil { if k8serrors.IsNotFound(err) { return nil } - klog.Errorf("failed to sync finalizer for dnat %s, %v", newSnat.Name, err) + klog.Errorf("failed to sync finalizer for dnat %s, %v", cachedSnat.Name, err) return err } } @@ -1337,12 +1319,12 @@ func (c *Controller) handleAddIptablesSnatFinalizer(key string) error { return err } if cachedIptablesSnat.DeletionTimestamp.IsZero() { - if slices.Contains(cachedIptablesSnat.Finalizers, util.FinalizerName) { + if slices.Contains(cachedIptablesSnat.Finalizers, util.KubeOVNControllerFinalizer) { return nil } } newIptablesSnat := cachedIptablesSnat.DeepCopy() - controllerutil.AddFinalizer(newIptablesSnat, util.FinalizerName) + controllerutil.AddFinalizer(newIptablesSnat, util.KubeOVNControllerFinalizer) patch, err := util.GenerateMergePatchPayload(cachedIptablesSnat, newIptablesSnat) if err != nil { klog.Errorf("failed to generate patch payload for iptables snat '%s', %v", cachedIptablesSnat.Name, err) @@ -1372,7 +1354,7 @@ func (c *Controller) handleDelIptablesSnatFinalizer(key string) error { return nil } newIptablesSnat := cachedIptablesSnat.DeepCopy() - controllerutil.RemoveFinalizer(newIptablesSnat, util.FinalizerName) + controllerutil.RemoveFinalizer(newIptablesSnat, util.KubeOVNControllerFinalizer) patch, err := util.GenerateMergePatchPayload(cachedIptablesSnat, newIptablesSnat) if err != nil { klog.Errorf("failed to generate patch payload for iptables snat '%s', %v", cachedIptablesSnat.Name, err) diff --git a/pkg/util/const.go b/pkg/util/const.go index e68dd8173a06..6a391d89f4fe 100644 --- a/pkg/util/const.go +++ b/pkg/util/const.go @@ -3,8 +3,8 @@ package util const ( CniTypeName = "kube-ovn" - DepreciatedFinalizerName = "kube-ovn-controller" - KubeOVNControllerFinalizer = "kubeovn.io/kube-ovn-controller" + DepreciatedFinalizerName = "kube-ovn-controller" + KubeOVNControllerFinalizer = "kubeovn.io/kube-ovn-controller" AllocatedAnnotation = "ovn.kubernetes.io/allocated" RoutedAnnotation = "ovn.kubernetes.io/routed" diff --git a/test/e2e/kube-ovn/subnet/subnet.go b/test/e2e/kube-ovn/subnet/subnet.go index 395de008ea43..8c784c3ba884 100644 --- a/test/e2e/kube-ovn/subnet/subnet.go +++ b/test/e2e/kube-ovn/subnet/subnet.go @@ -148,7 +148,7 @@ var _ = framework.Describe("[group:subnet]", func() { subnet = subnetClient.CreateSync(subnet) ginkgo.By("Validating subnet finalizers") - framework.ExpectContainElement(subnet.Finalizers, util.FinalizerName) + framework.ExpectContainElement(subnet.Finalizers, util.KubeOVNControllerFinalizer) ginkgo.By("Validating subnet spec fields") framework.ExpectFalse(subnet.Spec.Default) @@ -206,7 +206,7 @@ var _ = framework.Describe("[group:subnet]", func() { subnet = subnetClient.CreateSync(subnet) ginkgo.By("Validating subnet finalizers") - framework.ExpectContainElement(subnet.ObjectMeta.Finalizers, util.FinalizerName) + framework.ExpectContainElement(subnet.ObjectMeta.Finalizers, util.KubeOVNControllerFinalizer) ginkgo.By("Validating subnet spec fields") framework.ExpectFalse(subnet.Spec.Default) @@ -251,7 +251,7 @@ var _ = framework.Describe("[group:subnet]", func() { subnet = subnetClient.CreateSync(subnet) ginkgo.By("Validating subnet finalizers") - framework.ExpectContainElement(subnet.ObjectMeta.Finalizers, util.FinalizerName) + framework.ExpectContainElement(subnet.ObjectMeta.Finalizers, util.KubeOVNControllerFinalizer) ginkgo.By("Validating subnet spec fields") framework.ExpectFalse(subnet.Spec.Default) @@ -301,7 +301,7 @@ var _ = framework.Describe("[group:subnet]", func() { subnet = subnetClient.CreateSync(subnet) ginkgo.By("Validating subnet finalizers") - framework.ExpectContainElement(subnet.Finalizers, util.FinalizerName) + framework.ExpectContainElement(subnet.Finalizers, util.KubeOVNControllerFinalizer) ginkgo.By("Validating subnet spec fields") framework.ExpectFalse(subnet.Spec.Default) @@ -345,7 +345,7 @@ var _ = framework.Describe("[group:subnet]", func() { subnet = subnetClient.CreateSync(subnet) ginkgo.By("Validating subnet finalizers") - framework.ExpectContainElement(subnet.Finalizers, util.FinalizerName) + framework.ExpectContainElement(subnet.Finalizers, util.KubeOVNControllerFinalizer) ginkgo.By("Validating subnet spec fields") framework.ExpectFalse(subnet.Spec.Default) @@ -388,7 +388,7 @@ var _ = framework.Describe("[group:subnet]", func() { subnet = subnetClient.PatchSync(subnet, modifiedSubnet) ginkgo.By("Validating subnet finalizers") - framework.ExpectContainElement(subnet.ObjectMeta.Finalizers, util.FinalizerName) + framework.ExpectContainElement(subnet.ObjectMeta.Finalizers, util.KubeOVNControllerFinalizer) ginkgo.By("Validating subnet spec fields") framework.ExpectFalse(subnet.Spec.Default) @@ -444,7 +444,7 @@ var _ = framework.Describe("[group:subnet]", func() { subnet = subnetClient.CreateSync(subnet) ginkgo.By("Validating subnet finalizers") - framework.ExpectContainElement(subnet.Finalizers, util.FinalizerName) + framework.ExpectContainElement(subnet.Finalizers, util.KubeOVNControllerFinalizer) ginkgo.By("Validating centralized subnet with active-standby mode") framework.ExpectFalse(subnet.Spec.EnableEcmp) @@ -947,7 +947,7 @@ var _ = framework.Describe("[group:subnet]", func() { subnet = subnetClient.CreateSync(subnet) ginkgo.By("Validating subnet load-balancer records exist") - framework.ExpectContainElement(subnet.Finalizers, util.FinalizerName) + framework.ExpectContainElement(subnet.Finalizers, util.KubeOVNControllerFinalizer) execCmd := "kubectl ko nbctl --format=csv --data=bare --no-heading --columns=load_balancer find logical-switch " + fmt.Sprintf("name=%s", subnetName) output, err := exec.Command("bash", "-c", execCmd).CombinedOutput() framework.ExpectNoError(err)