Skip to content

Commit

Permalink
Remove nil errors
Browse files Browse the repository at this point in the history
Signed-off-by: z1cheng <[email protected]>
  • Loading branch information
z1cheng committed Nov 18, 2023
1 parent d23ebcf commit 4a1df35
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 69 deletions.
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 @@ func (c *FederateController) ensureFinalizer(
) (*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.NewString(FinalizerFederateController))
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(
ctx,
sourceObj,
metav1.UpdateOptions{},
Expand All @@ -446,17 +443,14 @@ func (c *FederateController) removeFinalizer(
) (*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.NewString(FinalizerFederateController))
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(
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 @@ func (c *FederatedClusterController) ensureFinalizer(
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.NewString(FinalizerFederatedClusterController))

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 @@ func (s *SyncController) syncToClusters(
)
continue
}
hasFinalizer, err := finalizersutil.HasFinalizer(cluster, FinalizerCascadingDelete)
if err != nil {
shouldRecheckAfterDispatch = true
dispatcher.RecordClusterError(fedcorev1a1.FinalizerCheckFailed, clusterName, err)
continue
}
hasFinalizer := finalizersutil.HasFinalizer(cluster, FinalizerCascadingDelete)
if !hasFinalizer {
// we should not sync before finalizer is added
shouldRecheckAfterDispatch = true
Expand Down Expand Up @@ -971,9 +966,9 @@ func (s *SyncController) ensureFinalizer(ctx context.Context, fedResource Federa
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.NewString(FinalizerSyncController))
if !isUpdated {
return fmt.Errorf("failed to add finalizer to federated object")
}

keyedLogger.V(1).Info("Adding finalizer to federated object")
Expand All @@ -995,9 +990,9 @@ func (s *SyncController) removeFinalizer(ctx context.Context, fedResource Federa
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.NewString(FinalizerSyncController))
if !isUpdated {
return fmt.Errorf("failed to remove finalizer from federated object")
}

keyedLogger.V(1).Info("Removing finalizer from federated object")
Expand All @@ -1024,9 +1019,9 @@ func (s *SyncController) ensureClusterFinalizer(ctx context.Context, cluster *fe
if err != nil {
return err
}
isUpdated, err := finalizersutil.AddFinalizers(cluster, sets.NewString(FinalizerCascadingDelete))
if err != nil || !isUpdated {
return err
isUpdated := finalizersutil.AddFinalizers(cluster, sets.NewString(FinalizerCascadingDelete))
if !isUpdated {
return fmt.Errorf("failed to add finalizer to federated cluster")
}
cluster, err = s.fedClient.CoreV1alpha1().FederatedClusters().Update(ctx, cluster, metav1.UpdateOptions{})
return err
Expand All @@ -1046,9 +1041,9 @@ func (s *SyncController) removeClusterFinalizer(ctx context.Context, cluster *fe
if err != nil {
return err
}
isUpdated, err := finalizersutil.RemoveFinalizers(cluster, sets.NewString(FinalizerCascadingDelete))
if err != nil || !isUpdated {
return err
isUpdated := finalizersutil.RemoveFinalizers(cluster, sets.NewString(FinalizerCascadingDelete))
if !isUpdated {
return fmt.Errorf("failed to remove finalizer from cluster")
}
cluster, err = s.fedClient.CoreV1alpha1().FederatedClusters().Update(ctx, cluster, metav1.UpdateOptions{})
return err
Expand Down
28 changes: 5 additions & 23 deletions pkg/controllers/sync/dispatch/unmanaged.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,9 @@ func (d *unmanagedDispatcherImpl) Delete(
// 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)
if needUpdate {
clusterObj, err = client.Resource(d.targetGVR).Namespace(targetName.Namespace).Update(
_, err := client.Resource(d.targetGVR).Namespace(targetName.Namespace).Update(
ctx,
clusterObj,
metav1.UpdateOptions{},
Expand Down Expand Up @@ -177,7 +167,7 @@ func (d *unmanagedDispatcherImpl) Delete(
// 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(
ctx,
targetName.Name,
metav1.DeleteOptions{PropagationPolicy: &deletionPropagation},
Expand Down Expand Up @@ -218,15 +208,7 @@ func (d *unmanagedDispatcherImpl) RemoveManagedLabel(
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)

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

func removeRetainObjectFinalizer(obj *unstructured.Unstructured) (bool, error) {
func removeRetainObjectFinalizer(obj *unstructured.Unstructured) bool {
return finalizers.RemoveFinalizers(obj, sets.NewString(RetainTerminatingObjectFinalizer))
}
4 changes: 1 addition & 3 deletions pkg/controllers/sync/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,7 @@ func (r *federatedResource) ObjectForCluster(clusterName string) (*unstructured.
}

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

Expand Down
16 changes: 8 additions & 8 deletions pkg/util/finalizers/finalizers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,31 @@ import (
)

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

// AddFinalizers adds the given finalizers to the given objects ObjectMeta.
// Returns true if the object was updated.
func AddFinalizers(obj metav1.Object, newFinalizers sets.String) (bool, error) {
func AddFinalizers(obj metav1.Object, newFinalizers sets.String) bool {
oldFinalizers := sets.NewString(obj.GetFinalizers()...)
if oldFinalizers.IsSuperset(newFinalizers) {
return false, nil
return false
}
allFinalizers := oldFinalizers.Union(newFinalizers)
obj.SetFinalizers(allFinalizers.List())
return true, nil
return true
}

// RemoveFinalizers removes the given finalizers from the given objects ObjectMeta.
// Returns true if the object was updated.
func RemoveFinalizers(obj metav1.Object, finalizers sets.String) (bool, error) {
func RemoveFinalizers(obj metav1.Object, finalizers sets.String) bool {
oldFinalizers := sets.NewString(obj.GetFinalizers()...)
if oldFinalizers.Intersection(finalizers).Len() == 0 {
return false, nil
return false
}
newFinalizers := oldFinalizers.Difference(finalizers)
obj.SetFinalizers(newFinalizers.List())
return true, nil
return true
}
6 changes: 3 additions & 3 deletions pkg/util/finalizers/finalizers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestHasFinalizer(t *testing.T) {
},
}
for index, test := range testCases {
hasFinalizer, _ := HasFinalizer(test.obj, test.finalizer)
hasFinalizer := HasFinalizer(test.obj, test.finalizer)
assert.Equal(
t,
hasFinalizer,
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestAddFinalizers(t *testing.T) {
},
}
for index, test := range testCases {
isUpdated, _ := AddFinalizers(test.obj, test.finalizers)
isUpdated := AddFinalizers(test.obj, test.finalizers)
assert.Equal(
t,
isUpdated,
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestRemoveFinalizers(t *testing.T) {
},
}
for index, test := range testCases {
isUpdated, _ := RemoveFinalizers(test.obj, test.finalizers)
isUpdated := RemoveFinalizers(test.obj, test.finalizers)
assert.Equal(
t,
isUpdated,
Expand Down

0 comments on commit 4a1df35

Please sign in to comment.