Makefile 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.25.0
  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) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
  47. ##@ Build
  48. .PHONY: build
  49. build: manifests 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. # If you wish built the manager image targeting other platforms you can use the --platform flag.
  55. # (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
  56. # More info: https://docs.docker.com/develop/develop-images/build_enhancements/
  57. .PHONY: docker-build
  58. docker-build: test ## Build docker image with the manager.
  59. docker build -t ${IMG} .
  60. .PHONY: docker-push
  61. docker-push: ## Push docker image with the manager.
  62. docker push ${IMG}
  63. # PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
  64. # architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
  65. # - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
  66. # - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
  67. # - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
  68. # To properly provided solutions that supports more than one platform you should use this option.
  69. PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
  70. .PHONY: docker-buildx
  71. docker-buildx: test ## Build and push docker image for the manager for cross-platform support
  72. # copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
  73. sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
  74. - docker buildx create --name project-v3-builder
  75. docker buildx use project-v3-builder
  76. - docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
  77. - docker buildx rm project-v3-builder
  78. rm Dockerfile.cross
  79. ##@ Deployment
  80. ifndef ignore-not-found
  81. ignore-not-found = false
  82. endif
  83. .PHONY: install
  84. install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
  85. $(KUSTOMIZE) build config/crd | kubectl apply -f -
  86. .PHONY: uninstall
  87. 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.
  88. $(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
  89. .PHONY: deploy
  90. deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
  91. cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
  92. $(KUSTOMIZE) build config/default | kubectl apply -f -
  93. .PHONY: undeploy
  94. 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.
  95. $(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
  96. ##@ Build Dependencies
  97. ## Location to install dependencies to
  98. LOCALBIN ?= $(shell pwd)/bin
  99. $(LOCALBIN):
  100. mkdir -p $(LOCALBIN)
  101. ## Tool Binaries
  102. KUSTOMIZE ?= $(LOCALBIN)/kustomize
  103. CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
  104. ENVTEST ?= $(LOCALBIN)/setup-envtest
  105. ## Tool Versions
  106. KUSTOMIZE_VERSION ?= v3.8.7
  107. CONTROLLER_TOOLS_VERSION ?= v0.10.0
  108. KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
  109. .PHONY: kustomize
  110. kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
  111. $(KUSTOMIZE): $(LOCALBIN)
  112. @if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
  113. echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
  114. rm -rf $(LOCALBIN)/kustomize; \
  115. fi
  116. test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }
  117. .PHONY: controller-gen
  118. controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
  119. $(CONTROLLER_GEN): $(LOCALBIN)
  120. test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
  121. GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
  122. .PHONY: envtest
  123. envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
  124. $(ENVTEST): $(LOCALBIN)
  125. test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest