Skip to content

Commit

Permalink
replace: use Patch() to clear PVC finalizers (#350)
Browse files Browse the repository at this point in the history
Operator updates PVC which is subject for removal, so this is expected
that update may conflict.
To avoid conflicts instead updating whole resource, patch is used.

Fixes #350
  • Loading branch information
zimnx committed Feb 12, 2021
1 parent b015732 commit 5862e8e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pkg/controllers/cluster/actions/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/scylladb/scylla-operator/pkg/naming"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -186,10 +187,10 @@ func (a *RackReplaceNode) replaceNode(ctx context.Context, state *State, member

// Remove finalizer, which will wait until pod is deleted.
// We want to delete pod anyway, so it's better to delete pvc immediately.
pvcCopy := p.DeepCopy()
pvcCopy.SetFinalizers([]string{})
if err := cc.Update(ctx, pvcCopy); err != nil {
return false, errors.Wrap(err, "failed to update pvc")
if err := cc.Patch(ctx, pvc, util.StrategicMergePatchFunc(func(_ runtime.Object) ([]byte, error) {
return []byte(fmt.Sprintf(`{"metadata":{"$patch":"replace","finalizers": [], "uid":"%s"}`, pvc.UID)), nil
})); err != nil {
return false, errors.Wrap(err, "clear PVC finalizers")
}

return false, nil
Expand Down
14 changes: 14 additions & 0 deletions pkg/controllers/cluster/util/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -82,3 +83,16 @@ func PatchService(ctx context.Context, old, new *corev1.Service, kubeClient kube
_, err = kubeClient.CoreV1().Services(old.Namespace).Patch(ctx, old.Name, types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{})
return err
}

// The StrategicMergePatchFunc type is an adapter to allow the use of ordinary functions as Strategic Merge Patch.
type StrategicMergePatchFunc func(obj runtime.Object) ([]byte, error)

// Type returns PatchType of Strategic Merge Patch
func (f StrategicMergePatchFunc) Type() types.PatchType {
return types.StrategicMergePatchType
}

// Data returns the raw data representing the patch.
func (f StrategicMergePatchFunc) Data(obj runtime.Object) ([]byte, error) {
return f(obj)
}

0 comments on commit 5862e8e

Please sign in to comment.