|
| 1 | +# Image URL to use all building/pushing image targets |
| 2 | +IMG_CONTROLLERS ?= cloudfoundry/cf-k8s-controllers:latest |
| 3 | +IMG_API ?= cloudfoundry/cf-k8s-api:latest |
| 4 | +# Produce CRDs that work back to Kubernetes 1.11 (no version conversion) |
| 5 | +CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false" |
| 6 | + |
| 7 | +# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) |
| 8 | +ifeq (,$(shell go env GOBIN)) |
| 9 | +GOBIN=$(shell go env GOPATH)/bin |
| 10 | +else |
| 11 | +GOBIN=$(shell go env GOBIN) |
| 12 | +endif |
| 13 | + |
| 14 | +# Setting SHELL to bash allows bash commands to be executed by recipes. |
| 15 | +# This is a requirement for 'setup-envtest.sh' in the test target. |
| 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 | +all: build |
| 21 | + |
| 22 | +##@ General |
| 23 | + |
| 24 | +# The help target prints out all targets with their descriptions organized |
| 25 | +# beneath their categories. The categories are represented by '##@' and the |
| 26 | +# target descriptions by '##'. The awk commands is responsible for reading the |
| 27 | +# entire set of makefiles included in this invocation, looking for lines of the |
| 28 | +# file as xyz: ## something, and then pretty-format the target and help. Then, |
| 29 | +# if there's a line with ##@ something, that gets pretty-printed as a category. |
| 30 | +# More info on the usage of ANSI control characters for terminal formatting: |
| 31 | +# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters |
| 32 | +# More info on the awk command: |
| 33 | +# http://linuxcommand.org/lc3_adv_awk.php |
| 34 | + |
| 35 | +help: ## Display this help. |
| 36 | + @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) |
| 37 | + |
| 38 | +##@ Development |
| 39 | + |
| 40 | +manifests: manifests-controllers manifests-api |
| 41 | + |
| 42 | +manifests-controllers: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. |
| 43 | + $(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./controllers/..." output:crd:artifacts:config=controllers/config/crd/bases output:rbac:artifacts:config=controllers/config/rbac output:webhook:artifacts:config=controllers/config/webhook |
| 44 | + |
| 45 | +manifests-api: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. |
| 46 | + $(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=cf-admin-clusterrole paths=./api/... output:rbac:artifacts:config=api/config/base/rbac |
| 47 | + |
| 48 | +generate-controllers: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. |
| 49 | + $(CONTROLLER_GEN) object:headerFile="controllers/hack/boilerplate.go.txt" paths="./controllers/..." |
| 50 | + |
| 51 | +generate-fakes: fmt vet |
| 52 | + go generate ./... |
| 53 | + |
| 54 | +fmt: ## Run go fmt against code. |
| 55 | + go fmt ./... |
| 56 | + |
| 57 | +vet: ## Run go vet against code. |
| 58 | + go vet ./... |
| 59 | + |
| 60 | +test: test-controllers test-api test-e2e |
| 61 | + |
| 62 | +test-unit: test-controllers test-api-unit |
| 63 | + |
| 64 | +test-controllers: ginkgo manifests-controllers generate-controllers fmt vet ## Run tests. |
| 65 | + cd controllers && ../scripts/run-tests.sh |
| 66 | + |
| 67 | +test-api: test-api-unit test-api-integration |
| 68 | + |
| 69 | +test-api-unit: ginkgo fmt vet |
| 70 | + cd api && ../scripts/run-tests.sh -skipPackage=test |
| 71 | + |
| 72 | +test-api-integration: ginkgo |
| 73 | + cd api && ../scripts/run-tests.sh tests/integration |
| 74 | + |
| 75 | +test-e2e: ginkgo |
| 76 | + cd api && ../scripts/run-tests.sh tests/e2e |
| 77 | + |
| 78 | +##@ Build |
| 79 | + |
| 80 | +build: generate-controllers fmt vet ## Build manager binary. |
| 81 | + go build -o controllers/bin/manager controllers/main.go |
| 82 | + |
| 83 | +run-controllers: manifests-controllers generate-controllers fmt vet ## Run a controller from your host. |
| 84 | + CONFIG=$(shell pwd)/controllers/config/base/cf_k8s_controllers_config.yaml ENABLE_WEBHOOKS=false go run ./controllers/main.go |
| 85 | + |
| 86 | +run-api: fmt vet |
| 87 | + CONFIG=$(shell pwd)/api/config/base/cf_k8s_api_config.yaml go run ./api/main.go |
| 88 | + |
| 89 | +docker-build: docker-build-controllers docker-build-api |
| 90 | + |
| 91 | +docker-build-controllers: |
| 92 | + docker buildx build --load -f controllers/Dockerfile -t ${IMG_CONTROLLERS} . |
| 93 | + |
| 94 | +docker-build-api: |
| 95 | + docker buildx build --load -f api/Dockerfile -t ${IMG_API} . |
| 96 | + |
| 97 | +docker-push: docker-push-controllers docker-push-api |
| 98 | + |
| 99 | +docker-push-controllers: |
| 100 | + docker push ${IMG_CONTROLLERS} |
| 101 | + |
| 102 | +docker-push-api: |
| 103 | + docker push ${IMG_API} |
| 104 | + |
| 105 | +##@ Deployment |
| 106 | + |
| 107 | +install-crds: manifests-controllers kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. |
| 108 | + $(KUSTOMIZE) build controllers/config/crd | kubectl apply -f - |
| 109 | + |
| 110 | +uninstall-crds: manifests-controllers kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. |
| 111 | + $(KUSTOMIZE) build controllers/config/crd | kubectl delete -f - |
| 112 | + |
| 113 | +deploy: deploy-controllers deploy-api |
| 114 | + |
| 115 | +deploy-controllers: manifests-controllers kustomize |
| 116 | + cd controllers/config/manager && $(KUSTOMIZE) edit set image cloudfoundry/cf-k8s-controllers=${IMG_CONTROLLERS} |
| 117 | + $(KUSTOMIZE) build controllers/config/default | kubectl apply -f - |
| 118 | + |
| 119 | +deploy-api: kustomize |
| 120 | + cd api/config/base && $(KUSTOMIZE) edit set image cloudfoundry/cf-k8s-api=${IMG_API} |
| 121 | + $(KUSTOMIZE) build api/config/base | kubectl apply -f - |
| 122 | + |
| 123 | +deploy-api-kind-auth: kustomize |
| 124 | + cd api/config/base && $(KUSTOMIZE) edit set image cloudfoundry/cf-k8s-api=${IMG_API} |
| 125 | + $(KUSTOMIZE) build api/config/overlays/kind-auth-enabled | kubectl apply -f - |
| 126 | + |
| 127 | +undeploy-controllers: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. |
| 128 | + $(KUSTOMIZE) build controllers/config/default | kubectl delete -f - |
| 129 | + |
| 130 | +undeploy-api: ## Undeploy api from the K8s cluster specified in ~/.kube/config. |
| 131 | + $(KUSTOMIZE) build api/config/base | kubectl delete -f - |
| 132 | + |
| 133 | +build-reference: build-reference-controllers build-reference-api |
| 134 | + |
| 135 | +build-reference-controllers: manifests-controllers kustomize ## Generate reference yaml and output to ./reference/cf-k8s-controllers.yaml |
| 136 | + cd controllers/config/manager && $(KUSTOMIZE) edit set image cloudfoundry/cf-k8s-controllers=${IMG_CONTROLLERS} |
| 137 | + $(KUSTOMIZE) build controllers/config/default -o controllers/reference/cf-k8s-controllers.yaml |
| 138 | + |
| 139 | +build-reference-api: manifests-api kustomize |
| 140 | + cd api/config/base && $(KUSTOMIZE) edit set image cloudfoundry/cf-k8s-api=${IMG_API} |
| 141 | + $(KUSTOMIZE) build api/config/base -o api/reference/cf-k8s-api.yaml |
| 142 | + |
| 143 | +CONTROLLER_GEN = $(shell pwd)/controllers/bin/controller-gen |
| 144 | +controller-gen: ## Download controller-gen locally if necessary. |
| 145 | + $(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.5.0) |
| 146 | + |
| 147 | +KUSTOMIZE = $(shell pwd)/controllers/bin/kustomize |
| 148 | +kustomize: ## Download kustomize locally if necessary. |
| 149 | + $(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v4@v4.2.0) |
| 150 | + |
| 151 | +GINKGO = $(shell pwd)/bin/ginkgo |
| 152 | +ginkgo: |
| 153 | + $(call go-get-tool,$(GINKGO),github.com/onsi/ginkgo/ginkgo@latest) |
| 154 | + |
| 155 | +# go-get-tool will 'go get' any package $2 and install it to $1. |
| 156 | +define go-get-tool |
| 157 | +@[ -f $(1) ] || { \ |
| 158 | +set -e ;\ |
| 159 | +TMP_DIR=$$(mktemp -d) ;\ |
| 160 | +cd $$TMP_DIR ;\ |
| 161 | +go mod init tmp ;\ |
| 162 | +echo "Downloading $(2)" ;\ |
| 163 | +GOBIN=$$(dirname $(CONTROLLER_GEN)) go get $(2) ;\ |
| 164 | +rm -rf $$TMP_DIR ;\ |
| 165 | +} |
| 166 | +endef |
0 commit comments