Skip to content

Commit a3078c8

Browse files
(docs/tutorials): ensure all fields follow Kubernetes API conventions
1 parent 58a7cd0 commit a3078c8

30 files changed

+271
-101
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,17 @@ 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-
ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy,omitempty"`
84+
// +kubebuilder:validation:Enum=Allow;Forbid;Replace
85+
// +kubebuilder:default:=Allow
86+
ConcurrencyPolicy *ConcurrencyPolicy `json:"concurrencyPolicy,omitempty"`
8587

8688
// suspend tells the controller to suspend subsequent executions, it does
8789
// not apply to already started executions. Defaults to false.
8890
// +optional
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=0
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/api/v1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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: 0
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: 0
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: 0
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/controller/cronjob_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,13 +441,13 @@ func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
441441
*/
442442
// figure out how to run this job -- concurrency policy might forbid us from running
443443
// multiple at the same time...
444-
if cronJob.Spec.ConcurrencyPolicy == batchv1.ForbidConcurrent && len(activeJobs) > 0 {
444+
if cronJob.Spec.ConcurrencyPolicy != nil && *cronJob.Spec.ConcurrencyPolicy == batchv1.ForbidConcurrent && len(activeJobs) > 0 {
445445
log.V(1).Info("concurrency policy blocks concurrent runs, skipping", "num active", len(activeJobs))
446446
return scheduledResult, nil
447447
}
448448

449449
// ...or instruct us to replace existing ones...
450-
if cronJob.Spec.ConcurrencyPolicy == batchv1.ReplaceConcurrent {
450+
if cronJob.Spec.ConcurrencyPolicy != nil && *cronJob.Spec.ConcurrencyPolicy == batchv1.ReplaceConcurrent {
451451
for _, activeJob := range activeJobs {
452452
// we don't care if the job was already deleted
453453
if err := r.Delete(ctx, activeJob, client.PropagationPolicy(metav1.DeletePropagationBackground)); client.IgnoreNotFound(err) != nil {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ func (d *CronJobCustomDefaulter) Default(_ context.Context, obj runtime.Object)
113113

114114
// applyDefaults applies default values to CronJob fields.
115115
func (d *CronJobCustomDefaulter) applyDefaults(cronJob *batchv1.CronJob) {
116-
if cronJob.Spec.ConcurrencyPolicy == "" {
117-
cronJob.Spec.ConcurrencyPolicy = d.DefaultConcurrencyPolicy
116+
if cronJob.Spec.ConcurrencyPolicy == nil || *cronJob.Spec.ConcurrencyPolicy == "" {
117+
cronJob.Spec.ConcurrencyPolicy = &d.DefaultConcurrencyPolicy
118118
}
119119
if cronJob.Spec.Suspend == nil {
120120
cronJob.Spec.Suspend = new(bool)

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

Lines changed: 11 additions & 9 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() {
@@ -39,9 +40,9 @@ var _ = Describe("CronJob Webhook", func() {
3940
obj = &batchv1.CronJob{
4041
Spec: batchv1.CronJobSpec{
4142
Schedule: schedule,
42-
ConcurrencyPolicy: batchv1.AllowConcurrent,
43-
SuccessfulJobsHistoryLimit: new(int32),
44-
FailedJobsHistoryLimit: new(int32),
43+
ConcurrencyPolicy: ptr.To(batchv1.AllowConcurrent),
44+
SuccessfulJobsHistoryLimit: ptr.To(int32(3)),
45+
FailedJobsHistoryLimit: ptr.To(int32(1)),
4546
},
4647
}
4748
*obj.Spec.SuccessfulJobsHistoryLimit = 3
@@ -50,9 +51,9 @@ var _ = Describe("CronJob Webhook", func() {
5051
oldObj = &batchv1.CronJob{
5152
Spec: batchv1.CronJobSpec{
5253
Schedule: schedule,
53-
ConcurrencyPolicy: batchv1.AllowConcurrent,
54-
SuccessfulJobsHistoryLimit: new(int32),
55-
FailedJobsHistoryLimit: new(int32),
54+
ConcurrencyPolicy: ptr.To(batchv1.AllowConcurrent),
55+
SuccessfulJobsHistoryLimit: ptr.To(int32(3)),
56+
FailedJobsHistoryLimit: ptr.To(int32(1)),
5657
},
5758
}
5859
*oldObj.Spec.SuccessfulJobsHistoryLimit = 3
@@ -77,7 +78,7 @@ var _ = Describe("CronJob Webhook", func() {
7778
Context("When creating CronJob under Defaulting Webhook", func() {
7879
It("Should apply defaults when a required field is empty", func() {
7980
By("simulating a scenario where defaults should be applied")
80-
obj.Spec.ConcurrencyPolicy = "" // This should default to AllowConcurrent
81+
obj.Spec.ConcurrencyPolicy = nil // This should default to AllowConcurrent
8182
obj.Spec.Suspend = nil // This should default to false
8283
obj.Spec.SuccessfulJobsHistoryLimit = nil // This should default to 3
8384
obj.Spec.FailedJobsHistoryLimit = nil // This should default to 1
@@ -86,15 +87,16 @@ var _ = Describe("CronJob Webhook", func() {
8687
_ = defaulter.Default(ctx, obj)
8788

8889
By("checking that the default values are set")
89-
Expect(obj.Spec.ConcurrencyPolicy).To(Equal(batchv1.AllowConcurrent), "Expected ConcurrencyPolicy to default to AllowConcurrent")
90+
Expect(obj.Spec.ConcurrencyPolicy).NotTo(BeNil(), "ConcurrencyPolicy should not be nil after defaulting")
91+
Expect(*obj.Spec.ConcurrencyPolicy).To(Equal(batchv1.AllowConcurrent), "Expected ConcurrencyPolicy to default to AllowConcurrent")
9092
Expect(*obj.Spec.Suspend).To(BeFalse(), "Expected Suspend to default to false")
9193
Expect(*obj.Spec.SuccessfulJobsHistoryLimit).To(Equal(int32(3)), "Expected SuccessfulJobsHistoryLimit to default to 3")
9294
Expect(*obj.Spec.FailedJobsHistoryLimit).To(Equal(int32(1)), "Expected FailedJobsHistoryLimit to default to 1")
9395
})
9496

9597
It("Should not overwrite fields that are already set", func() {
9698
By("setting fields that would normally get a default")
97-
obj.Spec.ConcurrencyPolicy = batchv1.ForbidConcurrent
99+
obj.Spec.ConcurrencyPolicy = ptr.To(batchv1.ForbidConcurrent)
98100
obj.Spec.Suspend = new(bool)
99101
*obj.Spec.Suspend = true
100102
obj.Spec.SuccessfulJobsHistoryLimit = new(int32)

docs/book/src/getting-started.md

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

0 commit comments

Comments
 (0)