Skip to content

📖 (docs/tutorials): ensure all fields follow Kubernetes API conventions #4942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type CronJobSpec struct {
// - "Forbid": forbids concurrent runs, skipping next run if previous run hasn't finished yet;
// - "Replace": cancels currently running job and replaces it with a new one
// +optional
// +kubebuilder:default:=Allow
ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy,omitempty"`

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

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

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

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

// lastScheduleTime defines when was the last time the job was successfully scheduled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ spec:
spec:
properties:
concurrencyPolicy:
default: Allow
enum:
- Allow
- Forbid
Expand Down Expand Up @@ -3835,7 +3836,10 @@ spec:
type: string
type: object
x-kubernetes-map-type: atomic
maxItems: 10
minItems: 1
type: array
x-kubernetes-list-type: atomic
conditions:
items:
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ spec:
spec:
properties:
concurrencyPolicy:
default: Allow
enum:
- Allow
- Forbid
Expand Down Expand Up @@ -3841,7 +3842,10 @@ spec:
type: string
type: object
x-kubernetes-map-type: atomic
maxItems: 10
minItems: 1
type: array
x-kubernetes-list-type: atomic
conditions:
items:
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ spec:
spec:
properties:
concurrencyPolicy:
default: Allow
enum:
- Allow
- Forbid
Expand Down Expand Up @@ -3843,7 +3844,10 @@ spec:
type: string
type: object
x-kubernetes-map-type: atomic
maxItems: 10
minItems: 1
type: array
x-kubernetes-list-type: atomic
conditions:
items:
properties:
Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/cronjob-tutorial/testdata/project/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
k8s.io/api v0.33.0
k8s.io/apimachinery v0.33.0
k8s.io/client-go v0.33.0
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738
sigs.k8s.io/controller-runtime v0.21.0
)

