sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v2/scaffolds/internal/templates/makefile.go (about) 1 /* 2 Copyright 2019 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package templates 18 19 import ( 20 "sigs.k8s.io/kubebuilder/v3/pkg/machinery" 21 ) 22 23 var _ machinery.Template = &Makefile{} 24 25 // Makefile scaffolds a file that defines project management CLI commands 26 type Makefile struct { 27 machinery.TemplateMixin 28 29 // Image is controller manager image name 30 Image string 31 // BoilerplatePath is the path to the boilerplate file 32 BoilerplatePath string 33 // Controller tools version to use in the project 34 ControllerToolsVersion string 35 // Kustomize version to use in the project 36 KustomizeVersion string 37 } 38 39 // SetTemplateDefaults implements file.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 return nil 54 } 55 56 //nolint:lll 57 const makefileTemplate = ` 58 # Image URL to use all building/pushing image targets 59 IMG ?= {{ .Image }} 60 # Produce CRDs that work back to Kubernetes 1.11 (no version conversion) 61 CRD_OPTIONS ?= "crd:trivialVersions=true" 62 63 # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) 64 ifeq (,$(shell go env GOBIN)) 65 GOBIN=$(shell go env GOPATH)/bin 66 else 67 GOBIN=$(shell go env GOBIN) 68 endif 69 70 .PHONY: all 71 all: build 72 73 ##@ General 74 75 # The help target prints out all targets with their descriptions organized 76 # beneath their categories. The categories are represented by '##@' and the 77 # target descriptions by '##'. The awk command is responsible for reading the 78 # entire set of makefiles included in this invocation, looking for lines of the 79 # file as xyz: ## something, and then pretty-format the target and help. Then, 80 # if there's a line with ##@ something, that gets pretty-printed as a category. 81 # More info on the usage of ANSI control characters for terminal formatting: 82 # https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters 83 # More info on the awk command: 84 # http://linuxcommand.org/lc3_adv_awk.php 85 86 .PHONY: help 87 help: ## Display this help. 88 @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) 89 90 ##@ Development 91 92 .PHONY: manifests 93 manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. 94 $(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases 95 96 .PHONY: generate 97 generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. 98 $(CONTROLLER_GEN) object:headerFile={{printf "%q" .BoilerplatePath}} paths="./..." 99 100 .PHONY: fmt 101 fmt: ## Run go fmt against code. 102 go fmt ./... 103 104 .PHONY: vet 105 vet: ## Run go vet against code. 106 go vet ./... 107 108 .PHONY: test 109 test: manifests generate fmt vet ## Run tests. 110 go test ./... -coverprofile cover.out 111 112 ##@ Build 113 114 .PHONY: build 115 build: generate fmt vet ## Build manager binary. 116 go build -o bin/manager main.go 117 118 # Backwards compatibility 119 .PHONY: manager 120 manager: build ## Build manager binary (alias for build target). 121 122 .PHONY: run 123 run: manifests generate fmt vet ## Run a controller from your host. 124 go run ./main.go 125 126 .PHONY: docker-build 127 docker-build: test ## Build docker image with the manager. 128 docker build -t ${IMG} . 129 130 .PHONY: docker-push 131 docker-push: ## Push docker image with the manager. 132 docker push ${IMG} 133 134 ##@ Deployment 135 136 ifndef ignore-not-found 137 ignore-not-found = false 138 endif 139 140 .PHONY: install 141 install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. 142 $(KUSTOMIZE) build config/crd | kubectl apply -f - 143 144 .PHONY: uninstall 145 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. 146 $(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f - 147 148 .PHONY: deploy 149 deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config. 150 cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} 151 $(KUSTOMIZE) build config/default | kubectl apply -f - 152 153 .PHONY: undeploy 154 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. 155 $(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f - 156 157 ##@ Build Dependencies 158 159 ## Location to install dependencies to 160 LOCALBIN ?= $(shell pwd)/bin 161 $(LOCALBIN): ## Ensure that the directory exists 162 mkdir -p $(LOCALBIN) 163 164 ## Tool Binaries 165 KUSTOMIZE ?= $(LOCALBIN)/kustomize 166 CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen 167 168 ## Tool Versions 169 KUSTOMIZE_VERSION ?= {{ .KustomizeVersion }} 170 CONTROLLER_TOOLS_VERSION ?= {{ .ControllerToolsVersion }} 171 172 KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" 173 .PHONY: kustomize 174 kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. 175 $(KUSTOMIZE): $(LOCALBIN) 176 test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); } 177 178 .PHONY: controller-gen 179 controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. 180 $(CONTROLLER_GEN): $(LOCALBIN) 181 test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION) 182 `