Skip to content

Commit eb13f46

Browse files
fix marker for webhook server in the tutorial samples
Apply the fix introduced in PR #4122. The default markers in the code have been replaced with the values defined in the hack/docs, but this change was missed in the tutorial samples.
1 parent 26c4b55 commit eb13f46

File tree

6 files changed

+40
-171
lines changed

6 files changed

+40
-171
lines changed

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

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,7 @@ func (r *CronJob) SetupWebhookWithManager(mgr ctrl.Manager) error {
5959
Complete()
6060
}
6161

62-
/*
63-
Notice that we use kubebuilder markers to generate webhook manifests.
64-
This marker is responsible for generating a mutating webhook manifest.
65-
66-
The meaning of each marker can be found [here](/reference/markers/webhook.md).
67-
*/
68-
69-
// +kubebuilder:webhook:path=/mutate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=true,failurePolicy=fail,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=mcronjob.kb.io,sideEffects=None,admissionReviewVersions=v1
70-
71-
/*
72-
We use the `webhook.CustomDefaulter` interface to set defaults to our CRD.
73-
A webhook will automatically be served that calls this defaulting.
74-
75-
The `Default` method is expected to mutate the receiver, setting the defaults.
76-
*/
62+
// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
7763

7864
// +kubebuilder:webhook:path=/mutate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=true,failurePolicy=fail,sideEffects=None,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=mcronjob-v1.kb.io,admissionReviewVersions=v1
7965

@@ -126,10 +112,15 @@ func (r *CronJob) Default() {
126112
}
127113

128114
/*
129-
This marker is responsible for generating a validating webhook manifest.
115+
Notice that we use kubebuilder markers to generate webhook manifests.
116+
This marker is responsible for generating a mutating webhook manifest.
117+
118+
The meaning of each marker can be found [here](/reference/markers/webhook.md).
130119
*/
131120

132-
// +kubebuilder:webhook:verbs=create;update;delete,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,sideEffects=None,admissionReviewVersions=v1
121+
/*
122+
This marker is responsible for generating a validating webhook manifest.
123+
*/
133124

134125
/*
135126
We can validate our CRD beyond what's possible with declarative
@@ -153,8 +144,6 @@ Here, however, we just use the same shared validation for `ValidateCreate` and
153144
validate anything on deletion.
154145
*/
155146

156-
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
157-
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
158147
// +kubebuilder:webhook:path=/validate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=false,failurePolicy=fail,sideEffects=None,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=vcronjob-v1.kb.io,admissionReviewVersions=v1
159148

160149
// +kubebuilder:object:generate=false

docs/book/src/cronjob-tutorial/testdata/project/config/webhook/manifests.yaml

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,6 @@ webhooks:
2424
resources:
2525
- cronjobs
2626
sideEffects: None
27-
- admissionReviewVersions:
28-
- v1
29-
clientConfig:
30-
service:
31-
name: webhook-service
32-
namespace: system
33-
path: /mutate-batch-tutorial-kubebuilder-io-v1-cronjob
34-
failurePolicy: Fail
35-
name: mcronjob.kb.io
36-
rules:
37-
- apiGroups:
38-
- batch.tutorial.kubebuilder.io
39-
apiVersions:
40-
- v1
41-
operations:
42-
- CREATE
43-
- UPDATE
44-
resources:
45-
- cronjobs
46-
sideEffects: None
4727
---
4828
apiVersion: admissionregistration.k8s.io/v1
4929
kind: ValidatingWebhookConfiguration
@@ -70,24 +50,3 @@ webhooks:
7050
resources:
7151
- cronjobs
7252
sideEffects: None
73-
- admissionReviewVersions:
74-
- v1
75-
clientConfig:
76-
service:
77-
name: webhook-service
78-
namespace: system
79-
path: /validate-batch-tutorial-kubebuilder-io-v1-cronjob
80-
failurePolicy: Fail
81-
name: vcronjob.kb.io
82-
rules:
83-
- apiGroups:
84-
- batch.tutorial.kubebuilder.io
85-
apiVersions:
86-
- v1
87-
operations:
88-
- CREATE
89-
- UPDATE
90-
- DELETE
91-
resources:
92-
- cronjobs
93-
sideEffects: None

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

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,7 @@ func (r *CronJob) SetupWebhookWithManager(mgr ctrl.Manager) error {
6363
Complete()
6464
}
6565

