Skip to content

Commit 583c1c0

Browse files
author
Mengqi Yu
committed
📖 fix broken pieces in our book
1 parent e98cf50 commit 583c1c0

File tree

11 files changed

+578
-157
lines changed

11 files changed

+578
-157
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
# Build the manager binary
2-
FROM golang:1.10.3 as builder
2+
FROM golang:1.12.5 as builder
33

4-
# Copy in the go src
5-
WORKDIR /go/src/tutorial.kubebuilder.io/project
6-
COPY vendor/ vendor/
4+
WORKDIR /workspace
5+
# Copy the Go Modules manifests
6+
COPY go.mod go.mod
7+
COPY go.sum go.sum
8+
# cache deps before building and copying source so that we don't need to re-download as much
9+
# and so that source changes don't invalidate our downloaded layer
10+
RUN go mod download
11+
12+
# Copy the go source
713
COPY main.go main.go
814
COPY api/ api/
915
COPY controllers/ controllers/
1016

1117
# Build
12-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager tutorial.kubebuilder.io/project/
18+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go
1319

14-
# Copy the controller-manager into a thin image
15-
FROM ubuntu:latest
20+
# Use distroless as minimal base image to package the manager binary
21+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
22+
FROM gcr.io/distroless/static:latest
1623
WORKDIR /
17-
COPY --from=builder /go/src/tutorial.kubebuilder.io/project/manager .
24+
COPY --from=builder /workspace/manager .
1825
ENTRYPOINT ["/manager"]

docs/book/src/cronjob-tutorial/testdata/project/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ docker-push:
5757
# download controller-gen if necessary
5858
controller-gen:
5959
ifeq (, $(shell which controller-gen))
60-
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.0-alpha.1
60+
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.0-beta.3
6161
CONTROLLER_GEN=$(shell go env GOPATH)/bin/controller-gen
6262
else
6363
CONTROLLER_GEN=$(shell which controller-gen)

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

+31-29
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ This markers is responsible for generating a mutating webhook manifest.
5555
The meaning of each marker can be found [here](../TODO.md).
5656
*/
5757

58-
// +kubebuilder:webhook:path=/mutate-batch-tutorial-kubebuilder-io-tutorial-kubebuilder-io-v1-cronjob,mutating=true,failurePolicy=fail,groups=batch.tutorial.kubebuilder.io.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=mcronjob.kb.io
58+
// +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
5959

6060
/*
6161
We use the `webhook.Defaulter` interface to set defaults to our CRD.
@@ -93,7 +93,7 @@ This markers is responsible for generating a validating webhook manifest.
9393
The meaning of each marker can be found [here](../TODO.md).
9494
*/
9595

96-
// +kubebuilder:webhook:path=/validate-batch-tutorial-kubebuilder-io-tutorial-kubebuilder-io-v1-cronjob,mutating=false,failurePolicy=fail,groups=batch.tutorial.kubebuilder.io.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=vcronjob.kb.io
96+
// +kubebuilder:webhook:path=/validate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=false,failurePolicy=fail,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=vcronjob.kb.io
9797

9898
/*
9999
To validate our CRD beyond what's possible with declarative validation.
@@ -133,15 +133,21 @@ func (r *CronJob) ValidateUpdate(old runtime.Object) error {
133133
We validate the name and the spec of the CronJob.
134134
*/
135135

