From 7135116e34f1c5e69453cf8e0d98bf479eb35a23 Mon Sep 17 00:00:00 2001 From: Antonin Bas Date: Wed, 13 Dec 2023 10:29:57 -0800 Subject: [PATCH] Uniform tool installation in Makefile (#94) 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 --- .gitignore | 4 +- Makefile | 76 ++++++++++++++++++------------- antrea-manifest/antrea.yml | 6 +-- hack/generate-antrea-resources.sh | 14 ++---- hack/verify-kustomize.sh | 58 ----------------------- 5 files changed, 54 insertions(+), 104 deletions(-) delete mode 100644 hack/verify-kustomize.sh diff --git a/.gitignore b/.gitignore index 538198fc..c342f17a 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Makefile b/Makefile index a26fd5ab..03c98ef0 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,9 @@ ifndef IS_CERTIFICATION IS_CERTIFICATION=false endif +.PHONY: all +all: generate golangci manager + include versioning.mk LDFLAGS += $(VERSION_LDFLAGS) @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/antrea-manifest/antrea.yml b/antrea-manifest/antrea.yml index f6305a1a..8ae6fcc9 100644 --- a/antrea-manifest/antrea.yml +++ b/antrea-manifest/antrea.yml @@ -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: diff --git a/hack/generate-antrea-resources.sh b/hack/generate-antrea-resources.sh index 8dc6ddaf..ddf0f2ee 100755 --- a/hack/generate-antrea-resources.sh +++ b/hack/generate-antrea-resources.sh @@ -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" @@ -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 @@ -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 diff --git a/hack/verify-kustomize.sh b/hack/verify-kustomize.sh deleted file mode 100644 index 0b61523d..00000000 --- a/hack/verify-kustomize.sh +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/env bash - -# Copyright 2019 Antrea Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -_BINDIR="$THIS_DIR/.bin" -# Must be an exact match, as the generated YAMLs may not be consistent across -# versions -_KUSTOMIZE_VERSION="v4.4.1" - -# Ensure the kustomize tool exists and is the correct version, or installs it -verify_kustomize() { - # Check if there is already a kustomize binary in $_BINDIR and if yes, check - # if the version matches the expected one. - local kustomize="$(PATH=$_BINDIR command -v kustomize)" - if [ -x "$kustomize" ]; then - # Verify version if kustomize was already installed. - local kustomize_version="$($kustomize version --short)" - kustomize_version="${kustomize_version##*/}" - kustomize_version="${kustomize_version%% *}" - if [ "${kustomize_version}" == "${_KUSTOMIZE_VERSION}" ]; then - # If version is exact match, stop here. - echo "$kustomize" - return 0 - fi - >&2 echo "Detected kustomize version ($kustomize_version) does not match expected one ($_KUSTOMIZE_VERSION), installing correct version" - fi - local ostype="" - if [[ "$OSTYPE" == "linux-gnu" ]]; then - ostype="linux" - elif [[ "$OSTYPE" == "darwin"* ]]; then - ostype="darwin" - else - >&2 echo "Unsupported OS type $OSTYPE" - return 1 - fi - >&2 echo "Installing kustomize" - local kustomize_url="https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/${_KUSTOMIZE_VERSION}/kustomize_${_KUSTOMIZE_VERSION}_${ostype}_amd64.tar.gz" - curl -sLo kustomize.tar.gz "${kustomize_url}" || return 1 - mkdir -p "$_BINDIR" || return 1 - tar -xzf kustomize.tar.gz -C "$_BINDIR" || return 1 - rm -f kustomize.tar.gz - kustomize="$_BINDIR/kustomize" - echo "$kustomize" - return 0 -}