Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/dataplane/v1beta1/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
22 changes: 11 additions & 11 deletions api/dataplane/v1beta1/openstackdataplanedeployment_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion api/dataplane/v1beta1/openstackdataplanenodeset_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions bindata/operator/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ webhooks:
operations:
- CREATE
- UPDATE
- DELETE
resources:
- openstackdataplanenodesets
sideEffects: None
Expand Down
1 change: 1 addition & 0 deletions config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ webhooks:
operations:
- CREATE
- UPDATE
- DELETE
resources:
- openstackdataplanenodesets
sideEffects: None
Expand Down
6 changes: 6 additions & 0 deletions docs/assemblies/dataplane_resources.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions docs/assemblies/dataplane_scaling_out.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <nodeset-name> dataplane.openstack.org/protect-deletion-
----
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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())
})
})
})
Loading