Skip to content

Commit ef48443

Browse files
author
Mengqi Yu
committed
✨ scaffold ValidateDelete for validating webhook
1 parent 863e785 commit ef48443

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

pkg/scaffold/v2/gomod.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ module {{ .Repo }}
4343
go 1.12
4444
4545
require (
46-
sigs.k8s.io/controller-runtime v0.2.0-beta.4
46+
sigs.k8s.io/controller-runtime v0.2.0-beta.5
4747
)
4848
`

pkg/scaffold/v2/webhook/webhook.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ func (r *{{ .Resource.Kind }}) Default() {
118118
`
119119

120120
ValidatingWebhookTemplate = `
121-
// +kubebuilder:webhook:path=/validate-{{ .GroupDomainWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=false,failurePolicy=fail,groups={{ .GroupDomain }},resources={{ .Plural }},verbs=create;update,versions={{ .Resource.Version }},name=v{{ lower .Resource.Kind }}.kb.io
121+
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
122+
// +kubebuilder:webhook:verbs=create;update,path=/validate-{{ .GroupDomainWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=false,failurePolicy=fail,groups={{ .GroupDomain }},resources={{ .Plural }},versions={{ .Resource.Version }},name=v{{ lower .Resource.Kind }}.kb.io
122123
123124
var _ webhook.Validator = &{{ .Resource.Kind }}{}
124125
@@ -137,5 +138,13 @@ func (r *{{ .Resource.Kind }}) ValidateUpdate(old runtime.Object) error {
137138
// TODO(user): fill in your validation logic upon object update.
138139
return nil
139140
}
141+
142+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
143+
func (r *{{ .Resource.Kind }}) ValidateDelete() error {
144+
{{ lower .Resource.Kind }}log.Info("validate delete", "name", r.Name)
145+
146+
// TODO(user): fill in your validation logic upon object deletion.
147+
return nil
148+
}
140149
`
141150
)

testdata/project-v2/api/v1/captain_webhook.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ func (r *Captain) Default() {
4545
// TODO(user): fill in your defaulting logic.
4646
}
4747

48-
// +kubebuilder:webhook:path=/validate-crew-testproject-org-v1-captain,mutating=false,failurePolicy=fail,groups=crew.testproject.org,resources=captains,verbs=create;update,versions=v1,name=vcaptain.kb.io
48+
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
49+
// +kubebuilder:webhook:verbs=create;update,path=/validate-crew-testproject-org-v1-captain,mutating=false,failurePolicy=fail,groups=crew.testproject.org,resources=captains,versions=v1,name=vcaptain.kb.io
4950

5051
var _ webhook.Validator = &Captain{}
5152

@@ -64,3 +65,11 @@ func (r *Captain) ValidateUpdate(old runtime.Object) error {
6465
// TODO(user): fill in your validation logic upon object update.
6566
return nil
6667
}
68+
69+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
70+
func (r *Captain) ValidateDelete() error {
71+
captainlog.Info("validate delete", "name", r.Name)
72+
73+
// TODO(user): fill in your validation logic upon object deletion.
74+
return nil
75+
}

testdata/project-v2/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ require (
99
k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b
1010
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d
1111
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
12-
sigs.k8s.io/controller-runtime v0.2.0-beta.4
12+
sigs.k8s.io/controller-runtime v0.2.0-beta.5
1313
)

testdata/project-v2/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ k8s.io/kube-openapi v0.0.0-20180731170545-e3762e86a74c h1:3KSCztE7gPitlZmWbNwue/
116116
k8s.io/kube-openapi v0.0.0-20180731170545-e3762e86a74c/go.mod h1:BXM9ceUBTj2QnfH2MK1odQs778ajze1RxcmP6S8RVVc=
117117
k8s.io/utils v0.0.0-20190506122338-8fab8cb257d5 h1:VBM/0P5TWxwk+Nw6Z+lAw3DKgO76g90ETOiA6rfLV1Y=
118118
k8s.io/utils v0.0.0-20190506122338-8fab8cb257d5/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew=
119-
sigs.k8s.io/controller-runtime v0.2.0-beta.4 h1:S1XVfRWR1MuIXZdkYx3jN8JDw+bbQxmWZroy0i87z/A=
120-
sigs.k8s.io/controller-runtime v0.2.0-beta.4/go.mod h1:HweyYKQ8fBuzdu2bdaeBJvsFgAi/OqBBnrVGXcqKhME=
119+
sigs.k8s.io/controller-runtime v0.2.0-beta.5 h1:W2jTb239QEwQ+HejhTCF9GriFPy2zmo1I6pPmJTeEy8=
120+
sigs.k8s.io/controller-runtime v0.2.0-beta.5/go.mod h1:HweyYKQ8fBuzdu2bdaeBJvsFgAi/OqBBnrVGXcqKhME=
121121
sigs.k8s.io/testing_frameworks v0.1.1 h1:cP2l8fkA3O9vekpy5Ks8mmA0NW/F7yBdXf8brkWhVrs=
122122
sigs.k8s.io/testing_frameworks v0.1.1/go.mod h1:VVBKrHmJ6Ekkfz284YKhQePcdycOzNH9qL6ht1zEr/U=
123123
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=

0 commit comments

Comments
 (0)