Skip to content

Commit 2448cf9

Browse files
rabiopenshift-cherrypick-robot
authored andcommitted
Require confirm-delete annotation for OpenStackDataPlaneDeployment deletion
Deleting a Deployment mid-execution may leave data plane nodes in an inconsistent or unrecoverable state. Require users to set the annotation dataplane.openstack.org/confirm-delete=true before deletion is allowed. Signed-off-by: rabi <ramishra@redhat.com>
1 parent f65820d commit 2448cf9

12 files changed

Lines changed: 196 additions & 8 deletions

File tree

‎api/dataplane/v1beta1/const.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ package v1beta1
1919
const (
2020
// CtlPlaneNetwork - default ctlplane Network Name in NetConfig
2121
CtlPlaneNetwork = "ctlplane"
22+
23+
// ConfirmDeleteAnnotation is the annotation key required to allow
24+
// deletion of an OpenStackDataPlaneDeployment. The value must be "true".
25+
ConfirmDeleteAnnotation = "dataplane.openstack.org/confirm-delete"
2226
)

‎api/dataplane/v1beta1/openstackdataplanedeployment_webhook.go‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"reflect"
2222

23+
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
2324
apierrors "k8s.io/apimachinery/pkg/api/errors"
2425
"k8s.io/apimachinery/pkg/runtime"
2526
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -117,6 +118,23 @@ func (spec *OpenStackDataPlaneDeploymentSpec) ValidateUpdate(old OpenStackDataPl
117118
func (r *OpenStackDataPlaneDeployment) ValidateDelete() (admission.Warnings, error) {
118119
openstackdataplanedeploymentlog.Info("validate delete", "name", r.Name)
119120

121+
deploymentCondition := r.Status.Conditions.Get(condition.DeploymentReadyCondition)
122+
isDeploymentRunning := deploymentCondition != nil &&
123+
deploymentCondition.Reason == condition.RequestedReason
124+
125+
if isDeploymentRunning &&
126+
(r.Annotations == nil || r.Annotations[ConfirmDeleteAnnotation] != "true") {
127+
warnings := admission.Warnings{
128+
"Deleting a running OpenStackDataPlaneDeployment may leave nodes in an inconsistent or " +
129+
"unrecoverable state. To confirm deletion, set the annotation " +
130+
ConfirmDeleteAnnotation + "=true and retry.",
131+
}
132+
return warnings, apierrors.NewForbidden(
133+
schema.GroupResource{Group: "dataplane.openstack.org", Resource: "openstackdataplanedeployments"},
134+
r.Name,
135+
fmt.Errorf("deletion of a running deployment not allowed: set annotation %s=true to confirm", ConfirmDeleteAnnotation))
136+
}
137+
120138
errors := r.Spec.ValidateDelete()
121139

122140
if len(errors) != 0 {
@@ -132,7 +150,5 @@ func (r *OpenStackDataPlaneDeployment) ValidateDelete() (admission.Warnings, err
132150

133151
// ValidateDelete validates the OpenStackDataPlaneDeploymentSpec on delete
134152
func (spec *OpenStackDataPlaneDeploymentSpec) ValidateDelete() field.ErrorList {
135-
// TODO(user): fill in your validation logic upon object creation.
136-
137153
return field.ErrorList{}
138154
}

‎bindata/operator/operator.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ webhooks:
419419
operations:
420420
- CREATE
421421
- UPDATE
422+
- DELETE
422423
resources:
423424
- openstackdataplanedeployments
424425
sideEffects: None

‎config/webhook/manifests.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ webhooks:
227227
operations:
228228
- CREATE
229229
- UPDATE
230+
- DELETE
230231
resources:
231232
- openstackdataplanedeployments
232233
sideEffects: None

‎internal/webhook/dataplane/v1beta1/openstackdataplanedeployment_webhook.go‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ func (d *OpenStackDataPlaneDeploymentCustomDefaulter) Default(_ context.Context,
7272
return nil
7373
}
7474

75-
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
76-
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
77-
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
78-
// +kubebuilder:webhook:path=/validate-dataplane-openstack-org-v1beta1-openstackdataplanedeployment,mutating=false,failurePolicy=fail,sideEffects=None,groups=dataplane.openstack.org,resources=openstackdataplanedeployments,verbs=create;update,versions=v1beta1,name=vopenstackdataplanedeployment-v1beta1.kb.io,admissionReviewVersions=v1
75+
// +kubebuilder:webhook:path=/validate-dataplane-openstack-org-v1beta1-openstackdataplanedeployment,mutating=false,failurePolicy=fail,sideEffects=None,groups=dataplane.openstack.org,resources=openstackdataplanedeployments,verbs=create;update;delete,versions=v1beta1,name=vopenstackdataplanedeployment-v1beta1.kb.io,admissionReviewVersions=v1
7976

8077
// OpenStackDataPlaneDeploymentCustomValidator struct is responsible for validating the OpenStackDataPlaneDeployment resource
8178
// when it is created, updated, or deleted.

‎test/functional/dataplane/base_test.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ func CreateDataplaneNodeSet(name types.NamespacedName, spec map[string]interface
8484

8585
// Create OpenStackDataPlaneDeployment in k8s and test that no errors occur
8686
func CreateDataplaneDeployment(name types.NamespacedName, spec map[string]interface{}) *unstructured.Unstructured {
87+
instance := DefaultDataplaneDeploymentTemplate(name, spec)
88+
metadata := instance["metadata"].(map[string]interface{})
89+
metadata["annotations"] = map[string]interface{}{
90+
dataplanev1.ConfirmDeleteAnnotation: "true",
91+
}
92+
return th.CreateUnstructured(instance)
93+
}
94+
95+
// Create OpenStackDataPlaneDeployment in k8s without the confirm-delete annotation.
96+
// Use this only in tests that need to verify delete rejection behavior.
97+
func CreateDataplaneDeploymentWithoutConfirm(name types.NamespacedName, spec map[string]interface{}) *unstructured.Unstructured {
8798
instance := DefaultDataplaneDeploymentTemplate(name, spec)
8899
return th.CreateUnstructured(instance)
89100
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package functional
2+
3+
import (
4+
"fmt"
5+
6+
. "github.com/onsi/ginkgo/v2" //revive:disable:dot-imports
7+
. "github.com/onsi/gomega" //revive:disable:dot-imports
8+
apierrors "k8s.io/apimachinery/pkg/api/errors"
9+
"k8s.io/apimachinery/pkg/types"
10+
11+
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
12+
dataplanev1 "github.com/openstack-k8s-operators/openstack-operator/api/dataplane/v1beta1"
13+
)
14+
15+
var _ = Describe("DataplaneDeployment Webhook", func() {
16+
var dataplaneDeploymentName types.NamespacedName
17+
18+
createDeployment := func(name types.NamespacedName) {
19+
CreateDataplaneDeploymentWithoutConfirm(name, DefaultDataPlaneDeploymentSpec())
20+
DeferCleanup(func() {
21+
instance := &dataplanev1.OpenStackDataPlaneDeployment{}
22+
err := th.K8sClient.Get(th.Ctx, name, instance)
23+
if apierrors.IsNotFound(err) {
24+
return
25+
}
26+
Expect(err).NotTo(HaveOccurred())
27+
28+
if instance.Annotations == nil {
29+
instance.Annotations = map[string]string{}
30+
}
31+
instance.Annotations[dataplanev1.ConfirmDeleteAnnotation] = "true"
32+
Expect(th.K8sClient.Update(th.Ctx, instance)).To(Succeed())
33+
Expect(th.K8sClient.Delete(th.Ctx, instance)).To(Succeed())
34+
})
35+
}
36+
37+
setDeploymentRunning := func(name types.NamespacedName) {
38+
Eventually(func(g Gomega) error {
39+
instance := &dataplanev1.OpenStackDataPlaneDeployment{}
40+
g.Expect(th.K8sClient.Get(th.Ctx, name, instance)).To(Succeed())
41+
instance.Status.Conditions = condition.Conditions{}
42+
instance.Status.Conditions.MarkFalse(
43+
condition.DeploymentReadyCondition,
44+
condition.RequestedReason,
45+
condition.SeverityInfo,
46+
condition.DeploymentReadyRunningMessage,
47+
)
48+
return th.K8sClient.Status().Update(th.Ctx, instance)
49+
}).Should(Succeed())
50+
}
51+
52+
setDeploymentCompleted := func(name types.NamespacedName) {
53+
Eventually(func(g Gomega) error {
54+
instance := &dataplanev1.OpenStackDataPlaneDeployment{}
55+
g.Expect(th.K8sClient.Get(th.Ctx, name, instance)).To(Succeed())
56+
instance.Status.Conditions = condition.Conditions{}
57+
instance.Status.Conditions.MarkTrue(
58+
condition.DeploymentReadyCondition,
59+
condition.DeploymentReadyMessage,
60+
)
61+
return th.K8sClient.Status().Update(th.Ctx, instance)
62+
}).Should(Succeed())
63+
}
64+
65+
When("a running deployment is deleted without the confirm annotation", func() {
66+
BeforeEach(func() {
67+
dataplaneDeploymentName = types.NamespacedName{
68+
Name: "edpm-running-delete-blocked",
69+
Namespace: namespace,
70+
}
71+
createDeployment(dataplaneDeploymentName)
72+
setDeploymentRunning(dataplaneDeploymentName)
73+
})
74+
75+
It("should reject the delete request", func() {
76+
Eventually(func() string {
77+
instance := GetDataplaneDeployment(dataplaneDeploymentName)
78+
err := th.K8sClient.Delete(th.Ctx, instance)
79+
return fmt.Sprintf("%s", err)
80+
}).Should(ContainSubstring("deletion of a running deployment not allowed"))
81+
})
82+
})
83+
84+
When("a running deployment is deleted with the confirm annotation", func() {
85+
BeforeEach(func() {
86+
dataplaneDeploymentName = types.NamespacedName{
87+
Name: "edpm-running-delete-confirmed",
88+
Namespace: namespace,
89+
}
90+
createDeployment(dataplaneDeploymentName)
91+
setDeploymentRunning(dataplaneDeploymentName)
92+
})
93+
94+
It("should allow the delete request", func() {
95+
Eventually(func(g Gomega) error {
96+
instance := GetDataplaneDeployment(dataplaneDeploymentName)
97+
if instance.Annotations == nil {
98+
instance.Annotations = map[string]string{}
99+
}
100+
instance.Annotations[dataplanev1.ConfirmDeleteAnnotation] = "true"
101+
g.Expect(th.K8sClient.Update(th.Ctx, instance)).To(Succeed())
102+
return th.K8sClient.Delete(th.Ctx, instance)
103+
}).Should(Succeed())
104+
105+
Eventually(func() bool {
106+
instance := &dataplanev1.OpenStackDataPlaneDeployment{}
107+
err := th.K8sClient.Get(th.Ctx, dataplaneDeploymentName, instance)
108+
return apierrors.IsNotFound(err)
109+
}).Should(BeTrue())
110+
})
111+
})
112+
113+
When("a completed deployment is deleted without the confirm annotation", func() {
114+
BeforeEach(func() {
115+
dataplaneDeploymentName = types.NamespacedName{
116+
Name: "edpm-completed-delete-allowed",
117+
Namespace: namespace,
118+
}
119+
createDeployment(dataplaneDeploymentName)
120+
setDeploymentCompleted(dataplaneDeploymentName)
121+
})
122+
123+
It("should allow the delete request", func() {
124+
Eventually(func() error {
125+
instance := GetDataplaneDeployment(dataplaneDeploymentName)
126+
return th.K8sClient.Delete(th.Ctx, instance)
127+
}).Should(Succeed())
128+
129+
Eventually(func() bool {
130+
instance := &dataplanev1.OpenStackDataPlaneDeployment{}
131+
err := th.K8sClient.Get(th.Ctx, dataplaneDeploymentName, instance)
132+
return apierrors.IsNotFound(err)
133+
}).Should(BeTrue())
134+
})
135+
})
136+
})

‎test/kuttl/tests/dataplane-deploy-no-nodes-test/08-ansiblevars-deploy.yaml‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@
33
apiVersion: kuttl.dev/v1beta1
44
kind: TestStep
55
commands:
6-
- script: oc delete openstackdataplanedeployment edpm-compute-no-nodes-non-existent-service -n openstack-kuttl-tests --ignore-not-found=true
7-
- script: oc delete openstackdataplanedeployment edpm-compute-node-selection -n openstack-kuttl-tests --ignore-not-found=true
6+
- script: |
7+
if oc get openstackdataplanedeployment edpm-compute-no-nodes-non-existent-service \
8+
-n openstack-kuttl-tests >/dev/null 2>&1; then
9+
oc annotate openstackdataplanedeployment edpm-compute-no-nodes-non-existent-service \
10+
-n openstack-kuttl-tests dataplane.openstack.org/confirm-delete=true --overwrite
11+
oc delete openstackdataplanedeployment edpm-compute-no-nodes-non-existent-service \
12+
-n openstack-kuttl-tests --ignore-not-found=true
13+
fi
14+
- script: |
15+
if oc get openstackdataplanedeployment edpm-compute-node-selection \
16+
-n openstack-kuttl-tests >/dev/null 2>&1; then
17+
oc annotate openstackdataplanedeployment edpm-compute-node-selection \
18+
-n openstack-kuttl-tests dataplane.openstack.org/confirm-delete=true --overwrite
19+
oc delete openstackdataplanedeployment edpm-compute-node-selection \
20+
-n openstack-kuttl-tests --ignore-not-found=true
21+
fi
822
---
923
# Create ConfigMap and Secret for ansibleVarsFrom
1024
apiVersion: v1

‎test/kuttl/tests/dataplane-deploy-tls-test/05-dataplane-redeploy.yaml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ apiVersion: dataplane.openstack.org/v1beta1
33
kind: OpenStackDataPlaneDeployment
44
metadata:
55
name: certs-refresh
6+
annotations:
7+
dataplane.openstack.org/confirm-delete: "true"
68
spec:
79
nodeSets:
810
- openstack-edpm-tls

‎test/kuttl/tests/dataplane-extramounts/00-dataplane-create.yaml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ apiVersion: dataplane.openstack.org/v1beta1
3232
kind: OpenStackDataPlaneDeployment
3333
metadata:
3434
name: edpm-extramounts
35+
annotations:
36+
dataplane.openstack.org/confirm-delete: "true"
3537
spec:
3638
nodeSets:
3739
- edpm-extramounts

0 commit comments

Comments
 (0)