Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor code-gen scaffolding #148

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions scaffold/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ var templates embed.FS

// default verions
var (
goVersion = "1.21.7"
goVersion = "1.23.0"
version = "latest"
kubernetesVersion = "v0.29.2"
controllerRuntimeVersion = "v0.17.2"
controllerToolsVersion = "v0.14.0"
codeGeneratorVersion = "v0.29.0"
admissionWebhookRuntimeVersion = "v0.1.0"
envtestKubernetesVersion = "1.28.3"
kubernetesVersion = "v0.31.0"
controllerRuntimeVersion = "v0.19.0"
controllerToolsVersion = "v0.16.2"
codeGeneratorVersion = "v0.31.0"
admissionWebhookRuntimeVersion = "v0.1.41"
envtestKubernetesVersion = "1.30.3"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion scaffold/templates/Dockerfile.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM --platform=$BUILDPLATFORM golang:{{ .goVersion }} as builder
FROM --platform=$BUILDPLATFORM golang:{{ .goVersion }} AS builder
ARG TARGETOS
ARG TARGETARCH

Expand Down
70 changes: 26 additions & 44 deletions scaffold/templates/Makefile.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,65 @@ all: build
##@ General

.PHONY: help
help: ## Display this help.
@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)
help: ## Display this help
@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)

##@ Development

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

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
generate: generate-deepcopy # generate-client ## Generate required code pieces

.PHONY: generate-deepcopy
generate-deepcopy: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./api/..."

.PHONY: generate-client
generate-client: client-gen informer-gen lister-gen ## Generate typed client.
generate-client: ## Generate typed client
./hack/genclient.sh

.PHONY: fmt
fmt: ## Run go fmt against code.
fmt: ## Run go fmt against code
go fmt ./...

.PHONY: vet
vet: ## Run go vet against code.
vet: ## Run go vet against code
go vet ./...

##@ Testing

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

##@ Build

.PHONY: build
build: generate fmt vet ## Build manager binary.
build: generate-deepcopy fmt vet ## Build manager binary
go build -o bin/manager main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
run: manifests generate-deepcopy fmt vet ## Run a controller from your host
go run ./main.go

# Build docker image in current architecture and tag it as ${IMG}.
# Build docker image in current architecture and tag it as ${IMG}
.PHONY: docker-build
docker-build: ## Build docker image with the manager.
docker-build: ## Build docker image with the manager
docker build -t ${IMG} .

# Push docker image to the target specified in ${IMG}.
# Push docker image to the target specified in ${IMG}
.PHONY: docker-push
docker-push: ## Push docker image with the manager.
docker-push: ## Push docker image with the manager
docker push ${IMG}

# Build and push docker image for all given platforms.
# Build and push docker image for all given platforms
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
.PHONY: docker-buildx
docker-buildx: ## Build and push docker image for the manager for cross-platform support.
docker-buildx: ## Build and push docker image for the manager for cross-platform support
- docker buildx create --name project-v3-builder
docker buildx use project-v3-builder
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} .
Expand All @@ -83,43 +86,22 @@ $(LOCALBIN):

## Tool Binaries
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
CLIENT_GEN ?= $(LOCALBIN)/client-gen
INFORMER_GEN ?= $(LOCALBIN)/informer-gen
LISTER_GEN ?= $(LOCALBIN)/lister-gen
SETUP_ENVTEST ?= $(LOCALBIN)/setup-envtest

## Tool Versions
CONTROLLER_TOOLS_VERSION ?= {{ .controllerToolsVersion }}
CODE_GENERATOR_VERSION ?= {{ .codeGeneratorVersion }}
SETUP_ENVTEST_VERSION ?= latest

.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
controller-gen: $(CONTROLLER_GEN) ## Install controller-gen
$(CONTROLLER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)

