-
Notifications
You must be signed in to change notification settings - Fork 25
/
generate.sh
58 lines (48 loc) · 2.17 KB
/
generate.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
PACKAGE="github.com/kubecost/cluster-turndown/v2"
API="turndownschedule"
VERSION="v1alpha1"
# Vendor so we have access to the generate-groups.sh script from the desired
# version of k8s.io/code-generator. If vendoring breaks because code is
# referencing generated code that has not yet been generated, refer to the
# old version of this script (commit 5c5e172) which did a little trick to be
# able to vendor if code was not generated.
go mod vendor
SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")
CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${SCRIPT_ROOT}"; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo code-generator)}
bash "${CODEGEN_PKG}/generate-groups.sh" all \
${PACKAGE}/pkg/generated \
${PACKAGE}/pkg/apis \
${API}:${VERSION} \
--go-header-file ./hack/custom-boilerplate.go.txt \
--output-base ${SCRIPT_ROOT}
# generate-groups.sh creates files at $PACKAGE/pkg/... because of the args
# passed in the above command. These files have to be moved to the correct
# place afterward. Unfortunately, if we try to create the files in the right
# location directly, like with the following:
# bash "${CODEGEN_PKG}/generate-groups.sh" all \
# ./pkg/generated \
# ./pkg/apis \
# ${API}:${VERSION} \
# --go-header-file ./hack/custom-boilerplate.go.txt \
# --output-base ${SCRIPT_ROOT}
#
# The import statements in the generated code will be incorrect and cause
# import errors like this:
# pkg/generated/informers/externalversions/generic.go:9:2: local import "./pkg/apis/turndownschedule/v1alpha1" in non-local package
#
# So we have to generate them in ./github.com/kubecost/cluster-turndown/v2/pkg
# and then move them to ./pkg. Frustrating, but it does work. I believe
# this is because the code gen was designed well before go modules and this
# is supposed to work in the GOPATH world.
# Remove old generated code first
rm -r ./pkg/generated
# Then move new generated code to the right place
mv ./${PACKAGE}/pkg/generated ./pkg/generated
mv ./${PACKAGE}/pkg/apis/${API}/${VERSION}/* ./pkg/apis/${API}/${VERSION}
# Clean up temp directories created for/by code gen
rm -r ./vendor
rm -r ./github.com