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

K8SPG-637: disable restore on pg-restore deletion #950

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions config/rbac/cluster/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ rules:
- create
- get
- list
- patch
- watch
- apiGroups:
- policy
Expand Down
1 change: 1 addition & 0 deletions config/rbac/namespace/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ rules:
- create
- get
- list
- patch
- watch
- apiGroups:
- policy
Expand Down
1 change: 1 addition & 0 deletions deploy/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45823,6 +45823,7 @@ rules:
- create
- get
- list
- patch
- watch
- apiGroups:
- policy
Expand Down
1 change: 1 addition & 0 deletions deploy/cw-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45823,6 +45823,7 @@ rules:
- create
- get
- list
- patch
- watch
- apiGroups:
- policy
Expand Down
1 change: 1 addition & 0 deletions deploy/cw-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ rules:
- create
- get
- list
- patch
- watch
- apiGroups:
- policy
Expand Down
1 change: 1 addition & 0 deletions deploy/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ rules:
- create
- get
- list
- patch
- watch
- apiGroups:
- policy
Expand Down
17 changes: 16 additions & 1 deletion e2e-tests/functions
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ deploy_operator_gh() {
| kubectl -n "${OPERATOR_NS:-$NAMESPACE}" apply -f -
}

remove_all_finalizers() {
resource_types=("pg-restore" "pg")

for resource in "${resource_types[@]}"; do
echo "removing all finalizers for $resource resources"

kubectl -n "${NAMESPACE}" get "$resource" -o json | jq '.items[] | .metadata.name' -r | while IFS= read -r name; do
if [[ $(kubectl -n "${NAMESPACE}" get "$resource" "$name" -o yaml | yq '.metadata.finalizers | length') == "0" ]]; then
continue
fi
kubectl -n "${NAMESPACE}" patch "$resource" "$name" --type=json -p='[{"op": "remove", "path": "/metadata/finalizers"}]'
done
done
}

destroy_operator() {
kubectl -n "${OPERATOR_NS:-$NAMESPACE}" delete deployment percona-postgresql-operator --force --grace-period=0 || true
if [[ $OPERATOR_NS ]]; then
Expand Down Expand Up @@ -765,7 +780,7 @@ get_postgresql_logs() {

for pod in $(kubectl get pods -l postgres-operator.crunchydata.com/data=postgres --no-headers | awk '{print $1}'); do
local phase=$(kubectl -n ${NAMESPACE} get ${pod} -o jsonpath={".status.phase"})
if [[ "${phase}" != "Running" ]]; then
if [[ ${phase} != "Running" ]]; then
echo "Waiting for ${pod} to start running"
continue
fi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
1 change: 1 addition & 0 deletions e2e-tests/tests/pitr/99-remove-cluster-gracefully.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ commands:

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
4 changes: 2 additions & 2 deletions percona/controller/pgcluster/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,14 +644,14 @@ func (r *PGClusterReconciler) stopExternalWatcher(ctx context.Context, cr *v2.Pe

func (r *PGClusterReconciler) ensureFinalizers(ctx context.Context, cr *v2.PerconaPGCluster) error {
for _, finalizer := range cr.Finalizers {
if finalizer == v2.FinalizerStopWatchers {
if finalizer == pNaming.FinalizerStopWatchers {
return nil
}
}

if *cr.Spec.Backups.TrackLatestRestorableTime {
orig := cr.DeepCopy()
cr.Finalizers = append(cr.Finalizers, v2.FinalizerStopWatchers)
cr.Finalizers = append(cr.Finalizers, pNaming.FinalizerStopWatchers)
if err := r.Client.Patch(ctx, cr.DeepCopy(), client.MergeFrom(orig)); err != nil {
return errors.Wrap(err, "patch finalizers")
}
Expand Down
41 changes: 9 additions & 32 deletions percona/controller/pgcluster/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"

"github.com/percona/percona-postgresql-operator/internal/logging"
"github.com/percona/percona-postgresql-operator/internal/naming"
"github.com/percona/percona-postgresql-operator/percona/controller"
pNaming "github.com/percona/percona-postgresql-operator/percona/naming"
v2 "github.com/percona/percona-postgresql-operator/pkg/apis/pgv2.percona.com/v2"
)

Expand Down Expand Up @@ -151,39 +152,15 @@ func (r *PGClusterReconciler) stopExternalWatchers(ctx context.Context, cr *v2.P
}

func (r *PGClusterReconciler) runFinalizers(ctx context.Context, cr *v2.PerconaPGCluster) error {
if err := r.runFinalizer(ctx, cr, v2.FinalizerDeletePVC, r.deletePVCAndSecrets); err != nil {
return errors.Wrapf(err, "run finalizer %s", v2.FinalizerDeletePVC)
finalizers := map[string]controller.FinalizerFunc[*v2.PerconaPGCluster]{
pNaming.FinalizerDeletePVC: r.deletePVCAndSecrets,
pNaming.FinalizerDeleteSSL: r.deleteTLSSecrets,
pNaming.FinalizerStopWatchers: r.stopExternalWatchers,
}

if err := r.runFinalizer(ctx, cr, v2.FinalizerDeleteSSL, r.deleteTLSSecrets); err != nil {
return errors.Wrapf(err, "run finalizer %s", v2.FinalizerDeleteSSL)
}

if err := r.runFinalizer(ctx, cr, v2.FinalizerStopWatchers, r.stopExternalWatchers); err != nil {
return errors.Wrapf(err, "run finalizer %s", v2.FinalizerStopWatchers)
}

return nil
}

func (r *PGClusterReconciler) runFinalizer(ctx context.Context, cr *v2.PerconaPGCluster, finalizer string, f finalizerFunc) error {
if !controllerutil.ContainsFinalizer(cr, finalizer) {
return nil
}

log := logging.FromContext(ctx)
log.Info("Running finalizer", "name", finalizer)

orig := cr.DeepCopy()

if err := f(ctx, cr); err != nil {
return errors.Wrapf(err, "run finalizer %s", finalizer)
}

if controllerutil.RemoveFinalizer(cr, finalizer) {
log.Info("Removing finalizer", "name", finalizer)
if err := r.Client.Patch(ctx, cr, client.MergeFrom(orig)); err != nil {
return errors.Wrap(err, "remove finalizers")
for finalizer, f := range finalizers {
if err := controller.RunFinalizer(ctx, r.Client, cr, finalizer, f); err != nil {
return errors.Wrapf(err, "run finalizer %s", finalizer)
}
}

Expand Down
Loading
Loading