From 5ceeab8062249626a938272d477acfd557e31f9a Mon Sep 17 00:00:00 2001 From: Rabi Mishra Date: Thu, 12 Nov 2026 10:54:08 +0530 Subject: [PATCH] Add protect-deletion annotation for OpenStackDataPlaneNodeSet deletion Let users opt a NodeSet into deletion protection by setting the dataplane.openstack.org/protect-deletion annotation: while the annotation is present, whatever its value, deletion is denied. Removing it is what releases the NodeSet. Denials are reported as field-path validation errors, so the confirm-delete guard on OpenStackDataPlaneDeployment moves into the accumulated error list as well. Signed-off-by: Rabi Mishra --- api/dataplane/v1beta1/const.go | 4 ++ .../openstackdataplanedeployment_webhook.go | 22 +++++----- .../openstackdataplanenodeset_webhook.go | 11 ++++- bindata/operator/operator.yaml | 1 + config/webhook/manifests.yaml | 1 + docs/assemblies/dataplane_resources.adoc | 6 +++ docs/assemblies/dataplane_scaling_out.adoc | 10 +++++ .../openstackdataplanenodeset_webhook.go | 3 +- .../openstackdataplanenodeset_webhook_test.go | 40 +++++++++++++++++++ 9 files changed, 84 insertions(+), 14 deletions(-) diff --git a/api/dataplane/v1beta1/const.go b/api/dataplane/v1beta1/const.go index 94a4c7e166..49837c7540 100644 --- a/api/dataplane/v1beta1/const.go +++ b/api/dataplane/v1beta1/const.go @@ -23,4 +23,8 @@ const ( // ConfirmDeleteAnnotation is the annotation key required to allow // deletion of an OpenStackDataPlaneDeployment. The value must be "true". ConfirmDeleteAnnotation = "dataplane.openstack.org/confirm-delete" + + // ProtectDeletionAnnotation opts an OpenStackDataPlaneNodeSet into deletion + // protection. Its mere presence, regardless of value, denies deletion. + ProtectDeletionAnnotation = "dataplane.openstack.org/protect-deletion" ) diff --git a/api/dataplane/v1beta1/openstackdataplanedeployment_webhook.go b/api/dataplane/v1beta1/openstackdataplanedeployment_webhook.go index 2d4d27df23..b37b2a6b8b 100644 --- a/api/dataplane/v1beta1/openstackdataplanedeployment_webhook.go +++ b/api/dataplane/v1beta1/openstackdataplanedeployment_webhook.go @@ -118,29 +118,29 @@ func (spec *OpenStackDataPlaneDeploymentSpec) ValidateUpdate(old OpenStackDataPl func (r *OpenStackDataPlaneDeployment) ValidateDelete() (admission.Warnings, error) { openstackdataplanedeploymentlog.Info("validate delete", "name", r.Name) + var warnings admission.Warnings + var errors field.ErrorList + deploymentCondition := r.Status.Conditions.Get(condition.DeploymentReadyCondition) isDeploymentRunning := deploymentCondition != nil && deploymentCondition.Reason == condition.RequestedReason if isDeploymentRunning && (r.Annotations == nil || r.Annotations[ConfirmDeleteAnnotation] != "true") { - warnings := admission.Warnings{ - "Deleting a running OpenStackDataPlaneDeployment may leave nodes in an inconsistent or " + - "unrecoverable state. To confirm deletion, set the annotation " + - ConfirmDeleteAnnotation + "=true and retry.", - } - return warnings, apierrors.NewForbidden( - schema.GroupResource{Group: "dataplane.openstack.org", Resource: "openstackdataplanedeployments"}, - r.Name, - fmt.Errorf("deletion of a running deployment not allowed: set annotation %s=true to confirm", ConfirmDeleteAnnotation)) + warnings = append(warnings, "Deleting a running OpenStackDataPlaneDeployment may leave nodes in an inconsistent or "+ + "unrecoverable state. To confirm deletion, set the annotation "+ + ConfirmDeleteAnnotation+"=true and retry.") + errors = append(errors, field.Forbidden( + field.NewPath("metadata", "annotations").Key(ConfirmDeleteAnnotation), + "deletion of a running deployment not allowed: set this annotation to \"true\" to confirm")) } - errors := r.Spec.ValidateDelete() + errors = append(errors, r.Spec.ValidateDelete()...) if len(errors) != 0 { openstackdataplanedeploymentlog.Info("validation failed", "name", r.Name) - return nil, apierrors.NewInvalid( + return warnings, apierrors.NewInvalid( schema.GroupKind{Group: "dataplane.openstack.org", Kind: "OpenStackDataPlaneDeployment"}, r.Name, errors) diff --git a/api/dataplane/v1beta1/openstackdataplanenodeset_webhook.go b/api/dataplane/v1beta1/openstackdataplanenodeset_webhook.go index 752ca31ac5..75274ed399 100644 --- a/api/dataplane/v1beta1/openstackdataplanenodeset_webhook.go +++ b/api/dataplane/v1beta1/openstackdataplanenodeset_webhook.go @@ -228,7 +228,16 @@ func (spec *OpenStackDataPlaneNodeSetSpec) ValidateUpdate(oldSpec *OpenStackData // ValidateDelete validates the OpenStackDataPlaneNodeSet on deletion func (r *OpenStackDataPlaneNodeSet) ValidateDelete(ctx context.Context, c client.Reader) (admission.Warnings, error) { openstackdataplanenodesetlog.Info("validate delete", "name", r.Name) - errors := r.Spec.ValidateDelete() + + var errors field.ErrorList + + if _, ok := r.Annotations[ProtectDeletionAnnotation]; ok { + errors = append(errors, field.Forbidden( + field.NewPath("metadata", "annotations").Key(ProtectDeletionAnnotation), + "deletion is not allowed while this annotation is set")) + } + + errors = append(errors, r.Spec.ValidateDelete()...) if len(errors) != 0 { openstackdataplanenodesetlog.Info("validation failed", "name", r.Name) diff --git a/bindata/operator/operator.yaml b/bindata/operator/operator.yaml index 0971d9afbc..c2a439e906 100644 --- a/bindata/operator/operator.yaml +++ b/bindata/operator/operator.yaml @@ -484,6 +484,7 @@ webhooks: operations: - CREATE - UPDATE + - DELETE resources: - openstackdataplanenodesets sideEffects: None diff --git a/config/webhook/manifests.yaml b/config/webhook/manifests.yaml index 4eb764ec6d..e6d2a648e5 100644 --- a/config/webhook/manifests.yaml +++ b/config/webhook/manifests.yaml @@ -288,6 +288,7 @@ webhooks: operations: - CREATE - UPDATE + - DELETE resources: - openstackdataplanenodesets sideEffects: None diff --git a/docs/assemblies/dataplane_resources.adoc b/docs/assemblies/dataplane_resources.adoc index c1820ebeee..d127e325b3 100644 --- a/docs/assemblies/dataplane_resources.adoc +++ b/docs/assemblies/dataplane_resources.adoc @@ -206,6 +206,12 @@ OpenStackDataPlaneDeploymentStatus defines the observed state of OpenStackDataPl OpenStackDataPlaneNodeSet is the Schema for the openstackdataplanenodesets API OpenStackDataPlaneNodeSet name must be a valid RFC1123 as it is used in labels +Deletion of an OpenStackDataPlaneNodeSet can be protected by setting the +`dataplane.openstack.org/protect-deletion` annotation on it. While the +annotation is present, whatever its value, deletion of the +OpenStackDataPlaneNodeSet is denied. Removing the annotation releases the +OpenStackDataPlaneNodeSet again. + |=== | Field | Description | Scheme | Required diff --git a/docs/assemblies/dataplane_scaling_out.adoc b/docs/assemblies/dataplane_scaling_out.adoc index 27493a9a79..20cd2f15de 100644 --- a/docs/assemblies/dataplane_scaling_out.adoc +++ b/docs/assemblies/dataplane_scaling_out.adoc @@ -237,3 +237,13 @@ of the remaining `OpenStackDataPlaneNodeSets`. To remove the ssh host keys of the removed nodes of this `OpenStackDataPlaneNodeSet` from other nodes a new `OpenStackDataPlaneDeployment` needs to be created that points to all the remaining `OpenStackDataPlaneNodeSets`. + +NOTE: Deletion of an `OpenStackDataPlaneNodeSet` can be protected by setting +the `dataplane.openstack.org/protect-deletion` annotation on it. While the +annotation is present, whatever its value, deletion of the +`OpenStackDataPlaneNodeSet` is denied. To delete the +`OpenStackDataPlaneNodeSet` the annotation needs to be removed first: + +---- +$ oc annotate openstackdataplanenodeset dataplane.openstack.org/protect-deletion- +---- diff --git a/internal/webhook/dataplane/v1beta1/openstackdataplanenodeset_webhook.go b/internal/webhook/dataplane/v1beta1/openstackdataplanenodeset_webhook.go index b32ad86ad2..0bff869e9f 100644 --- a/internal/webhook/dataplane/v1beta1/openstackdataplanenodeset_webhook.go +++ b/internal/webhook/dataplane/v1beta1/openstackdataplanenodeset_webhook.go @@ -78,10 +78,9 @@ func (d *OpenStackDataPlaneNodeSetCustomDefaulter) Default(_ context.Context, ob return nil } -// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. // NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here. // Modifying the path for an invalid path can cause API server errors; failing to locate the webhook. -// +kubebuilder:webhook:path=/validate-dataplane-openstack-org-v1beta1-openstackdataplanenodeset,mutating=false,failurePolicy=fail,sideEffects=None,groups=dataplane.openstack.org,resources=openstackdataplanenodesets,verbs=create;update,versions=v1beta1,name=vopenstackdataplanenodeset-v1beta1.kb.io,admissionReviewVersions=v1 +// +kubebuilder:webhook:path=/validate-dataplane-openstack-org-v1beta1-openstackdataplanenodeset,mutating=false,failurePolicy=fail,sideEffects=None,groups=dataplane.openstack.org,resources=openstackdataplanenodesets,verbs=create;update;delete,versions=v1beta1,name=vopenstackdataplanenodeset-v1beta1.kb.io,admissionReviewVersions=v1 // OpenStackDataPlaneNodeSetCustomValidator struct is responsible for validating the OpenStackDataPlaneNodeSet resource // when it is created, updated, or deleted. diff --git a/test/functional/dataplane/openstackdataplanenodeset_webhook_test.go b/test/functional/dataplane/openstackdataplanenodeset_webhook_test.go index 7a9a591483..af1a96f114 100644 --- a/test/functional/dataplane/openstackdataplanenodeset_webhook_test.go +++ b/test/functional/dataplane/openstackdataplanenodeset_webhook_test.go @@ -6,6 +6,7 @@ import ( . "github.com/onsi/ginkgo/v2" //revive:disable:dot-imports . "github.com/onsi/gomega" //revive:disable:dot-imports + apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/types" @@ -279,4 +280,43 @@ var _ = Describe("DataplaneNodeSet Webhook", func() { dataplaneDeploymentName.Name, string(v1beta1.NodeSetDeploymentReadyCondition)))) }) }) + + When("the protect-deletion annotation is set on the NodeSet", func() { + BeforeEach(func() { + CreateDataplaneNodeSet(dataplaneNodeSetName, DefaultDataPlaneNoNodeSetSpec(false)) + }) + + It("Should block deletion while the annotation is set", func() { + Eventually(func(g Gomega) string { + instance := GetDataplaneNodeSet(dataplaneNodeSetName) + instance.Annotations = map[string]string{v1beta1.ProtectDeletionAnnotation: ""} + g.Expect(th.K8sClient.Update(th.Ctx, instance)).To(Succeed()) + + err := th.K8sClient.Delete(th.Ctx, instance) + g.Expect(apierrors.IsInvalid(err)).To(BeTrue()) + return fmt.Sprintf("%s", err) + }).Should(ContainSubstring("metadata.annotations[dataplane.openstack.org/protect-deletion]: Forbidden: deletion is not allowed while this annotation is set")) + + DeferCleanup(func() { + Eventually(func(g Gomega) { + instance := GetDataplaneNodeSet(dataplaneNodeSetName) + delete(instance.Annotations, v1beta1.ProtectDeletionAnnotation) + g.Expect(th.K8sClient.Update(th.Ctx, instance)).To(Succeed()) + g.Expect(th.K8sClient.Delete(th.Ctx, instance)).To(Succeed()) + }).Should(Succeed()) + }) + }) + + It("Should allow deletion once the annotation is removed", func() { + Eventually(func(g Gomega) { + instance := GetDataplaneNodeSet(dataplaneNodeSetName) + instance.Annotations = map[string]string{v1beta1.ProtectDeletionAnnotation: "true"} + g.Expect(th.K8sClient.Update(th.Ctx, instance)).To(Succeed()) + + delete(instance.Annotations, v1beta1.ProtectDeletionAnnotation) + g.Expect(th.K8sClient.Update(th.Ctx, instance)).To(Succeed()) + g.Expect(th.K8sClient.Delete(th.Ctx, instance)).To(Succeed()) + }).Should(Succeed()) + }) + }) })