66-
/*
67-
Notice that we use kubebuilder markers to generate webhook manifests.
68-
This marker is responsible for generating a mutating webhook manifest.
69-
70-
The meaning of each marker can be found [here](/reference/markers/webhook.md).
71-
*/
72-
73-
// +kubebuilder:webhook:path=/mutate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=true,failurePolicy=fail,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=mcronjob.kb.io,sideEffects=None,admissionReviewVersions=v1
74-
75-
/*
76-
We use the `webhook.CustomDefaulter` interface to set defaults to our CRD.
77-
A webhook will automatically be served that calls this defaulting.
78-
79-
The `Default` method is expected to mutate the receiver, setting the defaults.
80-
*/
66+
// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
8167

8268
// +kubebuilder:webhook:path=/mutate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=true,failurePolicy=fail,sideEffects=None,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=mcronjob-v1.kb.io,admissionReviewVersions=v1
8369

@@ -130,10 +116,15 @@ func (r *CronJob) Default() {
130116
}
131117

132118
/*
133-
This marker is responsible for generating a validating webhook manifest.
119+
Notice that we use kubebuilder markers to generate webhook manifests.
120+
This marker is responsible for generating a mutating webhook manifest.
121+
122+
The meaning of each marker can be found [here](/reference/markers/webhook.md).
134123
*/
135124

136-
// +kubebuilder:webhook:verbs=create;update;delete,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,sideEffects=None,admissionReviewVersions=v1
125+
/*
126+
This marker is responsible for generating a validating webhook manifest.
127+
*/
137128

138129
/*
139130
We can validate our CRD beyond what's possible with declarative
@@ -157,8 +148,6 @@ Here, however, we just use the same shared validation for `ValidateCreate` and
157148
validate anything on deletion.
158149
*/
159150

160-
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
161-
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
162151
// +kubebuilder:webhook:path=/validate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=false,failurePolicy=fail,sideEffects=None,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=vcronjob-v1.kb.io,admissionReviewVersions=v1
163152

164153
// +kubebuilder:object:generate=false

docs/book/src/multiversion-tutorial/testdata/project/config/webhook/manifests.yaml

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,6 @@ webhooks:
2424
resources:
2525
- cronjobs
2626
sideEffects: None
27-
- admissionReviewVersions:
28-
- v1
29-
clientConfig:
30-
service:
31-
name: webhook-service
32-
namespace: system
33-
path: /mutate-batch-tutorial-kubebuilder-io-v1-cronjob
34-
failurePolicy: Fail
35-
name: mcronjob.kb.io
36-
rules:
37-
- apiGroups:
38-
- batch.tutorial.kubebuilder.io
39-
apiVersions:
40-
- v1
41-
operations:
42-
- CREATE
43-
- UPDATE
44-
resources:
45-
- cronjobs
46-
sideEffects: None
4727
- admissionReviewVersions:
4828
- v1
4929
clientConfig:
@@ -90,27 +70,6 @@ webhooks:
9070
resources:
9171
- cronjobs
9272
sideEffects: None
93-
- admissionReviewVersions:
94-
- v1
95-
clientConfig:
96-
service:
97-
name: webhook-service
98-
namespace: system
99-
path: /validate-batch-tutorial-kubebuilder-io-v1-cronjob
100-
failurePolicy: Fail
101-
name: vcronjob.kb.io
102-
rules:
103-
- apiGroups:
104-
- batch.tutorial.kubebuilder.io
105-
apiVersions:
106-
- v1
107-
operations:
108-
- CREATE
109-
- UPDATE
110-
- DELETE
111-
resources:
112-
- cronjobs
113-
sideEffects: None
11473
- admissionReviewVersions:
11574
- v1
11675
clientConfig:

hack/docs/internal/cronjob-tutorial/generate_cronjob.go

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ func (sp *Sample) updateWebhook() {
433433
434434
// nolint:unused
435435
// log is for logging in this package.
436-
`, WebhookIntro)
436+
`, webhookIntro)
437437
hackutils.CheckError("fixing cronjob_webhook.go", err)
438438

439439
err = pluginutil.InsertCode(
@@ -447,7 +447,8 @@ Then, we set up the webhook with the manager.
447447

448448
err = pluginutil.ReplaceInFile(
449449
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
450-
`// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!`, WebhookMarker)
450+
`// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
451+
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.`, explanationValidateCRD)
451452
hackutils.CheckError("fixing cronjob_webhook.go by replacing TODO", err)
452453

