Skip to content

Commit

Permalink
📖 fix broken pieces in our book
Browse files Browse the repository at this point in the history
  • Loading branch information
Mengqi Yu committed Jul 15, 2019
1 parent e98cf50 commit 583c1c0
Show file tree
Hide file tree
Showing 11 changed files with 578 additions and 157 deletions.
23 changes: 15 additions & 8 deletions docs/book/src/cronjob-tutorial/testdata/project/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
# Build the manager binary
FROM golang:1.10.3 as builder
FROM golang:1.12.5 as builder

# Copy in the go src
WORKDIR /go/src/tutorial.kubebuilder.io/project
COPY vendor/ vendor/
WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download

# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY controllers/ controllers/

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

# Copy the controller-manager into a thin image
FROM ubuntu:latest
# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:latest
WORKDIR /
COPY --from=builder /go/src/tutorial.kubebuilder.io/project/manager .
COPY --from=builder /workspace/manager .
ENTRYPOINT ["/manager"]
2 changes: 1 addition & 1 deletion docs/book/src/cronjob-tutorial/testdata/project/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ docker-push:
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.0-alpha.1
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.0-beta.3
CONTROLLER_GEN=$(shell go env GOPATH)/bin/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ This markers is responsible for generating a mutating webhook manifest.
The meaning of each marker can be found [here](../TODO.md).
*/

// +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
// +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

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

// +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
// +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

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

func (c *CronJob) validateCronJob() error {
allErrs := c.validateCronJobSpec()
if err := c.validateCronJobName(); err != nil {
func (r *CronJob) validateCronJob() error {
var allErrs field.ErrorList
if err := r.validateCronJobName(); err != nil {
allErrs = append(allErrs, err)
}
if err := r.validateCronJobSpec(); err != nil {
allErrs = append(allErrs, err)
}
if len(allErrs) == 0 {
return nil
}

return apierrors.NewInvalid(
schema.GroupKind{Group: "batch.tutorial.kubebuilder.io", Kind: "CronJob"},
c.Name, allErrs)
r.Name, allErrs)
}

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

func (c *CronJob) validateCronJobSpec() field.ErrorList {
func (r *CronJob) validateCronJobSpec() *field.Error {
// The field helpers from the kubernetes API machinery help us return nicely
// structured validation errors.
allErrs := field.ErrorList{}
allErrs = append(allErrs, validateScheduleFormat(
c.Spec.Schedule,
field.NewPath("spec").Child("schedule"))...)
return allErrs
return validateScheduleFormat(
r.Spec.Schedule,
field.NewPath("spec").Child("schedule"))
}

/*
We'll need to validate the [cron](https://en.wikipedia.org/wiki/Cron) schedule
is well-formatted.
*/

func validateScheduleFormat(schedule string, fldPath *field.Path) *field.Error {
if _, err := cron.ParseStandard(schedule); err != nil {
return field.Invalid(fldPath, schedule, err.Error())
}
return nil
}

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

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

// +kubebuilder:docs-gen:collapse=Validate object name

/*
We'll need to validate the [cron](https://en.wikipedia.org/wiki/Cron) schedule
is well-formatted.
*/

func validateScheduleFormat(schedule string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if _, err := cron.ParseStandard(schedule); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, schedule, err.Error()))
}

return allErrs
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ package v1

import (
corev1 "k8s.io/api/core/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
Expand Down Expand Up @@ -92,6 +93,22 @@ func (in *CronJobSpec) DeepCopyInto(out *CronJobSpec) {
*out = new(int64)
**out = **in
}
if in.Suspend != nil {
in, out := &in.Suspend, &out.Suspend
*out = new(bool)
**out = **in
}
in.JobTemplate.DeepCopyInto(&out.JobTemplate)
if in.SuccessfulJobsHistoryLimit != nil {
in, out := &in.SuccessfulJobsHistoryLimit, &out.SuccessfulJobsHistoryLimit
*out = new(int32)
**out = **in
}
if in.FailedJobsHistoryLimit != nil {
in, out := &in.FailedJobsHistoryLimit, &out.FailedJobsHistoryLimit
*out = new(int32)
**out = **in
}
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CronJobSpec.
Expand All @@ -112,6 +129,11 @@ func (in *CronJobStatus) DeepCopyInto(out *CronJobStatus) {
*out = make([]corev1.ObjectReference, len(*in))
copy(*out, *in)
}
if in.LastScheduleTime != nil {
in, out := &in.LastScheduleTime, &out.LastScheduleTime
*out = new(metav1.Time)
(*in).DeepCopyInto(*out)
}
}

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

0 comments on commit 583c1c0

Please sign in to comment.