136-
func (c *CronJob) validateCronJob() error {
137-
allErrs := c.validateCronJobSpec()
138-
if err := c.validateCronJobName(); err != nil {
136+
func (r *CronJob) validateCronJob() error {
137+
var allErrs field.ErrorList
138+
if err := r.validateCronJobName(); err != nil {
139139
allErrs = append(allErrs, err)
140140
}
141+
if err := r.validateCronJobSpec(); err != nil {
142+
allErrs = append(allErrs, err)
143+
}
144+
if len(allErrs) == 0 {
145+
return nil
146+
}
141147

142148
return apierrors.NewInvalid(
143149
schema.GroupKind{Group: "batch.tutorial.kubebuilder.io", Kind: "CronJob"},
144-
c.Name, allErrs)
150+
r.Name, allErrs)
145151
}
146152

147153
/*
@@ -152,14 +158,24 @@ You can find all of the kubebuilder supported markers for
152158
declaring validation in [here](../TODO.md).
153159
*/
154160

155-
func (c *CronJob) validateCronJobSpec() field.ErrorList {
161+
func (r *CronJob) validateCronJobSpec() *field.Error {
156162
// The field helpers from the kubernetes API machinery help us return nicely
157163
// structured validation errors.
158-
allErrs := field.ErrorList{}
159-
allErrs = append(allErrs, validateScheduleFormat(
160-
c.Spec.Schedule,
161-
field.NewPath("spec").Child("schedule"))...)
162-
return allErrs
164+
return validateScheduleFormat(
165+
r.Spec.Schedule,
166+
field.NewPath("spec").Child("schedule"))
167+
}
168+
169+
/*
170+
We'll need to validate the [cron](https://en.wikipedia.org/wiki/Cron) schedule
171+
is well-formatted.
172+
*/
173+
174+
func validateScheduleFormat(schedule string, fldPath *field.Path) *field.Error {
175+
if _, err := cron.ParseStandard(schedule); err != nil {
176+
return field.Invalid(fldPath, schedule, err.Error())
177+
}
178+
return nil
163179
}
164180

165181
/*
@@ -171,31 +187,17 @@ the apimachinery repo, so we can't declaratively validate it using
171187
the validation schema.
172188
*/
173189

174-
func (c *CronJob) validateCronJobName() *field.Error {
175-
if len(c.ObjectMeta.Name) > validationutils.DNS1035LabelMaxLength-11 {
190+
func (r *CronJob) validateCronJobName() *field.Error {
191+
if len(r.ObjectMeta.Name) > validationutils.DNS1035LabelMaxLength-11 {
176192
// The job name length is 63 character like all Kubernetes objects
177193
// (which must fit in a DNS subdomain). The cronjob controller appends
178194
// a 11-character suffix to the cronjob (`-$TIMESTAMP`) when creating
179195
// a job. The job name length limit is 63 characters. Therefore cronjob
180196
// names must have length <= 63-11=52. If we don't validate this here,
181197
// then job creation will fail later.
182-
return field.Invalid(field.NewPath("metadata").Child("name"), c.Name, "must be no more than 52 characters")
198+
return field.Invalid(field.NewPath("metadata").Child("name"), r.Name, "must be no more than 52 characters")
183199
}
184200
return nil
185201
}
186202

187203
// +kubebuilder:docs-gen:collapse=Validate object name
188-
189-
/*
190-
We'll need to validate the [cron](https://en.wikipedia.org/wiki/Cron) schedule
191-
is well-formatted.
192-
*/
193-
194-
func validateScheduleFormat(schedule string, fldPath *field.Path) field.ErrorList {
195-
allErrs := field.ErrorList{}
196-
if _, err := cron.ParseStandard(schedule); err != nil {
197-
allErrs = append(allErrs, field.Invalid(fldPath, schedule, err.Error()))
198-
}
199-
200-
return allErrs
201-
}

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

+23-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ package v1
2222

2323
import (
2424
corev1 "k8s.io/api/core/v1"
25-
runtime "k8s.io/apimachinery/pkg/runtime"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"k8s.io/apimachinery/pkg/runtime"
2627
)
2728

2829
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
@@ -92,6 +93,22 @@ func (in *CronJobSpec) DeepCopyInto(out *CronJobSpec) {
9293
*out = new(int64)
9394
**out = **in
9495
}
96+
if in.Suspend != nil {
97+
in, out := &in.Suspend, &out.Suspend
98+
*out = new(bool)
99+
**out = **in
100+
}
101+
in.JobTemplate.DeepCopyInto(&out.JobTemplate)
102+
if in.SuccessfulJobsHistoryLimit != nil {
103+
in, out := &in.SuccessfulJobsHistoryLimit, &out.SuccessfulJobsHistoryLimit
104+
*out = new(int32)
105+
**out = **in
106+
}
107+
if in.FailedJobsHistoryLimit != nil {
108+
in, out := &in.FailedJobsHistoryLimit, &out.FailedJobsHistoryLimit
109+
*out = new(int32)
110+
**out = **in
111+
}
95112
}
96113

97114
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CronJobSpec.
@@ -112,6 +129,11 @@ func (in *CronJobStatus) DeepCopyInto(out *CronJobStatus) {
112129
*out = make([]corev1.ObjectReference, len(*in))
113130
copy(*out, *in)
114131
}
132+
if in.LastScheduleTime != nil {
133+
in, out := &in.LastScheduleTime, &out.LastScheduleTime
134+
*out = new(metav1.Time)
135+
(*in).DeepCopyInto(*out)
136+
}
115137
}
116138

117139
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CronJobStatus.

0 commit comments

Comments
 (0)