Skip to content

Commit 2a88104

Browse files
author
Mengqi Yu
committed
📖 add ValidateDelete in the book
1 parent ef48443 commit 2a88104

File tree

7 files changed

+36
-16
lines changed

7 files changed

+36
-16
lines changed

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

+19-6
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

+1-3
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

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

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

+10-1
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

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

+2-2
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=

0 commit comments

Comments
 (0)