Makefile 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Image URL to use all building/pushing image targets
  2. IMG ?= controller:latest
  3. # ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
  4. ENVTEST_K8S_VERSION = 1.24.2
  5. # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
  6. ifeq (,$(shell go env GOBIN))
  7. GOBIN=$(shell go env GOPATH)/bin
  8. else
  9. GOBIN=$(shell go env GOBIN)
  10. endif
  11. # Setting SHELL to bash allows bash commands to be executed by recipes.
  12. # Options are set to exit when a recipe line exits non-zero or a piped command fails.
  13. SHELL = /usr/bin/env bash -o pipefail
  14. .SHELLFLAGS = -ec
  15. .PHONY: all
  16. all: build
  17. ##@ General
  18. # The help target prints out all targets with their descriptions organized
  19. # beneath their categories. The categories are represented by '##@' and the
  20. # target descriptions by '##'. The awk commands is responsible for reading the
  21. # entire set of makefiles included in this invocation, looking for lines of the
  22. # file as xyz: ## something, and then pretty-format the target and help. Then,
  23. # if there's a line with ##@ something, that gets pretty-printed as a category.
  24. # More info on the usage of ANSI control characters for terminal formatting:
  25. # https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
  26. # More info on the awk command:
  27. # http://linuxcommand.org/lc3_adv_awk.php
  28. .PHONY: help
  29. help: ## Display this help.
  30. @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)
  31. ##@ Development
  32. .PHONY: manifests
  33. manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
  34. $(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
  35. .PHONY: generate
  36. generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
  37. $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
  38. .PHONY: fmt
  39. fmt: ## Run go fmt against code.
  40. go fmt ./...
  41. .PHONY: vet
  42. vet: ## Run go vet against code.
  43. go vet ./...
  44. .PHONY: test
  45. test: manifests generate fmt vet envtest ## Run tests.
  46. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
  47. ##@ Build
  48. .PHONY: build
  49. build: generate fmt vet ## Build manager binary.
  50. go build -o bin/manager main.go
  51. .PHONY: run
  52. run: manifests generate fmt vet ## Run a controller from your host.
  53. go run ./main.go
  54. .PHONY: docker-build
  55. docker-build: test ## Build docker image with the manager.
  56. docker build -t ${IMG} .
  57. .PHONY: docker-push
  58. docker-push: ## Push docker image with the manager.
  59. docker push ${IMG}
  60. ##@ Deployment
  61. ifndef ignore-not-found
  62. ignore-not-found = false
  63. endif
  64. .PHONY: install
  65. install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
  66. $(KUSTOMIZE) build config/crd | kubectl apply -f -
  67. .PHONY: uninstall
  68. 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.
  69. $(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
  70. .PHONY: deploy
  71. deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
  72. cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
  73. $(KUSTOMIZE) build config/default | kubectl apply -f -
  74. .PHONY: undeploy
  75. 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.
  76. $(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
  77. ##@ Build Dependencies
  78. ## Location to install dependencies to
  79. LOCALBIN ?= $(shell pwd)/bin
  80. $(LOCALBIN):
  81. mkdir -p $(LOCALBIN)
  82. ## Tool Binaries
  83. KUSTOMIZE ?= $(LOCALBIN)/kustomize
  84. CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
  85. ENVTEST ?= $(LOCALBIN)/setup-envtest
  86. ## Tool Versions
  87. KUSTOMIZE_VERSION ?= v3.8.7
  88. CONTROLLER_TOOLS_VERSION ?= v0.9.2
  89. KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
  90. .PHONY: kustomize
  91. kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
  92. $(KUSTOMIZE): $(LOCALBIN)
  93. test -s $(LOCALBIN)/kustomize || { curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }
  94. .PHONY: controller-gen
  95. controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
  96. $(CONTROLLER_GEN): $(LOCALBIN)
  97. test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
  98. .PHONY: envtest
  99. envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
  100. $(ENVTEST): $(LOCALBIN)
  101. test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest