Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
Signed-off-by: bobz965 <[email protected]>
  • Loading branch information
zbb88888 committed Feb 21, 2024
1 parent d689c85 commit 4c2b5ec
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 202 deletions.
19 changes: 18 additions & 1 deletion pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
26 changes: 10 additions & 16 deletions pkg/controller/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
Expand Down
23 changes: 10 additions & 13 deletions pkg/controller/ovn_dnat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
}
Expand Down
23 changes: 10 additions & 13 deletions pkg/controller/ovn_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
}
Expand Down
26 changes: 10 additions & 16 deletions pkg/controller/ovn_fip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
}
Expand Down
26 changes: 10 additions & 16 deletions pkg/controller/ovn_snat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
}
Expand Down
26 changes: 10 additions & 16 deletions pkg/controller/qos_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 4c2b5ec

Please sign in to comment.