Skip to content

Commit

Permalink
Scale down statefulset before deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
olliewalsh committed Oct 16, 2024
1 parent 8d760a2 commit f273c15
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 14 additions & 1 deletion controllers/ovndbcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -196,7 +197,19 @@ func (r *OVNDBClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request

// Handle service delete
if !instance.DeletionTimestamp.IsZero() {
return r.reconcileDelete(ctx, instance, helper)
// Scale statefulset down to 0 first before deleting
serviceName := ovnv1.ServiceNameNB
if instance.Spec.DBType == ovnv1.SBDBType {
serviceName = ovnv1.ServiceNameSB
}
sset, err := statefulset.GetStatefulSetWithName(ctx, helper, serviceName, instance.Namespace)
if err != nil && !k8s_errors.IsNotFound(err) {
return ctrl.Result{}, err
}
if k8s_errors.IsNotFound(err) || sset.Status.Replicas == 0 {
return r.reconcileDelete(ctx, instance, helper)
}
instance.Spec.Replicas = ptr.To(int32(0))
}

// Handle non-deleted clusters
Expand Down
4 changes: 2 additions & 2 deletions tests/kuttl/tests/ovn_db_delete/04-delete-pods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- script: |
oc delete pods -n $NAMESPACE -l service=ovsdbserver-nb
oc delete pods -n $NAMESPACE -l service=ovsdbserver-nb --grace-period=10
- script: |
oc delete pods -n $NAMESPACE -l service=ovsdbserver-sb
oc delete pods -n $NAMESPACE -l service=ovsdbserver-sb --grace-period=10

0 comments on commit f273c15

Please sign in to comment.