| gio | 2446de0 | 2025-04-11 11:20:39 +0400 | [diff] [blame] | 1 | docker_flags=--provenance=false --sbom=false |
| Giorgi Lekveishvili | 7366dbb | 2023-06-16 12:31:03 +0400 | [diff] [blame] | 2 | |
| 3 | # Image URL to use all building/pushing image targets |
| Giorgi Lekveishvili | 44f9d59 | 2023-06-21 14:58:19 +0400 | [diff] [blame] | 4 | IMG ?= giolekva/headscale-controller:latest |
| Giorgi Lekveishvili | 7366dbb | 2023-06-16 12:31:03 +0400 | [diff] [blame] | 5 | # ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary. |
| 6 | ENVTEST_K8S_VERSION = 1.24.2 |
| 7 | |
| 8 | # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) |
| 9 | ifeq (,$(shell go env GOBIN)) |
| 10 | GOBIN=$(shell go env GOPATH)/bin |
| 11 | else |
| 12 | GOBIN=$(shell go env GOBIN) |
| 13 | endif |
| 14 | |
| 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. |
| 17 | SHELL = /usr/bin/env bash -o pipefail |
| 18 | .SHELLFLAGS = -ec |
| 19 | |
| 20 | .PHONY: all |
| 21 | all: 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 |
| 37 | help: ## 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 |
| 43 | manifests: 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 |
| 47 | generate: 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 |
| 51 | fmt: ## Run go fmt against code. |
| 52 | go fmt ./... |
| 53 | |
| 54 | .PHONY: vet |
| 55 | vet: ## Run go vet against code. |
| 56 | go vet ./... |
| 57 | |
| 58 | .PHONY: test |
| 59 | test: manifests generate fmt vet envtest ## Run tests. |
| 60 | KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out |
| 61 | |
| 62 | ##@ Build |
| 63 | |
| 64 | .PHONY: build |
| 65 | build: generate fmt vet ## Build manager binary. |
| 66 | go build -o bin/manager main.go |
| 67 | |
| 68 | .PHONY: run |
| 69 | run: manifests generate fmt vet ## Run a controller from your host. |
| 70 | go run ./main.go --headscale-api=localhost:8081 |
| 71 | |
| Giorgi Lekveishvili | 2d843c3 | 2023-07-20 10:10:24 +0400 | [diff] [blame] | 72 | clean: |
| 73 | rm -rf manager_arm64 manager_amd64 |
| 74 | |
| 75 | build_arm64: export CGO_ENABLED=0 |
| 76 | build_arm64: export GO111MODULE=on |
| 77 | build_arm64: export GOOS=linux |
| 78 | build_arm64: export GOARCH=arm64 |
| 79 | build_arm64: |
| 80 | go build -a -o manager_arm64 main.go |
| 81 | |
| 82 | build_amd64: export CGO_ENABLED=0 |
| 83 | build_amd64: export GO111MODULE=on |
| 84 | build_amd64: export GOOS=linux |
| 85 | build_amd64: export GOARCH=amd64 |
| 86 | build_amd64: |
| 87 | go build -a -o manager_amd64 main.go |
| 88 | |
| Giorgi Lekveishvili | 7366dbb | 2023-06-16 12:31:03 +0400 | [diff] [blame] | 89 | .PHONY: docker-build |
| Giorgi Lekveishvili | 2d843c3 | 2023-07-20 10:10:24 +0400 | [diff] [blame] | 90 | docker-build: test clean build_arm64 build_amd64 ## Build docker image with the manager. |
| gio | 2446de0 | 2025-04-11 11:20:39 +0400 | [diff] [blame] | 91 | docker build --platform linux/arm64 --tag ${IMG}-arm64 $(docker_flags) . |
| 92 | docker build --platform linux/amd64 --tag ${IMG}-amd64 $(docker_flags) . |
| Giorgi Lekveishvili | 7366dbb | 2023-06-16 12:31:03 +0400 | [diff] [blame] | 93 | |
| 94 | .PHONY: docker-push |
| gio | 2446de0 | 2025-04-11 11:20:39 +0400 | [diff] [blame] | 95 | docker-push: docker-build |
| 96 | docker push ${IMG}-arm64 |
| 97 | docker push ${IMG}-amd64 |
| 98 | docker manifest create ${IMG} ${IMG}-arm64 ${IMG}-amd64 |
| 99 | docker manifest push --purge ${IMG} |
| Giorgi Lekveishvili | 7366dbb | 2023-06-16 12:31:03 +0400 | [diff] [blame] | 100 | |
| 101 | ##@ Deployment |
| 102 | |
| 103 | ifndef ignore-not-found |
| 104 | ignore-not-found = false |
| 105 | endif |
| 106 | |
| 107 | .PHONY: install |
| 108 | install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. |
| 109 | $(KUSTOMIZE) build config/crd | kubectl apply -f - |
| 110 | |
| 111 | .PHONY: uninstall |
| 112 | uninstall: 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. |
| 113 | $(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f - |
| 114 | |
| 115 | .PHONY: deploy |
| 116 | deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config. |
| 117 | cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} |
| 118 | $(KUSTOMIZE) build config/default | kubectl apply -f - |
| 119 | |
| 120 | .PHONY: undeploy |
| 121 | undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion. |
| 122 | $(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f - |
| 123 | |
| 124 | ##@ Build Dependencies |
| 125 | |
| 126 | ## Location to install dependencies to |
| 127 | LOCALBIN ?= $(shell pwd)/bin |
| 128 | $(LOCALBIN): |
| 129 | mkdir -p $(LOCALBIN) |
| 130 | |
| 131 | ## Tool Binaries |
| 132 | KUSTOMIZE ?= $(LOCALBIN)/kustomize |
| 133 | CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen |
| 134 | ENVTEST ?= $(LOCALBIN)/setup-envtest |
| 135 | |
| 136 | ## Tool Versions |
| 137 | KUSTOMIZE_VERSION ?= v3.8.7 |
| 138 | CONTROLLER_TOOLS_VERSION ?= v0.9.2 |
| 139 | |
| 140 | KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" |
| 141 | .PHONY: kustomize |
| 142 | kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. |
| 143 | $(KUSTOMIZE): $(LOCALBIN) |
| 144 | test -s $(LOCALBIN)/kustomize || { curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); } |
| 145 | |
| 146 | .PHONY: controller-gen |
| 147 | controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. |
| 148 | $(CONTROLLER_GEN): $(LOCALBIN) |
| 149 | test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION) |
| 150 | |
| 151 | .PHONY: envtest |
| 152 | envtest: $(ENVTEST) ## Download envtest-setup locally if necessary. |
| 153 | $(ENVTEST): $(LOCALBIN) |
| 154 | test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest |
| Giorgi Lekveishvili | 44f9d59 | 2023-06-21 14:58:19 +0400 | [diff] [blame] | 155 | |
| 156 | generate-helm-chart: manifests kustomize |
| 157 | cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} |
| 158 | $(KUSTOMIZE) build config/default | sed 's/: controller-system/: {{ .Release.Namespace }}/g' > ../../../charts/headscale-controller/templates/install.yaml |