Makefile 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.28.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. # CONTAINER_TOOL defines the container tool to be used for building images.
  12. # Be aware that the target commands are only tested with Docker which is
  13. # scaffolded by default. However, you might want to replace it to use other
  14. # tools. (i.e. podman)
  15. CONTAINER_TOOL ?= docker
  16. # Setting SHELL to bash allows bash commands to be executed by recipes.
  17. # Options are set to exit when a recipe line exits non-zero or a piped command fails.
  18. SHELL = /usr/bin/env bash -o pipefail
  19. .SHELLFLAGS = -ec
  20. .PHONY: all
  21. all: build
  22. ##@ General
  23. # The help target prints out all targets with their descriptions organized
  24. # beneath their categories. The categories are represented by '##@' and the
  25. # target descriptions by '##'. The awk command is responsible for reading the
  26. # entire set of makefiles included in this invocation, looking for lines of the
  27. # file as xyz: ## something, and then pretty-format the target and help. Then,
  28. # if there's a line with ##@ something, that gets pretty-printed as a category.
  29. # More info on the usage of ANSI control characters for terminal formatting:
  30. # https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
  31. # More info on the awk command:
  32. # http://linuxcommand.org/lc3_adv_awk.php
  33. .PHONY: help
  34. help: ## Display this help.
  35. @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)
  36. ##@ Development
  37. .PHONY: manifests
  38. manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
  39. $(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
  40. .PHONY: generate
  41. generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
  42. $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
  43. .PHONY: fmt
  44. fmt: ## Run go fmt against code.
  45. go fmt ./...
  46. .PHONY: vet
  47. vet: ## Run go vet against code.
  48. go vet ./...
  49. .PHONY: test
  50. test: manifests generate fmt vet envtest ## Run tests.
  51. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
  52. ##@ Build
  53. .PHONY: build
  54. build: manifests generate fmt vet ## Build manager binary.
  55. go build -o bin/manager cmd/main.go
  56. .PHONY: run
  57. run: manifests generate fmt vet ## Run a controller from your host.
  58. go run ./cmd/main.go
  59. # If you wish to build the manager image targeting other platforms you can use the --platform flag.
  60. # (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
  61. # More info: https://docs.docker.com/develop/develop-images/build_enhancements/
  62. .PHONY: docker-build
  63. docker-build: ## Build docker image with the manager.
  64. $(CONTAINER_TOOL) build -t ${IMG} .
  65. .PHONY: docker-push
  66. docker-push: ## Push docker image with the manager.
  67. $(CONTAINER_TOOL) push ${IMG}
  68. # PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
  69. # architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
  70. # - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
  71. # - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
  72. # - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
  73. # To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
  74. PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
  75. .PHONY: docker-buildx
  76. docker-buildx: ## Build and push docker image for the manager for cross-platform support
  77. # copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
  78. sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
  79. - $(CONTAINER_TOOL) buildx create --name project-v3-builder
  80. $(CONTAINER_TOOL) buildx use project-v3-builder
  81. - $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
  82. - $(CONTAINER_TOOL) buildx rm project-v3-builder
  83. rm Dockerfile.cross
  84. ##@ Deployment
  85. ifndef ignore-not-found
  86. ignore-not-found = false
  87. endif
  88. .PHONY: install
  89. install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
  90. $(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
  91. .PHONY: uninstall
  92. 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.
  93. $(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
  94. .PHONY: deploy
  95. deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
  96. cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
  97. $(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
  98. .PHONY: undeploy
  99. 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.
  100. $(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
  101. ##@ Build Dependencies
  102. ## Location to install dependencies to
  103. LOCALBIN ?= $(shell pwd)/bin
  104. $(LOCALBIN):
  105. mkdir -p $(LOCALBIN)
  106. ## Tool Binaries
  107. KUBECTL ?= kubectl
  108. KUSTOMIZE ?= $(LOCALBIN)/kustomize
  109. CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
  110. ENVTEST ?= $(LOCALBIN)/setup-envtest
  111. ## Tool Versions
  112. KUSTOMIZE_VERSION ?= v5.1.1
  113. CONTROLLER_TOOLS_VERSION ?= v0.13.0
  114. .PHONY: kustomize
  115. kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
  116. $(KUSTOMIZE): $(LOCALBIN)
  117. @if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
  118. echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
  119. rm -rf $(LOCALBIN)/kustomize; \
  120. fi
  121. test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
  122. .PHONY: controller-gen
  123. controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
  124. $(CONTROLLER_GEN): $(LOCALBIN)
  125. test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
  126. GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
  127. .PHONY: envtest
  128. envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
  129. $(ENVTEST): $(LOCALBIN)
  130. test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest