This repository was archived by the owner on Dec 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMakefile
More file actions
326 lines (259 loc) · 14.2 KB
/
Makefile
File metadata and controls
326 lines (259 loc) · 14.2 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
# VERSION defines the project version.
# Update this value when you upgrade the version of your project.
VERSION ?= 0.2.0
# IMAGE_TAG_BASE defines the docker.io namespace and part of the image name for remote images.
IMAGE_TAG_BASE ?= project-flotta.io/flotta-operator
TEST_IMAGE ?= quay.io/project-flotta/edgedevice:latest
RELEASE_REPO ?= project-flotta/flotta-operator
SKIP_TEST_IMAGE_PULL ?= false
# Image URL to use all building/pushing image targets
IMG ?= controller:latest
HTTP_IMG ?= edge-api:latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
# Cluster type - k8s/ocp/all
TARGET ?= k8s
# Host name for ingress creation
HOST ?= flotta-operator.srv
# Docker command to use, can be podman
DOCKER ?= docker
# Kubectl command
KUBECTL ?= kubectl
# for cert-installer
CERT_MANAGER_VERSION ?= v1.7.1
OS = $(shell go env GOOS)
ARCH = $(shell go env GOARCH)
# 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
# Default Flotta-operator namespace
FLOTTA_OPERATOR_NAMESPACE ?= "flotta"
# Set quiet mode by default
Q=@
# 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%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
##@ Development
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(Q)$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
generate-tools:
ifeq (, $(shell which mockery))
(cd /tmp && go install github.com/vektra/mockery/...@v1.1.2)
endif
ifeq (, $(shell which mockgen))
(cd /tmp/ && go install github.com/golang/mock/mockgen@v1.6.0)
endif
@exit
generate: generate-tools controller-gen validate-swagger generate-from-swagger ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
go generate ./...
generate-%:
./hack/generate.sh generate_$(subst -,_,$*)
fmt: ## Run go fmt against code.
go fmt ./...
vet: ## Run go vet against code.
go vet ./...
gosec: ## Run gosec locally
$(DOCKER) run --rm -v $(PWD):/opt/data/:z docker.io/securego/gosec -exclude-generated /opt/data/...
GO_IMAGE=golang:1.17.8-alpine3.14
GOIMPORTS_IMAGE=golang.org/x/tools/cmd/goimports@latest
FILES_LIST=$(shell ls -d */ | grep -v -E "vendor|tools|test|client|restapi|models|generated")
MODULE_NAME=$(shell head -n 1 go.mod | cut -d '/' -f 3)
imports: ## fix and format go imports
@# Removes blank lines within import block so that goimports does its magic in a deterministic way
find $(FILES_LIST) -type f -name "*.go" | xargs -L 1 sed -i '/import (/,/)/{/import (/n;/)/!{/^$$/d}}'
$(DOCKER) run --rm -v $(CURDIR):$(CURDIR):z -w="$(CURDIR)" $(GO_IMAGE) \
sh -c 'go install $(GOIMPORTS_IMAGE) && goimports -w -local github.com/project-flotta $(FILES_LIST) && goimports -w -local github.com/project-flotta/$(MODULE_NAME) $(FILES_LIST)'
LINT_IMAGE=golangci/golangci-lint:v1.45.0
lint: ## Check if the go code is properly written, rules are in .golangci.yml
$(DOCKER) run --rm -v $(CURDIR):$(CURDIR) -w="$(CURDIR)" $(LINT_IMAGE) sh -c 'golangci-lint run'
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: ## Run tests.
test: GINKGO_OPTIONS ?= --skip e2e
test: manifests pre-build test-fast
integration-test: ginkgo get-certs
ifeq ($(SKIP_TEST_IMAGE_PULL), false)
$(DOCKER) pull $(TEST_IMAGE)
endif
$(GINKGO) -focus=$(FOCUS) run test/e2e
TEST_PACKAGES := ./...
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test-fast: ginkgo
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
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); $(GINKGO) --cover -output-dir=. -coverprofile=cover.out -v -progress $(GINKGO_OPTIONS) $(TEST_PACKAGES)
test-create-coverage:
sed -i '/mock_/d' cover.out
sed -i '/zz_generated/d' cover.out
go tool cover -func cover.out
go tool cover --html=cover.out -o coverage.html
test-coverage:
go tool cover --html=cover.out
vendor:
go mod tidy -go=1.17
go mod vendor
get-certs: # Write certificates to /tmp/ folder
kubectl get secret -n flotta flotta-ca -o go-template='{{ index .data "ca.crt" | base64decode}}' >/tmp/ca.pem
$(eval REG_SECRET_NAME := $(shell kubectl get secrets -n flotta -l reg-client-ca=true --sort-by=.metadata.creationTimestamp | tail -1 | awk '{print $$1}'))
kubectl get secret -n flotta $(REG_SECRET_NAME) -o go-template='{{ index .data "client.crt" | base64decode}}' > /tmp/cert.pem
kubectl get secret -n flotta $(REG_SECRET_NAME) -o go-template='{{ index .data "client.key" | base64decode}}' > /tmp/key.pem
check-certs: # Check cert subject
openssl x509 -noout -in /tmp/cert.pem --subject
##@ Build
pre-build: ## Generate code, format it and organize imports before executing build
pre-build: generate fmt imports vet
build: pre-build ## Build manager binary.
go build -mod=vendor -o bin/manager cmd/manager/main.go
fast-build: generate ## Fast build manager binary for local dev.
go build -mod=vendor -o bin/manager cmd/manager/main.go
run: manifests pre-build ## Run a controller from your host.
$(Q) kubectl create ns $(FLOTTA_OPERATOR_NAMESPACE) 2> /dev/null || exit 0
OBC_AUTO_CREATE=false ENABLE_WEBHOOKS=false LOG_LEVEL=debug go run -mod=vendor cmd/manager/main.go
http-api-run: ## Run HTTP API in localhost
METRICS_ADDR=":8089" go run cmd/httpapi/main.go
docker-build: ## Build docker image with the manager.
$(DOCKER) build -f build/manager/Dockerfile -t ${IMG} .
$(DOCKER) build -f build/httpapi/Dockerfile -t ${HTTP_IMG} .
docker-push: ## Push docker image with the manager.
$(DOCKER) push ${IMG}
$(DOCKER) push ${HTTP_IMG}
release: ## Release the operator in github releases, tagged by its version.
release: gen-manifests
gh release create v$(VERSION) --notes "Release v$(VERSION) of Flotta Operator"\
--repo=$(RELEASE_REPO) --title "Release v$(VERSION)"\
'$(TMP_ODIR)/ocp-flotta-operator.yaml# Flotta Operator for OCP'\
'$(TMP_ODIR)/k8s-flotta-operator.yaml# Flotta Operator for kubernetes'
$(Q)rm -rf $(TMP_ODIR)
##@ 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: ## Deploy controller to the K8s cluster specified in ~/.kube/config.
deploy: gen-manifests install-cert-manager
kubectl apply -f $(TMP_ODIR)/$(TARGET)-flotta-operator.yaml
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
ifeq ($(TARGET), k8s)
$(KUSTOMIZE) build config/k8s | kubectl delete -f -
else ifeq ($(TARGET), ocp)
$(KUSTOMIZE) build config/ocp | kubectl delete -f -
endif
kubectl delete -f https://github.com/cert-manager/cert-manager/releases/download/${CERT_MANAGER_VERSION}/cert-manager.yaml
$(eval TMP_ODIR := $(shell mktemp -d))
gen-manifests: manifests kustomize ## Generates manifests for deploying the operator into $(TARGET)-flotta-operator.yaml
$(Q)cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(Q)cd config/edge-api && $(KUSTOMIZE) edit set image edge-api=${HTTP_IMG}
ifneq (,$(filter $(TARGET), k8s all))
$(Q)$(KUSTOMIZE) build config/k8s > $(TMP_ODIR)/k8s-flotta-operator.yaml
$(Q)echo -e "\033[92mDeployment file: $(TMP_ODIR)/k8s-flotta-operator.yaml\033[0m"
endif
ifneq (,$(filter $(TARGET), ocp all))
$(Q)$(KUSTOMIZE) build config/ocp > $(TMP_ODIR)/ocp-flotta-operator.yaml
$(Q)echo -e "\033[92mDeployment file: $(TMP_ODIR)/ocp-flotta-operator.yaml\033[0m"
endif
$(Q)cd config/manager && $(KUSTOMIZE) edit set image controller=quay.io/project-flotta/flotta-operator
$(Q)cd config/edge-api && $(KUSTOMIZE) edit set image edge-api=quay.io/project-flotta/flotta-edge-api
install-router: ## Install openshift router
install-router:
$(KUBECTL) apply -f https://raw.githubusercontent.com/openshift/router/master/deploy/router_rbac.yaml
$(KUBECTL) apply -f https://raw.githubusercontent.com/openshift/router/master/deploy/route_crd.yaml
$(KUBECTL) apply -f https://raw.githubusercontent.com/openshift/router/master/deploy/router.yaml
$(KUBECTL) wait --for=condition=Ready pods --all -n openshift-ingress --timeout=60s
install-cert-manager: ## Install cert-manager dependency
install-cert-manager: cmctl
$(KUBECTL) apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.7.1/cert-manager.yaml
${CMCTL} check api --wait=5m
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
controller-gen: ## Download controller-gen locally if necessary.
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.4.1)
KUSTOMIZE = $(shell pwd)/bin/kustomize
kustomize: ## Download kustomize locally if necessary.
$(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v4@latest)
GINKGO = $(shell pwd)/bin/ginkgo
ginkgo: ## Download ginkgo locally if necessary.
ifeq (, $(shell which ginkgo 2> /dev/null))
$(call go-install-tool,$(GINKGO),github.com/onsi/ginkgo/v2/ginkgo@v2.1.3)
endif
CLIENTGEN = $(shell pwd)/bin/client-gen
client-gen: ## Download client-gen locally if necessary.
$(call go-install-tool,$(CLIENTGEN),k8s.io/code-generator/cmd/client-gen@v0.23.5)
LISTERGEN = $(shell pwd)/bin/lister-gen
lister-gen: ## Download lister-gen locally if necessary.
$(call go-install-tool,$(LISTERGEN),k8s.io/code-generator/cmd/lister-gen@v0.23.5)
INFORMERGEN = $(shell pwd)/bin/informer-gen
informer-gen: ## Download client-gen locally if necessary.
$(call go-install-tool,$(INFORMERGEN),k8s.io/code-generator/cmd/informer-gen@v0.23.5)
k8s-client-gen: client-gen lister-gen informer-gen ## Generate typed client for flotta project.
mkdir gen-tmp
$(CLIENTGEN) --clientset-name versioned --input-base "" --input github.com/project-flotta/flotta-operator/api//v1alpha1 \
--output-package github.com/project-flotta/flotta-operator/generated/clientset \
--output-base gen-tmp --go-header-file hack/boilerplate.go.txt --v 10
# since the api folder structure has no group folder, the client fails to generate client with group prefix and needs to be renamed
mv gen-tmp/github.com/project-flotta/flotta-operator/generated/clientset/versioned/typed/v1alpha1/_client.go gen-tmp/github.com/project-flotta/flotta-operator/generated/clientset/versioned/typed/v1alpha1/client.go
$(LISTERGEN) --input-dirs github.com/project-flotta/flotta-operator/api/v1alpha1 \
--output-package github.com/project-flotta/flotta-operator/generated/listers \
--output-base gen-tmp --go-header-file hack/boilerplate.go.txt --v 10
$(INFORMERGEN) --input-dirs github.com/project-flotta/flotta-operator/api/v1alpha1 \
--versioned-clientset-package github.com/project-flotta/flotta-operator/generated/clientset/versioned \
--listers-package github.com/project-flotta/flotta-operator/generated/listers \
--output-package github.com/project-flotta/flotta-operator/generated/informers \
--output-base gen-tmp --go-header-file hack/boilerplate.go.txt --v 10
rm -rf generated
mv gen-tmp/github.com/project-flotta/flotta-operator/generated ./
rm -rf gen-tmp
validate-swagger: ## Validate swagger
$(DOCKER) run --rm -v $(PWD)/.spectral.yaml:/tmp/.spectral.yaml:z -v $(PWD)/swagger.yaml:/tmp/swagger.yaml:z -v $(PWD)/swagger-backend.yaml:/tmp/swagger-backend.yaml:z stoplight/spectral lint --ruleset "/tmp/.spectral.yaml" /tmp/swagger.yaml /tmp/swagger-backend.yaml
generate-agent-install-ostree:
sed -e "/<CA_PEM>/r /tmp/ca.pem" -e '/<CA_PEM>/d' \
-e "/<CERT_PEM>/r /tmp/cert.pem" -e '/<CERT_PEM>/d' \
-e "/<KEY_PEM>/r /tmp/key.pem" -e '/<KEY_PEM>/d' \
-e "/<CONFIG_TOML>/r hack/config.toml" -e '/<CONFIG_TOML>/d' \
hack/install-agent-rpm-ostree.sh.template > hack/install-agent-rpm-ostree.sh
chmod +x hack/install-agent-rpm-ostree.sh
generate-agent-install-dnf:
sed -e "/<CA_PEM>/r /tmp/ca.pem" -e '/<CA_PEM>/d' \
-e "/<CERT_PEM>/r /tmp/cert.pem" -e '/<CERT_PEM>/d' \
-e "/<KEY_PEM>/r /tmp/key.pem" -e '/<KEY_PEM>/d' \
-e "/<CONFIG_TOML>/r hack/config.toml" -e '/<CONFIG_TOML>/d' \
hack/install-agent-dnf.sh.template > hack/install-agent-dnf.sh
chmod +x hack/install-agent-dnf.sh
agent-install-scripts: get-certs generate-agent-install-ostree generate-agent-install-dnf
CMCTL = $(shell pwd)/bin/cmctl
.PHONY: cmctl
cmctl: ## Download cmctl locally if necessary.
ifeq ("$(wildcard $(CMCTL))","")
curl -sSL -o /tmp/cmctl.tar.gz https://github.com/cert-manager/cert-manager/releases/download/${CERT_MANAGER_VERSION}/cmctl-${OS}-${ARCH}.tar.gz
tar xzf /tmp/cmctl.tar.gz -C $(shell pwd)/bin/ cmctl
endif
# go-install-tool will 'go install' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-install-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