Skip to content

Commit

Permalink
Uniform tool installation in Makefile (#94)
Browse files Browse the repository at this point in the history
Use a uniform installation process for golangci-lint, controller-gen and
kustomize.

The process ensures that the correct version of the tool is used for
every "build", regardless of what other version may already be installed
on the system.

For kustomize, we ensure that the Makefile is the single source of
truth, and we remove hack/verify-kustomize.sh.

Signed-off-by: Antonin Bas <antonin.bas@broadcom.com>
  • Loading branch information
antoninbas committed Dec 13, 2023
1 parent cc783cf commit 7135116
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 104 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ tags
# End of https://www.gitignore.io/api/go,vim,emacs,visualstudiocode
### kube cache ###
.kube
### kustomize image ###
hack/.bin/kustomize
.kustomize
.controller-gen
76 changes: 45 additions & 31 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ ifndef IS_CERTIFICATION
IS_CERTIFICATION=false
endif

.PHONY: all
all: generate golangci manager

include versioning.mk
LDFLAGS += $(VERSION_LDFLAGS)

Expand All @@ -43,21 +46,24 @@ PKG_IS_DEFAULT_CHANNEL := --default-channel
endif
PKG_MAN_OPTS ?= $(FROM_VERSION) $(PKG_CHANNELS) $(PKG_IS_DEFAULT_CHANNEL)

all: generate golangci manager
GOLANGCI_LINT_VERSION := v1.51.0
GOLANGCI_LINT_BINDIR := $(CURDIR)/.golangci-bin
GOLANGCI_LINT_BIN := $(GOLANGCI_LINT_BINDIR)/$(GOLANGCI_LINT_VERSION)/golangci-lint

.golangci-bin:
$(GOLANGCI_LINT_BIN):
@echo "===> Installing Golangci-lint <==="
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $@ v1.51.0
@rm -rf $(GOLANGCI_LINT_BINDIR)/* # remove old versions
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOLANGCI_LINT_BINDIR)/$(GOLANGCI_LINT_VERSION) $(GOLANGCI_LINT_VERSION)

.PHONY: golangci
golangci: .golangci-bin
golangci: $(GOLANGCI_LINT_BIN)
@echo "===> Running golangci <==="
@GOOS=linux .golangci-bin/golangci-lint run -c .golangci.yml
@GOOS=linux $(GOLANGCI_LINT_BIN) run -c $(CURDIR)/.golangci.yml

.PHONY: golangci-fix
golangci-fix: .golangci-bin
@echo "===> Running golangci-fix<==="
@GOOS=linux .golangci-bin/golangci-lint run -c .golangci.yml --fix
golangci-fix: $(GOLANGCI_LINT_BIN)
@echo "===> Running golangci-fix <==="
@GOOS=linux $(GOLANGCI_LINT_BIN) run -c $(CURDIR)/.golangci.yml --fix

# Run tests
ENVTEST_ASSETS_DIR = $(shell pwd)/testbin
Expand Down Expand Up @@ -102,29 +108,30 @@ docker-build:
docker build -f build/Dockerfile --label version="$(VERSION)" . -t ${IMG}
docker tag ${IMG} antrea/antrea-operator

# find or download controller-gen
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.6.2 ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
CONTROLLER_GEN_VERSION := v0.6.2
CONTROLLER_GEN_BINDIR := $(CURDIR)/.controller-gen
CONTROLLER_GEN := $(CONTROLLER_GEN_BINDIR)/$(CONTROLLER_GEN_VERSION)/controller-gen

kustomize:
ifeq (, $(shell which kustomize))
@{ \
set -e ;\
go install sigs.k8s.io/kustomize/kustomize/v5@v5.2.1 ;\
}
KUSTOMIZE=$(GOBIN)/kustomize
else
KUSTOMIZE=$(shell which kustomize)
endif
$(CONTROLLER_GEN):
@echo "===> Installing Controller-gen <==="
@rm -rf $(CONTROLLER_GEN_BINDIR)/* # remove old versions
GOBIN=$(CONTROLLER_GEN_BINDIR)/$(CONTROLLER_GEN_VERSION) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION)

.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN)

KUSTOMIZE_VERSION := 5.3.0
KUSTOMIZE_BINDIR := $(CURDIR)/.kustomize
KUSTOMIZE := $(KUSTOMIZE_BINDIR)/$(KUSTOMIZE_VERSION)/kustomize

$(KUSTOMIZE):
@echo "===> Installing Kustomize <==="
@rm -rf $(KUSTOMIZE_BINDIR)/* # remove old versions
@mkdir -p $(KUSTOMIZE_BINDIR)/$(KUSTOMIZE_VERSION)
@curl -sSfL https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh | bash -s -- $(KUSTOMIZE_VERSION) $(KUSTOMIZE_BINDIR)/$(KUSTOMIZE_VERSION)

.PHONY: kustomize
kustomize: $(KUSTOMIZE)

# Generate bundle manifests and metadata, then validate generated files.
.PHONY: bundle
Expand Down Expand Up @@ -156,7 +163,7 @@ bundle-build:
docker tag ${BUNDLE_IMG} antrea/antrea-operator-bundle

antrea-resources:
./hack/generate-antrea-resources.sh --platform $(ANTREA_PLATFORM) --version $(VERSION)
KUSTOMIZE=$(KUSTOMIZE) ./hack/generate-antrea-resources.sh --platform $(ANTREA_PLATFORM) --version $(VERSION)
cp ./config/rbac/role.yaml ./deploy/$(ANTREA_PLATFORM)/role.yaml
cp ./config/samples/operator_v1_antreainstall.yaml ./deploy/$(ANTREA_PLATFORM)/operator.antrea.vmware.com_v1_antreainstall_cr.yaml

Expand All @@ -178,3 +185,10 @@ tidy:

.PHONY: bin
bin: manager

.PHONY: clean
clean:
@rm -rf bin
@rm -rf $(GOLANGCI_LINT_BINDIR)
@rm -rf $(KUSTOMIZE_BINDIR)
@rm -rf $(CONTROLLER_GEN_BINDIR)
6 changes: 3 additions & 3 deletions antrea-manifest/antrea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6413,11 +6413,11 @@ subjects:
apiVersion: v1
data:
antrea-agent.conf: |
{{- .AntreaAgentConfig | nindent 4 }}
{{- .AntreaAgentConfig | nindent 4 }}
antrea-cni.conflist: |
{{- .AntreaCNIConfig | nindent 4 }}
{{- .AntreaCNIConfig | nindent 4 }}
antrea-controller.conf: |
{{- .AntreaControllerConfig | nindent 4 }}
{{- .AntreaControllerConfig | nindent 4 }}
kind: ConfigMap
metadata:
labels:
Expand Down
14 changes: 4 additions & 10 deletions hack/generate-antrea-resources.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ Generate a YAML manifest for Antrea using Kustomize and print it to stdout.
--help, -h Print this message and exit
This tool uses kustomize (https://github.com/kubernetes-sigs/kustomize) to generate manifests for
Antrea. You can set the KUSTOMIZE environment variable to the path of the kustomize binary you want
us to use. Otherwise we will download the appropriate version of the kustomize binary and use
it (this is the recommended approach since different versions of kustomize may create different
output YAMLs)."
Antrea. You must set the KUSTOMIZE environment variable to the path of the kustomize binary you want
us to use."

function print_usage {
echoerr "$_usage"
Expand Down Expand Up @@ -59,12 +57,8 @@ fi

THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

source $THIS_DIR/verify-kustomize.sh

if [ -z "$KUSTOMIZE" ]; then
KUSTOMIZE="$(verify_kustomize)"
elif ! $KUSTOMIZE version > /dev/null 2>&1; then
echoerr "$KUSTOMIZE does not appear to be a valid kustomize binary"
echoerr "KUSTOMIZE environment variable must be set"
print_help
exit 1
fi
Expand Down Expand Up @@ -119,7 +113,7 @@ $KUSTOMIZE edit add patch --path controllerImage.yml
$KUSTOMIZE edit add patch --path agentImagePullPolicy.yml
$KUSTOMIZE edit add patch --path controllerImagePullPolicy.yml

$KUSTOMIZE build | sed 's/^\s*{{/{{/; s/\\"\({{.*}}\)\\"/"\1"/; '"s/'\({{.*}}\)'/\1/" > $THIS_DIR/../antrea-manifest/antrea.yml
$KUSTOMIZE build | sed 's/\\"\({{.*}}\)\\"/"\1"/; '"s/'\({{.*}}\)'/\1/" > $THIS_DIR/../antrea-manifest/antrea.yml

popd > /dev/null

Expand Down
58 changes: 0 additions & 58 deletions hack/verify-kustomize.sh

This file was deleted.

0 comments on commit 7135116

Please sign in to comment.