Skip to content

Commit 5ca8edc

Browse files
refactor code-gen scaffolding (#148)
* refactor code-gen scaffolding * Update Makefile.tpl * update scaffold versions * scaffold: fix Makefile * scaffold: improve Makefile template --------- Co-authored-by: Zdenko Kovac <128780884+zdenko-kovac@users.noreply.github.com>
1 parent 60a05c4 commit 5ca8edc

File tree

6 files changed

+112
-83
lines changed

6 files changed

+112
-83
lines changed

scaffold/scaffold.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ var templates embed.FS
8585

8686
// default verions
8787
var (
88-
goVersion = "1.21.7"
89-
kubernetesVersion = "v0.29.2"
90-
controllerRuntimeVersion = "v0.17.2"
91-
controllerToolsVersion = "v0.14.0"
92-
codeGeneratorVersion = "v0.29.0"
93-
admissionWebhookRuntimeVersion = "v0.1.0"
94-
envtestKubernetesVersion = "1.28.3"
88+
goVersion = "1.23.4"
89+
kubernetesVersion = "v0.32.0"
90+
controllerRuntimeVersion = "v0.19.3"
91+
controllerToolsVersion = "v0.16.5"
92+
codeGeneratorVersion = "v0.32.0"
93+
admissionWebhookRuntimeVersion = "v0.1.52"
94+
envtestKubernetesVersion = "1.30.3"
9595
)
9696

9797
func main() {

scaffold/templates/Dockerfile.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM --platform=$BUILDPLATFORM golang:{{ .goVersion }} as builder
2+
FROM --platform=$BUILDPLATFORM golang:{{ .goVersion }} AS builder
33
ARG TARGETOS
44
ARG TARGETARCH
55

scaffold/templates/Makefile.tpl

Lines changed: 46 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,62 +13,65 @@ all: build
1313
##@ General
1414

1515
.PHONY: help
16-
help: ## Display this help.
17-
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
16+
help: ## Display this help
17+
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
1818

1919
##@ Development
2020

2121
.PHONY: manifests
22-
manifests: controller-gen ## Generate CustomResourceDefinition objects.
23-
$(CONTROLLER_GEN) crd paths="./api/..." output:crd:artifacts:config=crds ;\
22+
manifests: controller-gen ## Generate CustomResourceDefinition objects
23+
$(LOCALBIN)/controller-gen crd paths="./api/..." output:crd:artifacts:config=crds && \
2424
test ! -d chart || test -e chart/crds || ln -s ../crds chart/crds
2525

2626
.PHONY: generate
27-
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
28-
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./api/..."
27+
generate: generate-deepcopy ## Generate required code pieces
28+
29+
.PHONY: generate-deepcopy
30+
generate-deepcopy: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations
31+
$(LOCALBIN)/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./api/..."
2932

3033
.PHONY: generate-client
31-
generate-client: client-gen informer-gen lister-gen ## Generate typed client.
34+
generate-client: ## Generate typed client
3235
./hack/genclient.sh
3336

3437
.PHONY: fmt
35-
fmt: ## Run go fmt against code.
38+
fmt: ## Run go fmt against code
3639
go fmt ./...
3740

3841
.PHONY: vet
39-
vet: ## Run go vet against code.
42+
vet: ## Run go vet against code
4043
go vet ./...
4144

4245
##@ Testing
4346

4447
.PHONY: test
45-
test: manifests generate fmt vet envtest ## Run tests.
48+
test: manifests generate-deepcopy fmt vet envtest ## Run tests
4649
KUBEBUILDER_ASSETS="$(LOCALBIN)/k8s/current" go test ./... -coverprofile cover.out
4750

4851
##@ Build
4952

5053
.PHONY: build
51-
build: generate fmt vet ## Build manager binary.
54+
build: generate-deepcopy fmt vet ## Build manager binary
5255
go build -o bin/manager main.go
5356

5457
.PHONY: run
55-
run: manifests generate fmt vet ## Run a controller from your host.
58+
run: manifests generate-deepcopy fmt vet ## Run a controller from your host
5659
go run ./main.go
5760

58-
# Build docker image in current architecture and tag it as ${IMG}.
61+
# Build docker image in current architecture and tag it as ${IMG}
5962
.PHONY: docker-build
60-
docker-build: ## Build docker image with the manager.
63+
docker-build: ## Build docker image with the manager
6164
docker build -t ${IMG} .
6265

63-
# Push docker image to the target specified in ${IMG}.
66+
# Push docker image to the target specified in ${IMG}
6467
.PHONY: docker-push
65-
docker-push: ## Push docker image with the manager.
68+
docker-push: ## Push docker image with the manager
6669
docker push ${IMG}
6770

68-
# Build and push docker image for all given platforms.
71+
# Build and push docker image for all given platforms
6972
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
7073
.PHONY: docker-buildx
71-
docker-buildx: ## Build and push docker image for the manager for cross-platform support.
74+
docker-buildx: ## Build and push docker image for the manager for cross-platform support
7275
- docker buildx create --name project-v3-builder
7376
docker buildx use project-v3-builder
7477
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} .
@@ -79,48 +82,35 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
7982
## Location to install dependencies to
8083
LOCALBIN ?= $(shell pwd)/bin
8184
$(LOCALBIN):
82-
mkdir -p $(LOCALBIN)
83-
84-
## Tool Binaries
85-
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
86-
CLIENT_GEN ?= $(LOCALBIN)/client-gen
87-
INFORMER_GEN ?= $(LOCALBIN)/informer-gen
88-
LISTER_GEN ?= $(LOCALBIN)/lister-gen
89-
SETUP_ENVTEST ?= $(LOCALBIN)/setup-envtest
90-
91-
## Tool Versions
92-
CONTROLLER_TOOLS_VERSION ?= {{ .controllerToolsVersion }}
93-
CODE_GENERATOR_VERSION ?= {{ .codeGeneratorVersion }}
94-
SETUP_ENVTEST_VERSION ?= latest
85+
@mkdir -p $(LOCALBIN)
9586

9687
.PHONY: controller-gen
97-
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
98-
$(CONTROLLER_GEN): $(LOCALBIN)
99-
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
100-
101-
.PHONY: client-gen
102-
client-gen: $(CLIENT_GEN) ## Download client-gen locally if necessary.
103-
$(CLIENT_GEN): $(LOCALBIN)
104-
test -s $(LOCALBIN)/client-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/client-gen@$(CODE_GENERATOR_VERSION)
105-
106-
.PHONY: informer-gen
107-
informer-gen: $(INFORMER_GEN) ## Download informer-gen locally if necessary.
108-
$(INFORMER_GEN): $(LOCALBIN)
109-
test -s $(LOCALBIN)/informer-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/informer-gen@$(CODE_GENERATOR_VERSION)
110-
111-
.PHONY: lister-gen
112-
lister-gen: $(LISTER_GEN) ## Download lister-gen locally if necessary.
113-
$(LISTER_GEN): $(LOCALBIN)
114-
test -s $(LOCALBIN)/lister-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/lister-gen@$(CODE_GENERATOR_VERSION)
88+
controller-gen: $(LOCALBIN) ## Install controller-gen
89+
@go mod download sigs.k8s.io/controller-tools && \
90+
VERSION=$$(go list -m -f '{{`{{`}}.Version{{`}}`}}' sigs.k8s.io/controller-tools) && \
91+
if [ ! -L $(LOCALBIN)/controller-gen ] || [ "$$(readlink $(LOCALBIN)/controller-gen)" != "controller-gen-$$VERSION" ]; then \
92+
echo "Installing controller-gen $$VERSION" && \
93+
rm -f $(LOCALBIN)/controller-gen && \
94+
GOBIN=$(LOCALBIN) go install $$(go list -m -f '{{`{{`}}.Dir{{`}}`}}' sigs.k8s.io/controller-tools)/cmd/controller-gen && \
95+
mv $(LOCALBIN)/controller-gen $(LOCALBIN)/controller-gen-$$VERSION && \
96+
ln -s controller-gen-$$VERSION $(LOCALBIN)/controller-gen; \
97+
fi
11598

11699
.PHONY: setup-envtest
117-
setup-envtest: $(SETUP_ENVTEST) ## Download setup-envtest locally if necessary.
118-
$(SETUP_ENVTEST): $(LOCALBIN)
119-
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@$(SETUP_ENVTEST_VERSION)
100+
setup-envtest: $(LOCALBIN) ## Install setup-envtest
101+
@go mod download sigs.k8s.io/controller-runtime/tools/setup-envtest && \
102+
VERSION=$$(go list -m -f '{{`{{`}}.Version{{`}}`}}' sigs.k8s.io/controller-runtime/tools/setup-envtest) && \
103+
if [ ! -L $(LOCALBIN)/setup-envtest ] || [ "$$(readlink $(LOCALBIN)/setup-envtest)" != "setup-envtest-$$VERSION" ]; then \
104+
echo "Installing setup-envtest $$VERSION" && \
105+
rm -f $(LOCALBIN)/setup-envtest && \
106+
GOBIN=$(LOCALBIN) go install $$(go list -m -f '{{`{{`}}.Dir{{`}}`}}' sigs.k8s.io/controller-runtime/tools/setup-envtest) && \
107+
mv $(LOCALBIN)/setup-envtest $(LOCALBIN)/setup-envtest-$$VERSION && \
108+
ln -s setup-envtest-$$VERSION $(LOCALBIN)/setup-envtest; \
109+
fi
120110

121111
.PHONY: envtest
122-
envtest: setup-envtest
123-
ENVTESTDIR=$$($(SETUP_ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path) ;\
124-
chmod -R u+w $$ENVTESTDIR ;\
125-
rm -f $(LOCALBIN)/k8s/current ;\
112+
envtest: setup-envtest ## Install envtest binaries
113+
@ENVTESTDIR=$$($(LOCALBIN)/setup-envtest use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path) && \
114+
chmod -R u+w $$ENVTESTDIR && \
115+
rm -f $(LOCALBIN)/k8s/current && \
126116
ln -s $$ENVTESTDIR $(LOCALBIN)/k8s/current

scaffold/templates/go.mod.tpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ replace github.com/sap/component-operator-runtime => {{ .version }}
88
{{- end }}
99

1010
require (
11-
{{ if or .validatingWebhookEnabled .mutatingWebhookEnabled }}
11+
{{- if or .validatingWebhookEnabled .mutatingWebhookEnabled }}
1212
github.com/sap/admission-webhook-runtime {{ .admissionWebhookRuntimeVersion }}
1313
{{- end }}
1414
{{- if contains "/" .version }}
@@ -19,6 +19,8 @@ require (
1919
k8s.io/apiextensions-apiserver {{ .kubernetesVersion }}
2020
k8s.io/apimachinery {{ .kubernetesVersion }}
2121
k8s.io/client-go {{ .kubernetesVersion }}
22+
k8s.io/code-generator {{ .codeGeneratorVersion }}
2223
k8s.io/kube-aggregator {{ .kubernetesVersion }}
2324
sigs.k8s.io/controller-runtime {{ .controllerRuntimeVersion }}
25+
sigs.k8s.io/controller-tools {{ .controllerToolsVersion }}
2426
)

scaffold/templates/hack/genclient.sh.tpl

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,53 @@
22

33
set -eo pipefail
44

5-
export GOROOT=$(go env GOROOT)
5+
BASEDIR=$(realpath "$(dirname "$0")"/..)
6+
7+
export GOBIN=$BASEDIR/bin
8+
mkdir -p "$GOBIN"
9+
10+
go mod download k8s.io/code-generator
11+
CODEGEN_DIR=$(go list -m -f '{{`{{`}}.Dir{{`}}`}}' k8s.io/code-generator)
12+
go install "$CODEGEN_DIR"/cmd/*
613

7-
BASEDIR=$(realpath $(dirname "$0")/..)
814
TEMPDIR=$BASEDIR/tmp/gen
915
trap 'rm -rf "$TEMPDIR"' EXIT
1016
mkdir -p "$TEMPDIR"
1117

1218
mkdir -p "$TEMPDIR"/apis/{{ .groupName }}
1319
ln -s "$BASEDIR"/api/{{ .groupVersion }} "$TEMPDIR"/apis/{{ .groupName }}/{{ .groupVersion }}
1420

15-
"$BASEDIR"/bin/client-gen \
21+
"$GOBIN"/client-gen \
1622
--clientset-name versioned \
17-
--input-base "" \
18-
--input {{ .goModule }}/tmp/gen/apis/{{ .groupName }}/{{ .groupVersion }} \
23+
--input-base "$TEMPDIR"/apis \
24+
--input {{ .groupName }}/{{ .groupVersion }} \
1925
--go-header-file "$BASEDIR"/hack/boilerplate.go.txt \
20-
--output-package {{ .goModule }}/pkg/client/clientset \
21-
--output-base "$TEMPDIR"/pkg/client \
26+
--output-pkg {{ .goModule }}/pkg/client/clientset \
27+
--output-dir "$TEMPDIR"/pkg/client/clientset \
2228
--plural-exceptions {{ .kind }}:{{ .resource }}
2329

24-
"$BASEDIR"/bin/lister-gen \
25-
--input-dirs {{ .goModule }}/tmp/gen/apis/{{ .groupName }}/{{ .groupVersion }} \
30+
"$GOBIN"/lister-gen \
2631
--go-header-file "$BASEDIR"/hack/boilerplate.go.txt \
27-
--output-package {{ .goModule }}/pkg/client/listers \
28-
--output-base "$TEMPDIR"/pkg/client \
29-
--plural-exceptions {{ .kind }}:{{ .resource }}
32+
--output-pkg {{ .goModule }}/pkg/client/listers \
33+
--output-dir "$TEMPDIR"/pkg/client/listers \
34+
--plural-exceptions {{ .kind }}:{{ .resource }} \
35+
{{ .goModule }}/tmp/gen/apis/{{ .groupName }}/{{ .groupVersion }}
3036

31-
"$BASEDIR"/bin/informer-gen \
32-
--input-dirs {{ .goModule }}/tmp/gen/apis/{{ .groupName }}/{{ .groupVersion }} \
37+
"$GOBIN"/informer-gen \
3338
--versioned-clientset-package {{ .goModule }}/pkg/client/clientset/versioned \
3439
--listers-package {{ .goModule }}/pkg/client/listers \
3540
--go-header-file "$BASEDIR"/hack/boilerplate.go.txt \
36-
--output-package {{ .goModule }}/pkg/client/informers \
37-
--output-base "$TEMPDIR"/pkg/client \
38-
--plural-exceptions {{ .kind }}:{{ .resource }}
41+
--output-pkg {{ .goModule }}/pkg/client/informers \
42+
--output-dir "$TEMPDIR"/pkg/client/informers \
43+
--plural-exceptions {{ .kind }}:{{ .resource }} \
44+
{{ .goModule }}/tmp/gen/apis/{{ .groupName }}/{{ .groupVersion }}
3945

4046
find "$TEMPDIR"/pkg/client -name "*.go" -exec \
4147
perl -pi -e "s#{{ .goModule | regexQuoteMeta }}/tmp/gen/apis/{{ .groupName | regexQuoteMeta }}/{{ .groupVersion | regexQuoteMeta }}#{{ .goModule }}/api/{{ .groupVersion }}#g" \
4248
{} +
4349

4450
rm -rf "$BASEDIR"/pkg/client
45-
mv "$TEMPDIR"/pkg/client/{{ .goModule }}/pkg/client "$BASEDIR"/pkg
51+
mv "$TEMPDIR"/pkg/client "$BASEDIR"/pkg
4652

4753
cd "$BASEDIR"
4854
go mod tidy
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//go:build tools
2+
// +build tools
3+
4+
/*
5+
{{- if .spdxLicenseHeaders }}
6+
SPDX-FileCopyrightText: {{ now.Year }} {{ .owner }}
7+
SPDX-License-Identifier: Apache-2.0
8+
{{- else }}
9+
Copyright {{ now.Year }} {{ .owner }}.
10+
11+
Licensed under the Apache License, Version 2.0 (the "License");
12+
you may not use this file except in compliance with the License.
13+
You may obtain a copy of the License at
14+
15+
http://www.apache.org/licenses/LICENSE-2.0
16+
17+
Unless required by applicable law or agreed to in writing, software
18+
distributed under the License is distributed on an "AS IS" BASIS,
19+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
See the License for the specific language governing permissions and
21+
limitations under the License.
22+
{{- end }}
23+
*/
24+
25+
package tools
26+
27+
import (
28+
_ "k8s.io/code-generator"
29+
_ "sigs.k8s.io/controller-runtime/tools/setup-envtest"
30+
_ "sigs.k8s.io/controller-tools/cmd/controller-gen"
31+
)

0 commit comments

Comments
 (0)