Skip to content

Commit 3e8df57

Browse files
📖 update multiversion tutorial to use go/v4 (#3282)
1 parent 1dc75e3 commit 3e8df57

32 files changed

+1081
-398
lines changed

docs/book/src/multiversion-tutorial/testdata/generate_multiversion.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function gen_cronjob_tutorial {
5050
mkdir project
5151
cd project
5252
header_text "generate base ..."
53-
kubebuilder init --domain=tutorial.kubebuilder.io --project-version=3 --repo=tutorial.kubebuilder.io/project --license apache2 --owner "The Kubernetes authors"
53+
kubebuilder init --plugins="go/v4" --domain=tutorial.kubebuilder.io --project-version=3 --repo=tutorial.kubebuilder.io/project --license apache2 --owner "The Kubernetes authors"
5454
kubebuilder create api --group batch --version v1 --kind CronJob --resource --controller --make=false
5555
kubebuilder create webhook --group batch --version v1 --kind CronJob --defaulting --programmatic-validation
5656
kubebuilder create webhook --group batch --version v1 --kind CronJob --conversion --force

docs/book/src/multiversion-tutorial/testdata/project/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ COPY go.sum go.sum
1212
RUN go mod download
1313

1414
# Copy the go source
15-
COPY main.go main.go
15+
COPY cmd/main.go cmd/main.go
1616
COPY api/ api/
17-
COPY controllers/ controllers/
17+
COPY internal/controller/ internal/controller/
1818

1919
# Build
2020
# the GOARCH has not a default value to allow the binary be built according to the host where the command
2121
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
2222
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
2323
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
24-
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager main.go
24+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
2525

2626
# Use distroless as minimal base image to package the manager binary
2727
# Refer to https://github.com/GoogleContainerTools/distroless for more details

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Image URL to use all building/pushing image targets
33
IMG ?= controller:latest
44
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
5-
ENVTEST_K8S_VERSION = 1.25.0
5+
ENVTEST_K8S_VERSION = 1.26.1
66

77
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
88
ifeq (,$(shell go env GOBIN))
@@ -65,12 +65,12 @@ test: manifests generate fmt vet envtest ## Run tests.
6565
##@ Build
6666

6767
.PHONY: build
68-
build: generate fmt vet ## Build manager binary.
69-
go build -o bin/manager main.go
68+
build: manifests generate fmt vet ## Build manager binary.
69+
go build -o bin/manager cmd/main.go
7070

7171
.PHONY: run
7272
run: manifests generate fmt vet ## Run a controller from your host.
73-
go run ./main.go
73+
go run ./cmd/main.go
7474

7575
# If you wish built the manager image targeting other platforms you can use the --platform flag.
7676
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
@@ -96,7 +96,7 @@ docker-buildx: test ## Build and push docker image for the manager for cross-pla
9696
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
9797
- docker buildx create --name project-v3-builder
9898
docker buildx use project-v3-builder
99-
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross
99+
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
100100
- docker buildx rm project-v3-builder
101101
rm Dockerfile.cross
102102

@@ -136,19 +136,24 @@ CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
136136
ENVTEST ?= $(LOCALBIN)/setup-envtest
137137

138138
## Tool Versions
139-
KUSTOMIZE_VERSION ?= v4.5.7
140-
CONTROLLER_TOOLS_VERSION ?= v0.10.0
139+
KUSTOMIZE_VERSION ?= v5.0.0
140+
CONTROLLER_TOOLS_VERSION ?= v0.11.3
141141

142142
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
143143
.PHONY: kustomize
144-
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
144+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
145145
$(KUSTOMIZE): $(LOCALBIN)
146-
test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }
146+
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
147+
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
148+
rm -rf $(LOCALBIN)/kustomize; \
149+
fi
150+
test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) --output install_kustomize.sh && bash install_kustomize.sh $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); rm install_kustomize.sh; }
147151

148152
.PHONY: controller-gen
149-
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
153+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
150154
$(CONTROLLER_GEN): $(LOCALBIN)
151-
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
155+
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
156+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
152157

153158
.PHONY: envtest
154159
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.