Expand Down Expand Up @@ -89,7 +90,6 @@ require (
k8s.io/component-base v0.33.0 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.2 // indirect
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
sigs.k8s.io/randfill v1.0.0 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

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

var _ = Describe("CronJob Webhook", func() {
Expand All @@ -40,8 +41,8 @@ var _ = Describe("CronJob Webhook", func() {
Spec: batchv1.CronJobSpec{
Schedule: schedule,
ConcurrencyPolicy: batchv1.AllowConcurrent,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it not a pointer?
See: kubernetes-sigs/kube-api-linter#128
We will need to configure the linter with WhenRequired option
@erikgb @JoelSpeed

SuccessfulJobsHistoryLimit: new(int32),
FailedJobsHistoryLimit: new(int32),
SuccessfulJobsHistoryLimit: ptr.To(int32(3)),
FailedJobsHistoryLimit: ptr.To(int32(1)),
},
}
*obj.Spec.SuccessfulJobsHistoryLimit = 3
Expand All @@ -51,8 +52,8 @@ var _ = Describe("CronJob Webhook", func() {
Spec: batchv1.CronJobSpec{
Schedule: schedule,
ConcurrencyPolicy: batchv1.AllowConcurrent,
SuccessfulJobsHistoryLimit: new(int32),
FailedJobsHistoryLimit: new(int32),
SuccessfulJobsHistoryLimit: ptr.To(int32(3)),
FailedJobsHistoryLimit: ptr.To(int32(1)),
},
}
*oldObj.Spec.SuccessfulJobsHistoryLimit = 3
Expand Down Expand Up @@ -95,20 +96,20 @@ var _ = Describe("CronJob Webhook", func() {
It("Should not overwrite fields that are already set", func() {
By("setting fields that would normally get a default")
obj.Spec.ConcurrencyPolicy = batchv1.ForbidConcurrent
obj.Spec.Suspend = new(bool)
*obj.Spec.Suspend = true
obj.Spec.SuccessfulJobsHistoryLimit = new(int32)
*obj.Spec.SuccessfulJobsHistoryLimit = 5
obj.Spec.FailedJobsHistoryLimit = new(int32)
*obj.Spec.FailedJobsHistoryLimit = 2
obj.Spec.Suspend = ptr.To(true)
obj.Spec.SuccessfulJobsHistoryLimit = ptr.To(int32(5))
obj.Spec.FailedJobsHistoryLimit = ptr.To(int32(2))

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

By("checking that the fields were not overwritten")
Expect(obj.Spec.ConcurrencyPolicy).To(Equal(batchv1.ForbidConcurrent), "Expected ConcurrencyPolicy to retain its set value")
Expect(obj.Spec.Suspend).NotTo(BeNil())
Expect(*obj.Spec.Suspend).To(BeTrue(), "Expected Suspend to retain its set value")
Expect(obj.Spec.SuccessfulJobsHistoryLimit).NotTo(BeNil())
Expect(*obj.Spec.SuccessfulJobsHistoryLimit).To(Equal(int32(5)), "Expected SuccessfulJobsHistoryLimit to retain its set value")
Expect(obj.Spec.FailedJobsHistoryLimit).NotTo(BeNil())
Expect(*obj.Spec.FailedJobsHistoryLimit).To(Equal(int32(2)), "Expected FailedJobsHistoryLimit to retain its set value")
})
})
Expand Down
4 changes: 3 additions & 1 deletion docs/book/src/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ we will allow configuring the number of instances with the following:
```go
type MemcachedSpec struct {
...
Size int32 `json:"size,omitempty"`
// +kubebuilder:validation:Minimum=0
// +required
Size *int32 `json:"size,omitempty"`
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type CronJobSpec struct {
// - "Forbid": forbids concurrent runs, skipping next run if previous run hasn't finished yet;
// - "Replace": cancels currently running job and replaces it with a new one
// +optional
// +kubebuilder:default:=Allow
ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy,omitempty"`

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

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

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

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

// lastScheduleTime defines when was the last time the job was successfully scheduled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ We'll leave our spec largely unchanged, except to change the schedule field to a
*/
// CronJobSpec defines the desired state of CronJob
type CronJobSpec struct {
// The schedule in Cron format, see https://en.wikipedia.org/wiki/Cron.
// schedule in Cron format, see https://en.wikipedia.org/wiki/Cron.
// +required
Schedule CronSchedule `json:"schedule"`

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

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

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

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

// lastScheduleTime defines the information when was the last time the job was successfully scheduled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ spec:
spec:
properties:
concurrencyPolicy:
default: Allow
enum:
- Allow
- Forbid
Expand Down Expand Up @@ -3835,7 +3836,10 @@ spec:
type: string
type: object
x-kubernetes-map-type: atomic
maxItems: 10
minItems: 1
type: array
x-kubernetes-list-type: atomic
conditions:
items:
properties:
Expand Down Expand Up @@ -3899,6 +3903,7 @@ spec:
spec:
properties:
concurrencyPolicy:
default: Allow
enum:
- Allow
- Forbid
Expand Down Expand Up @@ -7717,7 +7722,10 @@ spec:
type: string
type: object
x-kubernetes-map-type: atomic
maxItems: 10
minItems: 1
type: array
x-kubernetes-list-type: atomic
conditions:
items:
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ spec:
spec:
properties:
concurrencyPolicy:
default: Allow
enum:
- Allow
- Forbid
Expand Down Expand Up @@ -3856,7 +3857,10 @@ spec:
type: string
type: object
x-kubernetes-map-type: atomic
maxItems: 10
minItems: 1
type: array
x-kubernetes-list-type: atomic
conditions:
items:
properties:
Expand Down Expand Up @@ -3920,6 +3924,7 @@ spec:
spec:
properties:
concurrencyPolicy:
default: Allow
enum:
- Allow
- Forbid
Expand Down Expand Up @@ -7738,7 +7743,10 @@ spec:
type: string
type: object
x-kubernetes-map-type: atomic
maxItems: 10
minItems: 1
type: array
x-kubernetes-list-type: atomic
conditions:
items:
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ spec:
spec:
properties:
concurrencyPolicy:
default: Allow
enum:
- Allow
- Forbid
Expand Down Expand Up @@ -3854,7 +3855,10 @@ spec:
type: string
type: object
x-kubernetes-map-type: atomic
maxItems: 10
minItems: 1
type: array
x-kubernetes-list-type: atomic
conditions:
items:
properties:
Expand Down Expand Up @@ -3918,6 +3922,7 @@ spec:
spec:
properties:
concurrencyPolicy:
default: Allow
enum:
- Allow
- Forbid
Expand Down Expand Up @@ -7736,7 +7741,10 @@ spec:
type: string
type: object
x-kubernetes-map-type: atomic
maxItems: 10
minItems: 1
type: array
x-kubernetes-list-type: atomic
conditions:
items:
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

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

var _ = Describe("CronJob Webhook", func() {
Expand All @@ -39,8 +40,8 @@ var _ = Describe("CronJob Webhook", func() {
Spec: batchv1.CronJobSpec{
Schedule: schedule,
ConcurrencyPolicy: batchv1.AllowConcurrent,
SuccessfulJobsHistoryLimit: new(int32),
FailedJobsHistoryLimit: new(int32),
SuccessfulJobsHistoryLimit: ptr.To(int32(3)),
FailedJobsHistoryLimit: ptr.To(int32(1)),
},
}
*obj.Spec.SuccessfulJobsHistoryLimit = 3
Expand All @@ -50,8 +51,8 @@ var _ = Describe("CronJob Webhook", func() {
Spec: batchv1.CronJobSpec{
Schedule: schedule,
ConcurrencyPolicy: batchv1.AllowConcurrent,
SuccessfulJobsHistoryLimit: new(int32),
FailedJobsHistoryLimit: new(int32),
SuccessfulJobsHistoryLimit: ptr.To(int32(3)),
FailedJobsHistoryLimit: ptr.To(int32(1)),
},
}
*oldObj.Spec.SuccessfulJobsHistoryLimit = 3
Expand Down
5 changes: 5 additions & 0 deletions hack/docs/internal/cronjob-tutorial/api_design.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const cronjobSpecStruct = `
// - "Forbid": forbids concurrent runs, skipping next run if previous run hasn't finished yet;
// - "Replace": cancels currently running job and replaces it with a new one
// +optional
// +kubebuilder:default:=Allow
ConcurrencyPolicy ConcurrencyPolicy` + " `" + `json:"concurrencyPolicy,omitempty"` + "`" + `

// suspend tells the controller to suspend subsequent executions, it does
Expand All @@ -75,6 +76,7 @@ const cronjobSpecStruct = `
Suspend *bool` + " `" + `json:"suspend,omitempty"` + "`" + `

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

// successfulJobsHistoryLimit defines the number of successful finished jobs to retain.
Expand Down Expand Up @@ -129,6 +131,9 @@ const cronjobList = `

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

// lastScheduleTime defines when was the last time the job was successfully scheduled.
Expand Down
Loading
Loading