Skip to content

Commit

Permalink
Adapt code generation script for k8s v0.30 using kube_codegen.sh
Browse files Browse the repository at this point in the history
…functions and update `Makefile`
  • Loading branch information
sujeet01 committed Nov 27, 2024
1 parent c6e5eff commit 76de044
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 149 deletions.
57 changes: 4 additions & 53 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ METALNETLET_IMG ?= metalnetlet:latest
KIND_CLUSTER_NAME ?= kind

# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.29.0
ENVTEST_K8S_VERSION = 1.30.3

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand Down Expand Up @@ -64,17 +64,10 @@ manifests: controller-gen ## Generate rbac objects.
./hack/promote-let-role.sh config/metalnetlet/apinet-rbac/role.yaml config/apiserver/rbac/metalnetlet_role.yaml apinet.ironcore.dev:system:metalnetlets

.PHONY: generate
generate: vgopath models-schema deepcopy-gen client-gen lister-gen informer-gen defaulter-gen conversion-gen openapi-gen applyconfiguration-gen
generate: vgopath models-schema openapi-gen
VGOPATH=$(VGOPATH) \
MODELS_SCHEMA=$(MODELS_SCHEMA) \
DEEPCOPY_GEN=$(DEEPCOPY_GEN) \
CLIENT_GEN=$(CLIENT_GEN) \
LISTER_GEN=$(LISTER_GEN) \
INFORMER_GEN=$(INFORMER_GEN) \
DEFAULTER_GEN=$(DEFAULTER_GEN) \
CONVERSION_GEN=$(CONVERSION_GEN) \
OPENAPI_GEN=$(OPENAPI_GEN) \
APPLYCONFIGURATION_GEN=$(APPLYCONFIGURATION_GEN) \
./hack/update-codegen.sh

.PHONY: add-license
Expand Down Expand Up @@ -330,14 +323,7 @@ KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
OPENAPI_EXTRACTOR ?= $(LOCALBIN)/openapi-extractor
DEEPCOPY_GEN ?= $(LOCALBIN)/deepcopy-gen
CLIENT_GEN ?= $(LOCALBIN)/client-gen
LISTER_GEN ?= $(LOCALBIN)/lister-gen
INFORMER_GEN ?= $(LOCALBIN)/informer-gen
DEFAULTER_GEN ?= $(LOCALBIN)/defaulter-gen
CONVERSION_GEN ?= $(LOCALBIN)/conversion-gen
OPENAPI_GEN ?= $(LOCALBIN)/openapi-gen
APPLYCONFIGURATION_GEN ?= $(LOCALBIN)/applyconfiguration-gen
VGOPATH ?= $(LOCALBIN)/vgopath
GEN_CRD_API_REFERENCE_DOCS ?= $(LOCALBIN)/gen-crd-api-reference-docs
ADDLICENSE ?= $(LOCALBIN)/addlicense
Expand All @@ -347,9 +333,8 @@ GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint

## Tool Versions
KUSTOMIZE_VERSION ?= v5.1.1
CODE_GENERATOR_VERSION ?= v0.29.0
VGOPATH_VERSION ?= v0.1.5
CONTROLLER_TOOLS_VERSION ?= v0.14.0
CONTROLLER_TOOLS_VERSION ?= v0.15.0
GEN_CRD_API_REFERENCE_DOCS_VERSION ?= v0.3.0
ADDLICENSE_VERSION ?= v1.1.1
GOIMPORTS_VERSION ?= v0.25.0
Expand All @@ -372,45 +357,11 @@ $(CONTROLLER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)

