Skip to content

Commit

Permalink
fix: should use domain-qualified finalizer name (#3748)
Browse files Browse the repository at this point in the history
* fix: should use domain-qualified finalizer name

---------

Signed-off-by: bobz965 <[email protected]>
Co-authored-by: 张祖建 <[email protected]>
  • Loading branch information
zbb88888 and zhangzujian authored Feb 21, 2024
1 parent 0405b92 commit d739b41
Show file tree
Hide file tree
Showing 14 changed files with 487 additions and 41 deletions.
4 changes: 4 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,10 @@ func (c *Controller) Run(ctx context.Context) {
util.LogFatalAndExit(err, "failed to sync crd ips")
}

if err := c.syncFinalizers(); err != nil {
util.LogFatalAndExit(err, "failed to initialize crd finalizers")
}

if err := c.InitIPAM(); err != nil {
util.LogFatalAndExit(err, "failed to initialize ipam")
}
Expand Down
74 changes: 73 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.ControllerName) {
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 @@ -827,3 +829,73 @@ func (c *Controller) initNodeChassis() error {
}
return nil
}

func (c *Controller) syncFinalizers() error {
// migrate depreciated finalizer to new finalizer
klog.Info("start to sync finalizers")
if err := c.syncIPFinalizer(); err != nil {
klog.Errorf("failed to sync ip finalizer: %v", err)
return err
}
if err := c.syncOvnDnatFinalizer(); err != nil {
klog.Errorf("failed to sync ovn dnat finalizer: %v", err)
return err
}
if err := c.syncOvnEipFinalizer(); err != nil {
klog.Errorf("failed to sync ovn eip finalizer: %v", err)
return err
}
if err := c.syncOvnFipFinalizer(); err != nil {
klog.Errorf("failed to sync ovn fip finalizer: %v", err)
return err
}
if err := c.syncOvnSnatFinalizer(); err != nil {
klog.Errorf("failed to sync ovn snat finalizer: %v", err)
return err
}
if err := c.syncQoSPolicyFinalizer(); err != nil {
klog.Errorf("failed to sync qos policy finalizer: %v", err)
return err
}
if err := c.syncSubnetFinalizer(); err != nil {
klog.Errorf("failed to sync subnet finalizer: %v", err)
return err
}
if err := c.syncVipFinalizer(); err != nil {
klog.Errorf("failed to sync vip finalizer: %v", err)
return err
}
if err := c.syncIptablesEipFinalizer(); err != nil {
klog.Errorf("failed to sync iptables eip finalizer: %v", err)
return err
}
if err := c.syncIptablesFipFinalizer(); err != nil {
klog.Errorf("failed to sync iptables fip finalizer: %v", err)
return err
}
if err := c.syncIptablesDnatFinalizer(); err != nil {
klog.Errorf("failed to sync iptables dnat finalizer: %v", err)
return err
}
if err := c.syncIptablesSnatFinalizer(); err != nil {
klog.Errorf("failed to sync iptables snat finalizer: %v", err)
return err
}
klog.Info("sync finalizers done")
return nil
}

func (c *Controller) ReplaceFinalizer(cachedObj client.Object) ([]byte, error) {
if controllerutil.ContainsFinalizer(cachedObj, 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
}
35 changes: 33 additions & 2 deletions pkg/controller/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
Expand Down Expand Up @@ -323,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.ControllerName); err != nil {
if err = c.handleDelIPFinalizer(cachedIP, util.KubeOVNControllerFinalizer); err != nil {
klog.Errorf("failed to handle del ip finalizer %v", err)
return err
}
Expand All @@ -341,6 +342,36 @@ func (c *Controller) handleDelIP(ip *kubeovnv1.IP) error {
return nil
}

func (c *Controller) syncIPFinalizer() error {
// migrate depreciated finalizer to new finalizer
ips, err := c.ipsLister.List(labels.Everything())
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Errorf("failed to list ips, %v", err)
return err
}
for _, cachedIP := range ips {
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", cachedIP.Name, err)
return err
}
}
}
return nil
}

