diff --git a/pkg/admission/pluginpreset_webhook.go b/pkg/admission/pluginpreset_webhook.go index 4020fe77f..7b4219944 100644 --- a/pkg/admission/pluginpreset_webhook.go +++ b/pkg/admission/pluginpreset_webhook.go @@ -19,6 +19,8 @@ import ( // Webhook for the PluginPreset custom resource. +const preventDeletionAnnotation = "greenhouse.sap/prevent-deletion" + func SetupPluginPresetWebhookWithManager(mgr ctrl.Manager) error { return setupWebhook(mgr, &greenhousev1alpha1.PluginPreset{}, @@ -116,9 +118,9 @@ func ValidateDeletePluginPreset(_ context.Context, _ client.Client, obj runtime. } var allErrs field.ErrorList - if _, ok := pluginPreset.Annotations["greenhouse.sap/prevent-deletion"]; ok { - allErrs = append(allErrs, field.Invalid(field.NewPath("metadata").Child("annotation").Child("greenhouse.sap/prevent-deletion"), pluginPreset.Annotations, - "Plugin preset has prevent deletion annotation")) + if _, ok := pluginPreset.Annotations[preventDeletionAnnotation]; ok { + allErrs = append(allErrs, field.Invalid(field.NewPath("metadata").Child("annotation").Child(preventDeletionAnnotation), + pluginPreset.Annotations, "Plugin preset has prevent deletion annotation")) } if len(allErrs) > 0 { diff --git a/pkg/admission/pluginpreset_webhook_test.go b/pkg/admission/pluginpreset_webhook_test.go index 3a4401741..0bd53a02a 100644 --- a/pkg/admission/pluginpreset_webhook_test.go +++ b/pkg/admission/pluginpreset_webhook_test.go @@ -170,4 +170,35 @@ var _ = Describe("PluginPreset Admission Tests", Ordered, func() { Expect(test.K8sClient.Delete(test.Ctx, cut)). To(Succeed(), "there must be no error deleting the PluginPreset") }) + + It("should reject delete operation when PluginPreset has prevent deletion annotation", func() { + pluginPreset := &greenhousev1alpha1.PluginPreset{ + ObjectMeta: metav1.ObjectMeta{ + Name: pluginPresetUpdate, + Namespace: test.TestNamespace, + Annotations: map[string]string{ + preventDeletionAnnotation: "true", + }, + }, + Spec: greenhousev1alpha1.PluginPresetSpec{ + Plugin: greenhousev1alpha1.PluginSpec{ + PluginDefinition: pluginPresetDefinition, + }, + ClusterSelector: metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}}, + }, + } + + err := test.K8sClient.Create(test.Ctx, pluginPreset) + Expect(err).ToNot(HaveOccurred()) + + err = test.K8sClient.Delete(test.Ctx, pluginPreset) + Expect(err).To(HaveOccurred()) + + pluginPreset.Annotations = map[string]string{} + err = test.K8sClient.Update(test.Ctx, pluginPreset) + Expect(err).ToNot(HaveOccurred()) + + err = test.K8sClient.Delete(test.Ctx, pluginPreset) + Expect(err).ToNot(HaveOccurred()) + }) })