|
| 1 | +# VERSION defines the project version for the bundle. |
| 2 | +# Update this value when you upgrade the version of your project. |
| 3 | +# To re-generate a bundle for another specific version without changing the standard setup, you can: |
| 4 | +# - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2) |
| 5 | +# - use environment variables to overwrite this value (e.g export VERSION=0.0.2) |
| 6 | +VERSION ?= 0.0.1 |
| 7 | + |
| 8 | +# CHANNELS define the bundle channels used in the bundle. |
| 9 | +# Add a new line here if you would like to change its default config. (E.g CHANNELS = "preview,fast,stable") |
| 10 | +# To re-generate a bundle for other specific channels without changing the standard setup, you can: |
| 11 | +# - use the CHANNELS as arg of the bundle target (e.g make bundle CHANNELS=preview,fast,stable) |
| 12 | +# - use environment variables to overwrite this value (e.g export CHANNELS="preview,fast,stable") |
| 13 | +ifneq ($(origin CHANNELS), undefined) |
| 14 | +BUNDLE_CHANNELS := --channels=$(CHANNELS) |
| 15 | +endif |
| 16 | + |
| 17 | +# DEFAULT_CHANNEL defines the default channel used in the bundle. |
| 18 | +# Add a new line here if you would like to change its default config. (E.g DEFAULT_CHANNEL = "stable") |
| 19 | +# To re-generate a bundle for any other default channel without changing the default setup, you can: |
| 20 | +# - use the DEFAULT_CHANNEL as arg of the bundle target (e.g make bundle DEFAULT_CHANNEL=stable) |
| 21 | +# - use environment variables to overwrite this value (e.g export DEFAULT_CHANNEL="stable") |
| 22 | +ifneq ($(origin DEFAULT_CHANNEL), undefined) |
| 23 | +BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL) |
| 24 | +endif |
| 25 | +BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL) |
| 26 | + |
| 27 | +# IMAGE_TAG_BASE defines the docker.io namespace and part of the image name for remote images. |
| 28 | +# This variable is used to construct full image tags for bundle and catalog images. |
| 29 | +# |
| 30 | +# For example, running 'make bundle-build bundle-push catalog-build catalog-push' will build and push both |
| 31 | +# glints.com/postgres-config-operator-bundle:$VERSION and glints.com/postgres-config-operator-catalog:$VERSION. |
| 32 | +IMAGE_TAG_BASE ?= glints.com/postgres-config-operator |
| 33 | + |
| 34 | +# BUNDLE_IMG defines the image:tag used for the bundle. |
| 35 | +# You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=<some-registry>/<project-name-bundle>:<tag>) |
| 36 | +BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION) |
| 37 | + |
| 38 | +# Image URL to use all building/pushing image targets |
| 39 | +IMG ?= controller:latest |
| 40 | +# Produce CRDs that work back to Kubernetes 1.11 (no version conversion) |
| 41 | +CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false" |
| 42 | + |
| 43 | +# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) |
| 44 | +ifeq (,$(shell go env GOBIN)) |
| 45 | +GOBIN=$(shell go env GOPATH)/bin |
| 46 | +else |
| 47 | +GOBIN=$(shell go env GOBIN) |
| 48 | +endif |
| 49 | + |
| 50 | +# Setting SHELL to bash allows bash commands to be executed by recipes. |
| 51 | +# This is a requirement for 'setup-envtest.sh' in the test target. |
| 52 | +# Options are set to exit when a recipe line exits non-zero or a piped command fails. |
| 53 | +SHELL = /usr/bin/env bash -o pipefail |
| 54 | +.SHELLFLAGS = -ec |
| 55 | + |
| 56 | +all: build |
| 57 | + |
| 58 | +##@ General |
| 59 | + |
| 60 | +# The help target prints out all targets with their descriptions organized |
| 61 | +# beneath their categories. The categories are represented by '##@' and the |
| 62 | +# target descriptions by '##'. The awk commands is responsible for reading the |
| 63 | +# entire set of makefiles included in this invocation, looking for lines of the |
| 64 | +# file as xyz: ## something, and then pretty-format the target and help. Then, |
| 65 | +# if there's a line with ##@ something, that gets pretty-printed as a category. |
| 66 | +# More info on the usage of ANSI control characters for terminal formatting: |
| 67 | +# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters |
| 68 | +# More info on the awk command: |
| 69 | +# http://linuxcommand.org/lc3_adv_awk.php |
| 70 | + |
| 71 | +help: ## Display this help. |
| 72 | + @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) |
| 73 | + |
| 74 | +##@ Development |
| 75 | + |
| 76 | +manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. |
| 77 | + $(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases |
| 78 | + |
| 79 | +generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. |
| 80 | + $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." |
| 81 | + |
| 82 | +fmt: ## Run go fmt against code. |
| 83 | + go fmt ./... |
| 84 | + |
| 85 | +vet: ## Run go vet against code. |
| 86 | + go vet ./... |
| 87 | + |
| 88 | +ENVTEST_ASSETS_DIR=$(shell pwd)/testbin |
| 89 | +test: manifests generate fmt vet ## Run tests. |
| 90 | + mkdir -p ${ENVTEST_ASSETS_DIR} |
| 91 | + test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh |
| 92 | + source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out |
| 93 | + |
| 94 | +##@ Build |
| 95 | + |
| 96 | +build: generate fmt vet ## Build manager binary. |
| 97 | + go build -o bin/manager main.go |
| 98 | + |
| 99 | +run: manifests generate fmt vet ## Run a controller from your host. |
| 100 | + go run ./main.go |
| 101 | + |
| 102 | +docker-build: test ## Build docker image with the manager. |
| 103 | + docker build -t ${IMG} . |
| 104 | + |
| 105 | +docker-push: ## Push docker image with the manager. |
| 106 | + docker push ${IMG} |
| 107 | + |
| 108 | +##@ Deployment |
| 109 | + |
| 110 | +install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. |
| 111 | + $(KUSTOMIZE) build config/crd | kubectl apply -f - |
| 112 | + |
| 113 | +uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. |
| 114 | + $(KUSTOMIZE) build config/crd | kubectl delete -f - |
| 115 | + |
| 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 | +undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. |
| 121 | + $(KUSTOMIZE) build config/default | kubectl delete -f - |
| 122 | + |
| 123 | + |
| 124 | +CONTROLLER_GEN = $(shell pwd)/bin/controller-gen |
| 125 | +controller-gen: ## Download controller-gen locally if necessary. |
| 126 | + $(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected]) |
| 127 | + |
| 128 | +KUSTOMIZE = $(shell pwd)/bin/kustomize |
| 129 | +kustomize: ## Download kustomize locally if necessary. |
| 130 | + $(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected]) |
| 131 | + |
| 132 | +# go-get-tool will 'go get' any package $2 and install it to $1. |
| 133 | +PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) |
| 134 | +define go-get-tool |
| 135 | +@[ -f $(1) ] || { \ |
| 136 | +set -e ;\ |
| 137 | +TMP_DIR=$$(mktemp -d) ;\ |
| 138 | +cd $$TMP_DIR ;\ |
| 139 | +go mod init tmp ;\ |
| 140 | +echo "Downloading $(2)" ;\ |
| 141 | +GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\ |
| 142 | +rm -rf $$TMP_DIR ;\ |
| 143 | +} |
| 144 | +endef |
| 145 | + |
| 146 | +.PHONY: bundle |
| 147 | +bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files. |
| 148 | + operator-sdk generate kustomize manifests -q |
| 149 | + cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG) |
| 150 | + $(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS) |
| 151 | + operator-sdk bundle validate ./bundle |
| 152 | + |
| 153 | +.PHONY: bundle-build |
| 154 | +bundle-build: ## Build the bundle image. |
| 155 | + docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) . |
| 156 | + |
| 157 | +.PHONY: bundle-push |
| 158 | +bundle-push: ## Push the bundle image. |
| 159 | + $(MAKE) docker-push IMG=$(BUNDLE_IMG) |
| 160 | + |
| 161 | +.PHONY: opm |
| 162 | +OPM = ./bin/opm |
| 163 | +opm: ## Download opm locally if necessary. |
| 164 | +ifeq (,$(wildcard $(OPM))) |
| 165 | +ifeq (,$(shell which opm 2>/dev/null)) |
| 166 | + @{ \ |
| 167 | + set -e ;\ |
| 168 | + mkdir -p $(dir $(OPM)) ;\ |
| 169 | + OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \ |
| 170 | + curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.15.1/$${OS}-$${ARCH}-opm ;\ |
| 171 | + chmod +x $(OPM) ;\ |
| 172 | + } |
| 173 | +else |
| 174 | +OPM = $(shell which opm) |
| 175 | +endif |
| 176 | +endif |
| 177 | + |
| 178 | +# A comma-separated list of bundle images (e.g. make catalog-build BUNDLE_IMGS=example.com/operator-bundle:v0.1.0,example.com/operator-bundle:v0.2.0). |
| 179 | +# These images MUST exist in a registry and be pull-able. |
| 180 | +BUNDLE_IMGS ?= $(BUNDLE_IMG) |
| 181 | + |
| 182 | +# The image tag given to the resulting catalog image (e.g. make catalog-build CATALOG_IMG=example.com/operator-catalog:v0.2.0). |
| 183 | +CATALOG_IMG ?= $(IMAGE_TAG_BASE)-catalog:v$(VERSION) |
| 184 | + |
| 185 | +# Set CATALOG_BASE_IMG to an existing catalog image tag to add $BUNDLE_IMGS to that image. |
| 186 | +ifneq ($(origin CATALOG_BASE_IMG), undefined) |
| 187 | +FROM_INDEX_OPT := --from-index $(CATALOG_BASE_IMG) |
| 188 | +endif |
| 189 | + |
| 190 | +# Build a catalog image by adding bundle images to an empty catalog using the operator package manager tool, 'opm'. |
| 191 | +# This recipe invokes 'opm' in 'semver' bundle add mode. For more information on add modes, see: |
| 192 | +# https://github.com/operator-framework/community-operators/blob/7f1438c/docs/packaging-operator.md#updating-your-existing-operator |
| 193 | +.PHONY: catalog-build |
| 194 | +catalog-build: opm ## Build a catalog image. |
| 195 | + $(OPM) index add --container-tool docker --mode semver --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT) |
| 196 | + |
| 197 | +# Push the catalog image. |
| 198 | +.PHONY: catalog-push |
| 199 | +catalog-push: ## Push a catalog image. |
| 200 | + $(MAKE) docker-push IMG=$(CATALOG_IMG) |
0 commit comments