Skip to content

Commit

Permalink
Use codegen for type generation
Browse files Browse the repository at this point in the history
  • Loading branch information
afritzler committed Dec 2, 2024
1 parent dbff6f9 commit 305db20
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 115 deletions.
23 changes: 1 addition & 22 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,9 @@ check: $(GOIMPORTS) $(GOLANGCI_LINT) $(MOCKGEN)
@REPO_ROOT=$(REPO_ROOT) bash $(GARDENER_HACK_DIR)/check-charts.sh ./charts

.PHONY: generate
generate: deepcopy-gen defaulter-gen conversion-gen $(CONTROLLER_GEN) $(HELM) $(MOCKGEN) $(YQ) $(VGOPATH)
generate: $(CONTROLLER_GEN) $(HELM) $(MOCKGEN) $(YQ) $(VGOPATH)
@GOPATH=$(GOPATH) VGOPATH=$(VGOPATH) \
MOCKGEN=$(MOCKGEN) \
DEEPCOPY_GEN=$(DEEPCOPY_GEN) \
DEFAULTER_GEN=$(DEFAULTER_GEN) \
CONVERSION_GEN=$(CONVERSION_GEN) \
REPO_ROOT=$(REPO_ROOT) \
GARDENER_HACK_DIR=$(GARDENER_HACK_DIR) \
bash $(GARDENER_HACK_DIR)/generate-sequential.sh ./charts/... ./cmd/... ./example/... ./pkg/...
Expand Down Expand Up @@ -163,9 +160,6 @@ $(LOCALBIN):
mkdir -p $(LOCALBIN)

## Tool Binaries
DEEPCOPY_GEN ?= $(LOCALBIN)/deepcopy-gen
CONVERSION_GEN ?= $(LOCALBIN)/conversion-gen
DEFAULTER_GEN ?= $(LOCALBIN)/defaulter-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest

## Tool Versions
Expand All @@ -178,18 +172,3 @@ ENVTEST_K8S_VERSION = 1.31.0
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

.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: 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)
86 changes: 12 additions & 74 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
@@ -1,86 +1,24 @@
#!/usr/bin/env bash
#!/bin/bash

set -o errexit
set -o nounset
set -o pipefail

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

VGOPATH="$VGOPATH"
DEEPCOPY_GEN="$DEEPCOPY_GEN"
DEFAULTER_GEN="$DEFAULTER_GEN"
CONVERSION_GEN="$CONVERSION_GEN"

bold="$(tput bold)"
blue="$(tput setaf 4)"
normal="$(tput sgr0)"

function qualify-gvs() {
APIS_PKG="$1"
GROUPS_WITH_VERSIONS="$2"
join_char=""
res=""

for GVs in ${GROUPS_WITH_VERSIONS}; do
IFS=: read -r G Vs <<<"${GVs}"

for V in ${Vs//,/ }; do
res="$res$join_char$APIS_PKG/$G/$V"
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"
}

# setup virtual GOPATH
source "$GARDENER_HACK_DIR"/vgopath-setup.sh

# We need to explicitly pass GO111MODULE=off to k8s.io/code-generator as it is significantly slower otherwise,
# see https://github.com/kubernetes/code-generator/issues/100.
export GO111MODULE=off

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

echo "Generating ${blue}deepcopy${normal}"
"$DEEPCOPY_GEN" \
--go-header-file "$BASE_DIR/boilerplate.go.txt" \
--input-dirs "$(qualify-gvs "github.com/ironcore-dev/gardener-extension-provider-metal/pkg/apis" "config:v1alpha1 metal:v1alpha1")" \
-O zz_generated.deepcopy
CODE_GEN_DIR=$(go list -m -f '{{.Dir}}' k8s.io/code-generator)
source "${CODE_GEN_DIR}/kube_codegen.sh"

echo "${bold}Internal types${normal}"
rm -f $GOPATH/bin/*-gen

echo "Generating ${blue}deepcopy${normal}"
"$DEEPCOPY_GEN" \
--go-header-file "$BASE_DIR/boilerplate.go.txt" \
--input-dirs "$(qualify-gs "github.com/ironcore-dev/gardener-extension-provider-metal/pkg/apis" "config metal")" \
-O zz_generated.deepcopy
CURRENT_DIR=$(dirname $0)
PROJECT_ROOT="${CURRENT_DIR}"/..

echo "Generating ${blue}defaulter${normal}"
"$DEFAULTER_GEN" \
--go-header-file "$BASE_DIR/boilerplate.go.txt" \
--input-dirs "$(qualify-gvs "github.com/ironcore-dev/gardener-extension-provider-metal/pkg/apis" "config:v1alpha1 metal:v1alpha1")" \
-O zz_generated.defaults
kube::codegen::gen_helpers \
--boilerplate "${GARDENER_HACK_DIR}/LICENSE_BOILERPLATE.txt" \
"${PROJECT_ROOT}/pkg/apis/metal"

echo "Generating ${blue}conversion${normal}"
"$CONVERSION_GEN" \
--go-header-file "$BASE_DIR/boilerplate.go.txt" \
--input-dirs "$(qualify-gs "github.com/ironcore-dev/gardener-extension-provider-metal/pkg/apis" "config metal")" \
--input-dirs "$(qualify-gvs "github.com/ironcore-dev/gardener-extension-provider-metal/pkg/apis" "config:v1alpha1 metal:v1alpha1")" \
-O zz_generated.conversion
kube::codegen::gen_helpers \
--boilerplate "${GARDENER_HACK_DIR}/LICENSE_BOILERPLATE.txt" \
"${PROJECT_ROOT}/pkg/apis/config"
3 changes: 2 additions & 1 deletion pkg/apis/config/v1alpha1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/apis/config/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/apis/config/v1alpha1/zz_generated.defaults.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/apis/config/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/apis/metal/v1alpha1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/apis/metal/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 2 additions & 12 deletions pkg/apis/metal/v1alpha1/zz_generated.defaults.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/apis/metal/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 305db20

Please sign in to comment.