diff --git a/Makefile b/Makefile index a9e6c4c838eb..4da73c7a6741 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,7 @@ CAPD_DIR := $(TEST_DIR)/infrastructure/docker CAPIM_DIR := $(TEST_DIR)/infrastructure/inmemory TEST_EXTENSION_DIR := $(TEST_DIR)/extension GO_INSTALL := ./scripts/go_install.sh +GO_TOOLS_BUILD := ./hack/go-tools-build.sh OBSERVABILITY_DIR := hack/observability export PATH := $(abspath $(TOOLS_BIN_DIR)):$(PATH) @@ -101,7 +102,7 @@ KUSTOMIZE_BIN := kustomize KUSTOMIZE := $(abspath $(TOOLS_BIN_DIR)/$(KUSTOMIZE_BIN)-$(KUSTOMIZE_VER)) KUSTOMIZE_PKG := sigs.k8s.io/kustomize/kustomize/v5 -SETUP_ENVTEST_VER := v0.0.0-20240215143116-d0396a3d6f9f +SETUP_ENVTEST_VER := v0.0.0-20240506183901-d950bb92291d SETUP_ENVTEST_BIN := setup-envtest SETUP_ENVTEST := $(abspath $(TOOLS_BIN_DIR)/$(SETUP_ENVTEST_BIN)-$(SETUP_ENVTEST_VER)) SETUP_ENVTEST_PKG := sigs.k8s.io/controller-runtime/tools/setup-envtest @@ -1456,7 +1457,7 @@ $(KUSTOMIZE): # Build kustomize from tools folder. CGO_ENABLED=0 GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) $(KUSTOMIZE_PKG) $(KUSTOMIZE_BIN) $(KUSTOMIZE_VER) $(SETUP_ENVTEST): # Build setup-envtest from tools folder. - GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) $(SETUP_ENVTEST_PKG) $(SETUP_ENVTEST_BIN) $(SETUP_ENVTEST_VER) + GOBIN=$(TOOLS_BIN_DIR) $(GO_TOOLS_BUILD) $(SETUP_ENVTEST_PKG) $(SETUP_ENVTEST_BIN) $(SETUP_ENVTEST_VER) $(TILT_PREPARE): $(TOOLS_DIR)/go.mod # Build tilt-prepare from tools folder. cd $(TOOLS_DIR); go build -tags=tools -o $(BIN_DIR)/tilt-prepare sigs.k8s.io/cluster-api/hack/tools/internal/tilt-prepare diff --git a/hack/go-tools-build.sh b/hack/go-tools-build.sh new file mode 100755 index 000000000000..b076d6498f8f --- /dev/null +++ b/hack/go-tools-build.sh @@ -0,0 +1,76 @@ +#!/usr/bin/env bash +# Copyright 2023 The Kubernetes 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. + +set -o errexit +set -o nounset +set -o pipefail + +if [ -z "${1}" ]; then + echo "must provide module as first parameter" + exit 1 +fi + +if [ -z "${2}" ]; then + echo "must provide binary name as second parameter" + exit 1 +fi + +if [ -z "${3}" ]; then + echo "must provide version as third parameter" + exit 1 +fi + +if [ -z "${GOBIN}" ]; then + echo "GOBIN is not set. Must set GOBIN to install the bin in a specified directory." + exit 1 +fi + +rm -f "${GOBIN}/${2}"* || true + +ORIGINAL_WORKDIR="$(pwd)" +TMP_MODULE_DIR="${2}.tmp" + +# Create TMP_MODULE_DIR to create a go module for building the binary. +rm -r "${TMP_MODULE_DIR}" || true +mkdir -p "${TMP_MODULE_DIR}" +cd "${TMP_MODULE_DIR}" + +# Initialize a go module and place a tools.go file for building the binary. +go mod init "tools" +# Set require for "sigs.k8s.io/cluster-api" to let go resolve the tools version via the CAPI version. +go mod edit -replace "sigs.k8s.io/controller-runtime/tools/setup-envtest=github.com/sbueringer/controller-runtime/tools/setup-envtest@v0.0.0-20240506183901-d950bb92291d" + +# Create go file which imports the required package and resolve dependencies. +cat << EOF > tools.go +//go:build tools +// +build tools +package tools + +import ( + _ "${1}" +) +EOF + +go mod tidy + +# Build the binary. +go build -tags=tools -o "${GOBIN}/${2}-${3}" "${1}" + +# Get back to the original directory and cleanup the temporary directory. +cd "${ORIGINAL_WORKDIR}" +rm -r "${TMP_MODULE_DIR}" + +# Link the unversioned name to the versioned binary. +ln -sf "${GOBIN}/${2}-${3}" "${GOBIN}/${2}"