.PHONY: client-gen
client-gen: $(CLIENT_GEN) ## Download client-gen locally if necessary.
$(CLIENT_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/client-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/client-gen@$(CODE_GENERATOR_VERSION)

.PHONY: informer-gen
informer-gen: $(INFORMER_GEN) ## Download informer-gen locally if necessary.
$(INFORMER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/informer-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/informer-gen@$(CODE_GENERATOR_VERSION)

.PHONY: lister-gen
lister-gen: $(LISTER_GEN) ## Download lister-gen locally if necessary.
$(LISTER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/lister-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/lister-gen@$(CODE_GENERATOR_VERSION)
go mod download sigs.k8s.io/controller-tools
GOBIN=$(LOCALBIN) go install $$(go list -m -f '{{`{{`}}.Dir{{`}}`}}' sigs.k8s.io/controller-tools)/cmd/controller-gen

.PHONY: setup-envtest
setup-envtest: $(SETUP_ENVTEST) ## Download setup-envtest locally if necessary.
setup-envtest: $(SETUP_ENVTEST) ## Install setup-envtest
$(SETUP_ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@$(SETUP_ENVTEST_VERSION)
go mod download sigs.k8s.io/controller-runtime
GOBIN=$(LOCALBIN) go install $$(go list -m -f '{{`{{`}}.Dir{{`}}`}}' sigs.k8s.io/controller-runtime/tools/setup-envtest)

.PHONY: envtest
envtest: setup-envtest
envtest: setup-envtest ## Install envtest binaries
ENVTESTDIR=$$($(SETUP_ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path) ;\
chmod -R u+w $$ENVTESTDIR ;\
rm -f $(LOCALBIN)/k8s/current ;\
Expand Down
4 changes: 3 additions & 1 deletion scaffold/templates/go.mod.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ replace github.com/sap/component-operator-runtime => {{ .version }}
{{- end }}

require (
{{ if or .validatingWebhookEnabled .mutatingWebhookEnabled }}
{{- if or .validatingWebhookEnabled .mutatingWebhookEnabled }}
github.com/sap/admission-webhook-runtime {{ .admissionWebhookRuntimeVersion }}
{{- end }}
{{- if contains "/" .version }}
Expand All @@ -19,6 +19,8 @@ require (
k8s.io/apiextensions-apiserver {{ .kubernetesVersion }}
k8s.io/apimachinery {{ .kubernetesVersion }}
k8s.io/client-go {{ .kubernetesVersion }}
k8s.io/code-generator {{ .codeGeneratorVersion }}
k8s.io/kube-aggregator {{ .kubernetesVersion }}
sigs.k8s.io/controller-runtime {{ .controllerRuntimeVersion }}
sigs.k8s.io/controller-tools {{ .controllerToolsVersion }}
)
42 changes: 24 additions & 18 deletions scaffold/templates/hack/genclient.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,53 @@

set -eo pipefail

export GOROOT=$(go env GOROOT)
BASEDIR=$(realpath "$(dirname "$0")"/..)

export GOBIN=$BASEDIR/bin
mkdir -p "$GOBIN"

go mod download k8s.io/code-generator
CODEGEN_DIR=$(go list -m -f '{{`{{`}}.Dir{{`}}`}}' k8s.io/code-generator)
go install "$CODEGEN_DIR"/cmd/*

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

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

"$BASEDIR"/bin/client-gen \
"$GOBIN"/client-gen \
--clientset-name versioned \
--input-base "" \
--input {{ .goModule }}/tmp/gen/apis/{{ .groupName }}/{{ .groupVersion }} \
--input-base "$TEMPDIR"/apis \
--input {{ .groupName }}/{{ .groupVersion }} \
--go-header-file "$BASEDIR"/hack/boilerplate.go.txt \
--output-package {{ .goModule }}/pkg/client/clientset \
--output-base "$TEMPDIR"/pkg/client \
--output-pkg {{ .goModule }}/pkg/client/clientset \
--output-dir "$TEMPDIR"/pkg/client/clientset \
--plural-exceptions {{ .kind }}:{{ .resource }}

"$BASEDIR"/bin/lister-gen \
--input-dirs {{ .goModule }}/tmp/gen/apis/{{ .groupName }}/{{ .groupVersion }} \
"$GOBIN"/lister-gen \
--go-header-file "$BASEDIR"/hack/boilerplate.go.txt \
--output-package {{ .goModule }}/pkg/client/listers \
--output-base "$TEMPDIR"/pkg/client \
--plural-exceptions {{ .kind }}:{{ .resource }}
--output-pkg {{ .goModule }}/pkg/client/listers \
--output-dir "$TEMPDIR"/pkg/client/listers \
--plural-exceptions {{ .kind }}:{{ .resource }} \
{{ .goModule }}/tmp/gen/apis/{{ .groupName }}/{{ .groupVersion }}

"$BASEDIR"/bin/informer-gen \
--input-dirs {{ .goModule }}/tmp/gen/apis/{{ .groupName }}/{{ .groupVersion }} \
"$GOBIN"/informer-gen \
--versioned-clientset-package {{ .goModule }}/pkg/client/clientset/versioned \
--listers-package {{ .goModule }}/pkg/client/listers \
--go-header-file "$BASEDIR"/hack/boilerplate.go.txt \
--output-package {{ .goModule }}/pkg/client/informers \
--output-base "$TEMPDIR"/pkg/client \
--plural-exceptions {{ .kind }}:{{ .resource }}
--output-pkg {{ .goModule }}/pkg/client/informers \
--output-dir "$TEMPDIR"/pkg/client/informers \
--plural-exceptions {{ .kind }}:{{ .resource }} \
{{ .goModule }}/tmp/gen/apis/{{ .groupName }}/{{ .groupVersion }}

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

rm -rf "$BASEDIR"/pkg/client
mv "$TEMPDIR"/pkg/client/{{ .goModule }}/pkg/client "$BASEDIR"/pkg
mv "$TEMPDIR"/pkg/client "$BASEDIR"/pkg

cd "$BASEDIR"
go mod tidy
Expand Down
31 changes: 31 additions & 0 deletions scaffold/templates/hack/tools.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//go:build tools
// +build tools

/*
{{- if .spdxLicenseHeaders }}
SPDX-FileCopyrightText: {{ now.Year }} {{ .owner }}
SPDX-License-Identifier: Apache-2.0
{{- else }}
Copyright {{ now.Year }} {{ .owner }}.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
{{- end }}
*/

package tools

import (
_ "k8s.io/code-generator"
_ "sigs.k8s.io/controller-runtime/tools/setup-envtest"
_ "sigs.k8s.io/controller-tools/cmd/controller-gen"
)
Loading