func (c *Controller) handleAddIPFinalizer(cachedIP *kubeovnv1.IP, finalizer string) error {
if cachedIP.DeletionTimestamp.IsZero() {
if slices.Contains(cachedIP.Finalizers, finalizer) {
Expand Down Expand Up @@ -535,7 +566,7 @@ func (c *Controller) createOrUpdateIPCR(ipCRName, podName, ip, mac, subnetName,
}
}

if err := c.handleAddIPFinalizer(ipCR, util.ControllerName); 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
37 changes: 35 additions & 2 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.ControllerName); 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.ControllerName); 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 @@ -660,6 +660,39 @@ func (c *Controller) DelDnatRule(vpcName, dnatName, externalIP, externalPort str
return nil
}

func (c *Controller) syncOvnDnatFinalizer() error {
// migrate depreciated finalizer to new finalizer
dnats, err := c.ovnDnatRulesLister.List(labels.Everything())
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Errorf("failed to list dnats, %v", err)
return err
}
for _, cachedDnat := range dnats {
if len(cachedDnat.Finalizers) == 0 {
continue
}
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", cachedDnat.Name, err)
return err
}
}
}
return nil
}

func (c *Controller) handleAddOvnDnatFinalizer(cachedDnat *kubeovnv1.OvnDnatRule, finalizer string) error {
if cachedDnat.DeletionTimestamp.IsZero() {
if slices.Contains(cachedDnat.Finalizers, finalizer) {
Expand Down
37 changes: 35 additions & 2 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.ControllerName); 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.ControllerName); 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 @@ -584,6 +584,39 @@ func (c *Controller) natLabelAndAnnoOvnEip(eipName, natName, vpcName string) err
return err
}

func (c *Controller) syncOvnEipFinalizer() error {
// migrate depreciated finalizer to new finalizer
eips, err := c.ovnEipsLister.List(labels.Everything())
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Errorf("failed to list eips, %v", err)
return err
}
for _, cachedEip := range eips {
if len(cachedEip.Finalizers) == 0 {
continue
}
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", cachedEip.Name, err)
return err
}
}
}
return nil
}

func (c *Controller) handleAddOvnEipFinalizer(cachedEip *kubeovnv1.OvnEip, finalizer string) error {
if cachedEip.DeletionTimestamp.IsZero() {
if slices.Contains(cachedEip.Finalizers, finalizer) {
Expand Down
34 changes: 32 additions & 2 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.ControllerName); 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.ControllerName); 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 @@ -599,6 +599,36 @@ func (c *Controller) GetOvnEip(eipName string) (*kubeovnv1.OvnEip, error) {
return cachedEip, nil
}

func (c *Controller) syncOvnFipFinalizer() error {
// migrate depreciated finalizer to new finalizer
fips, err := c.ovnFipsLister.List(labels.Everything())
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Errorf("failed to list fips, %v", err)
return err
}
for _, cachedFip := range fips {
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", cachedFip.Name, err)
return err
}
}
}
return nil
}

func (c *Controller) handleAddOvnFipFinalizer(cachedFip *kubeovnv1.OvnFip, finalizer string) error {
if cachedFip.DeletionTimestamp.IsZero() {
if slices.Contains(cachedFip.Finalizers, finalizer) {
Expand Down
35 changes: 33 additions & 2 deletions pkg/controller/ovn_snat.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
Expand Down Expand Up @@ -257,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.ControllerName); 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 @@ -419,7 +420,7 @@ func (c *Controller) handleDelOvnSnatRule(key string) error {
return err
}
}
if err = c.handleDelOvnSnatFinalizer(cachedSnat, util.ControllerName); 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 @@ -542,6 +543,36 @@ func (c *Controller) ovnSnatChangeEip(snat *kubeovnv1.OvnSnatRule, eip *kubeovnv
return false
}

func (c *Controller) syncOvnSnatFinalizer() error {
// migrate depreciated finalizer to new finalizer
snats, err := c.ovnSnatRulesLister.List(labels.Everything())
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Errorf("failed to list snats, %v", err)
return err
}
for _, cachedSnat := range snats {
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", cachedSnat.Name, err)
return err
}
}
}
return nil
}

func (c *Controller) handleAddOvnSnatFinalizer(cachedSnat *kubeovnv1.OvnSnatRule, finalizer string) error {
if cachedSnat.DeletionTimestamp.IsZero() {
if slices.Contains(cachedSnat.Finalizers, finalizer) {
Expand Down
Loading

0 comments on commit d739b41

Please sign in to comment.