-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
272 lines (214 loc) · 8.6 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
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
# Copyright 2022.
#
# 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.
# If you update this file, please follow
# https://suva.sh/posts/well-documented-makefiles
# Ensure Make is run with bash shell as some syntax below is bash-specific
SHELL := /usr/bin/env bash
.DEFAULT_GOAL := help
#
# Go.
#
GO_VERSION ?= 1.22.5
# 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
# go-get-tool will 'go get' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-get-tool
@[ -f $(1) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef
# Directories
ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
BIN_DIR := $(ROOT_DIR)/bin
TOOLS_DIR := $(ROOT_DIR)/hack/tools
TOOLS_BIN_DIR := $(TOOLS_DIR)/bin
export PATH := $(abspath $(TOOLS_BIN_DIR)):$(PATH)
# Allow overriding manifest generation destination directory
MANIFEST_ROOT ?= ./config
CRD_ROOT ?= $(MANIFEST_ROOT)/crd
RBAC_ROOT ?= $(MANIFEST_ROOT)/rbac
RELEASE_DIR := out
BUILD_DIR := .build
# Architecture variables
IMAGE_TAG ?= dev
ARCH ?= amd64
ALL_ARCH = amd64 arm64
# Set build time variables including version details
LDFLAGS := $(shell hack/version.sh)
# Common docker variables
IMAGE_NAME ?= cape-ip-manager
PULL_POLICY ?= Always
# Release docker variables
REGISTRY ?= docker.io/smartxworks
CONTROLLER_IMG ?= $(REGISTRY)/$(IMAGE_NAME)
## --------------------------------------
## 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)
## --------------------------------------
## Testing
## --------------------------------------
test: generate ## Run tests.
source ./hack/fetch_ext_bins.sh; fetch_tools; setup_envs; go test -v ./controllers/... ./pkg/... -coverprofile=cover.out
## --------------------------------------
## Tooling Binaries
## --------------------------------------
KUSTOMIZE = $(shell pwd)/bin/kustomize
kustomize: ## Download kustomize locally if necessary.
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
controller-gen: ## Download controller-gen locally if necessary.
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
GINKGO := $(shell pwd)/bin/ginkgo
ginkgo: ## Download ginkgo locally if necessary.
$(call go-get-tool,$(GINKGO),github.com/onsi/ginkgo/v2/[email protected])
KIND := $(shell pwd)/bin/kind
kind: ## Download kind locally if necessary.
$(call go-get-tool,$(KIND),sigs.k8s.io/[email protected])
GOLANGCI_LINT := $(shell pwd)/bin/golangci-lint
golangci-lint: ## Download golangci-lint locally if necessary.
$(call go-get-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/[email protected])
## --------------------------------------
## Linting and fixing linter errors
## --------------------------------------
.PHONY: lint
lint: golangci-lint ## Lint codebase
$(GOLANGCI_LINT) run -v --fast=false
.PHONY: lint-fix
lint-fix: $(GOLANGCI_LINT) ## Lint the codebase and run auto-fixers if supported by the linter.
GOLANGCI_LINT_EXTRA_ARGS=--fix $(MAKE) lint
## --------------------------------------
## Generate
## --------------------------------------
.PHONY: generate
generate: ## Generate code
$(MAKE) generate-go
$(MAKE) generate-manifests
.PHONY: generate-go
generate-go: controller-gen ## Runs Go related generate targets
go generate ./...
$(CONTROLLER_GEN) \
paths=./... \
object:headerFile=./hack/boilerplate.go.txt
.PHONY: generate-manifests
generate-manifests: controller-gen ## Generate manifests e.g. CRD, RBAC etc.
$(CONTROLLER_GEN) \
paths=./... \
crd:crdVersions=v1 \
output:crd:dir=$(CRD_ROOT) \
webhook
$(CONTROLLER_GEN) \
paths=./controllers/... \
output:rbac:dir=$(RBAC_ROOT) \
rbac:roleName=manager-role
## --------------------------------------
## Release
## --------------------------------------
$(RELEASE_DIR):
@mkdir -p $(RELEASE_DIR)
$(BUILD_DIR):
@mkdir -p $(BUILD_DIR)
.PHONY: release
release: clean-release
$(MAKE) docker-build-all
$(MAKE) docker-push-all
$(MAKE) release-manifests
.PHONY: release-manifests
release-manifests:
$(MAKE) manifests MANIFEST_DIR=$(RELEASE_DIR) PULL_POLICY=IfNotPresent IMAGE=$(CONTROLLER_IMG):$(IMAGE_TAG)
.PHONY: manifests
manifests: $(MANIFEST_DIR) $(BUILD_DIR) kustomize
rm -rf $(BUILD_DIR)/config
cp -R config $(BUILD_DIR)
sed -i'' -e 's@imagePullPolicy: .*@imagePullPolicy: '"$(PULL_POLICY)"'@' $(BUILD_DIR)/config/default/manager_pull_policy.yaml
sed -i'' -e 's@image: .*@image: '"$(IMAGE)"'@' $(BUILD_DIR)/config/default/manager_image_patch.yaml
$(KUSTOMIZE) build $(BUILD_DIR)/config/default > $(MANIFEST_DIR)/static-ip-components.yaml
cp metadata.yaml $(MANIFEST_DIR)/metadata.yaml
## --------------------------------------
## Cleanup / Verification
## --------------------------------------
.PHONY: clean-release
clean-release: ## Remove the release folder
rm -rf $(RELEASE_DIR)
## --------------------------------------
## Development
## --------------------------------------
.PHONY: build
build: generate ## Build manager binary.
go build -o bin/manager main.go
.PHONY: run
run: generate ## Run a controller from your host.
go run ./main.go --leader-election-namespace default -v 2
.PHONY: install
install: generate kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl apply -f -
.PHONY: uninstall
uninstall: generate kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl delete -f -
.PHONY: deploy
deploy: generate kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
sed -i'' -e 's@image: .*@image: '"$(CONTROLLER_IMG):$(IMAGE_TAG)"'@' config/default/manager_image_patch.yaml
$(KUSTOMIZE) build config/default | kubectl apply -f -
.PHONY: undeploy
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/default | kubectl delete -f -
## --------------------------------------
## Docker
## --------------------------------------
.PHONY: docker-build
docker-build: docker-pull-prerequisites ## Build the docker image for controller-manager
docker build --build-arg ARCH=$(ARCH) --build-arg ldflags="$(LDFLAGS)" . -t $(CONTROLLER_IMG)-$(ARCH):$(IMAGE_TAG)
.PHONY: docker-push
docker-push: ## Push the docker image
docker push $(CONTROLLER_IMG)-$(ARCH):$(IMAGE_TAG)
.PHONY: docker-pull-prerequisites
docker-pull-prerequisites:
docker pull docker.io/docker/dockerfile:1.4
docker pull gcr.io/distroless/static:latest
## --------------------------------------
## Docker — All ARCH
## --------------------------------------
.PHONY: docker-build-all ## Build all the architecture docker images
docker-build-all: $(addprefix docker-build-,$(ALL_ARCH))
docker-build-%:
$(MAKE) ARCH=$* docker-build
.PHONY: docker-push-all ## Push all the architecture docker images
docker-push-all: $(addprefix docker-push-,$(ALL_ARCH))
$(MAKE) docker-push-manifest
docker-push-%:
$(MAKE) ARCH=$* docker-push
.PHONY: docker-push-manifest
docker-push-manifest: ## Push the fat manifest docker image.
## Minimum docker version 18.06.0 is required for creating and pushing manifest images.
docker manifest create --amend $(CONTROLLER_IMG):$(IMAGE_TAG) $(shell echo $(ALL_ARCH) | sed -e "s~[^ ]*~$(CONTROLLER_IMG)\-&:$(IMAGE_TAG)~g")
@for arch in $(ALL_ARCH); do docker manifest annotate --arch $${arch} ${CONTROLLER_IMG}:${IMAGE_TAG} ${CONTROLLER_IMG}-$${arch}:${IMAGE_TAG}; done
docker manifest push --purge ${CONTROLLER_IMG}:${IMAGE_TAG}
## --------------------------------------
## Helpers
## --------------------------------------
##@ helpers:
go-version: ## Print the go version we use to compile our binaries and images
@echo $(GO_VERSION)