Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 3931dd1

Browse files
authored
Merge pull request #42 from packethost/update-controller-gen
ensure controller-gen exists and is minimum right version
2 parents 7f9b54d + 7858ace commit 3931dd1

File tree

2 files changed

+89
-14
lines changed

2 files changed

+89
-14
lines changed

Makefile

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
KUBEBUILDER_VERSION ?= 2.3.1
44
KUBEBUILDER ?= /usr/local/kubebuilder/bin/kubebuilder
5+
CONTROLLER_GEN_VERSION ?= v0.3.0
6+
CONTROLLER_GEN=$(GOBIN)/controller-gen
57

68
CERTMANAGER_URL ?= https://github.com/jetstack/cert-manager/releases/download/v0.14.1/cert-manager.yaml
79

@@ -179,20 +181,11 @@ push:
179181

180182
# find or download controller-gen
181183
# download controller-gen if necessary
182-
controller-gen:
183-
ifeq (, $(shell which controller-gen))
184-
@{ \
185-
set -e ;\
186-
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
187-
cd $$CONTROLLER_GEN_TMP_DIR ;\
188-
go mod init tmp ;\
189-
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.5 ;\
190-
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
191-
}
192-
CONTROLLER_GEN=$(GOBIN)/controller-gen
193-
else
194-
CONTROLLER_GEN=$(shell which controller-gen)
195-
endif
184+
# version must be at least the given version
185+
.PHONY: $(CONTROLLER_GEN)
186+
controller-gen: $(CONTROLLER_GEN)
187+
$(CONTROLLER_GEN):
188+
scripts/controller-gen.sh $@ $(CONTROLLER_GEN_VERSION)
196189

197190
examples:
198191
./generate-examples.sh

scripts/controller-gen.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
# ensure controller-gen is at least the right version
6+
7+
8+
download() {
9+
local version=$1
10+
local P=$(pwd)
11+
local TMP_DIR=$(mktemp -d)
12+
cd $TMP_DIR
13+
go mod init tmp
14+
go get -u sigs.k8s.io/controller-tools/cmd/controller-gen@${version}
15+
cd ${P}
16+
17+
# clean up the temporary working directory
18+
rm -rf $TMP_DIR
19+
}
20+
21+
# see if the first semver argument is >= the second semver argument
22+
later() {
23+
local compare=$1
24+
local base=$2
25+
# strip off the optional "v" at the beginning
26+
compare=${compare#v}
27+
base=${base#v}
28+
29+
local compareMajor=${compare%%.*}
30+
local baseMajor=${base%%.*}
31+
32+
local compareMinorPatch=${compare#*.}
33+
local baseMinorPatch=${base#*.}
34+
35+
local compareMinor=${compareMinorPatch%%.*}
36+
local baseMinor=${baseMinorPatch%%.*}
37+
38+
local comparePatch=${compare##*.}
39+
local basePatch=${base##*.}
40+
# check major version
41+
42+
# start with major - if greater, it is later; if less, it is earlier
43+
[ $compareMajor -lt $baseMajor ] && return 1
44+
[ $compareMajor -gt $baseMajor ] && return 0
45+
46+
# major matches, so check minor
47+
[ $compareMinor -lt $baseMinor ] && return 1
48+
[ $compareMinor -gt $baseMinor ] && return 0
49+
50+
# minor matches, so check patch
51+
[ $comparePatch -lt $basePatch ] && return 1
52+
[ $comparePatch -gt $basePatch ] && return 0
53+
54+
# patch matches, so it is the same
55+
return 0
56+
}
57+
58+
59+
BINARY=$1
60+
VERSION=$2
61+
62+
# if no version given, just download the latest and go
63+
if [ -z "$VERSION" ]; then
64+
download master
65+
exit 0
66+
fi
67+
68+
# check if we have one and what its version is
69+
if [ ! -e "${BINARY}" ]; then
70+
download ${VERSION}
71+
exit 0
72+
fi
73+
74+
# if we got here, we have one, and we were not asked to take latest, so check its version
75+
existing=$(${BINARY} --version | awk '{print $2}')
76+
# get the three parts of the semver and the three parts of the requested version, and compare
77+
78+
later "${existing}" "${VERSION}" || download ${VERSION}
79+
80+
exit 0
81+
82+

0 commit comments

Comments
 (0)