Skip to content

Commit d75b709

Browse files
committed
first makefile foo
1 parent 4d97b4c commit d75b709

File tree

2 files changed

+104
-3
lines changed

2 files changed

+104
-3
lines changed

Makefile

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export GO111MODULE=on
3939
#
4040
# Kubebuilder.
4141
#
42-
export KUBEBUILDER_ENVTEST_KUBERNETES_VERSION ?= 1.28.0
42+
export KUBEBUILDER_ENVTEST_KUBERNETES_VERSION ?= 1.27.1
4343
export KUBEBUILDER_CONTROLPLANE_START_TIMEOUT ?= 60s
4444
export KUBEBUILDER_CONTROLPLANE_STOP_TIMEOUT ?= 60s
4545

@@ -64,6 +64,7 @@ CAPD_DIR := $(TEST_DIR)/infrastructure/docker
6464
CAPIM_DIR := $(TEST_DIR)/infrastructure/inmemory
6565
TEST_EXTENSION_DIR := $(TEST_DIR)/extension
6666
GO_INSTALL := ./scripts/go_install.sh
67+
GO_TOOLS_BUILD := ./hack/go-tools-build.sh
6768
OBSERVABILITY_DIR := hack/observability
6869

6970
export PATH := $(abspath $(TOOLS_BIN_DIR)):$(PATH)
@@ -169,6 +170,12 @@ GOVULNCHECK_VER := v1.0.0
169170
GOVULNCHECK := $(abspath $(TOOLS_BIN_DIR)/$(GOVULNCHECK_BIN)-$(GOVULNCHECK_VER))
170171
GOVULNCHECK_PKG := golang.org/x/vuln/cmd/govulncheck
171172

173+
METRIC_GEN_VER := v2.9.2
174+
METRIC_GEN_BIN := metric-gen
175+
METRIC_GEN := $(abspath $(TOOLS_BIN_DIR)/$(METRIC_GEN_BIN)-$(METRIC_GEN_VER))
176+
METRIC_GEN_PKG := k8s.io/kube-state-metrics/exp/metric-gen/v2
177+
METRIC_GEN_MOD_REPLACE := $(METRIC_GEN_PKG)=github.com/chrischdi/kube-state-metrics/exp/metric-gen/v2@$(METRIC_GEN_VER) k8s.io/kube-state-metrics/v2=k8s.io/kube-state-metrics/v2@$(METRIC_GEN_VER)
178+
172179
CONVERSION_VERIFIER_BIN := conversion-verifier
173180
CONVERSION_VERIFIER := $(abspath $(TOOLS_BIN_DIR)/$(CONVERSION_VERIFIER_BIN))
174181

@@ -556,8 +563,9 @@ generate-e2e-templates-main: $(KUSTOMIZE)
556563
$(KUSTOMIZE) build $(INMEMORY_TEMPLATES)/main/cluster-template --load-restrictor LoadRestrictionsNone > $(INMEMORY_TEMPLATES)/main/cluster-template.yaml
557564

558565
.PHONY: generate-metrics-config
559-
generate-metrics-config: $(ENVSUBST_BIN) ## Generate ./hack/observability/kube-state-metrics/crd-config.yaml
560-
OUTPUT_FILE="${OBSERVABILITY_DIR}/kube-state-metrics/crd-config.yaml"; \
566+
generate-metrics-config: $(METRIC_GEN) ## Generate ./hack/observability/kube-state-metrics/crd-config.yaml
567+
$(METRIC_GEN) ./apis/... > "${OBSERVABILITY_DIR}/kube-state-metrics/crd-config.yaml"
568+
561569
METRICS_DIR="${OBSERVABILITY_DIR}/kube-state-metrics/metrics"; \
562570
echo "# This file was auto-generated via: make generate-metrics-config" > "$${OUTPUT_FILE}"; \
563571
cat "$${METRICS_DIR}/header.yaml" >> "$${OUTPUT_FILE}"; \
@@ -1292,6 +1300,9 @@ $(GOLANGCI_LINT_BIN): $(GOLANGCI_LINT) ## Build a local copy of golangci-lint.
12921300
.PHONY: $(GOVULNCHECK_BIN)
12931301
$(GOVULNCHECK_BIN): $(GOVULNCHECK) ## Build a local copy of govulncheck.
12941302

