Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace runtime.Object with metav1.Object in pkg/util/finalizers #270

Merged
merged 1 commit into from
Nov 28, 2023
Merged
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
14 changes: 4 additions & 10 deletions pkg/controllers/federate/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,17 +417,14 @@
) (*unstructured.Unstructured, error) {
logger := klog.FromContext(ctx).WithValues("finalizer", FinalizerFederateController)

isUpdated, err := finalizersutil.AddFinalizers(sourceObj, sets.NewString(FinalizerFederateController))
if err != nil {
return nil, fmt.Errorf("failed to add finalizer to source object: %w", err)
}
isUpdated := finalizersutil.AddFinalizers(sourceObj, sets.New(FinalizerFederateController))

Check warning on line 420 in pkg/controllers/federate/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/federate/controller.go#L420

Added line #L420 was not covered by tests
if !isUpdated {
return sourceObj, nil
}

logger.V(1).Info("Adding finalizer to source object")

sourceObj, err = c.dynamicClient.Resource(sourceGVR).Namespace(sourceObj.GetNamespace()).Update(
sourceObj, err := c.dynamicClient.Resource(sourceGVR).Namespace(sourceObj.GetNamespace()).Update(

Check warning on line 427 in pkg/controllers/federate/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/federate/controller.go#L427

Added line #L427 was not covered by tests
ctx,
sourceObj,
metav1.UpdateOptions{},
Expand All @@ -446,17 +443,14 @@
) (*unstructured.Unstructured, error) {
logger := klog.FromContext(ctx).WithValues("finalizer", FinalizerFederateController)

isUpdated, err := finalizersutil.RemoveFinalizers(sourceObj, sets.NewString(FinalizerFederateController))
if err != nil {
return nil, fmt.Errorf("failed to remove finalizer from source object: %w", err)
}
isUpdated := finalizersutil.RemoveFinalizers(sourceObj, sets.New(FinalizerFederateController))

Check warning on line 446 in pkg/controllers/federate/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/federate/controller.go#L446

Added line #L446 was not covered by tests
if !isUpdated {
return sourceObj, nil
}

logger.V(1).Info("Removing finalizer from source object")

sourceObj, err = c.dynamicClient.Resource(sourceGVR).Namespace(sourceObj.GetNamespace()).Update(
sourceObj, err := c.dynamicClient.Resource(sourceGVR).Namespace(sourceObj.GetNamespace()).Update(

Check warning on line 453 in pkg/controllers/federate/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/federate/controller.go#L453

Added line #L453 was not covered by tests
ctx,
sourceObj,
metav1.UpdateOptions{},
Expand Down
5 changes: 1 addition & 4 deletions pkg/controllers/federatedcluster/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,7 @@
ctx context.Context,
cluster *fedcorev1a1.FederatedCluster,
) (*fedcorev1a1.FederatedCluster, error) {
updated, err := finalizers.AddFinalizers(cluster, sets.NewString(FinalizerFederatedClusterController))
if err != nil {
return nil, err
}
updated := finalizers.AddFinalizers(cluster, sets.New(FinalizerFederatedClusterController))

Check warning on line 356 in pkg/controllers/federatedcluster/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/federatedcluster/controller.go#L356

Added line #L356 was not covered by tests

if updated {
return c.fedClient.CoreV1alpha1().FederatedClusters().Update(ctx, cluster, metav1.UpdateOptions{})
Expand Down
31 changes: 13 additions & 18 deletions pkg/controllers/sync/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,12 +624,7 @@
)
continue
}
hasFinalizer, err := finalizersutil.HasFinalizer(cluster, FinalizerCascadingDelete)
if err != nil {
shouldRecheckAfterDispatch = true
dispatcher.RecordClusterError(fedcorev1a1.FinalizerCheckFailed, clusterName, err)
continue
}
hasFinalizer := finalizersutil.HasFinalizer(cluster, FinalizerCascadingDelete)

Check warning on line 627 in pkg/controllers/sync/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/sync/controller.go#L627

Added line #L627 was not covered by tests
if !hasFinalizer {
// we should not sync before finalizer is added
shouldRecheckAfterDispatch = true
Expand Down Expand Up @@ -971,9 +966,9 @@
ctx, keyedLogger := logging.InjectLoggerValues(ctx, "finalizer-name", FinalizerSyncController)

obj := fedResource.Object()
isUpdated, err := finalizersutil.AddFinalizers(obj, sets.NewString(FinalizerSyncController))
if err != nil || !isUpdated {
return err
isUpdated := finalizersutil.AddFinalizers(obj, sets.New(FinalizerSyncController))
if !isUpdated {
return nil

Check warning on line 971 in pkg/controllers/sync/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/sync/controller.go#L969-L971

Added lines #L969 - L971 were not covered by tests
}

keyedLogger.V(1).Info("Adding finalizer to federated object")
Expand All @@ -995,9 +990,9 @@
ctx, keyedLogger := logging.InjectLoggerValues(ctx, "finalizer-name", FinalizerSyncController)

obj := fedResource.Object()
isUpdated, err := finalizersutil.RemoveFinalizers(obj, sets.NewString(FinalizerSyncController))
if err != nil || !isUpdated {
return err
isUpdated := finalizersutil.RemoveFinalizers(obj, sets.New(FinalizerSyncController))
if !isUpdated {
return nil

Check warning on line 995 in pkg/controllers/sync/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/sync/controller.go#L993-L995

Added lines #L993 - L995 were not covered by tests
}

keyedLogger.V(1).Info("Removing finalizer from federated object")
Expand All @@ -1024,9 +1019,9 @@
if err != nil {
return err
}
isUpdated, err := finalizersutil.AddFinalizers(cluster, sets.NewString(FinalizerCascadingDelete))
if err != nil || !isUpdated {
return err
isUpdated := finalizersutil.AddFinalizers(cluster, sets.New(FinalizerCascadingDelete))
if !isUpdated {
return nil

Check warning on line 1024 in pkg/controllers/sync/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/sync/controller.go#L1022-L1024

Added lines #L1022 - L1024 were not covered by tests
}
cluster, err = s.fedClient.CoreV1alpha1().FederatedClusters().Update(ctx, cluster, metav1.UpdateOptions{})
return err
Expand All @@ -1046,9 +1041,9 @@
if err != nil {
return err
}
isUpdated, err := finalizersutil.RemoveFinalizers(cluster, sets.NewString(FinalizerCascadingDelete))
if err != nil || !isUpdated {
return err
isUpdated := finalizersutil.RemoveFinalizers(cluster, sets.New(FinalizerCascadingDelete))
if !isUpdated {
return nil

Check warning on line 1046 in pkg/controllers/sync/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/sync/controller.go#L1044-L1046

Added lines #L1044 - L1046 were not covered by tests
}
cluster, err = s.fedClient.CoreV1alpha1().FederatedClusters().Update(ctx, cluster, metav1.UpdateOptions{})
return err
Expand Down
29 changes: 6 additions & 23 deletions pkg/controllers/sync/dispatch/unmanaged.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,9 @@
// Avoid mutating the resource in the informer cache
clusterObj := clusterObj.DeepCopy()

needUpdate, err := removeRetainObjectFinalizer(clusterObj)
if err != nil {
if d.recorder == nil {
wrappedErr := d.wrapOperationError(err, clusterName, op)
keyedLogger.Error(wrappedErr, "Failed to delete target object in cluster")
} else {
d.recorder.recordOperationError(ctx, fedcorev1a1.DeletionFailed, clusterName, op, err)
}
result = false
return result
}
needUpdate := removeRetainObjectFinalizer(clusterObj)

Check warning on line 135 in pkg/controllers/sync/dispatch/unmanaged.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/sync/dispatch/unmanaged.go#L135

Added line #L135 was not covered by tests
if needUpdate {
var err error

Check warning on line 137 in pkg/controllers/sync/dispatch/unmanaged.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/sync/dispatch/unmanaged.go#L137

Added line #L137 was not covered by tests
clusterObj, err = client.Resource(d.targetGVR).Namespace(targetName.Namespace).Update(
ctx,
clusterObj,
Expand Down Expand Up @@ -177,7 +168,7 @@
// To avoid this, we explicitly set the PropagationPolicy to Background like `kubectl delete` does by default.
// Ref: https://github.com/kubernetes/kubernetes/pull/65908
deletionPropagation := metav1.DeletePropagationBackground
err = client.Resource(d.targetGVR).Namespace(targetName.Namespace).Delete(
err := client.Resource(d.targetGVR).Namespace(targetName.Namespace).Delete(

Check warning on line 171 in pkg/controllers/sync/dispatch/unmanaged.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/sync/dispatch/unmanaged.go#L171

Added line #L171 was not covered by tests
ctx,
targetName.Name,
metav1.DeleteOptions{PropagationPolicy: &deletionPropagation},
Expand Down Expand Up @@ -218,15 +209,7 @@
updateObj := clusterObj.DeepCopy()

managedlabel.RemoveManagedLabel(updateObj)
if _, err := removeRetainObjectFinalizer(updateObj); err != nil {
if d.recorder == nil {
wrappedErr := d.wrapOperationError(err, clusterName, op)
keyedLogger.Error(wrappedErr, "Failed to remove managed label from target object in cluster")
} else {
d.recorder.recordOperationError(ctx, fedcorev1a1.LabelRemovalFailed, clusterName, op, err)
}
return false
}
removeRetainObjectFinalizer(updateObj)

Check warning on line 212 in pkg/controllers/sync/dispatch/unmanaged.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/sync/dispatch/unmanaged.go#L212

Added line #L212 was not covered by tests

var err error
if _, err = client.Resource(d.targetGVR).Namespace(clusterObj.GetNamespace()).Update(
Expand Down Expand Up @@ -258,6 +241,6 @@
return errors.Wrapf(err, "Failed to "+eventTemplate, operation, targetGVR, targetName, clusterName)
}

func removeRetainObjectFinalizer(obj *unstructured.Unstructured) (bool, error) {
return finalizers.RemoveFinalizers(obj, sets.NewString(RetainTerminatingObjectFinalizer))
func removeRetainObjectFinalizer(obj *unstructured.Unstructured) bool {
return finalizers.RemoveFinalizers(obj, sets.New(RetainTerminatingObjectFinalizer))

Check warning on line 245 in pkg/controllers/sync/dispatch/unmanaged.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/sync/dispatch/unmanaged.go#L244-L245

Added lines #L244 - L245 were not covered by tests
}
15 changes: 4 additions & 11 deletions pkg/controllers/sync/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@
return nil, err
}

if err := addRetainObjectFinalizer(obj); err != nil {
return nil, err
}
addRetainObjectFinalizer(obj)

Check warning on line 180 in pkg/controllers/sync/resource.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/sync/resource.go#L180

Added line #L180 was not covered by tests
case common.ServiceGVK:
if err := dropServiceFields(obj); err != nil {
return nil, err
Expand All @@ -189,19 +187,14 @@
return nil, err
}

if err := addRetainObjectFinalizer(obj); err != nil {
return nil, err
}
addRetainObjectFinalizer(obj)

Check warning on line 190 in pkg/controllers/sync/resource.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/sync/resource.go#L190

Added line #L190 was not covered by tests
}

return obj, nil
}

func addRetainObjectFinalizer(obj *unstructured.Unstructured) error {
if _, err := finalizers.AddFinalizers(obj, sets.NewString(dispatch.RetainTerminatingObjectFinalizer)); err != nil {
return err
}
return nil
func addRetainObjectFinalizer(obj *unstructured.Unstructured) {
finalizers.AddFinalizers(obj, sets.New(dispatch.RetainTerminatingObjectFinalizer))

Check warning on line 197 in pkg/controllers/sync/resource.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/sync/resource.go#L196-L197

Added lines #L196 - L197 were not covered by tests
}

func dropJobFields(obj *unstructured.Unstructured) error {
Expand Down
41 changes: 14 additions & 27 deletions pkg/util/finalizers/finalizers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,49 +22,36 @@ are Copyright 2023 The KubeAdmiral Authors.
package finalizers

import (
meta "k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
)

// HasFinalizer returns true if the given object has the given finalizer in its ObjectMeta.
func HasFinalizer(obj runtime.Object, finalizer string) (bool, error) {
accessor, err := meta.Accessor(obj)
if err != nil {
return false, err
}
finalizers := sets.NewString(accessor.GetFinalizers()...)
return finalizers.Has(finalizer), nil
func HasFinalizer(obj metav1.Object, finalizer string) bool {
finalizers := sets.New(obj.GetFinalizers()...)
return finalizers.Has(finalizer)
}

// AddFinalizers adds the given finalizers to the given objects ObjectMeta.
// Returns true if the object was updated.
func AddFinalizers(obj runtime.Object, newFinalizers sets.String) (bool, error) {
accessor, err := meta.Accessor(obj)
if err != nil {
return false, err
}
oldFinalizers := sets.NewString(accessor.GetFinalizers()...)
func AddFinalizers(obj metav1.Object, newFinalizers sets.Set[string]) bool {
oldFinalizers := sets.New(obj.GetFinalizers()...)
if oldFinalizers.IsSuperset(newFinalizers) {
return false, nil
return false
}
allFinalizers := oldFinalizers.Union(newFinalizers)
accessor.SetFinalizers(allFinalizers.List())
return true, nil
obj.SetFinalizers(sets.List(allFinalizers))
return true
}

// RemoveFinalizers removes the given finalizers from the given objects ObjectMeta.
// Returns true if the object was updated.
func RemoveFinalizers(obj runtime.Object, finalizers sets.String) (bool, error) {
accessor, err := meta.Accessor(obj)
if err != nil {
return false, err
}
oldFinalizers := sets.NewString(accessor.GetFinalizers()...)
func RemoveFinalizers(obj metav1.Object, finalizers sets.Set[string]) bool {
oldFinalizers := sets.New(obj.GetFinalizers()...)
if oldFinalizers.Intersection(finalizers).Len() == 0 {
return false, nil
return false
}
newFinalizers := oldFinalizers.Difference(finalizers)
accessor.SetFinalizers(newFinalizers.List())
return true, nil
obj.SetFinalizers(sets.List(newFinalizers))
return true
}
Loading