Skip to content

Commit

Permalink
consolidate go list -m k8s.io/client-go calls for ENVTEST_VERSION, KU…
Browse files Browse the repository at this point in the history
…BE_VERSION, and KIND_NODE_VERSION

Avoid multiple redundant shell executions by extracting the client-go version once and deriving ENVTEST_VERSION, KUBE_VERSION (for PSA label), and KIND_NODE_VERSION from it. This improves efficiency and prevents unnecessary re-evaluations in Makefile.
  • Loading branch information
camilamacedo86 committed Mar 3, 2025
1 parent bde4f4e commit ff3b48d
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export IMAGE_TAG
OPCON_IMG := $(OPCON_IMAGE_REPO):$(IMAGE_TAG)
CATD_IMG := $(CATD_IMAGE_REPO):$(IMAGE_TAG)

# Extract the Kubernetes client-go version to set KUBE_MINOR used in the manifests.
ifeq ($(origin K8S_CLIENT_GO_VERSION), undefined)
K8S_VERSION := $(shell go list -m k8s.io/client-go | cut -d" " -f2 | sed 's/^v0\.\([[:digit:]]\{1,\}\)\.[[:digit:]]\{1,\}$$/1.\1/')
endif

# Define dependency versions (use go.mod if we also use Go code from dependency)
export CERT_MGR_VERSION := v1.15.3
export WAIT_TIMEOUT := 60s
Expand All @@ -55,11 +60,10 @@ KIND_CLUSTER_NAME := operator-controller
endif

# Not guaranteed to have patch releases available and node image tags are full versions (i.e v1.28.0 - no v1.28, v1.29, etc.)
# The KIND_NODE_VERSION is set by getting the version of the k8s.io/client-go dependency from the go.mod
# The K8S_VERSION is set by getting the version of the k8s.io/client-go dependency from the go.mod
# and sets major version to "1" and the patch version to "0". For example, a client-go version of v0.28.5
# will map to a KIND_NODE_VERSION of 1.28.0
KIND_NODE_VERSION := $(shell go list -m k8s.io/client-go | cut -d" " -f2 | sed 's/^v0\.\([[:digit:]]\{1,\}\)\.[[:digit:]]\{1,\}$$/1.\1.0/')
KIND_CLUSTER_IMAGE := kindest/node:v$(KIND_NODE_VERSION)
# will map to a K8S_VERSION of 1.28.0
KIND_CLUSTER_IMAGE := kindest/node:v$(K8S_VERSION).0

ifneq (, $(shell command -v docker 2>/dev/null))
CONTAINER_RUNTIME := docker
Expand Down Expand Up @@ -152,7 +156,7 @@ KUBE_MINOR ?= $(shell go list -m k8s.io/client-go | cut -d" " -f2 | sed 's/^v0\.
.PHONY: update-k8s-values # HELP Update PSA labels in config manifests with Kubernetes version
update-k8s-values:
find config -type f -name '*.yaml' -exec \
sed -i.bak -E 's/(pod-security.kubernetes.io\/[a-zA-Z-]+-version:).*/\1 "v$(KUBE_MINOR)"/g' {} +;
sed -i.bak -E 's/(pod-security.kubernetes.io\/[a-zA-Z-]+-version:).*/\1 "v$(K8S_VERSION)"/g' {} +;
find config -type f -name '*.yaml.bak' -delete

.PHONY: fix-lint
Expand Down Expand Up @@ -203,14 +207,13 @@ test-ext-dev-e2e: $(OPERATOR_SDK) $(KUSTOMIZE) $(KIND) #HELP Run extension creat
test/extension-developer-e2e/setup.sh $(OPERATOR_SDK) $(CONTAINER_RUNTIME) $(KUSTOMIZE) $(KIND) $(KIND_CLUSTER_NAME) $(E2E_REGISTRY_NAMESPACE)
go test -count=1 -v ./test/extension-developer-e2e/...

ENVTEST_VERSION := $(shell go list -m k8s.io/client-go | cut -d" " -f2 | sed 's/^v0\.\([[:digit:]]\{1,\}\)\.[[:digit:]]\{1,\}$$/1.\1.x/')
UNIT_TEST_DIRS := $(shell go list ./... | grep -v /test/)
COVERAGE_UNIT_DIR := $(ROOT_DIR)/coverage/unit

.PHONY: envtest-k8s-bins #HELP Uses setup-envtest to download and install the binaries required to run ENVTEST-test based locally at the project/bin directory.
envtest-k8s-bins: $(SETUP_ENVTEST)
mkdir -p $(ROOT_DIR)/bin
$(SETUP_ENVTEST) use -p env $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE)
$(SETUP_ENVTEST) use -p env $(KUBE_MINOR).x $(SETUP_ENVTEST_BIN_DIR_OVERRIDE)

.PHONY: test-unit
test-unit: $(SETUP_ENVTEST) envtest-k8s-bins #HELP Run the unit tests
Expand Down

0 comments on commit ff3b48d

Please sign in to comment.