453454
err = pluginutil.ReplaceInFile(
@@ -474,48 +475,31 @@ Then, we set up the webhook with the manager.
474475
err = pluginutil.ReplaceInFile(
475476
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
476477
`// TODO(user): fill in your defaulting logic.
477-
`, WebhookDefaultingSettings)
478+
479+
return nil
480+
}`, webhookDefaultingSettings)
478481
hackutils.CheckError("fixing cronjob_webhook.go by adding logic", err)
479482

480483
err = pluginutil.ReplaceInFile(
481484
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
482485
`// TODO(user): fill in your validation logic upon object creation.
483486
484487
return nil, nil`,
485-
`
486-
return nil, cronjob.validateCronJob()`)
488+
`return nil, cronjob.validateCronJob()`)
487489
hackutils.CheckError("fixing cronjob_webhook.go by fill in your validation", err)
488490

489491
err = pluginutil.ReplaceInFile(
490492
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
491493
`// TODO(user): fill in your validation logic upon object update.
492494
493495
return nil, nil`,
494-
`
495-
return nil, cronjob.validateCronJob()`)
496+
`return nil, cronjob.validateCronJob()`)
496497
hackutils.CheckError("fixing cronjob_webhook.go by adding validation logic upon object update", err)
497498

498-
err = pluginutil.InsertCode(
499+
err = pluginutil.AppendCodeAtTheEnd(
499500
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
500-
`// TODO(user): fill in your validation logic upon object deletion.
501-
502-
return nil, nil
503-
}`, WebhookValidateSpec)
504-
505-
hackutils.CheckError("fixing cronjob_webhook.go upon object deletion", err)
506-
507-
err = pluginutil.ReplaceInFile(
508-
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
509-
`validate anything on deletion.
510-
*/
511-
512-
return nil
513-
}`, `validate anything on deletion.
514-
*/
515-
516-
`)
517-
hackutils.CheckError("fixing cronjob_webhook.go by removing wrong return nil", err)
518-
501+
webhookValidateSpecMethods)
502+
hackutils.CheckError("adding validation spec methods at the end", err)
519503
}
520504

521505
func (sp *Sample) updateSuiteTest() {

hack/docs/internal/cronjob-tutorial/webhook_implementation.go

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616

1717
package cronjob
1818

19-
const WebhookIntro = `"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
19+
const webhookIntro = `"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2020
)
2121
2222
// +kubebuilder:docs-gen:collapse=Go imports
@@ -27,26 +27,7 @@ Next, we'll setup a logger for the webhooks.
2727
2828
`
2929

30-
const WebhookMarker = `/*
31-
Notice that we use kubebuilder markers to generate webhook manifests.
32-
This marker is responsible for generating a mutating webhook manifest.
33-
34-
The meaning of each marker can be found [here](/reference/markers/webhook.md).
35-
*/
36-
37-
// +kubebuilder:webhook:path=/mutate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=true,failurePolicy=fail,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=mcronjob.kb.io,sideEffects=None,admissionReviewVersions=v1
38-
39-
/*
40-
We use the` + " `" + `webhook.CustomDefaulter` + "`" + ` interface to set defaults to our CRD.
41-
A webhook will automatically be served that calls this defaulting.
42-
43-
The` + " `" + `Default` + "`" + ` method is expected to mutate the receiver, setting the defaults.
44-
*/
45-
`
46-
47-
const WebhookDefaultingSettings = `
48-
49-
// Set default values
30+
const webhookDefaultingSettings = `// Set default values
5031
cronjob.Default()
5132
5233
return nil
@@ -70,11 +51,18 @@ func (r *CronJob) Default() {
7051
}
7152
7253
/*
73-
This marker is responsible for generating a validating webhook manifest.
54+
Notice that we use kubebuilder markers to generate webhook manifests.
55+
This marker is responsible for generating a mutating webhook manifest.
56+
57+
The meaning of each marker can be found [here](/reference/markers/webhook.md).
7458
*/
7559
76-
// +kubebuilder:webhook:verbs=create;update;delete,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,sideEffects=None,admissionReviewVersions=v1
60+
/*
61+
This marker is responsible for generating a validating webhook manifest.
62+
*/
63+
`
7764

65+
const explanationValidateCRD = `
7866
/*
7967
We can validate our CRD beyond what's possible with declarative
8068
validation. Generally, declarative validation should be sufficient, but
@@ -96,8 +84,9 @@ Here, however, we just use the same shared validation for` + " `" + `ValidateCre
9684
` + "`" + `ValidateUpdate` + "`" + `. And we do nothing in` + " `" + `ValidateDelete` + "`" + `, since we don't need to
9785
validate anything on deletion.
9886
*/
87+
9988
`
100-
const WebhookValidateSpec = `
89+
const webhookValidateSpecMethods = `
10190
/*
10291
We validate the name and the spec of the CronJob.
10392
*/

0 commit comments

Comments
 (0)