Skip to content

Commit 3517a5b

Browse files
authored
Merge pull request #4942 from camilamacedo86/fix-tutorails-kal
📖 (docs/tutorials): ensure all fields follow Kubernetes API conventions
2 parents ccab29f + 4c98bb2 commit 3517a5b

File tree

18 files changed

+115
-34
lines changed

18 files changed

+115
-34
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ type CronJobSpec struct {
8181
// - "Forbid": forbids concurrent runs, skipping next run if previous run hasn't finished yet;
8282
// - "Replace": cancels currently running job and replaces it with a new one
8383
// +optional
84+
// +kubebuilder:default:=Allow
8485
ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy,omitempty"`
8586

8687
// suspend tells the controller to suspend subsequent executions, it does
@@ -89,6 +90,7 @@ type CronJobSpec struct {
8990
Suspend *bool `json:"suspend,omitempty"`
9091

9192
// jobTemplate defines the job that will be created when executing a CronJob.
93+
// +required
9294
JobTemplate batchv1.JobTemplateSpec `json:"jobTemplate"`
9395

9496
// successfulJobsHistoryLimit defines the number of successful finished jobs to retain.
@@ -146,6 +148,9 @@ type CronJobStatus struct {
146148

147149
// active defines a list of pointers to currently running jobs.
148150
// +optional
151+
// +listType=atomic
152+
// +kubebuilder:validation:MinItems=1
153+
// +kubebuilder:validation:MaxItems=10
149154
Active []corev1.ObjectReference `json:"active,omitempty"`
150155

151156
// lastScheduleTime defines when was the last time the job was successfully scheduled.

docs/book/src/cronjob-tutorial/testdata/project/config/crd/bases/batch.tutorial.kubebuilder.io_cronjobs.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ spec:
2727
spec:
2828
properties:
2929
concurrencyPolicy:
30+
default: Allow
3031
enum:
3132
- Allow
3233
- Forbid
@@ -3835,7 +3836,10 @@ spec:
38353836
type: string
38363837
type: object
38373838
x-kubernetes-map-type: atomic
3839+
maxItems: 10
3840+
minItems: 1
38383841
type: array
3842+
x-kubernetes-list-type: atomic
38393843
conditions:
38403844
items:
38413845
properties:

docs/book/src/cronjob-tutorial/testdata/project/dist/chart/templates/crd/batch.tutorial.kubebuilder.io_cronjobs.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ spec:
3333
spec:
3434
properties:
3535
concurrencyPolicy:
36+
default: Allow
3637
enum:
3738
- Allow
3839
- Forbid
@@ -3841,7 +3842,10 @@ spec:
38413842
type: string
38423843
type: object
38433844
x-kubernetes-map-type: atomic
3845+
maxItems: 10
3846+
minItems: 1
38443847
type: array
3848+
x-kubernetes-list-type: atomic
38453849
conditions:
38463850
items:
38473851
properties:

docs/book/src/cronjob-tutorial/testdata/project/dist/install.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ spec:
3535
spec:
3636
properties:
3737
concurrencyPolicy:
38+
default: Allow
3839
enum:
3940
- Allow
4041
- Forbid
@@ -3843,7 +3844,10 @@ spec:
38433844
type: string
38443845
type: object
38453846
x-kubernetes-map-type: atomic
3847+
maxItems: 10
3848+
minItems: 1
38463849
type: array
3850+
x-kubernetes-list-type: atomic
38473851
conditions:
38483852
items:
38493853
properties:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
k8s.io/api v0.33.0
1010
k8s.io/apimachinery v0.33.0
1111
k8s.io/client-go v0.33.0
12+
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738
1213
sigs.k8s.io/controller-runtime v0.21.0
1314
)
1415

@@ -89,7 +90,6 @@ require (
8990
k8s.io/component-base v0.33.0 // indirect
9091
k8s.io/klog/v2 v2.130.1 // indirect
9192
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect
92-
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect
9393
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.2 // indirect
9494
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
9595
sigs.k8s.io/randfill v1.0.0 // indirect

docs/book/src/cronjob-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
batchv1 "tutorial.kubebuilder.io/project/api/v1"
2424
// TODO (user): Add any additional imports if needed
25+
"k8s.io/utils/ptr"
2526
)
2627

2728
var _ = Describe("CronJob Webhook", func() {
@@ -40,8 +41,8 @@ var _ = Describe("CronJob Webhook", func() {
4041
Spec: batchv1.CronJobSpec{
4142
Schedule: schedule,
4243
ConcurrencyPolicy: batchv1.AllowConcurrent,
43-
SuccessfulJobsHistoryLimit: new(int32),
44-
FailedJobsHistoryLimit: new(int32),
44+
SuccessfulJobsHistoryLimit: ptr.To(int32(3)),
45+
FailedJobsHistoryLimit: ptr.To(int32(1)),
4546
},
4647
}
4748
*obj.Spec.SuccessfulJobsHistoryLimit = 3
@@ -51,8 +52,8 @@ var _ = Describe("CronJob Webhook", func() {
5152
Spec: batchv1.CronJobSpec{
5253
Schedule: schedule,
5354
ConcurrencyPolicy: batchv1.AllowConcurrent,
54-
SuccessfulJobsHistoryLimit: new(int32),
55-
FailedJobsHistoryLimit: new(int32),
55+
SuccessfulJobsHistoryLimit: ptr.To(int32(3)),
56+
FailedJobsHistoryLimit: ptr.To(int32(1)),
5657
},
5758
}
5859
*oldObj.Spec.SuccessfulJobsHistoryLimit = 3
@@ -95,20 +96,20 @@ var _ = Describe("CronJob Webhook", func() {
9596
It("Should not overwrite fields that are already set", func() {
9697
By("setting fields that would normally get a default")
9798
obj.Spec.ConcurrencyPolicy = batchv1.ForbidConcurrent
98-
obj.Spec.Suspend = new(bool)
99-
*obj.Spec.Suspend = true
100-
obj.Spec.SuccessfulJobsHistoryLimit = new(int32)
101-
*obj.Spec.SuccessfulJobsHistoryLimit = 5
102-
obj.Spec.FailedJobsHistoryLimit = new(int32)
103-
*obj.Spec.FailedJobsHistoryLimit = 2
99+
obj.Spec.Suspend = ptr.To(true)
100+
obj.Spec.SuccessfulJobsHistoryLimit = ptr.To(int32(5))
101+
obj.Spec.FailedJobsHistoryLimit = ptr.To(int32(2))
104102

105103
By("calling the Default method to apply defaults")
106104
_ = defaulter.Default(ctx, obj)
107105

108106
By("checking that the fields were not overwritten")
109107
Expect(obj.Spec.ConcurrencyPolicy).To(Equal(batchv1.ForbidConcurrent), "Expected ConcurrencyPolicy to retain its set value")
108+
Expect(obj.Spec.Suspend).NotTo(BeNil())
110109
Expect(*obj.Spec.Suspend).To(BeTrue(), "Expected Suspend to retain its set value")
110+
Expect(obj.Spec.SuccessfulJobsHistoryLimit).NotTo(BeNil())
111111
Expect(*obj.Spec.SuccessfulJobsHistoryLimit).To(Equal(int32(5)), "Expected SuccessfulJobsHistoryLimit to retain its set value")
112+
Expect(obj.Spec.FailedJobsHistoryLimit).NotTo(BeNil())
112113
Expect(*obj.Spec.FailedJobsHistoryLimit).To(Equal(int32(2)), "Expected FailedJobsHistoryLimit to retain its set value")
113114
})
114115
})

docs/book/src/getting-started.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ we will allow configuring the number of instances with the following:
8383
```go
8484
type MemcachedSpec struct {
8585
...
86-
Size int32 `json:"size,omitempty"`
86+
// +kubebuilder:validation:Minimum=0
87+
// +required
88+
Size *int32 `json:"size,omitempty"`
8789
}
8890
```
8991

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type CronJobSpec struct {
5353
// - "Forbid": forbids concurrent runs, skipping next run if previous run hasn't finished yet;
5454
// - "Replace": cancels currently running job and replaces it with a new one
5555
// +optional
56+
// +kubebuilder:default:=Allow
5657
ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy,omitempty"`
5758

5859
// suspend tells the controller to suspend subsequent executions, it does
@@ -61,6 +62,7 @@ type CronJobSpec struct {
6162
Suspend *bool `json:"suspend,omitempty"`
6263

6364
// jobTemplate defines the job that will be created when executing a CronJob.
65+
// +required
6466
JobTemplate batchv1.JobTemplateSpec `json:"jobTemplate"`
6567

6668
// successfulJobsHistoryLimit defines the number of successful finished jobs to retain.
@@ -102,6 +104,9 @@ type CronJobStatus struct {
102104

103105
// active defines a list of pointers to currently running jobs.
104106
// +optional
107+
// +listType=atomic
108+
// +kubebuilder:validation:MinItems=1
109+
// +kubebuilder:validation:MaxItems=10
105110
Active []corev1.ObjectReference `json:"active,omitempty"`
106111

107112
// lastScheduleTime defines when was the last time the job was successfully scheduled.

docs/book/src/multiversion-tutorial/testdata/project/api/v2/cronjob_types.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ We'll leave our spec largely unchanged, except to change the schedule field to a
4141
*/
4242
// CronJobSpec defines the desired state of CronJob
4343
type CronJobSpec struct {
44-
// The schedule in Cron format, see https://en.wikipedia.org/wiki/Cron.
44+
// schedule in Cron format, see https://en.wikipedia.org/wiki/Cron.
45+
// +required
4546
Schedule CronSchedule `json:"schedule"`
4647

4748
/*
@@ -59,6 +60,7 @@ type CronJobSpec struct {
5960
// - "Forbid": forbids concurrent runs, skipping next run if previous run hasn't finished yet;
6061
// - "Replace": cancels currently running job and replaces it with a new one
6162
// +optional
63+
// +kubebuilder:default:=Allow
6264
ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy,omitempty"`
6365

6466
// suspend tells the controller to suspend subsequent executions, it does
@@ -67,6 +69,7 @@ type CronJobSpec struct {
6769
Suspend *bool `json:"suspend,omitempty"`
6870

6971
// jobTemplate defines the job that will be created when executing a CronJob.
72+
// +required
7073
JobTemplate batchv1.JobTemplateSpec `json:"jobTemplate"`
7174

7275
// successfulJobsHistoryLimit defines the number of successful finished jobs to retain.
@@ -148,6 +151,9 @@ type CronJobStatus struct {
148151
// Important: Run "make" to regenerate code after modifying this file
149152
// active defines a list of pointers to currently running jobs.
150153
// +optional
154+
// +listType=atomic
155+
// +kubebuilder:validation:MinItems=1
156+
// +kubebuilder:validation:MaxItems=10
151157
Active []corev1.ObjectReference `json:"active,omitempty"`
152158

153159
// lastScheduleTime defines the information when was the last time the job was successfully scheduled.

docs/book/src/multiversion-tutorial/testdata/project/config/crd/bases/batch.tutorial.kubebuilder.io_cronjobs.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ spec:
2727
spec:
2828
properties:
2929
concurrencyPolicy:
30+
default: Allow
3031
enum:
3132
- Allow
3233
- Forbid
@@ -3835,7 +3836,10 @@ spec:
38353836
type: string
38363837
type: object
38373838
x-kubernetes-map-type: atomic
3839+
maxItems: 10
3840+
minItems: 1
38383841
type: array
3842+
x-kubernetes-list-type: atomic
38393843
conditions:
38403844
items:
38413845
properties:
@@ -3899,6 +3903,7 @@ spec:
38993903
spec:
39003904
properties:
39013905
concurrencyPolicy:
3906+
default: Allow
39023907
enum:
39033908
- Allow
39043909
- Forbid
@@ -7717,7 +7722,10 @@ spec:
77177722
type: string
77187723
type: object
77197724
x-kubernetes-map-type: atomic
7725+
maxItems: 10
7726+
minItems: 1
77207727
type: array
7728+
x-kubernetes-list-type: atomic
77217729
conditions:
77227730
items:
77237731
properties:

0 commit comments

Comments
 (0)