1303+
.PHONY: $(METRIC_GEN_BIN)
1304+
$(METRIC_GEN_BIN): $(METRIC_GEN) ## Build a local copy of metric-gen.
1305+
12951306
$(CONTROLLER_GEN): # Build controller-gen from tools folder.
12961307
GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) $(CONTROLLER_GEN_PKG) $(CONTROLLER_GEN_BIN) $(CONTROLLER_GEN_VER)
12971308

@@ -1346,6 +1357,9 @@ $(GOLANGCI_LINT): # Build golangci-lint from tools folder.
13461357
$(GOVULNCHECK): # Build govulncheck.
13471358
GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) $(GOVULNCHECK_PKG) $(GOVULNCHECK_BIN) $(GOVULNCHECK_VER)
13481359

1360+
$(METRIC_GEN): # Build metric-gen.
1361+
GOBIN=$(TOOLS_BIN_DIR) GOMOD_REPLACE="$(METRIC_GEN_MOD_REPLACE)" $(GO_TOOLS_BUILD) $(METRIC_GEN_PKG) $(METRIC_GEN_BIN) $(METRIC_GEN_VER)
1362+
13491363
## --------------------------------------
13501364
## Helpers
13511365
## --------------------------------------

hack/go-tools-build.sh

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2023 The Kubernetes Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -o errexit
17+
set -o nounset
18+
set -o pipefail
19+
20+
set -x
21+
22+
if [ -z "${1}" ]; then
23+
echo "must provide module as first parameter"
24+
exit 1
25+
fi
26+
27+
if [ -z "${2}" ]; then
28+
echo "must provide binary name as second parameter"
29+
exit 1
30+
fi
31+
32+
if [ -z "${3}" ]; then
33+
echo "must provide version as third parameter"
34+
exit 1
35+
fi
36+
37+
if [ -z "${GOBIN}" ]; then
38+
echo "GOBIN is not set. Must set GOBIN to install the bin in a specified directory."
39+
exit 1
40+
fi
41+
42+
GOMOD_REPLACE=${GOMOD_REPLACE:=}
43+
GOMOD_REQUIRE=${GOMOD_REQUIRE:=}
44+
45+
rm -f "${GOBIN}/${2}"* || true
46+
47+
ORIGINAL_WORKDIR="$(pwd)"
48+
TMP_MODULE_DIR="${2}.tmp"
49+
50+
# Create TMP_MODULE_DIR to create a go module for building the binary.
51+
# This is required because CAPI's hack/tools is not compatible to `go install`.
52+
rm -r "${TMP_MODULE_DIR}" || true
53+
mkdir -p "${TMP_MODULE_DIR}"
54+
cd "${TMP_MODULE_DIR}"
55+
56+
# Initialize a go module and place a tools.go file for building the binary.
57+
go mod init "tools"
58+
59+
for PARAM in ${GOMOD_REPLACE}; do
60+
eval go mod edit -replace ${PARAM}
61+
done
62+
for PARAM in ${GOMOD_REQUIRE}; do
63+
eval go mod edit -require ${PARAM}
64+
done
65+
66+
# Create go file which imports the required package and resolve dependencies.
67+
cat << EOF > tools.go
68+
//go:build tools
69+
// +build tools
70+
package tools
71+
72+
import (
73+
_ "${1}"
74+
)
75+
EOF
76+
77+
go mod tidy
78+
79+
# Build the binary.
80+
go build -tags=tools -o "${GOBIN}/${2}-${3}" "${1}"
81+
82+
# Get back to the original directory and cleanup the temporary directory.
83+
cd "${ORIGINAL_WORKDIR}"
84+
rm -r "${TMP_MODULE_DIR}"
85+
86+
# Link the unversioned name to the versioned binary.
87+
ln -sf "${GOBIN}/${2}-${3}" "${GOBIN}/${2}"

0 commit comments

Comments
 (0)