Skip to content

Commit c6e41ee

Browse files
(docs/tutorials): ensure all fields follow Kubernetes API conventions
1 parent 215d16e commit c6e41ee

File tree

19 files changed

+202
-71
lines changed

19 files changed

+202
-71
lines changed

diff.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ index e4ce67d37..df8b86128 100644
1212
- // +kubebuilder:validation:Minimum=1
1313
- // +kubebuilder:validation:Maximum=3
1414
- // +kubebuilder:validation:ExclusiveMaximum=false
15-
- Size int32 `json:"size,omitempty"`
15+
- Size *int32 `json:"size,omitempty"`
1616
+ // size defines the number of Busybox instances
1717
+ // +kubebuilder:default=1
1818
+ // +kubebuilder:validation:Minimum=0

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ 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:validation:Enum=Allow;Forbid;Replace
85+
// +kubebuilder:default:=Allow
8486
ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy,omitempty"`
8587

8688
// suspend tells the controller to suspend subsequent executions, it does
@@ -89,6 +91,7 @@ type CronJobSpec struct {
8991
Suspend *bool `json:"suspend,omitempty"`
9092

9193
// jobTemplate defines the job that will be created when executing a CronJob.
94+
// +required
9295
JobTemplate batchv1.JobTemplateSpec `json:"jobTemplate"`
9396

9497
// successfulJobsHistoryLimit defines the number of successful finished jobs to retain.
@@ -146,6 +149,9 @@ type CronJobStatus struct {
146149

147150
// active defines a list of pointers to currently running jobs.
148151
// +optional
152+
// +listType=atomic
153+
// +kubebuilder:validation:MinItems=1
154+
// +kubebuilder:validation:MaxItems=10
149155
Active []corev1.ObjectReference `json:"active,omitempty"`
150156

151157
// 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: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@ spec:
2727
spec:
2828
properties:
2929
concurrencyPolicy:
30-
enum:
31-
- Allow
32-
- Forbid
33-
- Replace
30+
allOf:
31+
- enum:
32+
- Allow
33+
- Forbid
34+
- Replace
35+
- enum:
36+
- Allow
37+
- Forbid
38+
- Replace
39+
default: Allow
3440
type: string
3541
failedJobsHistoryLimit:
3642
format: int32
@@ -3835,7 +3841,10 @@ spec:
38353841
type: string
38363842
type: object
38373843
x-kubernetes-map-type: atomic
3844+
maxItems: 10
3845+
minItems: 1
38383846
type: array
3847+
x-kubernetes-list-type: atomic
38393848
conditions:
38403849
items:
38413850
properties:

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@ spec:
3333
spec:
3434
properties:
3535
concurrencyPolicy:
36-
enum:
37-
- Allow
38-
- Forbid
39-
- Replace
36+
allOf:
37+
- enum:
38+
- Allow
39+
- Forbid
40+
- Replace
41+
- enum:
42+
- Allow
43+
- Forbid
44+
- Replace
45+
default: Allow
4046
type: string
4147
failedJobsHistoryLimit:
4248
format: int32
@@ -3841,7 +3847,10 @@ spec:
38413847
type: string
38423848
type: object
38433849
x-kubernetes-map-type: atomic
3850+
maxItems: 10
3851+
minItems: 1
38443852
type: array
3853+
x-kubernetes-list-type: atomic
38453854
conditions:
38463855
items:
38473856
properties:

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ spec:
3535
spec:
3636
properties:
3737
concurrencyPolicy:
38-
enum:
39-
- Allow
40-
- Forbid
41-
- Replace
38+
allOf:
39+
- enum:
40+
- Allow
41+
- Forbid
42+
- Replace
43+
- enum:
44+
- Allow
45+
- Forbid
46+
- Replace
47+
default: Allow
4248
type: string
4349
failedJobsHistoryLimit:
4450
format: int32
@@ -3843,7 +3849,10 @@ spec:
38433849
type: string
38443850
type: object
38453851
x-kubernetes-map-type: atomic
3852+
maxItems: 10
3853+
minItems: 1
38463854
type: array
3855+
x-kubernetes-list-type: atomic
38473856
conditions:
38483857
items:
38493858
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ 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:validation:Enum=Allow;Forbid;Replace
57+
// +kubebuilder:default:=Allow
5658
ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy,omitempty"`
5759

5860
// suspend tells the controller to suspend subsequent executions, it does
@@ -61,6 +63,7 @@ type CronJobSpec struct {
6163
Suspend *bool `json:"suspend,omitempty"`
6264

6365
// jobTemplate defines the job that will be created when executing a CronJob.
66+
// +required
6467
JobTemplate batchv1.JobTemplateSpec `json:"jobTemplate"`
6568

6669
// successfulJobsHistoryLimit defines the number of successful finished jobs to retain.
@@ -102,6 +105,9 @@ type CronJobStatus struct {
102105

103106
// active defines a list of pointers to currently running jobs.
104107
// +optional
108+
// +listType=atomic
109+
// +kubebuilder:validation:MinItems=1
110+
// +kubebuilder:validation:MaxItems=10
105111
Active []corev1.ObjectReference `json:"active,omitempty"`
106112

107113
// 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: 8 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,8 @@ 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:validation:Enum=Allow;Forbid;Replace
64+
// +kubebuilder:default:=Allow
6265
ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy,omitempty"`
6366

6467
// suspend tells the controller to suspend subsequent executions, it does
@@ -67,6 +70,7 @@ type CronJobSpec struct {
6770
Suspend *bool `json:"suspend,omitempty"`
6871

6972
// jobTemplate defines the job that will be created when executing a CronJob.
73+
// +required
7074
JobTemplate batchv1.JobTemplateSpec `json:"jobTemplate"`
7175

7276
// successfulJobsHistoryLimit defines the number of successful finished jobs to retain.
@@ -148,6 +152,9 @@ type CronJobStatus struct {
148152
// Important: Run "make" to regenerate code after modifying this file
149153
// active defines a list of pointers to currently running jobs.
150154
// +optional
155+
// +listType=atomic
156+
// +kubebuilder:validation:MinItems=1
157+
// +kubebuilder:validation:MaxItems=10
151158
Active []corev1.ObjectReference `json:"active,omitempty"`
152159

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

0 commit comments

Comments
 (0)