.PHONY: deepcopy-gen
deepcopy-gen: $(DEEPCOPY_GEN) ## Download deepcopy-gen locally if necessary.
$(DEEPCOPY_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/deepcopy-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/deepcopy-gen@$(CODE_GENERATOR_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: 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)

.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: defaulter-gen
defaulter-gen: $(DEFAULTER_GEN) ## Download defaulter-gen locally if necessary.
$(DEFAULTER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/defaulter-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/defaulter-gen@$(CODE_GENERATOR_VERSION)

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

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

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

.PHONY: vgopath
vgopath: $(VGOPATH) ## Download vgopath locally if necessary.
Expand Down
146 changes: 50 additions & 96 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -o nounset
set -o pipefail

SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
PROJECT_ROOT="$SCRIPT_DIR/.."
export TERM="xterm-256color"

bold="$(tput bold)"
Expand All @@ -22,133 +23,86 @@ function qualify-gvs() {

for V in ${Vs//,/ }; do
res="$res$join_char$APIS_PKG/$G/$V"
join_char=","
join_char=" "
done
done

echo "$res"
}

function qualify-gs() {
APIS_PKG="$1"
unset GROUPS
IFS=' ' read -ra GROUPS <<< "$2"
join_char=""
res=""

for G in "${GROUPS[@]}"; do
res="$res$join_char$APIS_PKG/$G"
join_char=","
done

echo "$res"
}

VGOPATH="$VGOPATH"
MODELS_SCHEMA="$MODELS_SCHEMA"
CLIENT_GEN="$CLIENT_GEN"
DEEPCOPY_GEN="$DEEPCOPY_GEN"
LISTER_GEN="$LISTER_GEN"
INFORMER_GEN="$INFORMER_GEN"
DEFAULTER_GEN="$DEFAULTER_GEN"
CONVERSION_GEN="$CONVERSION_GEN"
OPENAPI_GEN="$OPENAPI_GEN"
APPLYCONFIGURATION_GEN="$APPLYCONFIGURATION_GEN"

VIRTUAL_GOPATH="$(mktemp -d)"
trap 'rm -rf "$GOPATH"' EXIT

# Setup virtual GOPATH so the codegen tools work as expected.
(cd "$SCRIPT_DIR/.."; go mod download && "$VGOPATH" -o "$VIRTUAL_GOPATH")
(cd "$PROJECT_ROOT"; go mod download && "$VGOPATH" -o "$VIRTUAL_GOPATH")

export GOROOT="${GOROOT:-"$(go env GOROOT)"}"
export GOPATH="$VIRTUAL_GOPATH"
export GO111MODULE=off

CODE_GEN_DIR=$(go list -m -f '{{.Dir}}' k8s.io/code-generator)
source "${CODE_GEN_DIR}/kube_codegen.sh"

IRONCORE_NET_ROOT="github.com/ironcore-dev/ironcore-net"
ALL_GROUPS="core"
ALL_VERSION_GROUPS="core:v1alpha1"

echo "${bold}Public types${normal}"

echo "Generating ${blue}deepcopy${normal}"
"$DEEPCOPY_GEN" \
--output-base "$GOPATH/src" \
--go-header-file "$SCRIPT_DIR/boilerplate.go.txt" \
--input-dirs "$(qualify-gvs "$IRONCORE_NET_ROOT/api" "$ALL_VERSION_GROUPS")" \
-O zz_generated.deepcopy
echo "Generating ${blue}deepcopy, defaulter and conversion${normal}"
kube::codegen::gen_helpers \
--boilerplate "$SCRIPT_DIR/boilerplate.go.txt" \
"$PROJECT_ROOT/api"

echo "Generating ${blue}openapi${normal}"
input_dirs=($(qualify-gvs "${IRONCORE_NET_ROOT}/api" "$ALL_VERSION_GROUPS"))
input_dirs+=("${IRONCORE_NET_ROOT}/apimachinery/api/net")
"$OPENAPI_GEN" \
--output-base "$GOPATH/src" \
--output-dir "$PROJECT_ROOT/client-go/openapi" \
--output-pkg "${IRONCORE_NET_ROOT}/client-go/openapi" \
--output-file "zz_generated.openapi.go" \
--report-filename "$PROJECT_ROOT/client-go/openapi/api_violations.report" \
--go-header-file "$SCRIPT_DIR/boilerplate.go.txt" \
--input-dirs "$(qualify-gvs "$IRONCORE_NET_ROOT/api" "$ALL_VERSION_GROUPS")" \
--input-dirs "$IRONCORE_NET_ROOT/apimachinery/api/net" \
--input-dirs "k8s.io/apimachinery/pkg/apis/meta/v1,k8s.io/apimachinery/pkg/runtime,k8s.io/apimachinery/pkg/version" \
--input-dirs "k8s.io/api/core/v1" \
--input-dirs "k8s.io/apimachinery/pkg/api/resource" \
--output-package "$IRONCORE_NET_ROOT/client-go/openapi" \
-O zz_generated.openapi \
--report-filename "$SCRIPT_DIR/../client-go/openapi/api_violations.report"

echo "Generating ${blue}applyconfiguration${normal}"
applyconfigurationgen_external_apis+=("k8s.io/apimachinery/pkg/apis/meta/v1")
applyconfigurationgen_external_apis+=("$(qualify-gvs "$IRONCORE_NET_ROOT/api" "$ALL_VERSION_GROUPS")")
"k8s.io/apimachinery/pkg/apis/meta/v1" \
"k8s.io/apimachinery/pkg/runtime" \
"k8s.io/apimachinery/pkg/version" \
"k8s.io/api/core/v1" \
"k8s.io/apimachinery/pkg/api/resource" \
"${input_dirs[@]}"

echo "Generating ${blue}client, lister, informer and applyconfiguration${normal}"
applyconfigurationgen_external_apis=()
for GV in ${ALL_VERSION_GROUPS}; do
IFS=: read -r G V <<<"${GV}"
applyconfigurationgen_external_apis+=("${IRONCORE_NET_ROOT}/api/${G}/${V}:${IRONCORE_NET_ROOT}/client-go/applyconfigurations/${G}/${V}")
done
applyconfigurationgen_external_apis_csv=$(IFS=,; echo "${applyconfigurationgen_external_apis[*]}")
"$APPLYCONFIGURATION_GEN" \
--output-base "$GOPATH/src" \
--go-header-file "$SCRIPT_DIR/boilerplate.go.txt" \
--input-dirs "${applyconfigurationgen_external_apis_csv}" \
--openapi-schema <("$MODELS_SCHEMA" --openapi-package "$IRONCORE_NET_ROOT/client-go/openapi" --openapi-title "ironcore-net") \
--output-package "$IRONCORE_NET_ROOT/client-go/applyconfigurations"

echo "Generating ${blue}client${normal}"
"$CLIENT_GEN" \
--output-base "$GOPATH/src" \
--go-header-file "$SCRIPT_DIR/boilerplate.go.txt" \
--input "$(qualify-gvs "$IRONCORE_NET_ROOT/api" "$ALL_VERSION_GROUPS")" \
--output-package "$IRONCORE_NET_ROOT/client-go" \
--apply-configuration-package "$IRONCORE_NET_ROOT/client-go/applyconfigurations" \
--clientset-name "ironcorenet" \
--input-base ""

echo "Generating ${blue}lister${normal}"
"$LISTER_GEN" \
--output-base "$GOPATH/src" \
--go-header-file "$SCRIPT_DIR/boilerplate.go.txt" \
--input-dirs "$(qualify-gvs "$IRONCORE_NET_ROOT/api" "$ALL_VERSION_GROUPS")" \
--output-package "$IRONCORE_NET_ROOT/client-go/listers"
# Do not rely on process substitution / GNU bash
tmp_schema_file=$(mktemp)
trap 'rm -f "$tmp_schema_file"' EXIT
"$MODELS_SCHEMA" --openapi-package "${IRONCORE_NET_ROOT}/client-go/openapi" --openapi-title "ironcore-net" > "$tmp_schema_file"

echo "Generating ${blue}informer${normal}"
"$INFORMER_GEN" \
--output-base "$GOPATH/src" \
--go-header-file "$SCRIPT_DIR/boilerplate.go.txt" \
--input-dirs "$(qualify-gvs "$IRONCORE_NET_ROOT/api" "$ALL_VERSION_GROUPS")" \
--versioned-clientset-package "$IRONCORE_NET_ROOT/client-go/ironcorenet" \
--listers-package "$IRONCORE_NET_ROOT/client-go/listers" \
--output-package "$IRONCORE_NET_ROOT/client-go/informers" \
--single-directory
kube::codegen::gen_client \
--with-applyconfig \
--applyconfig-name "applyconfigurations" \
--applyconfig-externals "${applyconfigurationgen_external_apis_csv}" \
--applyconfig-openapi-schema <("$MODELS_SCHEMA" --openapi-package "${IRONCORE_NET_ROOT}/client-go/openapi" --openapi-title "ironcore-net") \
--clientset-name "ironcorenet" \
--listers-name "listers" \
--informers-name "informers" \
--with-watch \
--output-dir "$PROJECT_ROOT/client-go" \
--output-pkg "${IRONCORE_NET_ROOT}/client-go" \
--boilerplate "$SCRIPT_DIR/boilerplate.go.txt" \
"$PROJECT_ROOT/api"

echo "${bold}Internal types${normal}"

echo "Generating ${blue}deepcopy${normal}"
"$DEEPCOPY_GEN" \
--output-base "$GOPATH/src" \
--go-header-file "$SCRIPT_DIR/boilerplate.go.txt" \
--input-dirs "$(qualify-gs "$IRONCORE_NET_ROOT/internal/apis" "$ALL_GROUPS")" \
-O zz_generated.deepcopy

echo "Generating ${blue}defaulter${normal}"
"$DEFAULTER_GEN" \
--output-base "$GOPATH/src" \
--go-header-file "$SCRIPT_DIR/boilerplate.go.txt" \
--input-dirs "$(qualify-gvs "$IRONCORE_NET_ROOT/internal/apis" "$ALL_VERSION_GROUPS")" \
-O zz_generated.defaults

echo "Generating ${blue}conversion${normal}"
"$CONVERSION_GEN" \
--output-base "$GOPATH/src" \
--go-header-file "$SCRIPT_DIR/boilerplate.go.txt" \
--input-dirs "$(qualify-gs "$IRONCORE_NET_ROOT/internal/apis" "$ALL_GROUPS")" \
--input-dirs "$(qualify-gvs "$IRONCORE_NET_ROOT/internal/apis" "$ALL_VERSION_GROUPS")" \
-O zz_generated.conversion
echo "Generating ${blue}deepcopy, defaulter and conversion${normal}"
kube::codegen::gen_helpers \
--boilerplate "$SCRIPT_DIR/boilerplate.go.txt" \
"$PROJECT_ROOT/internal/apis"

0 comments on commit 76de044

Please sign in to comment.