blob: 2d11b7f4a081a959d6511cdc96d185d1757fa2df [file] [log] [blame]
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +04001
2# Image URL to use all building/pushing image targets
3IMG ?= giolekva/dns-ns-controller:latest
4# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
5ENVTEST_K8S_VERSION = 1.24.2
6
7# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
8ifeq (,$(shell go env GOBIN))
9GOBIN=$(shell go env GOPATH)/bin
10else
11GOBIN=$(shell go env GOBIN)
12endif
Giorgi Lekveishvilie58fc592023-12-07 13:24:07 +040013GOBIN=/usr/local/go/bin/go
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +040014
15# Setting SHELL to bash allows bash commands to be executed by recipes.
16# Options are set to exit when a recipe line exits non-zero or a piped command fails.
17SHELL = /usr/bin/env bash -o pipefail
18.SHELLFLAGS = -ec
19
20.PHONY: all
21all: build
22
23##@ General
24
25# The help target prints out all targets with their descriptions organized
26# beneath their categories. The categories are represented by '##@' and the
27# target descriptions by '##'. The awk commands is responsible for reading the
28# entire set of makefiles included in this invocation, looking for lines of the
29# file as xyz: ## something, and then pretty-format the target and help. Then,
30# if there's a line with ##@ something, that gets pretty-printed as a category.
31# More info on the usage of ANSI control characters for terminal formatting:
32# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
33# More info on the awk command:
34# http://linuxcommand.org/lc3_adv_awk.php
35
36.PHONY: help
37help: ## Display this help.
38 @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)
39
40##@ Development
41
42.PHONY: manifests
43manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
44 $(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
45
46.PHONY: generate
47generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
48 $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
49
50.PHONY: fmt
51fmt: ## Run go fmt against code.
Giorgi Lekveishvilie58fc592023-12-07 13:24:07 +040052 /usr/local/go/bin/go fmt ./...
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +040053
54.PHONY: vet
55vet: ## Run go vet against code.
Giorgi Lekveishvilie58fc592023-12-07 13:24:07 +040056 /usr/local/go/bin/go vet ./...
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +040057
58.PHONY: test
59test: manifests generate fmt vet envtest ## Run tests.
Giorgi Lekveishvilie58fc592023-12-07 13:24:07 +040060 KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" /usr/local/go/bin/go test ./... -coverprofile cover.out
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +040061
62##@ Build
63
64.PHONY: build
65build: generate fmt vet ## Build manager binary.
Giorgi Lekveishvilie58fc592023-12-07 13:24:07 +040066 /usr/local/go/bin/go build -o bin/manager *.go
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +040067
68.PHONY: run
69run: manifests generate fmt vet ## Run a controller from your host.
Giorgi Lekveishvilie58fc592023-12-07 13:24:07 +040070 /usr/local/go/bin/go run ./*.go
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +040071
72clean:
73 rm -rf manager_arm64 manager_amd64
74
75build_arm64: export CGO_ENABLED=0
76build_arm64: export GO111MODULE=on
77build_arm64: export GOOS=linux
78build_arm64: export GOARCH=arm64
79build_arm64:
Giorgi Lekveishvilie58fc592023-12-07 13:24:07 +040080 /usr/local/go/bin/go build -a -o manager_arm64 *.go
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +040081
82build_amd64: export CGO_ENABLED=0
83build_amd64: export GO111MODULE=on
84build_amd64: export GOOS=linux
85build_amd64: export GOARCH=amd64
86build_amd64:
Giorgi Lekveishvilie58fc592023-12-07 13:24:07 +040087 /usr/local/go/bin/go build -a -o manager_amd64 *.go
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +040088
89.PHONY: docker-build
90docker-build: clean build_arm64 build_amd64 ## Build docker image with the manager.
91 podman build --platform linux/arm64 --tag ${IMG}-arm64 .
92 podman build --platform linux/amd64 --tag ${IMG}-amd64 .
93
94.PHONY: docker-push
95docker-push: ## Push docker image with the manager.
96 podman push ${IMG}-arm64
97 podman push ${IMG}-amd64
98 podman manifest create ${IMG} ${IMG}-arm64 ${IMG}-amd64
99 podman manifest push ${IMG} docker://docker.io/${IMG}
100 podman manifest rm ${IMG}
101
102##@ Deployment
103
104ifndef ignore-not-found
105 ignore-not-found = false
106endif
107
108.PHONY: install
109install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
110 $(KUSTOMIZE) build config/crd | kubectl apply -f -
111
112.PHONY: uninstall
113uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
114 $(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
115
116.PHONY: deploy
117deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
118 cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
119 $(KUSTOMIZE) build config/default | kubectl apply -f -
120
121.PHONY: undeploy
122undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
123 $(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
124
125##@ Build Dependencies
126
127## Location to install dependencies to
128LOCALBIN ?= $(shell pwd)/bin
129$(LOCALBIN):
130 mkdir -p $(LOCALBIN)
131
132## Tool Binaries
133KUSTOMIZE ?= $(LOCALBIN)/kustomize
134CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
135ENVTEST ?= $(LOCALBIN)/setup-envtest
136
137## Tool Versions
138KUSTOMIZE_VERSION ?= v3.8.7
139CONTROLLER_TOOLS_VERSION ?= v0.9.2
140
141KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
142.PHONY: kustomize
143kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
144$(KUSTOMIZE): $(LOCALBIN)
145 test -s $(LOCALBIN)/kustomize || { curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }
146
147.PHONY: controller-gen
148controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
149$(CONTROLLER_GEN): $(LOCALBIN)
Giorgi Lekveishvilie58fc592023-12-07 13:24:07 +0400150 test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) /usr/local/go/bin/go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +0400151
152.PHONY: envtest
153envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
154$(ENVTEST): $(LOCALBIN)
Giorgi Lekveishvilie58fc592023-12-07 13:24:07 +0400155 test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) /usr/local/go/bin/go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +0400156
157generate-helm-chart: manifests kustomize
158 $(KUSTOMIZE) build config/crd > ../../charts/dns-ns-controller/templates/crds.yaml
159 cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
160 $(KUSTOMIZE) build config/default | sed 's/: ns-controller-system/: {{ .Release.Namespace }}/g' > ../../charts/dns-ns-controller/templates/install.yaml