From 56364aad8a17a58fd36e892f7d7c1182af06a52f Mon Sep 17 00:00:00 2001 From: Lennart Jern Date: Wed, 1 Oct 2025 11:07:46 +0000 Subject: [PATCH] Refactor generate-codegen This commit changes how to run and build codegen. Instead of using a script inside the openapi-gen package, we now build and call the binary directly. This avoids issues where the upstream package is incompatible with the go version we use (as seen currently on release-0.12). Signed-off-by: Lennart Jern --- Makefile | 65 +++++++++++++++++++++++++++++++++-- hack/tools/Makefile | 20 +++++++++++ hack/update-codegen.sh | 77 ------------------------------------------ 3 files changed, 83 insertions(+), 79 deletions(-) delete mode 100755 hack/update-codegen.sh diff --git a/Makefile b/Makefile index a4fe820341..edc6bc8b33 100644 --- a/Makefile +++ b/Makefile @@ -68,6 +68,11 @@ GOLANGCI_LINT := $(TOOLS_BIN_DIR)/golangci-lint GOTESTSUM := $(TOOLS_BIN_DIR)/gotestsum KUSTOMIZE := $(TOOLS_BIN_DIR)/kustomize MOCKGEN := $(TOOLS_BIN_DIR)/mockgen +OPENAPI_GEN := $(TOOLS_BIN_DIR)/openapi-gen +APPLYCONFIGURATION_GEN := $(TOOLS_BIN_DIR)/applyconfiguration-gen +CLIENT_GEN := $(TOOLS_BIN_DIR)/client-gen +LISTER_GEN := $(TOOLS_BIN_DIR)/lister-gen +INFORMER_GEN := $(TOOLS_BIN_DIR)/informer-gen RELEASE_NOTES := $(TOOLS_BIN_DIR)/release-notes SETUP_ENVTEST := $(TOOLS_BIN_DIR)/setup-envtest GEN_CRD_API_REFERENCE_DOCS := $(TOOLS_BIN_DIR)/gen-crd-api-reference-docs @@ -305,8 +310,64 @@ generate-controller-gen: $(CONTROLLER_GEN) object:headerFile=./hack/boilerplate/boilerplate.generatego.txt .PHONY: generate-codegen -generate-codegen: generate-controller-gen - ./hack/update-codegen.sh +generate-codegen: generate-controller-gen $(OPENAPI_GEN) $(APPLYCONFIGURATION_GEN) $(CLIENT_GEN) $(LISTER_GEN) $(INFORMER_GEN) + @echo "** Generating OpenAPI definitions **" + # The package list includes: + # - CAPO's own API packages (v1alpha1, v1beta1) that have // +k8s:openapi-gen= markers + # - Dependency packages from CAPI and k8s.io that are referenced by CAPO's APIs + # - Base k8s.io/apimachinery packages + $(OPENAPI_GEN) \ + --go-header-file=./hack/boilerplate.go.txt \ + --output-file=zz_generated.openapi.go \ + --output-dir=./cmd/models-schema \ + --output-pkg=main \ + --report-filename=./api_violations.report \ + sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha1 \ + sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1 \ + sigs.k8s.io/cluster-api/api/core/v1beta2 \ + sigs.k8s.io/cluster-api/api/ipam/v1beta2 \ + sigs.k8s.io/cluster-api/api/core/v1beta1 \ + sigs.k8s.io/cluster-api/api/ipam/v1beta1 \ + k8s.io/api/core/v1 \ + k8s.io/apimachinery/pkg/apis/meta/v1 \ + k8s.io/apimachinery/pkg/runtime \ + k8s.io/apimachinery/pkg/version + @echo "** Generating openapi.json **" + go run ./cmd/models-schema | jq > ./openapi.json + @echo "** Generating applyconfiguration code **" + $(APPLYCONFIGURATION_GEN) \ + --go-header-file=./hack/boilerplate.go.txt \ + --output-dir=./pkg/generated/applyconfiguration \ + --output-pkg=sigs.k8s.io/cluster-api-provider-openstack/pkg/generated/applyconfiguration \ + --openapi-schema=./openapi.json \ + sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha1 \ + sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1 + @echo "** Generating clientset code **" + $(CLIENT_GEN) \ + --go-header-file=./hack/boilerplate.go.txt \ + --output-dir=./pkg/generated/clientset \ + --output-pkg=sigs.k8s.io/cluster-api-provider-openstack/pkg/generated/clientset \ + --clientset-name=clientset \ + --input-base=sigs.k8s.io/cluster-api-provider-openstack \ + --apply-configuration-package=sigs.k8s.io/cluster-api-provider-openstack/pkg/generated/applyconfiguration \ + --input=api/v1alpha1 \ + --input=api/v1beta1 + @echo "** Generating lister code **" + $(LISTER_GEN) \ + --go-header-file=./hack/boilerplate.go.txt \ + --output-dir=./pkg/generated/listers \ + --output-pkg=sigs.k8s.io/cluster-api-provider-openstack/pkg/generated/listers \ + sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha1 \ + sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1 + @echo "** Generating informer code **" + $(INFORMER_GEN) \ + --go-header-file=./hack/boilerplate.go.txt \ + --output-dir=./pkg/generated/informers \ + --output-pkg=sigs.k8s.io/cluster-api-provider-openstack/pkg/generated/informers \ + --versioned-clientset-package=sigs.k8s.io/cluster-api-provider-openstack/pkg/generated/clientset/clientset \ + --listers-package=sigs.k8s.io/cluster-api-provider-openstack/pkg/generated/listers \ + sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha1 \ + sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1 .PHONY: generate-manifests generate-manifests: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc. diff --git a/hack/tools/Makefile b/hack/tools/Makefile index 54b9c50b34..39ce22128d 100644 --- a/hack/tools/Makefile +++ b/hack/tools/Makefile @@ -71,6 +71,26 @@ CONVERSION_GEN := $(BIN_DIR)/conversion-gen $(CONVERSION_GEN): go.mod go.sum | $(BIN_DIR) go build -tags=tools -o $@ k8s.io/code-generator/cmd/conversion-gen +OPENAPI_GEN := $(BIN_DIR)/openapi-gen +$(OPENAPI_GEN): go.mod go.sum | $(BIN_DIR) + go build -tags=tools -o $@ k8s.io/kube-openapi/cmd/openapi-gen + +APPLYCONFIGURATION_GEN := $(BIN_DIR)/applyconfiguration-gen +$(APPLYCONFIGURATION_GEN): go.mod go.sum | $(BIN_DIR) + go build -tags=tools -o $@ k8s.io/code-generator/cmd/applyconfiguration-gen + +CLIENT_GEN := $(BIN_DIR)/client-gen +$(CLIENT_GEN): go.mod go.sum | $(BIN_DIR) + go build -tags=tools -o $@ k8s.io/code-generator/cmd/client-gen + +LISTER_GEN := $(BIN_DIR)/lister-gen +$(LISTER_GEN): go.mod go.sum | $(BIN_DIR) + go build -tags=tools -o $@ k8s.io/code-generator/cmd/lister-gen + +INFORMER_GEN := $(BIN_DIR)/informer-gen +$(INFORMER_GEN): go.mod go.sum | $(BIN_DIR) + go build -tags=tools -o $@ k8s.io/code-generator/cmd/informer-gen + ENVSUBST := $(BIN_DIR)/envsubst $(ENVSUBST): go.mod go.sum | $(BIN_DIR) # Build envsubst from tools folder. go build -tags=tools -o $@ github.com/a8m/envsubst/cmd/envsubst diff --git a/hack/update-codegen.sh b/hack/update-codegen.sh deleted file mode 100755 index f7161d819f..0000000000 --- a/hack/update-codegen.sh +++ /dev/null @@ -1,77 +0,0 @@ -#!/usr/bin/env bash - -# Copyright 2024 The Kubernetes Authors. -# -# 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. - -set -euo pipefail -set -x - -SCRIPT_ROOT=$(realpath $(dirname "${BASH_SOURCE[0]}")) -PROJECT_ROOT=$(realpath "${SCRIPT_ROOT}/..") - -GENERATED_PKG="pkg/generated" - -# Ensure tools built by kube_codegen go in our own GOBIN rather than something -# shared under GOPATH. This guards against these tools being rebuilt by some -# other concurrent invocation, potentially with a different version. -export GOBIN="${PROJECT_ROOT}/bin" - -cd "$PROJECT_ROOT" - -# For this to work, the current working directory must be under a Go module which -# lists k8s.io/code-generator -CODEGEN_PKG=$(go list -f '{{ .Dir }}' k8s.io/code-generator) - -source "${CODEGEN_PKG}/kube_codegen.sh" - -# Deep-copies and what-not are generated by controller-gen, so we don't need to use kube::codegen::gen_helpers - -declare -a gen_openapi_args=( - --report-filename "${PROJECT_ROOT}/api_violations.report" - --output-dir "${PROJECT_ROOT}/cmd/models-schema" - --output-pkg main - --boilerplate "${SCRIPT_ROOT}/boilerplate.go.txt" - - # We need to include all referenced types in our generated openapi schema - # or applyconfiguration-gen won't be able to use it. Helpfully it will - # generate an error including the missing type. - --extra-pkgs sigs.k8s.io/cluster-api/api/core/v1beta2 - --extra-pkgs sigs.k8s.io/cluster-api/api/ipam/v1beta2 - --extra-pkgs sigs.k8s.io/cluster-api/api/core/v1beta1 - --extra-pkgs sigs.k8s.io/cluster-api/api/ipam/v1beta1 - --extra-pkgs k8s.io/api/core/v1 -) - -# It is an error to make a change which updates the api violations. When doing -# this intentionally, for example when fixing violations, run with -# UPDATE_API_KNOWN_VIOLATIONS=true to update the api violations report. -if [ ! -z "${UPDATE_API_KNOWN_VIOLATIONS:-}" ]; then - gen_openapi_args+=(--update-report) -fi - -kube::codegen::gen_openapi "${gen_openapi_args[@]}" "${PROJECT_ROOT}/api" - -openapi="${PROJECT_ROOT}/openapi.json" -go run "${PROJECT_ROOT}/cmd/models-schema" | jq > "$openapi" - -kube::codegen::gen_client \ - --with-applyconfig \ - --with-watch \ - --applyconfig-openapi-schema "$openapi" \ - --output-dir "${PROJECT_ROOT}/${GENERATED_PKG}" \ - --output-pkg sigs.k8s.io/cluster-api-provider-openstack/${GENERATED_PKG} \ - --versioned-name clientset \ - --boilerplate "${SCRIPT_ROOT}/boilerplate.go.txt" \ - --one-input-api "api" \ - "${PROJECT_ROOT}"