forked from kubernetes-sigs/kro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
340 lines (275 loc) · 12.8 KB
/
Makefile
File metadata and controls
340 lines (275 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
OCI_REPO ?= registry.k8s.io/kro
HELM_IMAGE ?= ${OCI_REPO}/charts/kro
KO_DOCKER_REPO ?= ${OCI_REPO}/kro
HELM ?= go run helm.sh/helm/v3/cmd/helm@v3.19.0
KOCACHE ?= ~/.ko
KO_PUSH ?= true
KO_LOCAL ?= true
export KIND_CLUSTER_NAME ?= kro
# Adapted from etcd-operator and k/k:
# Set the GOTOOLCHAIN to force the toolchain defined in go.mod
GOTOOLCHAIN ?= auto
ifeq (auto,$(GOTOOLCHAIN)) # User didn't specify the GOTOOLCHAIN, or is set to auto.
ifeq (,$(FORCE_HOST_GO)) # User didn't provide FORCE_HOST_GO, use go.mod's toolchain
export GOTOOLCHAIN=$(shell grep '^toolchain go' go.mod | cut -d' ' -f2)
else # User provided FORCE_HOST_GO, use the local version
export GOTOOLCHAIN=local
endif
endif
GIT_TAG ?= dirty-tag
GIT_VERSION ?= $(shell git describe --tags --always --dirty)
GIT_HASH ?= $(shell git rev-parse HEAD)
DATE_FMT = +%Y-%m-%dT%H:%M:%SZ
SOURCE_DATE_EPOCH ?= $(shell git log -1 --no-show-signature --pretty=%ct)
ifdef SOURCE_DATE_EPOCH
BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u "$(DATE_FMT)")
else
BUILD_DATE ?= $(shell date "$(DATE_FMT)")
endif
GIT_TREESTATE = "clean"
DIFF = $(shell git diff --quiet >/dev/null 2>&1; if [ $$? -eq 1 ]; then echo "1"; fi)
ifeq ($(DIFF), 1)
GIT_TREESTATE = "dirty"
endif
LDFLAGS="-buildid= -X sigs.k8s.io/release-utils/version.gitVersion=$(GIT_VERSION) \
-X sigs.k8s.io/release-utils/version.gitCommit=$(GIT_HASH) \
-X sigs.k8s.io/release-utils/version.gitTreeState=$(GIT_TREESTATE) \
-X sigs.k8s.io/release-utils/version.buildDate=$(BUILD_DATE)"
WITH_GOFLAGS = GOFLAGS="$(GOFLAGS)"
HELM_STATIC_MANIFESTS_FLAGS ?= --set metadata.includeHelmChart=false --set metadata.includeManagedBy=false --include-crds --namespace kro-system
HELM_STATIC_MANIFEST_IMAGE_FLAGS ?= --set image.tag=${RELEASE_VERSION}
ifeq ($(shell uname -s),Darwin)
SED_INPLACE_FLAGS ?= -i ''
else
SED_INPLACE_FLAGS ?= -i
endif
HELM_DIR = ./helm
WHAT ?= unit
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif
# CONTAINER_TOOL defines the container tool to be used for building images.
# Be aware that the target commands are only tested with Docker which is
# scaffolded by default. However, you might want to replace it to use other
# tools. (i.e. finch)
CONTAINER_TOOL ?= finch
# Setting SHELL to bash allows bash commands to be executed by recipes.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec
.PHONY: all
all: build
##@ General
# The help target prints out all targets with their descriptions organized
# beneath their categories. The categories are represented by '##@' and the
# target descriptions by '##'. The awk command is responsible for reading the
# entire set of makefiles included in this invocation, looking for lines of the
# file as xyz: ## something, and then pretty-format the target and help. Then,
# if there's a line with ##@ something, that gets pretty-printed as a category.
# More info on the usage of ANSI control characters for terminal formatting:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php
.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
##@ Development
.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) crd webhook paths="./..." output:crd:artifacts:config=helm/crds
@echo "Copying CRD to website docs..."
@mkdir -p website/static/crds
@cp helm/crds/kro.run_resourcegraphdefinitions.yaml website/static/crds/kro.run_resourcegraphdefinitions.yaml
@echo "CRD copied successfully"
.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object paths="./..."
tt:
$(CONTROLLER_GEN) object paths="./pkg/controller/resourcegraphdefinition"
.PHONY: fmt
fmt: go-generate ## Run go fmt against code and add licenses.
go fmt ./...
.PHONY: go-generate
go-generate: ## Run go generate against code.
go generate
.PHONY: vet
vet: ## Run go vet against code.
go vet ./...
.PHONY: test
test: manifests generate fmt vet envtest ## Run tests. Use WHAT=unit or WHAT=integration
ifeq ($(WHAT),integration)
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_VERSION) --bin-dir $(LOCALBIN) -p path)" go test -v ./test/integration/suites/... -coverprofile integration-cover.out -ginkgo.v
else ifeq ($(WHAT),unit)
go test -v ./pkg/... -coverprofile unit-cover.out
else
@echo "Error: WHAT must be either 'unit' or 'integration'"
@echo "Usage: make test WHAT=unit|integration"
@exit 1
endif
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
GOLANGCI_LINT_VERSION ?= v2.6.1
golangci-lint:
@[ -f $(GOLANGCI_LINT) ] || { \
set -e ;\
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) $(GOLANGCI_LINT_VERSION) ;\
}
.PHONY: lint
lint: golangci-lint ## Run golangci-lint linter & yamllint
$(GOLANGCI_LINT) run
.PHONY: lint-fix
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
$(GOLANGCI_LINT) run --fix
##@ Build
.PHONY: build
build: manifests generate fmt vet ## Build controller binary.
go build -ldflags=${LDFLAGS} -o bin/controller ./cmd/controller/main.go
.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
go run ./cmd/controller/main.go
##@ Build Dependencies
## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)
## Tool Binaries
KUBECTL ?= kubectl
KIND ?= kind
KUSTOMIZE ?= $(LOCALBIN)/kustomize
KO ?= $(LOCALBIN)/ko
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
CHAINSAW ?= $(LOCALBIN)/chainsaw
## Tool Versions
KO_VERSION ?= v0.17.1
KUSTOMIZE_VERSION ?= v5.2.1
CONTROLLER_TOOLS_VERSION ?= v0.19.0
CHAINSAW_VERSION ?= v0.2.12
.PHONY: chainsaw
chainsaw: $(CHAINSAW) ## Download chainsaw locally if necessary. If wrong version is installed, it will be removed before downloading.
$(CHAINSAW): $(LOCALBIN)
@if test -x $(LOCALBIN)/chainsaw && ! $(LOCALBIN)/chainsaw version | grep -q $(CHAINSAW_VERSION); then \
echo "$(LOCALBIN)/chainsaw version is not expected $(CHAINSAW_VERSION). Removing it before installing."; \
rm -rf $(LOCALBIN)/chainsaw; \
fi
test -s $(LOCALBIN)/chainsaw || GOBIN=$(LOCALBIN) GO111MODULE=on go install github.com/kyverno/chainsaw@$(CHAINSAW_VERSION)
ENVTEST_VERSION ?= 1.31.x
.PHONY: ko
ko: $(KO) ## Download ko locally if necessary. If wrong version is installed, it will be removed before downloading.
$(KO): $(LOCALBIN)
@if test -x $(LOCALBIN)/ko && ! $(LOCALBIN)/ko version | grep -q $(KO_VERSION); then \
echo "$(LOCALBIN)/ko version is not expected $(KO_VERSION). Removing it before installing."; \
rm -rf $(LOCALBIN)/ko; \
fi
test -s $(LOCALBIN)/ko || GOBIN=$(LOCALBIN) GO111MODULE=on go install github.com/google/ko@$(KO_VERSION)
.PHONY: kustomize
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
$(KUSTOMIZE): $(LOCALBIN)
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
rm -rf $(LOCALBIN)/kustomize; \
fi
test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
$(CONTROLLER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) $(GOPREFIX) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
.PHONY: image
build-image: ko ## Build the kro controller images using ko build
echo "Building kro image $(RELEASE_VERSION).."
$(WITH_GOFLAGS) KOCACHE=$(KOCACHE) KO_DOCKER_REPO=$(KO_DOCKER_REPO) \
$(KO) build --bare github.com/kubernetes-sigs/kro/cmd/controller \
$(if $(filter true,$(KO_LOCAL)),--local,--oci-layout-path rendered/oci/layout) \
--push=false --tags ${RELEASE_VERSION} --sbom=none
.PHONY: publish
publish-image: ko ## Publish the kro controller images
$(WITH_GOFLAGS) KOCACHE=$(KOCACHE) KO_DOCKER_REPO=$(KO_DOCKER_REPO) \
$(KO) publish --bare github.com/kubernetes-sigs/kro/cmd/controller \
--tags ${RELEASE_VERSION} --sbom=none
.PHONY: inject-helm-version
inject-helm-version:
sed $(SED_INPLACE_FLAGS) 's/tag: .*/tag: "$(RELEASE_VERSION)"/' helm/values.yaml
sed $(SED_INPLACE_FLAGS) 's/version: .*/version: $(RELEASE_VERSION)/' helm/Chart.yaml
sed $(SED_INPLACE_FLAGS) 's/appVersion: .*/appVersion: "$(RELEASE_VERSION)"/' helm/Chart.yaml
.PHONY: package-helm
package-helm: inject-helm-version ## Package Helm chart
${HELM} package helm
.PHONY: publish-helm
publish-helm: ## Helm publish
${HELM} push ./kro-${RELEASE_VERSION}.tgz oci://${HELM_IMAGE}
.PHONY: render-static-manifests
render-static-manifests: inject-helm-version
mkdir -p manifests/rendered
@command -v yq >/dev/null 2>&1 || { echo >&2 "yq is required but not installed. Please install yq v4+"; exit 1; }
@variants=$$(yq -r '.variants[].name' manifests/variants.yaml); \
for v in $$variants; do \
echo "Rendering variant: $$v"; \
tmpfile=$$(mktemp); \
yq -r ".variants[] | select(.name==\"$${v}\") | .values" manifests/variants.yaml > $$tmpfile; \
${HELM} template ${HELM_STATIC_MANIFESTS_FLAGS} ${HELM_STATIC_MANIFEST_IMAGE_FLAGS} -f $$tmpfile kro ./helm > manifests/rendered/$${v}.yaml; \
rm -f $$tmpfile; \
done
.PHONY:
release: build-image publish-image package-helm publish-helm
##@ Deployment
ifndef ignore-not-found
ignore-not-found = true
endif
.PHONY: install
install: manifests ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUBECTL) apply -f ./helm/crds
.PHONY: uninstall
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config
$(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f ./helm/crds
.PHONY: start-kind
start-kind:
$(KIND) delete cluster --name ${KIND_CLUSTER_NAME} || true
$(KIND) create cluster --name ${KIND_CLUSTER_NAME}
$(KUBECTL) --context kind-${KIND_CLUSTER_NAME} create namespace kro-system
.PHONY: deploy-kind-helm
deploy-kind-helm: export KO_DOCKER_REPO=kind.local
deploy-kind-helm: ko start-kind
make install
# This generates deployment with ko://... used in image.
# ko then intercepts it builds image, pushes to kind node, replaces the image in deployment and applies it
${HELM} template kro ./helm --namespace kro-system --set image.pullPolicy=Never --set image.ko=true --set config.allowCRDDeletion=true | $(KO) apply -f -
kubectl wait --for=condition=ready --timeout=1m pod -n kro-system -l app.kubernetes.io/component=controller
$(KUBECTL) --context kind-${KIND_CLUSTER_NAME} get pods -A
.PHONY: deploy-kind-%
deploy-kind-%: export KO_DOCKER_REPO=kind.local
deploy-kind-%: export HELM_STATIC_MANIFEST_IMAGE_FLAGS=--set image.pullPolicy=Never --set image.ko=true
deploy-kind-%: export RELEASE_VERSION=v0.0.0-dev
deploy-kind-%: ko start-kind render-static-manifests ## Apply the static manifests for the given variant
$(KO) apply -f manifests/rendered/kro-$*.yaml
kubectl wait --for=condition=ready --timeout=1m pod -n kro-system -l app.kubernetes.io/component=controller
$(KUBECTL) --context kind-${KIND_CLUSTER_NAME} get pods -A
.PHONY: ko-apply
ko-apply: ko
${HELM} template kro ./helm --namespace kro-system --set image.pullPolicy=Never --set image.ko=true | $(KO) apply -f -
## CLI
.PHONY: cli
cli:
go build -o bin/kro cmd/kro/main.go
sudo mv bin/kro /usr/local/bin
@echo "CLI built successfully"
##@ E2E Tests
.PHONY: test-e2e
test-e2e: chainsaw ## Run e2e tests
$(CHAINSAW) test ./test/e2e/chainsaw
.PHONY: test-e2e-kind-%
test-e2e-kind-%: deploy-kind-%
make test-e2e
# Default deployment uses helm deployments
.PHONY: deploy-kind
deploy-kind: export KO_DOCKER_REPO=kind.local
deploy-kind: ko deploy-kind-helm ## Deploy kro to a kind cluster
# Default end to end tests uses helm deployments
.PHONY: test-e2e-kind
test-e2e-kind: deploy-kind-helm