Skip to content

Commit

Permalink
replace: eliminate conflict during cleanup of PVC finalizers (#350)
Browse files Browse the repository at this point in the history
Operator updated 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 b4a41e2 commit bedaa73
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
15 changes: 9 additions & 6 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 @@ -184,12 +185,14 @@ func (a *RackReplaceNode) replaceNode(ctx context.Context, state *State, member
return false, errors.Wrap(err, "failed to get pvc")
}

// 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 len(p.Finalizers) != 0 {
// Remove finalizer, which will wait until pod is deleted.
// We want to delete pod anyway, so it's better to delete pvc immediately.
if err := cc.Patch(ctx, pvc, util.StrategicMergePatchFunc(func(_ runtime.Object) ([]byte, error) {
return []byte(fmt.Sprintf(`{"metadata":{"$patch":"replace","finalizers":[],"name":"%s","uid":"%s"}}`, p.Name, p.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 bedaa73

Please sign in to comment.