Skip to content

Commit

Permalink
Merge pull request #67 from atlassian-labs/vportella/add-finalizer-retry
Browse files Browse the repository at this point in the history
Add retries when managing the finalizer on the node
  • Loading branch information
vincentportella authored Sep 20, 2023
2 parents 31cdc7b + 089e759 commit 2fa30a5
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -104,30 +105,32 @@ func (rm *ResourceManager) RemoveFinalizerFromNode(nodeName string) error {
}

func (rm *ResourceManager) manageFinalizerOnNode(nodeName string, fn func(*v1.Node, string, kubernetes.Interface) error) error {
// Get the node
node, err := rm.GetNode(nodeName)

// If the node is not found then skip the finalizer operation
if err != nil && apierrors.IsNotFound(err) {
rm.Logger.Info("node deleted, skip adding finalizer", "node", nodeName)
return nil
}
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
// Get the node
node, err := rm.GetNode(nodeName)

// If the node is not found then skip the finalizer operation
if err != nil && apierrors.IsNotFound(err) {
rm.Logger.Info("node deleted, skip adding finalizer", "node", nodeName)
return nil
}

// Account for any other possible errors
if err != nil {
return err
}

// The node exists as of the previous step, try managing the finalizer for
// it now
err = fn(node, nodeFinalizerName, rm.RawClient)

// Account for possible race conditions with other controllers managing
// nodes in the cluster
if apierrors.IsNotFound(err) {
rm.Logger.Info("updating finalizer failed, node already deleted", "node", nodeName)
return nil
}

// Account for any other possible errors
if err != nil {
return err
}

// The node exists as of the previous step, try managing the finalizer for
// it now
err = fn(node, nodeFinalizerName, rm.RawClient)

// Account for possible race conditions with other controllers managing
// nodes in the cluster
if apierrors.IsNotFound(err) {
rm.Logger.Info("adding finalizer failed, node already deleted", "node", nodeName)
return nil
}

return err
})
}

0 comments on commit 2fa30a5

Please sign in to comment.