-
Notifications
You must be signed in to change notification settings - Fork 5
/
Makefile
235 lines (180 loc) · 9.67 KB
/
Makefile
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
# Image URL to use all building/pushing image targets
REGISTRY ?= <YOUR REGISTRY>
IMAGE_NAME ?= cluster-api-cox-controller:<YOUR TAG>
IMG ?= $(REGISTRY)/$(IMAGE_NAME)
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
# Allow overriding the imagePullPolicy
PULL_POLICY ?= Always
GO_INSTALL = ./scripts/go_install.sh
TOOLS_BIN := bin
TOOLS_BIN_DIR := $(strip $(abspath $(TOOLS_BIN)))
KUSTOMIZE_VER := v4.5.7
KUSTOMIZE_BIN := kustomize
KUSTOMIZE := $(TOOLS_BIN_DIR)/$(KUSTOMIZE_BIN)-$(KUSTOMIZE_VER)
CONTROLLER_GEN_VER := v0.10.0
CONTROLLER_GEN_BIN := controller-gen
CONTROLLER_GEN := $(TOOLS_BIN_DIR)/$(CONTROLLER_GEN_BIN)-$(CONTROLLER_GEN_VER)
CONVERSION_GEN_VER := v0.25.0
CONVERSION_GEN_BIN := conversion-gen
CONVERSION_GEN := $(TOOLS_BIN_DIR)/$(CONVERSION_GEN_BIN)-$(CONVERSION_GEN_VER)
$(KUSTOMIZE): ## Build kustomize from tools folder.
GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) sigs.k8s.io/kustomize/kustomize/v4 $(KUSTOMIZE_BIN) $(KUSTOMIZE_VER)
$(CONTROLLER_GEN): ## Build controller-gen from tools folder.
GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) sigs.k8s.io/controller-tools/cmd/controller-gen $(CONTROLLER_GEN_BIN) $(CONTROLLER_GEN_VER)
$(CONVERSION_GEN): ## Build conversion-gen.
GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) k8s.io/code-generator/cmd/conversion-gen $(CONVERSION_GEN_BIN) $(CONVERSION_GEN_VER)
# 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
# Setting SHELL to bash allows bash commands to be executed by recipes.
# This is a requirement for 'setup-envtest.sh' in the test target.
# 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
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 commands 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
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
manifests: generate $(CONTROLLER_GEN) ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
generate: $(CONTROLLER_GEN) ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
.PHONY: verify
verify: ## Run all static analysis checks.
# Check if codebase is formatted.
@bash -c "[ -z \"$$(gofmt -l . | grep -v '^vendor')\" ] && echo 'OK' || (echo 'ERROR: files are not formatted:' && gofmt -l . | grep -v '^vendor' && echo -e \"\nRun 'make format' or manually fix the formatting issues.\n\" && false)"
# Run static checks on codebase.
go vet .
.PHONY: format
format: ## Run all formatters.
# Format the Go codebase.
gofmt -w -s .
# Format the go.mod file.
go mod tidy
.PHONY: verify-generate
verify-generate: ## Verify that all code generation is up to date
hack/verify-codegen.sh
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: generate manifests verify ## Run tests.
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh
# Note: explicitly setting the GOARCH to fix the bugged setup-envtest.sh for Apple M1.
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; export GOARCH=amd64; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
.PHONY: clean
clean: docker-clean ## Clean up build-generated artifacts.
rm -rf bin/
rm -rf build/
##@ Build
build: generate verify ## Build manager binary.
go build -o bin/manager main.go
run: manifests generate ## Run a controller from your host.
go run ./main.go
##@ Docker
docker-build: ## Build docker image with the manager.
DOCKER_BUILDKIT=1 docker build -t ${IMG} .
docker-push: ## Push docker image with the manager.
docker push ${IMG}
.PHONY: docker-clean
docker-clean:
docker rmi ${IMG} || true
##@ Deployment
install: manifests $(KUSTOMIZE) ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl apply -f -
uninstall: manifests $(KUSTOMIZE) ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl delete -f -
deploy: manifests $(KUSTOMIZE) ## Deploy controller to the K8s cluster specified in ~/.kube/config.
$(MAKE) set-manifest-image MANIFEST_IMG=$(IMG)
$(KUSTOMIZE) build config/default | kubectl apply -f -
git restore config/default/manager_image_patch.yaml || true # Clean up changes made by kustomize edit.
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/default | kubectl delete -f -
.PHONY: manifest-build
manifest-build: kustomize
mkdir -p build
$(MAKE) set-manifest-image MANIFEST_IMG=$(IMG)
$(KUSTOMIZE) build config/default > $(MANIFEST_BUILD_PATH)
git restore config/default/manager_image_patch.yaml || true # Clean up changes made by kustomize edit.
.PHONY: tools
tools: $(CONTROLLER_GEN) $(KUSTOMIZE)
##@ Release commands
RELEASE_TAG := $(shell git describe --abbrev=0 2>/dev/null)
RELEASE_DIR := build/releases
$(RELEASE_DIR):
mkdir -p $(RELEASE_DIR)/
.PHONY: release
release: clean-release ## Builds and push container images using the latest git tag for the commit.
@if [ -z "${RELEASE_TAG}" ]; then echo "RELEASE_TAG is not set"; exit 1; fi
@if ! [ -z "$$(git status --porcelain)" ]; then echo "Your local git repository contains uncommitted changes, use git clean before proceeding."; exit 1; fi
git checkout "${RELEASE_TAG}"
$(MAKE) set-manifest-image MANIFEST_IMG=$(IMG)
$(MAKE) set-manifest-pull-policy PULL_POLICY=IfNotPresent
$(MAKE) release-manifests
#$(MAKE) release-templates
.PHONY: release-manifests
release-manifests: $(KUSTOMIZE) $(RELEASE_DIR) ## Builds the manifests to publish with a release
@if [ -z "${RELEASE_TAG}" ]; then echo "RELEASE_TAG is not set"; exit 1; fi
$(MAKE) set-manifest-image MANIFEST_IMG=$(IMG)
$(MAKE) set-manifest-pull-policy PULL_POLICY=IfNotPresent
cp metadata.yaml $(RELEASE_DIR)/metadata.yaml
$(KUSTOMIZE) build config/default > $(RELEASE_DIR)/infrastructure-components.yaml
git restore config/default/manager_image_patch.yaml || true # Clean up changes made by kustomize edit.
.PHONY: release-manifests-clusterctl
release-manifests-clusterctl: ## Create the releases directory to conform with clusterctl provider contract for a local provider
@if [ -z "${RELEASE_TAG}" ]; then echo "RELEASE_TAG is not set"; exit 1; fi
$(MAKE) release-manifests RELEASE_DIR=$(RELEASE_DIR)/infrastructure-cox/latest RELEASE_TAG=latest
$(MAKE) release-manifests RELEASE_DIR=$(RELEASE_DIR)/infrastructure-cox/$(RELEASE_TAG)
.PHONY: release-templates
release-templates: $(RELEASE_DIR)
cp templates/cluster-template* $(RELEASE_DIR)/
.PHONY: release-staging
release-staging: ## Builds and push container images to the staging registry.
REGISTRY=$(STAGING_REGISTRY) $(MAKE) docker-build-all docker-push-all release-alias-tag
.PHONY: set-manifest-image
set-manifest-image:
$(info Updating kustomize image patch file for default resource)
sed -i'' -e 's@image: .*@image: '"$(MANIFEST_IMG)"'@' ./config/default/manager_image_patch.yaml
rm -f ./config/default/manager_image_patch.yaml-e # Needed for MacOS sed
.PHONY: set-manifest-pull-policy
set-manifest-pull-policy:
$(info Updating kustomize pull policy file for default resource)
sed -i'' -e 's@imagePullPolicy: .*@imagePullPolicy: '"$(PULL_POLICY)"'@' ./config/default/manager_pull_policy.yaml
rm -f ./config/default/manager_pull_policy.yaml-e # Needed for MacOS sed
.PHONY: clean-release
clean-release: ## Remove the release folder
rm -rf $(RELEASE_DIR)
##@ Kind commands
KIND_CLUSTER_NAME=kind
KIND_IMG=cluster-api-cox-controller:kind
KIND_CONTROLLER_NAMESPACE=capc-system
KIND_KUBECONFIG_PATH=build/kind-$(KIND_CLUSTER_NAME).kubeconfig
.PHONY: kind-kubeconfig
kind-kubeconfig:
mkdir -p build
kind get kubeconfig --name $(KIND_CLUSTER_NAME) > $(KIND_KUBECONFIG_PATH)
.PHONY: kind-deploy
kind-deploy: docker-build kind-kubeconfig ## Build and deploy an image into a local KinD cluster
# Verify that the "kind" cluster exists
kind get clusters | grep -q $(KIND_CLUSTER_NAME)
# Rename the image to avoid conflicts
docker tag $(IMG) $(KIND_IMG)
# Alternative 'kind load', because it is broken for Apple M1 in some cases.
docker save $(KIND_IMG) | docker exec --privileged -i $(KIND_CLUSTER_NAME)-control-plane ctr --namespace=k8s.io images import --all-platforms -
# Update the deployment
$(MAKE) deploy KUBECONFIG=$(KIND_KUBECONFIG_PATH) IMG=$(KIND_IMG)
# Recreate the pods to ensure that the newer image is used
kubectl --kubeconfig=$(KIND_KUBECONFIG_PATH) -n $(KIND_CONTROLLER_NAMESPACE) delete pod --all || true