Skip to content

Commit 3b35bcb

Browse files
authored
Merge pull request #908 from mengqiy/scaffolddeletewh
✨ scaffold ValidateDelete for validating webhook
2 parents 887a9e6 + 2a88104 commit 3b35bcb

File tree

12 files changed

+60
-22
lines changed

12 files changed

+60
-22
lines changed

docs/book/src/cronjob-tutorial/testdata/project/api/v1/cronjob_webhook.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ func (r *CronJob) Default() {
9090
This marker is responsible for generating a validating webhook manifest.
9191
*/
9292

93-
// +kubebuilder:webhook:path=/validate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=false,failurePolicy=fail,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=vcronjob.kb.io
93+
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
94+
// +kubebuilder:webhook:verbs=create;update,path=/validate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=false,failurePolicy=fail,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,versions=v1,name=vcronjob.kb.io
9495

9596
/*
9697
To validate our CRD beyond what's possible with declarative validation.
@@ -103,11 +104,15 @@ schedule without making up a long regular expression.
103104
If `webhook.Validator` interface is implemented, a webhook will automatically be
104105
served that calls the validation.
105106
106-
The `ValidateCreate` and `ValidateUpdate` methods are expected to validate that its
107-
receiver upon creation and update respectively. We separate out ValidateCreate
108-
from ValidateUpdate to allow behavior like making certain fields immutable, so
109-
that they can only be set on creation.
110-
Here, however, we just use the same shared validation.
107+
The `ValidateCreate`, `ValidateUpdate` and `ValidateDelete` methods are expected
108+
to validate that its receiver upon creation, update and deletion respectively.
109+
We separate out ValidateCreate from ValidateUpdate to allow behavior like making
110+
certain fields immutable, so that they can only be set on creation.
111+
ValidateDelete is also separated from ValidateUpdate to allow different
112+
validation behavior on deletion.
113+
Here, however, we just use the same shared validation for `ValidateCreate` and
114+
`ValidateUpdate`. And we do nothing in `ValidateDelete`, since we don't need to
115+
validate anything on deletion.
111116
*/
112117

113118
var _ webhook.Validator = &CronJob{}
@@ -126,6 +131,14 @@ func (r *CronJob) ValidateUpdate(old runtime.Object) error {
126131
return r.validateCronJob()
127132
}
128133

134+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
135+
func (r *CronJob) ValidateDelete() error {
136+
cronjoblog.Info("validate delete", "name", r.Name)
137+
138+
// TODO(user): fill in your validation logic upon object deletion.
139+
return nil
140+
}
141+
129142
/*
130143
We validate the name and the spec of the CronJob.
131144
*/

docs/book/src/cronjob-tutorial/testdata/project/api/v1/zz_generated.deepcopy.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ package v1
2222

2323
import (
2424
corev1 "k8s.io/api/core/v1"
25-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2625
"k8s.io/apimachinery/pkg/runtime"
2726
)
2827

@@ -131,8 +130,7 @@ func (in *CronJobStatus) DeepCopyInto(out *CronJobStatus) {
131130
}
132131
if in.LastScheduleTime != nil {
133132
in, out := &in.LastScheduleTime, &out.LastScheduleTime
134-
*out = new(metav1.Time)
135-
(*in).DeepCopyInto(*out)
133+
*out = (*in).DeepCopy()
136134
}
137135
}
138136

docs/book/src/cronjob-tutorial/testdata/project/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ require (
1818
k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b
1919
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d
2020
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
21-
sigs.k8s.io/controller-runtime v0.2.0-beta.4
21+
sigs.k8s.io/controller-runtime v0.2.0-beta.5
2222
)

