|
| 1 | +# |
| 2 | +# Copyright (c) 2021, Oracle and/or its affiliates. |
| 3 | +# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl. |
| 4 | +# |
| 5 | + |
| 6 | +# Current Operator version |
| 7 | +VERSION ?= 0.0.1 |
| 8 | +# Default bundle image tag |
| 9 | +BUNDLE_IMG ?= controller-bundle:$(VERSION) |
| 10 | +# Options for 'bundle-build' |
| 11 | +ifneq ($(origin CHANNELS), undefined) |
| 12 | +BUNDLE_CHANNELS := --channels=$(CHANNELS) |
| 13 | +endif |
| 14 | +ifneq ($(origin DEFAULT_CHANNEL), undefined) |
| 15 | +BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL) |
| 16 | +endif |
| 17 | +BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL) |
| 18 | + |
| 19 | +# Image URL to use all building/pushing image targets |
| 20 | +IMG ?= controller:latest |
| 21 | +# Produce CRDs that work back to Kubernetes 1.11 (no version conversion) |
| 22 | +# API version has to be v1 to use defaulting (https://github.com/kubernetes-sigs/controller-tools/issues/478) |
| 23 | +CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false" |
| 24 | +# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary. |
| 25 | +ENVTEST_K8S_VERSION = 1.21 |
| 26 | + |
| 27 | +# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) |
| 28 | +ifeq (,$(shell go env GOBIN)) |
| 29 | +GOBIN=$(shell go env GOPATH)/bin |
| 30 | +else |
| 31 | +GOBIN=$(shell go env GOBIN) |
| 32 | +endif |
| 33 | + |
| 34 | +# Setting SHELL to bash allows bash commands to be executed by recipes. |
| 35 | +# This is a requirement for 'setup-envtest.sh' in the test target. |
| 36 | +# Options are set to exit when a recipe line exits non-zero or a piped command fails. |
| 37 | +SHELL = /usr/bin/env bash -o pipefail |
| 38 | +.SHELLFLAGS = -ec |
| 39 | + |
| 40 | +all: build |
| 41 | + |
| 42 | +##@ Development |
| 43 | + |
| 44 | +manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. |
| 45 | + $(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases |
| 46 | + |
| 47 | +generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. |
| 48 | + $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." |
| 49 | + |
| 50 | +fmt: ## Run go fmt against code. |
| 51 | + go fmt ./... |
| 52 | + |
| 53 | +vet: ## Run go vet against code. |
| 54 | + go vet ./... |
| 55 | + |
| 56 | +TEST ?= ./apis/... ./commons/... ./controllers/... |
| 57 | +test: manifests generate fmt vet envtest ## Run unit tests. |
| 58 | + KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test $(TEST) -coverprofile cover.out |
| 59 | + |
| 60 | +E2ETEST ?= ./test/e2e/ |
| 61 | +e2e: manifests generate fmt vet envtest ## Run e2e tests. |
| 62 | + KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test $(E2ETEST) -v -timeout 40m -ginkgo.v -ginkgo.failFast |
| 63 | + |
| 64 | +##@ Build |
| 65 | + |
| 66 | +build: generate fmt vet ## Build manager binary. |
| 67 | + go build -o bin/manager main.go |
| 68 | + |
| 69 | +run: manifests generate fmt vet ## Run a controller from your host. |
| 70 | + go run ./main.go |
| 71 | + |
| 72 | +docker-build: test ## Build docker image with the manager. |
| 73 | + docker build --no-cache=true --build-arg http_proxy=${HTTP_PROXY} --build-arg https_proxy=${HTTPS_PROXY} . -t ${IMG} |
| 74 | + |
| 75 | +#docker-build-proxy: test |
| 76 | +# docker build --build-arg http_proxy=${http_proxy} --build-arg https_proxy=${https_proxy} build . -t ${IMG} |
| 77 | + |
| 78 | +docker-push: ## Push docker image with the manager. |
| 79 | + docker push ${IMG} |
| 80 | + |
| 81 | +##@ Deployment |
| 82 | + |
| 83 | +install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. |
| 84 | + $(KUSTOMIZE) build config/crd | kubectl apply -f - |
| 85 | + |
| 86 | +uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. |
| 87 | + $(KUSTOMIZE) build config/crd | kubectl delete -f - |
| 88 | + |
| 89 | +deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config. |
| 90 | + cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} |
| 91 | + $(KUSTOMIZE) build config/default | kubectl apply -f - |
| 92 | + |
| 93 | +operator-yaml: manifests kustomize |
| 94 | + cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} |
| 95 | + $(KUSTOMIZE) build config/default > $$(basename $$(pwd)).yaml |
| 96 | + |
| 97 | +undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. |
| 98 | + $(KUSTOMIZE) build config/default | kubectl delete -f - |
| 99 | + |
| 100 | + |
| 101 | +CONTROLLER_GEN = $(shell pwd)/bin/controller-gen |
| 102 | +controller-gen: ## Download controller-gen locally if necessary. |
| 103 | + $(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected]) |
| 104 | + |
| 105 | +KUSTOMIZE = $(shell pwd)/bin/kustomize |
| 106 | +kustomize: ## Download kustomize locally if necessary. |
| 107 | + $(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected]) |
| 108 | + |
| 109 | +ENVTEST = $(shell pwd)/bin/setup-envtest |
| 110 | +envtest: ## Download envtest-setup locally if necessary. |
| 111 | + $(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest) |
| 112 | + |
| 113 | +# go-get-tool will 'go get' any package $2 and install it to $1. |
| 114 | +PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) |
| 115 | +define go-get-tool |
| 116 | +@[ -f $(1) ] || { \ |
| 117 | +set -e ;\ |
| 118 | +TMP_DIR=$$(mktemp -d) ;\ |
| 119 | +cd $$TMP_DIR ;\ |
| 120 | +go mod init tmp ;\ |
| 121 | +echo "Downloading $(2)" ;\ |
| 122 | +GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\ |
| 123 | +rm -rf $$TMP_DIR ;\ |
| 124 | +} |
| 125 | +endef |
| 126 | + |
| 127 | +.PHONY: bundle |
| 128 | +bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files. |
| 129 | + operator-sdk generate kustomize manifests -q |
| 130 | + cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG) |
| 131 | + $(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS) |
| 132 | + operator-sdk bundle validate ./bundle |
| 133 | + |
| 134 | +.PHONY: bundle-build |
| 135 | +bundle-build: ## Build the bundle image. |
| 136 | + docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) . |
| 137 | + |
| 138 | +.PHONY: bundle-push |
| 139 | +bundle-push: ## Push the bundle image. |
| 140 | + $(MAKE) docker-push IMG=$(BUNDLE_IMG) |
| 141 | + |
| 142 | +.PHONY: opm |
| 143 | +OPM = ./bin/opm |
| 144 | +opm: ## Download opm locally if necessary. |
| 145 | +ifeq (,$(wildcard $(OPM))) |
| 146 | +ifeq (,$(shell which opm 2>/dev/null)) |
| 147 | + @{ \ |
| 148 | + set -e ;\ |
| 149 | + mkdir -p $(dir $(OPM)) ;\ |
| 150 | + OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \ |
| 151 | + curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.15.1/$${OS}-$${ARCH}-opm ;\ |
| 152 | + chmod +x $(OPM) ;\ |
| 153 | + } |
| 154 | +else |
| 155 | +OPM = $(shell which opm) |
| 156 | +endif |
| 157 | +endif |
| 158 | + |
| 159 | +# 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). |
| 160 | +# These images MUST exist in a registry and be pull-able. |
| 161 | +BUNDLE_IMGS ?= $(BUNDLE_IMG) |
| 162 | + |
| 163 | +# The image tag given to the resulting catalog image (e.g. make catalog-build CATALOG_IMG=example.com/operator-catalog:v0.2.0). |
| 164 | +CATALOG_IMG ?= $(IMAGE_TAG_BASE)-catalog:v$(VERSION) |
| 165 | + |
| 166 | +# Set CATALOG_BASE_IMG to an existing catalog image tag to add $BUNDLE_IMGS to that image. |
| 167 | +ifneq ($(origin CATALOG_BASE_IMG), undefined) |
| 168 | +FROM_INDEX_OPT := --from-index $(CATALOG_BASE_IMG) |
| 169 | +endif |
| 170 | + |
| 171 | +# Build a catalog image by adding bundle images to an empty catalog using the operator package manager tool, 'opm'. |
| 172 | +# This recipe invokes 'opm' in 'semver' bundle add mode. For more information on add modes, see: |
| 173 | +# https://github.com/operator-framework/community-operators/blob/7f1438c/docs/packaging-operator.md#updating-your-existing-operator |
| 174 | +.PHONY: catalog-build |
| 175 | +catalog-build: opm ## Build a catalog image. |
| 176 | + $(OPM) index add --container-tool docker --mode semver --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT) |
| 177 | + |
| 178 | +# Push the catalog image. |
| 179 | +.PHONY: catalog-push |
| 180 | +catalog-push: ## Push a catalog image. |
| 181 | + $(MAKE) docker-push IMG=$(CATALOG_IMG) |
0 commit comments