docs/book/src/multiversion-tutorial/testdata/project/PROJECT

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
# Code generated by tool. DO NOT EDIT.
2+
# This file is used to track the info used to scaffold your project
3+
# and allow the plugins properly work.
4+
# More info: https://book.kubebuilder.io/reference/project-config.html
15
domain: tutorial.kubebuilder.io
26
layout:
3-
- go.kubebuilder.io/v3
7+
- go.kubebuilder.io/v4
48
projectName: project
59
repo: tutorial.kubebuilder.io/project
610
resources:

docs/book/src/multiversion-tutorial/testdata/project/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ kubectl apply -f config/samples/
1616
```
1717

1818
2. Build and push your image to the location specified by `IMG`:
19-
19+
2020
```sh
2121
make docker-build docker-push IMG=<some-registry>/project:tag
2222
```
23-
23+
2424
3. Deploy the controller to the cluster with the image specified by `IMG`:
2525

2626
```sh
@@ -35,7 +35,7 @@ make uninstall
3535
```
3636

3737
### Undeploy controller
38-
UnDeploy the controller to the cluster:
38+
UnDeploy the controller from the cluster:
3939

4040
```sh
4141
make undeploy
@@ -45,10 +45,10 @@ make undeploy
4545
// TODO(user): Add detailed information on how you would like others to contribute to this project
4646

4747
### How it works
48-
This project aims to follow the Kubernetes [Operator pattern](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/)
48+
This project aims to follow the Kubernetes [Operator pattern](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/).
4949

50-
It uses [Controllers](https://kubernetes.io/docs/concepts/architecture/controller/)
51-
which provides a reconcile function responsible for synchronizing resources until the desired state is reached on the cluster
50+
It uses [Controllers](https://kubernetes.io/docs/concepts/architecture/controller/),
51+
which provide a reconcile function responsible for synchronizing resources until the desired state is reached on the cluster.
5252

5353
### Test It Out
5454
1. Install the CRDs into the cluster:
@@ -78,7 +78,7 @@ More information can be found via the [Kubebuilder Documentation](https://book.k
7878

7979
## License
8080

81-
Copyright 2022 The Kubernetes authors.
81+
Copyright 2023 The Kubernetes authors.
8282

8383
Licensed under the Apache License, Version 2.0 (the "License");
8484
you may not use this file except in compliance with the License.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 The Kubernetes authors.
2+
Copyright 2023 The Kubernetes authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 The Kubernetes authors.
2+
Copyright 2023 The Kubernetes authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.

docs/book/src/multiversion-tutorial/testdata/project/api/v1/groupversion_info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 The Kubernetes authors.
2+
Copyright 2023 The Kubernetes authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.

docs/book/src/multiversion-tutorial/testdata/project/api/v1/webhook_suite_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 The Kubernetes authors.
2+
Copyright 2023 The Kubernetes authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@ import (
2828
. "github.com/onsi/ginkgo/v2"
2929
. "github.com/onsi/gomega"
3030

31-
admissionv1beta1 "k8s.io/api/admission/v1beta1"
31+
admissionv1 "k8s.io/api/admission/v1"
3232
//+kubebuilder:scaffold:imports
3333
"k8s.io/apimachinery/pkg/runtime"
3434
"k8s.io/client-go/rest"
@@ -51,7 +51,7 @@ var cancel context.CancelFunc
5151
func TestAPIs(t *testing.T) {
5252
RegisterFailHandler(Fail)
5353

54-
RunSpecs(t, "Controller Suite")
54+
RunSpecs(t, "Webhook Suite")
5555
}
5656

5757
var _ = BeforeSuite(func() {
@@ -78,7 +78,7 @@ var _ = BeforeSuite(func() {
7878
err = AddToScheme(scheme)
7979
Expect(err).NotTo(HaveOccurred())
8080

81-
err = admissionv1beta1.AddToScheme(scheme)
81+
err = admissionv1.AddToScheme(scheme)
8282
Expect(err).NotTo(HaveOccurred())
8383

8484
//+kubebuilder:scaffold:scheme

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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)