Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

KUBEBUILDER_VERSION ?= 2.3.1
KUBEBUILDER ?= /usr/local/kubebuilder/bin/kubebuilder
CONTROLLER_GEN_VERSION ?= v0.3.0
CONTROLLER_GEN=$(GOBIN)/controller-gen

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

Expand Down Expand Up @@ -179,20 +181,11 @@ push:

# find or download controller-gen
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.5 ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
# version must be at least the given version
.PHONY: $(CONTROLLER_GEN)
controller-gen: $(CONTROLLER_GEN)
$(CONTROLLER_GEN):
scripts/controller-gen.sh $@ $(CONTROLLER_GEN_VERSION)

examples:
./generate-examples.sh
Expand Down
82 changes: 82 additions & 0 deletions scripts/controller-gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/sh

set -e

# ensure controller-gen is at least the right version


download() {
local version=$1
local P=$(pwd)
local TMP_DIR=$(mktemp -d)
cd $TMP_DIR
go mod init tmp
go get -u sigs.k8s.io/controller-tools/cmd/controller-gen@${version}
cd ${P}

# clean up the temporary working directory
rm -rf $TMP_DIR
}

# see if the first semver argument is >= the second semver argument
later() {
local compare=$1
local base=$2
# strip off the optional "v" at the beginning
compare=${compare#v}
base=${base#v}

local compareMajor=${compare%%.*}
local baseMajor=${base%%.*}

local compareMinorPatch=${compare#*.}
local baseMinorPatch=${base#*.}

local compareMinor=${compareMinorPatch%%.*}
local baseMinor=${baseMinorPatch%%.*}

local comparePatch=${compare##*.}
local basePatch=${base##*.}
# check major version

# start with major - if greater, it is later; if less, it is earlier
[ $compareMajor -lt $baseMajor ] && return 1
[ $compareMajor -gt $baseMajor ] && return 0

# major matches, so check minor
[ $compareMinor -lt $baseMinor ] && return 1
[ $compareMinor -gt $baseMinor ] && return 0

# minor matches, so check patch
[ $comparePatch -lt $basePatch ] && return 1
[ $comparePatch -gt $basePatch ] && return 0

# patch matches, so it is the same
return 0
}


BINARY=$1
VERSION=$2

# if no version given, just download the latest and go
if [ -z "$VERSION" ]; then
download master
exit 0
fi

# check if we have one and what its version is
if [ ! -e "${BINARY}" ]; then
download ${VERSION}
exit 0
fi

# if we got here, we have one, and we were not asked to take latest, so check its version
existing=$(${BINARY} --version | awk '{print $2}')
# get the three parts of the semver and the three parts of the requested version, and compare

later "${existing}" "${VERSION}" || download ${VERSION}

exit 0