Skip to content

Commit

Permalink
Stop relying on -modfile to allow vendorization
Browse files Browse the repository at this point in the history
The Makefile targets we use to build our tools rely on the `-modfile`
flag of the `go build` command.  Unfortunately, the golang build
toolchain doesn't respect the vendor directory relative to the `go.mod`
file specified by `-modfile`, and instead uses the vendor directory
relative to the current working directory.  Since each subdirectory in
this repository that contains a `go.mod` file needs to have its own
vendor directory in order to be built in vendorized scenarios, relying
on `-modfile` doesn't work.

The patch modifies the affected Makefile targets to instead change to
the directory of the tool being built instead of using the `-modfile`
flag.  This makes vendorization possible in downstream environments.

Finally, for consistency, we start building the kustomize tool in the
same way as controller-gen, and golangci-lint.
  • Loading branch information
honza committed Sep 8, 2021
1 parent ae74cdc commit b7b9e8c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
34 changes: 20 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ COVER_PROFILE = cover.out
BIN_DIR := bin

CRD_OPTIONS ?= "crd:trivialVersions=false,allowDangerousTypes=true,crdVersions=v1"
KUSTOMIZE ?= go run -modfile=hack/tools/go.mod sigs.k8s.io/kustomize/kustomize/v3
KUSTOMIZE = tools/bin/kustomize
CONTROLLER_GEN = tools/bin/controller-gen

# See pkg/version.go for details
SOURCE_GIT_COMMIT ?= $(shell git rev-parse --short HEAD)
Expand Down Expand Up @@ -83,7 +84,7 @@ unit-verbose: ## Run unit tests with verbose output
linters: lint generate-check fmt-check

tools/bin/golangci-lint: hack/tools/go.mod
go build -o $@ -modfile=$< github.com/golangci/golangci-lint/cmd/golangci-lint
cd hack/tools; go build -o $(abspath $@) github.com/golangci/golangci-lint/cmd/golangci-lint

.PHONY: lint
lint: tools/bin/golangci-lint
Expand Down Expand Up @@ -117,30 +118,33 @@ run-test-mode: generate fmt-check lint manifests ## Run against the configured K
go run -ldflags $(LDFLAGS) ./main.go -namespace=$(RUN_NAMESPACE) -dev -test-mode -webhook-port=0

.PHONY: install
install: manifests ## Install CRDs into a cluster
$(KUSTOMIZE) build config/crd | kubectl apply -f -
install: $(KUSTOMIZE) manifests ## Install CRDs into a cluster
$< build config/crd | kubectl apply -f -

.PHONY: uninstall
uninstall: manifests ## Uninstall CRDs from a cluster
$(KUSTOMIZE) build config/crd | kubectl delete -f -
uninstall: $(KUSTOMIZE) manifests ## Uninstall CRDs from a cluster
$< build config/crd | kubectl delete -f -

.PHONY: deploy
deploy: manifests ## Deploy controller in the configured Kubernetes cluster in ~/.kube/config
deploy: $(KUSTOMIZE) manifests ## Deploy controller in the configured Kubernetes cluster in ~/.kube/config
cd config/manager && kustomize edit set image controller=${IMG}
$(KUSTOMIZE) build config/default | kubectl apply -f -
$< build config/default | kubectl apply -f -

tools/bin/controller-gen: hack/tools/go.mod
go build -o $@ -modfile=$< sigs.k8s.io/controller-tools/cmd/controller-gen
$(CONTROLLER_GEN): hack/tools/go.mod
cd hack/tools; go build -o $(abspath $@) sigs.k8s.io/controller-tools/cmd/controller-gen

$(KUSTOMIZE): hack/tools/go.mod
cd hack/tools; go build -o $(abspath $@) sigs.k8s.io/kustomize/kustomize/v3

.PHONY: manifests
manifests: tools/bin/controller-gen ## Generate manifests e.g. CRD, RBAC etc.
cd apis; ../$< $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:webhook:dir=../config/webhook/ output:crd:artifacts:config=../config/crd/bases
manifests: $(CONTROLLER_GEN) $(KUSTOMIZE) ## Generate manifests e.g. CRD, RBAC etc.
cd apis; $(abspath $<) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:webhook:dir=../config/webhook/ output:crd:artifacts:config=../config/crd/bases
$< rbac:roleName=manager-role paths="./..." output:rbac:artifacts:config=config/rbac
$(KUSTOMIZE) build config/default > config/render/capm3.yaml

.PHONY: generate
generate: tools/bin/controller-gen ## Generate code
cd apis; ../$< object:headerFile="../hack/boilerplate.go.txt" paths="./..."
generate: $(CONTROLLER_GEN) ## Generate code
cd apis; $(abspath $<) object:headerFile="../hack/boilerplate.go.txt" paths="./..."
$< object:headerFile="hack/boilerplate.go.txt" paths="./..."

## --------------------------------------
Expand Down Expand Up @@ -218,3 +222,5 @@ mod: ## Clean up go module settings
go mod verify
cd apis; go mod tidy
cd apis; go mod verify
cd hack/tools; go mod tidy
cd hack/tools; go mod verify
4 changes: 3 additions & 1 deletion tools/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ DEPLOY_BASIC_AUTH="${4,,}"
DEPLOY_KEEPALIVED="${5,,}"
MARIADB_HOST_IP="${MARIADB_HOST_IP:-"127.0.0.1"}"
KUBECTL_ARGS="${KUBECTL_ARGS:-""}"
KUSTOMIZE="go run -modfile=hack/tools/go.mod sigs.k8s.io/kustomize/kustomize/v3"
KUSTOMIZE="tools/bin/kustomize"
RESTART_CONTAINER_CERTIFICATE_UPDATED=${RESTART_CONTAINER_CERTIFICATE_UPDATED:-"false"}
export NAMEPREFIX=${NAMEPREFIX:-"capm3"}

Expand All @@ -44,6 +44,8 @@ IRONIC_DEPLOY_FILES="${SCRIPTDIR}/ironic-deployment/basic-auth/default/auth.yaml
${SCRIPTDIR}/ironic-deployment/tls/keepalived/kustomization.yaml \
${SCRIPTDIR}/ironic-deployment/tls/keepalived/tls.yaml"

make ${KUSTOMIZE}

for DEPLOY_FILE in ${IRONIC_DEPLOY_FILES}; do
cp "$DEPLOY_FILE" "$DEPLOY_FILE".bak
# shellcheck disable=SC2094
Expand Down

0 comments on commit b7b9e8c

Please sign in to comment.