Skip to content

Commit

Permalink
fix(validation): #696 drop deletion requests if the resources with an…
Browse files Browse the repository at this point in the history
…notation ignore-deletion
  • Loading branch information
powerfooI committed Dec 25, 2024
1 parent edf3f1e commit 5c67edf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion api/v1alpha1/obcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (r *OBCluster) Default() {
}

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
//+kubebuilder:webhook:path=/validate-oceanbase-oceanbase-com-v1alpha1-obcluster,mutating=false,failurePolicy=fail,sideEffects=None,groups=oceanbase.oceanbase.com,resources=obclusters,verbs=create;update,versions=v1alpha1,name=vobcluster.kb.io,admissionReviewVersions=v1
//+kubebuilder:webhook:path=/validate-oceanbase-oceanbase-com-v1alpha1-obcluster,mutating=false,failurePolicy=fail,sideEffects=None,groups=oceanbase.oceanbase.com,resources=obclusters,verbs=create;update;delete,versions=v1alpha1,name=vobcluster.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &OBCluster{}

Expand Down Expand Up @@ -267,6 +267,9 @@ func (r *OBCluster) ValidateUpdate(old runtime.Object) (admission.Warnings, erro

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *OBCluster) ValidateDelete() (admission.Warnings, error) {
if r.Annotations[oceanbaseconst.AnnotationsIgnoreDeletion] == "true" {
return nil, apierrors.NewBadRequest("OBCluster " + r.Name + " is protected from deletion by annotation " + oceanbaseconst.AnnotationsIgnoreDeletion)
}
return nil, nil
}

Expand Down
6 changes: 4 additions & 2 deletions api/v1alpha1/obtenant_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (r *OBTenant) Default() {
}

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
//+kubebuilder:webhook:path=/validate-oceanbase-oceanbase-com-v1alpha1-obtenant,mutating=false,failurePolicy=fail,sideEffects=None,groups=oceanbase.oceanbase.com,resources=obtenants,verbs=create;update,versions=v1alpha1,name=vobtenant.kb.io,admissionReviewVersions=v1
//+kubebuilder:webhook:path=/validate-oceanbase-oceanbase-com-v1alpha1-obtenant,mutating=false,failurePolicy=fail,sideEffects=None,groups=oceanbase.oceanbase.com,resources=obtenants,verbs=create;update;delete,versions=v1alpha1,name=vobtenant.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &OBTenant{}

Expand Down Expand Up @@ -311,7 +311,9 @@ func (r *OBTenant) validateMutation() error {

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *OBTenant) ValidateDelete() (admission.Warnings, error) {
// TODO(user): fill in your validation logic upon object deletion.
if r.Annotations[oceanbaseconst.AnnotationsIgnoreDeletion] == "true" {
return nil, apierrors.NewBadRequest("OBTenant " + r.Name + " is protected from deletion by annotation " + oceanbaseconst.AnnotationsIgnoreDeletion)
}
return nil, nil
}

Expand Down
2 changes: 2 additions & 0 deletions config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ webhooks:
operations:
- CREATE
- UPDATE
- DELETE
resources:
- obclusters
sideEffects: None
Expand Down Expand Up @@ -247,6 +248,7 @@ webhooks:
operations:
- CREATE
- UPDATE
- DELETE
resources:
- obtenants
sideEffects: None
Expand Down

0 comments on commit 5c67edf

Please sign in to comment.