docs/book/src/cronjob-tutorial/testdata/project/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ k8s.io/kube-openapi v0.0.0-20180731170545-e3762e86a74c h1:3KSCztE7gPitlZmWbNwue/
143143
k8s.io/kube-openapi v0.0.0-20180731170545-e3762e86a74c/go.mod h1:BXM9ceUBTj2QnfH2MK1odQs778ajze1RxcmP6S8RVVc=
144144
k8s.io/utils v0.0.0-20190506122338-8fab8cb257d5 h1:VBM/0P5TWxwk+Nw6Z+lAw3DKgO76g90ETOiA6rfLV1Y=
145145
k8s.io/utils v0.0.0-20190506122338-8fab8cb257d5/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew=
146-
sigs.k8s.io/controller-runtime v0.2.0-beta.4 h1:S1XVfRWR1MuIXZdkYx3jN8JDw+bbQxmWZroy0i87z/A=
147-
sigs.k8s.io/controller-runtime v0.2.0-beta.4/go.mod h1:HweyYKQ8fBuzdu2bdaeBJvsFgAi/OqBBnrVGXcqKhME=
146+
sigs.k8s.io/controller-runtime v0.2.0-beta.5 h1:W2jTb239QEwQ+HejhTCF9GriFPy2zmo1I6pPmJTeEy8=
147+
sigs.k8s.io/controller-runtime v0.2.0-beta.5/go.mod h1:HweyYKQ8fBuzdu2bdaeBJvsFgAi/OqBBnrVGXcqKhME=
148148
sigs.k8s.io/testing_frameworks v0.1.1 h1:cP2l8fkA3O9vekpy5Ks8mmA0NW/F7yBdXf8brkWhVrs=
149149
sigs.k8s.io/testing_frameworks v0.1.1/go.mod h1:VVBKrHmJ6Ekkfz284YKhQePcdycOzNH9qL6ht1zEr/U=
150150
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=

docs/book/src/multiversion-tutorial/testdata/project/api/v1/cronjob_webhook.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ func (r *CronJob) Default() {
7272
}
7373
}
7474

75-
// +kubebuilder:webhook:path=/validate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=false,failurePolicy=fail,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=vcronjob.kb.io
75+
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
76+
// +kubebuilder:webhook:verbs=create;update,path=/validate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=false,failurePolicy=fail,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,versions=v1,name=vcronjob.kb.io
7677

7778
var _ webhook.Validator = &CronJob{}
7879

@@ -90,6 +91,14 @@ func (r *CronJob) ValidateUpdate(old runtime.Object) error {
9091
return r.validateCronJob()
9192
}
9293

94+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
95+
func (r *CronJob) ValidateDelete() error {
96+
cronjoblog.Info("validate delete", "name", r.Name)
97+
98+
// TODO(user): fill in your validation logic upon object deletion.
99+
return nil
100+
}
101+
93102
func (r *CronJob) validateCronJob() error {
94103
var allErrs field.ErrorList
95104
if err := r.validateCronJobName(); err != nil {

docs/book/src/multiversion-tutorial/testdata/project/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ require (
1818
k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b
1919
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d
2020
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
21-
sigs.k8s.io/controller-runtime v0.2.0-beta.4
21+
sigs.k8s.io/controller-runtime v0.2.0-beta.5
2222
)

docs/book/src/multiversion-tutorial/testdata/project/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ k8s.io/kube-openapi v0.0.0-20180731170545-e3762e86a74c h1:3KSCztE7gPitlZmWbNwue/
143143
k8s.io/kube-openapi v0.0.0-20180731170545-e3762e86a74c/go.mod h1:BXM9ceUBTj2QnfH2MK1odQs778ajze1RxcmP6S8RVVc=
144144
k8s.io/utils v0.0.0-20190506122338-8fab8cb257d5 h1:VBM/0P5TWxwk+Nw6Z+lAw3DKgO76g90ETOiA6rfLV1Y=
145145
k8s.io/utils v0.0.0-20190506122338-8fab8cb257d5/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew=
146-
sigs.k8s.io/controller-runtime v0.2.0-beta.4 h1:S1XVfRWR1MuIXZdkYx3jN8JDw+bbQxmWZroy0i87z/A=
147-
sigs.k8s.io/controller-runtime v0.2.0-beta.4/go.mod h1:HweyYKQ8fBuzdu2bdaeBJvsFgAi/OqBBnrVGXcqKhME=
146+
sigs.k8s.io/controller-runtime v0.2.0-beta.5 h1:W2jTb239QEwQ+HejhTCF9GriFPy2zmo1I6pPmJTeEy8=
147+
sigs.k8s.io/controller-runtime v0.2.0-beta.5/go.mod h1:HweyYKQ8fBuzdu2bdaeBJvsFgAi/OqBBnrVGXcqKhME=
148148
sigs.k8s.io/testing_frameworks v0.1.1 h1:cP2l8fkA3O9vekpy5Ks8mmA0NW/F7yBdXf8brkWhVrs=
149149
sigs.k8s.io/testing_frameworks v0.1.1/go.mod h1:VVBKrHmJ6Ekkfz284YKhQePcdycOzNH9qL6ht1zEr/U=
150150
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=

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+
}

0 commit comments

Comments
 (0)