From 66aa98b1a3ef172ce895abdc4590454a34d3aece Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Sat, 11 Mar 2023 12:49:23 +0000 Subject: [PATCH] :book: update cronjob tutorial sample to use go/v4 --- docs/book/install-and-build.sh | 2 +- .../controller-implementation.md | 2 +- .../src/cronjob-tutorial/main-revisited.md | 2 +- .../testdata/generate_cronjob.sh | 2 +- .../testdata/project/Dockerfile | 6 +- .../testdata/project/Makefile | 8 +- .../cronjob-tutorial/testdata/project/PROJECT | 2 +- .../project/api/v1/webhook_suite_test.go | 4 +- .../testdata/project/{ => cmd}/main.go | 4 +- .../config/certmanager/certificate.yaml | 10 +- .../config/certmanager/kustomizeconfig.yaml | 10 +- ...atch.tutorial.kubebuilder.io_cronjobs.yaml | 2 +- .../crd/patches/cainjection_in_cronjobs.yaml | 2 +- .../project/config/default/kustomization.yaml | 138 +++++++++++++----- .../default/webhookcainjection_patch.yaml | 6 +- .../project/config/manager/kustomization.yaml | 1 - .../project/config/rbac/service_account.yaml | 2 +- .../project/config/samples/kustomization.yaml | 4 + .../config/webhook/kustomizeconfig.yaml | 5 +- .../cronjob-tutorial/testdata/project/go.mod | 2 +- .../cronjob-tutorial/testdata/project/go.sum | 4 +- .../controller}/cronjob_controller.go | 4 +- .../controller}/cronjob_controller_test.go | 2 +- .../controller}/suite_test.go | 6 +- .../src/cronjob-tutorial/writing-tests.md | 4 +- 25 files changed, 149 insertions(+), 85 deletions(-) rename docs/book/src/cronjob-tutorial/testdata/project/{ => cmd}/main.go (98%) create mode 100644 docs/book/src/cronjob-tutorial/testdata/project/config/samples/kustomization.yaml rename docs/book/src/cronjob-tutorial/testdata/project/{controllers => internal/controller}/cronjob_controller.go (99%) rename docs/book/src/cronjob-tutorial/testdata/project/{controllers => internal/controller}/cronjob_controller_test.go (99%) rename docs/book/src/cronjob-tutorial/testdata/project/{controllers => internal/controller}/suite_test.go (96%) diff --git a/docs/book/install-and-build.sh b/docs/book/install-and-build.sh index 8a4ef89b451..fc9c6a51199 100755 --- a/docs/book/install-and-build.sh +++ b/docs/book/install-and-build.sh @@ -67,7 +67,7 @@ ${cmd} /tmp/mdbook.${ext} chmod +x /tmp/mdbook echo "grabbing the latest released controller-gen" -go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.11.1 +go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.11.3 # make sure we add the go bin directory to our path gobin=$(go env GOBIN) diff --git a/docs/book/src/cronjob-tutorial/controller-implementation.md b/docs/book/src/cronjob-tutorial/controller-implementation.md index 95dc605f152..be548e069a1 100644 --- a/docs/book/src/cronjob-tutorial/controller-implementation.md +++ b/docs/book/src/cronjob-tutorial/controller-implementation.md @@ -18,7 +18,7 @@ The basic logic of our CronJob controller is this: 7. Requeue when we either see a running job (done automatically) or it's time for the next scheduled run. -{{#literatego ./testdata/project/controllers/cronjob_controller.go}} +{{#literatego ./testdata/project/internal/controller/cronjob_controller.go}} That was a doozy, but now we've got a working controller. Let's test against the cluster, then, if we don't have any issues, deploy it! diff --git a/docs/book/src/cronjob-tutorial/main-revisited.md b/docs/book/src/cronjob-tutorial/main-revisited.md index 395b67e5bd3..4a8c0fd33dc 100644 --- a/docs/book/src/cronjob-tutorial/main-revisited.md +++ b/docs/book/src/cronjob-tutorial/main-revisited.md @@ -4,6 +4,6 @@ But first, remember how we said we'd [come back to `main.go` again](/cronjob-tutorial/empty-main.md)? Let's take a look and see what's changed, and what we need to add. -{{#literatego ./testdata/project/main.go}} +{{#literatego ./testdata/project/cmd/main.go}} *Now* we can implement our controller. diff --git a/docs/book/src/cronjob-tutorial/testdata/generate_cronjob.sh b/docs/book/src/cronjob-tutorial/testdata/generate_cronjob.sh index 073de3ec5fc..2adc6603cef 100755 --- a/docs/book/src/cronjob-tutorial/testdata/generate_cronjob.sh +++ b/docs/book/src/cronjob-tutorial/testdata/generate_cronjob.sh @@ -52,7 +52,7 @@ function gen_cronjob_tutorial { mkdir project cd project header_text "creating tutorial.kubebuilder.io base ..." - kubebuilder init --domain tutorial.kubebuilder.io --repo tutorial.kubebuilder.io/project --license apache2 --owner "The Kubernetes authors" + kubebuilder init --plugins=go/v4 --domain tutorial.kubebuilder.io --repo tutorial.kubebuilder.io/project --license apache2 --owner "The Kubernetes authors" kubebuilder create api --group batch --version v1 --kind CronJob --resource --controller kubebuilder create webhook --group batch --version v1 --kind CronJob --defaulting --programmatic-validation go mod tidy diff --git a/docs/book/src/cronjob-tutorial/testdata/project/Dockerfile b/docs/book/src/cronjob-tutorial/testdata/project/Dockerfile index 8f9cca18eb6..ef4cfaf90bd 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/Dockerfile +++ b/docs/book/src/cronjob-tutorial/testdata/project/Dockerfile @@ -12,16 +12,16 @@ COPY go.sum go.sum RUN go mod download # Copy the go source -COPY main.go main.go +COPY cmd/main.go cmd/main.go COPY api/ api/ -COPY controllers/ controllers/ +COPY internal/controller/ internal/controller/ # Build # the GOARCH has not a default value to allow the binary be built according to the host where the command # was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO # the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore, # by leaving it empty we can ensure that the container and binary shipped on it will have the same platform. -RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager main.go +RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details diff --git a/docs/book/src/cronjob-tutorial/testdata/project/Makefile b/docs/book/src/cronjob-tutorial/testdata/project/Makefile index e3e61f60c84..87dd37d4e2f 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/Makefile +++ b/docs/book/src/cronjob-tutorial/testdata/project/Makefile @@ -62,11 +62,11 @@ test: manifests generate fmt vet envtest ## Run tests. .PHONY: build build: manifests generate fmt vet ## Build manager binary. - go build -o bin/manager main.go + go build -o bin/manager cmd/main.go .PHONY: run run: manifests generate fmt vet ## Run a controller from your host. - go run ./main.go + go run ./cmd/main.go # If you wish built the manager image targeting other platforms you can use the --platform flag. # (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it. @@ -132,8 +132,8 @@ CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen ENVTEST ?= $(LOCALBIN)/setup-envtest ## Tool Versions -KUSTOMIZE_VERSION ?= v3.8.7 -CONTROLLER_TOOLS_VERSION ?= v0.11.1 +KUSTOMIZE_VERSION ?= v5.0.0 +CONTROLLER_TOOLS_VERSION ?= v0.11.3 KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" .PHONY: kustomize diff --git a/docs/book/src/cronjob-tutorial/testdata/project/PROJECT b/docs/book/src/cronjob-tutorial/testdata/project/PROJECT index 8c1ce305ea2..573e2e5ff1f 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/PROJECT +++ b/docs/book/src/cronjob-tutorial/testdata/project/PROJECT @@ -4,7 +4,7 @@ # More info: https://book.kubebuilder.io/reference/project-config.html domain: tutorial.kubebuilder.io layout: -- go.kubebuilder.io/v3 +- go.kubebuilder.io/v4 projectName: project repo: tutorial.kubebuilder.io/project resources: diff --git a/docs/book/src/cronjob-tutorial/testdata/project/api/v1/webhook_suite_test.go b/docs/book/src/cronjob-tutorial/testdata/project/api/v1/webhook_suite_test.go index 620effb6197..9abf343fbdb 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/api/v1/webhook_suite_test.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/api/v1/webhook_suite_test.go @@ -28,7 +28,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - admissionv1beta1 "k8s.io/api/admission/v1beta1" + admissionv1 "k8s.io/api/admission/v1" //+kubebuilder:scaffold:imports "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/rest" @@ -78,7 +78,7 @@ var _ = BeforeSuite(func() { err = AddToScheme(scheme) Expect(err).NotTo(HaveOccurred()) - err = admissionv1beta1.AddToScheme(scheme) + err = admissionv1.AddToScheme(scheme) Expect(err).NotTo(HaveOccurred()) //+kubebuilder:scaffold:scheme diff --git a/docs/book/src/cronjob-tutorial/testdata/project/main.go b/docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go similarity index 98% rename from docs/book/src/cronjob-tutorial/testdata/project/main.go rename to docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go index 4577185fb12..65054cbc361 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/main.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go @@ -20,6 +20,7 @@ package main import ( "flag" "os" + "tutorial.kubebuilder.io/project/internal/controller" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. @@ -33,7 +34,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log/zap" batchv1 "tutorial.kubebuilder.io/project/api/v1" - "tutorial.kubebuilder.io/project/controllers" //+kubebuilder:scaffold:imports ) @@ -109,7 +109,7 @@ func main() { // +kubebuilder:docs-gen:collapse=old stuff - if err = (&controllers.CronJobReconciler{ + if err = (&controller.CronJobReconciler{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { diff --git a/docs/book/src/cronjob-tutorial/testdata/project/config/certmanager/certificate.yaml b/docs/book/src/cronjob-tutorial/testdata/project/config/certmanager/certificate.yaml index 886202b4ed7..36b9d42e69d 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/config/certmanager/certificate.yaml +++ b/docs/book/src/cronjob-tutorial/testdata/project/config/certmanager/certificate.yaml @@ -5,8 +5,8 @@ apiVersion: cert-manager.io/v1 kind: Issuer metadata: labels: - app.kubernetes.io/name: issuer - app.kubernetes.io/instance: selfsigned-issuer + app.kubernetes.io/name: certificate + app.kubernetes.io/instance: serving-cert app.kubernetes.io/component: certificate app.kubernetes.io/created-by: project app.kubernetes.io/part-of: project @@ -29,10 +29,10 @@ metadata: name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml namespace: system spec: - # $(SERVICE_NAME) and $(SERVICE_NAMESPACE) will be substituted by kustomize + # SERVICE_NAME and SERVICE_NAMESPACE will be substituted by kustomize dnsNames: - - $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc - - $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc.cluster.local + - SERVICE_NAME.SERVICE_NAMESPACE.svc + - SERVICE_NAME.SERVICE_NAMESPACE.svc.cluster.local issuerRef: kind: Issuer name: selfsigned-issuer diff --git a/docs/book/src/cronjob-tutorial/testdata/project/config/certmanager/kustomizeconfig.yaml b/docs/book/src/cronjob-tutorial/testdata/project/config/certmanager/kustomizeconfig.yaml index e631f777366..cf6f89e8892 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/config/certmanager/kustomizeconfig.yaml +++ b/docs/book/src/cronjob-tutorial/testdata/project/config/certmanager/kustomizeconfig.yaml @@ -1,4 +1,4 @@ -# This configuration is for teaching kustomize how to update name ref and var substitution +# This configuration is for teaching kustomize how to update name ref substitution nameReference: - kind: Issuer group: cert-manager.io @@ -6,11 +6,3 @@ nameReference: - kind: Certificate group: cert-manager.io path: spec/issuerRef/name - -varReference: -- kind: Certificate - group: cert-manager.io - path: spec/commonName -- kind: Certificate - group: cert-manager.io - path: spec/dnsNames diff --git a/docs/book/src/cronjob-tutorial/testdata/project/config/crd/bases/batch.tutorial.kubebuilder.io_cronjobs.yaml b/docs/book/src/cronjob-tutorial/testdata/project/config/crd/bases/batch.tutorial.kubebuilder.io_cronjobs.yaml index 670fd84e429..8279fb83eb7 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/config/crd/bases/batch.tutorial.kubebuilder.io_cronjobs.yaml +++ b/docs/book/src/cronjob-tutorial/testdata/project/config/crd/bases/batch.tutorial.kubebuilder.io_cronjobs.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.11.1 + controller-gen.kubebuilder.io/version: v0.11.3 creationTimestamp: null name: cronjobs.batch.tutorial.kubebuilder.io spec: diff --git a/docs/book/src/cronjob-tutorial/testdata/project/config/crd/patches/cainjection_in_cronjobs.yaml b/docs/book/src/cronjob-tutorial/testdata/project/config/crd/patches/cainjection_in_cronjobs.yaml index 7b037c0e41f..752fa9ac6a0 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/config/crd/patches/cainjection_in_cronjobs.yaml +++ b/docs/book/src/cronjob-tutorial/testdata/project/config/crd/patches/cainjection_in_cronjobs.yaml @@ -3,5 +3,5 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME) + cert-manager.io/inject-ca-from: CERTIFICATE_NAMESPACE/CERTIFICATE_NAME name: cronjobs.batch.tutorial.kubebuilder.io diff --git a/docs/book/src/cronjob-tutorial/testdata/project/config/default/kustomization.yaml b/docs/book/src/cronjob-tutorial/testdata/project/config/default/kustomization.yaml index 178e48bdb97..d9dc63e0d2a 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/config/default/kustomization.yaml +++ b/docs/book/src/cronjob-tutorial/testdata/project/config/default/kustomization.yaml @@ -9,10 +9,12 @@ namespace: project-system namePrefix: project- # Labels to add to all resources and selectors. -#commonLabels: -# someName: someValue +#labels: +#- includeSelectors: true +# pairs: +# someName: someValue -bases: +resources: - ../crd - ../rbac - ../manager @@ -22,7 +24,7 @@ bases: # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required. - ../certmanager # [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'. -#- ../prometheus +- ../prometheus patchesStrategicMerge: # Protect the /metrics endpoint by putting it behind auth. @@ -34,39 +36,109 @@ patchesStrategicMerge: # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in # crd/kustomization.yaml -- manager_webhook_patch.yaml +#- manager_webhook_patch.yaml # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. # Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks. # 'CERTMANAGER' needs to be enabled to use ca injection - webhookcainjection_patch.yaml -# the following config is for teaching kustomize how to do var substitution -vars: # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix. -- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR - objref: - kind: Certificate - group: cert-manager.io - version: v1 - name: serving-cert # this name should match the one in certificate.yaml - fieldref: - fieldpath: metadata.namespace -- name: CERTIFICATE_NAME - objref: - kind: Certificate - group: cert-manager.io - version: v1 - name: serving-cert # this name should match the one in certificate.yaml -- name: SERVICE_NAMESPACE # namespace of the service - objref: - kind: Service - version: v1 - name: webhook-service - fieldref: - fieldpath: metadata.namespace -- name: SERVICE_NAME - objref: - kind: Service - version: v1 - name: webhook-service +# Uncomment the following replacements to add the cert-manager CA injection annotations +replacements: + - source: # Add cert-manager annotation to ValidatingWebhookConfiguration, MutatingWebhookConfiguration and CRDs + kind: Certificate + group: cert-manager.io + version: v1 + name: serving-cert # this name should match the one in certificate.yaml + fieldPath: .metadata.namespace # namespace of the certificate CR + targets: + - select: + kind: ValidatingWebhookConfiguration + fieldPaths: + - .metadata.annotations.[cert-manager.io/inject-ca-from] + options: + delimiter: '/' + index: 0 + create: true + - select: + kind: MutatingWebhookConfiguration + fieldPaths: + - .metadata.annotations.[cert-manager.io/inject-ca-from] + options: + delimiter: '/' + index: 0 + create: true + - select: + kind: CustomResourceDefinition + fieldPaths: + - .metadata.annotations.[cert-manager.io/inject-ca-from] + options: + delimiter: '/' + index: 0 + create: true + - source: + kind: Certificate + group: cert-manager.io + version: v1 + name: serving-cert # this name should match the one in certificate.yaml + fieldPath: .metadata.name + targets: + - select: + kind: ValidatingWebhookConfiguration + fieldPaths: + - .metadata.annotations.[cert-manager.io/inject-ca-from] + options: + delimiter: '/' + index: 1 + create: true + - select: + kind: MutatingWebhookConfiguration + fieldPaths: + - .metadata.annotations.[cert-manager.io/inject-ca-from] + options: + delimiter: '/' + index: 1 + create: true + - select: + kind: CustomResourceDefinition + fieldPaths: + - .metadata.annotations.[cert-manager.io/inject-ca-from] + options: + delimiter: '/' + index: 1 + create: true + - source: # Add cert-manager annotation to the webhook Service + kind: Service + version: v1 + name: webhook-service + fieldPath: .metadata.name # namespace of the service + targets: + - select: + kind: Certificate + group: cert-manager.io + version: v1 + fieldPaths: + - .spec.dnsNames.0 + - .spec.dnsNames.1 + options: + delimiter: '.' + index: 0 + create: true + - source: + kind: Service + version: v1 + name: webhook-service + fieldPath: .metadata.namespace # namespace of the service + targets: + - select: + kind: Certificate + group: cert-manager.io + version: v1 + fieldPaths: + - .spec.dnsNames.0 + - .spec.dnsNames.1 + options: + delimiter: '.' + index: 1 + create: true diff --git a/docs/book/src/cronjob-tutorial/testdata/project/config/default/webhookcainjection_patch.yaml b/docs/book/src/cronjob-tutorial/testdata/project/config/default/webhookcainjection_patch.yaml index 88cd5d22870..202f1d83d52 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/config/default/webhookcainjection_patch.yaml +++ b/docs/book/src/cronjob-tutorial/testdata/project/config/default/webhookcainjection_patch.yaml @@ -1,5 +1,5 @@ # This patch add annotation to admission webhook config and -# the variables $(CERTIFICATE_NAMESPACE) and $(CERTIFICATE_NAME) will be substituted by kustomize. +# CERTIFICATE_NAMESPACE and CERTIFICATE_NAME will be substituted by kustomize apiVersion: admissionregistration.k8s.io/v1 kind: MutatingWebhookConfiguration metadata: @@ -12,7 +12,7 @@ metadata: app.kubernetes.io/managed-by: kustomize name: mutating-webhook-configuration annotations: - cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME) + cert-manager.io/inject-ca-from: CERTIFICATE_NAMESPACE/CERTIFICATE_NAME --- apiVersion: admissionregistration.k8s.io/v1 kind: ValidatingWebhookConfiguration @@ -26,4 +26,4 @@ metadata: app.kubernetes.io/managed-by: kustomize name: validating-webhook-configuration annotations: - cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME) + cert-manager.io/inject-ca-from: CERTIFICATE_NAMESPACE/CERTIFICATE_NAME diff --git a/docs/book/src/cronjob-tutorial/testdata/project/config/manager/kustomization.yaml b/docs/book/src/cronjob-tutorial/testdata/project/config/manager/kustomization.yaml index 372a75ae43f..5c5f0b84cba 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/config/manager/kustomization.yaml +++ b/docs/book/src/cronjob-tutorial/testdata/project/config/manager/kustomization.yaml @@ -1,3 +1,2 @@ resources: - manager.yaml - diff --git a/docs/book/src/cronjob-tutorial/testdata/project/config/rbac/service_account.yaml b/docs/book/src/cronjob-tutorial/testdata/project/config/rbac/service_account.yaml index 018f2dd1b59..c38941e4f9e 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/config/rbac/service_account.yaml +++ b/docs/book/src/cronjob-tutorial/testdata/project/config/rbac/service_account.yaml @@ -3,7 +3,7 @@ kind: ServiceAccount metadata: labels: app.kubernetes.io/name: serviceaccount - app.kubernetes.io/instance: controller-manager + app.kubernetes.io/instance: controller-manager-sa app.kubernetes.io/component: rbac app.kubernetes.io/created-by: project app.kubernetes.io/part-of: project diff --git a/docs/book/src/cronjob-tutorial/testdata/project/config/samples/kustomization.yaml b/docs/book/src/cronjob-tutorial/testdata/project/config/samples/kustomization.yaml new file mode 100644 index 00000000000..9324acc15c9 --- /dev/null +++ b/docs/book/src/cronjob-tutorial/testdata/project/config/samples/kustomization.yaml @@ -0,0 +1,4 @@ +## Append samples of your project ## +resources: +- batch_v1_cronjob.yaml +#+kubebuilder:scaffold:manifestskustomizesamples diff --git a/docs/book/src/cronjob-tutorial/testdata/project/config/webhook/kustomizeconfig.yaml b/docs/book/src/cronjob-tutorial/testdata/project/config/webhook/kustomizeconfig.yaml index 25e21e3c963..206316e54ff 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/config/webhook/kustomizeconfig.yaml +++ b/docs/book/src/cronjob-tutorial/testdata/project/config/webhook/kustomizeconfig.yaml @@ -1,4 +1,4 @@ -# the following config is for teaching kustomize where to look at when substituting vars. +# the following config is for teaching kustomize where to look at when substituting nameReference. # It requires kustomize v2.1.0 or newer to work properly. nameReference: - kind: Service @@ -20,6 +20,3 @@ namespace: group: admissionregistration.k8s.io path: webhooks/clientConfig/service/namespace create: true - -varReference: -- path: metadata/annotations diff --git a/docs/book/src/cronjob-tutorial/testdata/project/go.mod b/docs/book/src/cronjob-tutorial/testdata/project/go.mod index 586d85969da..80df4571d5f 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/go.mod +++ b/docs/book/src/cronjob-tutorial/testdata/project/go.mod @@ -8,7 +8,7 @@ require ( k8s.io/api v0.26.1 k8s.io/apimachinery v0.26.1 k8s.io/client-go v0.26.1 - sigs.k8s.io/controller-runtime v0.14.2 + sigs.k8s.io/controller-runtime v0.14.4 ) require ( diff --git a/docs/book/src/cronjob-tutorial/testdata/project/go.sum b/docs/book/src/cronjob-tutorial/testdata/project/go.sum index 4709c2a2d1e..4c702bb0401 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/go.sum +++ b/docs/book/src/cronjob-tutorial/testdata/project/go.sum @@ -607,8 +607,8 @@ k8s.io/utils v0.0.0-20221128185143-99ec85e7a448/go.mod h1:OLgZIPagt7ERELqWJFomSt rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= -sigs.k8s.io/controller-runtime v0.14.2 h1:P6IwDhbsRWsBClt/8/h8Zy36bCuGuW5Op7MHpFrN/60= -sigs.k8s.io/controller-runtime v0.14.2/go.mod h1:WqIdsAY6JBsjfc/CqO0CORmNtoCtE4S6qbPc9s68h+0= +sigs.k8s.io/controller-runtime v0.14.4 h1:Kd/Qgx5pd2XUL08eOV2vwIq3L9GhIbJ5Nxengbd4/0M= +sigs.k8s.io/controller-runtime v0.14.4/go.mod h1:WqIdsAY6JBsjfc/CqO0CORmNtoCtE4S6qbPc9s68h+0= sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 h1:iXTIw73aPyC+oRdyqqvVJuloN1p0AC/kzH07hu3NE+k= sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= sigs.k8s.io/structured-merge-diff/v4 v4.2.3 h1:PRbqxJClWWYMNV1dhaG4NsibJbArud9kFxnAMREiWFE= diff --git a/docs/book/src/cronjob-tutorial/testdata/project/controllers/cronjob_controller.go b/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller.go similarity index 99% rename from docs/book/src/cronjob-tutorial/testdata/project/controllers/cronjob_controller.go rename to docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller.go index c6e599eaf86..64f0eef969c 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/controllers/cronjob_controller.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller.go @@ -19,7 +19,7 @@ limitations under the License. We'll start out with some imports. You'll see below that we'll need a few more imports than those scaffolded for us. We'll talk about each one when we use it. */ -package controllers +package controller import ( "context" @@ -94,7 +94,7 @@ var ( // the user. // // For more details, check Reconcile and its Result here: -// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.14.0/pkg/reconcile +// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.14.4/pkg/reconcile func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := log.FromContext(ctx) diff --git a/docs/book/src/cronjob-tutorial/testdata/project/controllers/cronjob_controller_test.go b/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go similarity index 99% rename from docs/book/src/cronjob-tutorial/testdata/project/controllers/cronjob_controller_test.go rename to docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go index aab07668d49..d28c25d53aa 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/controllers/cronjob_controller_test.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go @@ -22,7 +22,7 @@ So, let's write our example test for the CronJob controller (`cronjob_controller /* As usual, we start with the necessary imports. We also define some utility variables. */ -package controllers +package controller import ( "context" diff --git a/docs/book/src/cronjob-tutorial/testdata/project/controllers/suite_test.go b/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/suite_test.go similarity index 96% rename from docs/book/src/cronjob-tutorial/testdata/project/controllers/suite_test.go rename to docs/book/src/cronjob-tutorial/testdata/project/internal/controller/suite_test.go index 9336f69319f..4fade8b1492 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/controllers/suite_test.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/suite_test.go @@ -17,12 +17,12 @@ limitations under the License. /* When we created the CronJob API with `kubebuilder create api` in a [previous chapter](/cronjob-tutorial/new-api.md), Kubebuilder already did some test work for you. -Kubebuilder scaffolded a `controllers/suite_test.go` file that does the bare bones of setting up a test environment. +Kubebuilder scaffolded a `internal/controller/suite_test.go` file that does the bare bones of setting up a test environment. First, it will contain the necessary imports. */ -package controllers +package controller import ( "context" @@ -77,7 +77,7 @@ var _ = BeforeSuite(func() { */ By("bootstrapping test environment") testEnv = &envtest.Environment{ - CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, + CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, ErrorIfCRDPathMissing: true, } diff --git a/docs/book/src/cronjob-tutorial/writing-tests.md b/docs/book/src/cronjob-tutorial/writing-tests.md index e62d8a31441..f31d87fc1c6 100644 --- a/docs/book/src/cronjob-tutorial/writing-tests.md +++ b/docs/book/src/cronjob-tutorial/writing-tests.md @@ -11,11 +11,11 @@ If you want to tinker with how your envtest cluster is configured, see section [ ## Test Environment Setup -{{#literatego ../cronjob-tutorial/testdata/project/controllers/suite_test.go}} +{{#literatego ../cronjob-tutorial/testdata/project/internal/controller/suite_test.go}} ## Testing your Controller's Behavior -{{#literatego ../cronjob-tutorial/testdata/project/controllers/cronjob_controller_test.go}} +{{#literatego ../cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go}} This Status update example above demonstrates a general testing strategy for a custom Kind with downstream objects. By this point, you hopefully have learned the following methods for testing your controller behavior: