|
| 1 | +// Copyright 2021 The Operator-SDK Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package templates |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + |
| 20 | + "sigs.k8s.io/kubebuilder/v3/pkg/machinery" |
| 21 | +) |
| 22 | + |
| 23 | +var _ machinery.Template = &Makefile{} |
| 24 | + |
| 25 | +// Makefile scaffolds the Makefile |
| 26 | +type Makefile struct { |
| 27 | + machinery.TemplateMixin |
| 28 | + |
| 29 | + // Image is controller manager image name |
| 30 | + Image string |
| 31 | + |
| 32 | + // Kustomize version to use in the project |
| 33 | + KustomizeVersion string |
| 34 | + |
| 35 | + // // AnsibleOperatorVersion is the version of the ansible-operator binary downloaded by the Makefile. |
| 36 | + // AnsibleOperatorVersion string |
| 37 | +} |
| 38 | + |
| 39 | +// SetTemplateDefaults implements machinery.Template |
| 40 | +func (f *Makefile) SetTemplateDefaults() error { |
| 41 | + if f.Path == "" { |
| 42 | + f.Path = "Makefile" |
| 43 | + } |
| 44 | + |
| 45 | + f.TemplateBody = makefileTemplate |
| 46 | + |
| 47 | + f.IfExistsAction = machinery.Error |
| 48 | + |
| 49 | + if f.Image == "" { |
| 50 | + f.Image = "controller:latest" |
| 51 | + } |
| 52 | + |
| 53 | + if f.KustomizeVersion == "" { |
| 54 | + return errors.New("kustomize version is required in scaffold") |
| 55 | + } |
| 56 | + |
| 57 | + // if f.AnsibleOperatorVersion == "" { |
| 58 | + // return errors.New("ansible-operator version is required in scaffold") |
| 59 | + // } |
| 60 | + |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +const makefileTemplate = ` |
| 65 | +# Image URL to use all building/pushing image targets |
| 66 | +IMG ?= {{ .Image }} |
| 67 | +
|
| 68 | +all: docker-build |
| 69 | +
|
| 70 | +##@ General |
| 71 | +
|
| 72 | +# The help target prints out all targets with their descriptions organized |
| 73 | +# beneath their categories. The categories are represented by '##@' and the |
| 74 | +# target descriptions by '##'. The awk commands is responsible for reading the |
| 75 | +# entire set of makefiles included in this invocation, looking for lines of the |
| 76 | +# file as xyz: ## something, and then pretty-format the target and help. Then, |
| 77 | +# if there's a line with ##@ something, that gets pretty-printed as a category. |
| 78 | +# More info on the usage of ANSI control characters for terminal formatting: |
| 79 | +# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters |
| 80 | +# More info on the awk command: |
| 81 | +# http://linuxcommand.org/lc3_adv_awk.php |
| 82 | +
|
| 83 | +help: ## Display this help. |
| 84 | + @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) |
| 85 | +
|
| 86 | +##@ Build |
| 87 | +
|
| 88 | +docker-build: ## Build docker image with the manager. |
| 89 | + mvn package -Dquarkus.container-image.build=true -Dquarkus.container-image.image=${IMG} |
| 90 | +
|
| 91 | +docker-push: ## Push docker image with the manager. |
| 92 | + mvn package -Dquarkus.container-image.push=true -Dquarkus.container-image.image=${IMG} |
| 93 | +
|
| 94 | +##@ Deployment |
| 95 | +
|
| 96 | +install: ## Install CRDs into the K8s cluster specified in ~/.kube/config. |
| 97 | + @$(foreach file, $(wildcard target/kubernetes/*-v1.yml), kubectl apply -f $(file);) |
| 98 | +
|
| 99 | +uninstall: ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. |
| 100 | + @$(foreach file, $(wildcard target/kubernetes/*-v1.yml), kubectl delete -f $(file);) |
| 101 | +
|
| 102 | +deploy: ## Deploy controller to the K8s cluster specified in ~/.kube/config. |
| 103 | + kubectl apply -f target/kubernetes/kubernetes.yml |
| 104 | +
|
| 105 | +undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. |
| 106 | + kubectl delete -f target/kubernetes/kubernetes.yml |
| 107 | +` |
0 commit comments