Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

update finalizer #159

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 12 additions & 29 deletions pkg/hub/managedcluster/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
listerv1 "open-cluster-management.io/api/client/cluster/listers/cluster/v1"
v1 "open-cluster-management.io/api/cluster/v1"
"open-cluster-management.io/registration/pkg/helpers"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
Expand Down Expand Up @@ -78,26 +79,26 @@ func (c *managedClusterController) sync(ctx context.Context, syncCtx factory.Syn

managedCluster = managedCluster.DeepCopy()
if managedCluster.DeletionTimestamp.IsZero() {
hasFinalizer := false
for i := range managedCluster.Finalizers {
if managedCluster.Finalizers[i] == managedClusterFinalizer {
hasFinalizer = true
break
if !controllerutil.ContainsFinalizer(managedCluster, managedClusterFinalizer) {
controllerutil.AddFinalizer(managedCluster, managedClusterFinalizer)
if _, err := c.clusterClient.ClusterV1().ManagedClusters().Update(ctx, managedCluster, metav1.UpdateOptions{}); err != nil {
return err
}
}
if !hasFinalizer {
managedCluster.Finalizers = append(managedCluster.Finalizers, managedClusterFinalizer)
_, err := c.clusterClient.ClusterV1().ManagedClusters().Update(ctx, managedCluster, metav1.UpdateOptions{})
return err
}
}

// Spoke cluster is deleting, we remove its related resources
if !managedCluster.DeletionTimestamp.IsZero() {
if err := c.removeManagedClusterResources(ctx, managedClusterName); err != nil {
return err
}
return c.removeManagedClusterFinalizer(ctx, managedCluster)
if controllerutil.ContainsFinalizer(managedCluster, managedClusterFinalizer) {
controllerutil.RemoveFinalizer(managedCluster, managedClusterFinalizer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hasn't take a look at the details of RemoveFinalizer, will it ensure the ordering after a deletion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't understand the ordering meaing, did you say the order of finalizes? Why we need order? RemoveFinalizer will not change finalize order.

			f = append(f[:i], f[i+1:]...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the ordering of the finalizers. Some controllers may rely on the ordering of the finalizers, it is not normal but it happens.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RemoveFinalizer will keep the order. By the way, could you specify which controller use this feature?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not in registration, but an external consumer (controller) could do that.

if _, err := c.clusterClient.ClusterV1().ManagedClusters().Update(ctx, managedCluster, metav1.UpdateOptions{}); err != nil {
return err
}
}
return nil
}

if !managedCluster.Spec.HubAcceptsClient {
Expand Down Expand Up @@ -196,21 +197,3 @@ func (c *managedClusterController) removeManagedClusterResources(ctx context.Con
}
return operatorhelpers.NewMultiLineAggregate(errs)
}

func (c *managedClusterController) removeManagedClusterFinalizer(ctx context.Context, managedCluster *v1.ManagedCluster) error {
copiedFinalizers := []string{}
for i := range managedCluster.Finalizers {
if managedCluster.Finalizers[i] == managedClusterFinalizer {
continue
}
copiedFinalizers = append(copiedFinalizers, managedCluster.Finalizers[i])
}

if len(managedCluster.Finalizers) != len(copiedFinalizers) {
managedCluster.Finalizers = copiedFinalizers
_, err := c.clusterClient.ClusterV1().ManagedClusters().Update(ctx, managedCluster, metav1.UpdateOptions{})
return err
}

return nil
}
1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,7 @@ sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client
sigs.k8s.io/controller-runtime/pkg/client
sigs.k8s.io/controller-runtime/pkg/client/apiutil
sigs.k8s.io/controller-runtime/pkg/client/config
sigs.k8s.io/controller-runtime/pkg/controller/controllerutil
sigs.k8s.io/controller-runtime/pkg/envtest
sigs.k8s.io/controller-runtime/pkg/envtest/printer
sigs.k8s.io/controller-runtime/pkg/internal/log
Expand Down