sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v3/scaffolds/internal/templates/makefile.go (about)

     1  /*
     2  Copyright 2020 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  	machinery.ComponentConfigMixin
    29  	machinery.ProjectNameMixin
    30  
    31  	// Image is controller manager image name
    32  	Image string
    33  	// BoilerplatePath is the path to the boilerplate file
    34  	BoilerplatePath string
    35  	// Controller tools version to use in the project
    36  	ControllerToolsVersion string
    37  	// Kustomize version to use in the project
    38  	KustomizeVersion string
    39  	// ControllerRuntimeVersion version to be used to download the envtest setup script
    40  	ControllerRuntimeVersion string
    41  }
    42  
    43  // SetTemplateDefaults implements file.Template
    44  func (f *Makefile) SetTemplateDefaults() error {
    45  	if f.Path == "" {
    46  		f.Path = "Makefile"
    47  	}
    48  
    49  	f.TemplateBody = makefileTemplate
    50  
    51  	f.IfExistsAction = machinery.Error
    52  
    53  	if f.Image == "" {
    54  		f.Image = "controller:latest"
    55  	}
    56  
    57  	return nil
    58  }
    59  
    60  //nolint:lll
    61  const makefileTemplate = `
    62  # Image URL to use all building/pushing image targets
    63  IMG ?= {{ .Image }}
    64  # ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
    65  ENVTEST_K8S_VERSION = 1.26.1
    66  
    67  # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
    68  ifeq (,$(shell go env GOBIN))
    69  GOBIN=$(shell go env GOPATH)/bin
    70  else
    71  GOBIN=$(shell go env GOBIN)
    72  endif
    73  
    74  # Setting SHELL to bash allows bash commands to be executed by recipes.
    75  # Options are set to exit when a recipe line exits non-zero or a piped command fails.
    76  SHELL = /usr/bin/env bash -o pipefail
    77  .SHELLFLAGS = -ec
    78  
    79  .PHONY: all
    80  all: build
    81  
    82  ##@ General
    83  
    84  # The help target prints out all targets with their descriptions organized
    85  # beneath their categories. The categories are represented by '##@' and the
    86  # target descriptions by '##'. The awk command is responsible for reading the
    87  # entire set of makefiles included in this invocation, looking for lines of the
    88  # file as xyz: ## something, and then pretty-format the target and help. Then,
    89  # if there's a line with ##@ something, that gets pretty-printed as a category.
    90  # More info on the usage of ANSI control characters for terminal formatting:
    91  # https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
    92  # More info on the awk command:
    93  # http://linuxcommand.org/lc3_adv_awk.php
    94  
    95  .PHONY: help
    96  help: ## Display this help.
    97  	@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)
    98  
    99  ##@ Development
   100  
   101  .PHONY: manifests
   102  manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
   103  	$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
   104  
   105  .PHONY: generate
   106  generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
   107  	$(CONTROLLER_GEN) object:headerFile={{printf "%q" .BoilerplatePath}} paths="./..."
   108  
   109  .PHONY: fmt
   110  fmt: ## Run go fmt against code.
   111  	go fmt ./...
   112  
   113  .PHONY: vet
   114  vet: ## Run go vet against code.
   115  	go vet ./...
   116  
   117  .PHONY: test
   118  test: manifests generate fmt vet envtest ## Run tests.
   119  	KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
   120  
   121  ##@ Build
   122  
   123  .PHONY: build
   124  build: manifests generate fmt vet ## Build manager binary.
   125  	go build -o bin/manager main.go
   126  
   127  .PHONY: run
   128  run: manifests generate fmt vet ## Run a controller from your host.
   129  	go run ./main.go
   130  
   131  # If you wish to build the manager image targeting other platforms you can use the --platform flag.
   132  # (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
   133  # More info: https://docs.docker.com/develop/develop-images/build_enhancements/
   134  .PHONY: docker-build
   135  docker-build: test ## Build docker image with the manager.
   136  	docker build -t ${IMG} .
   137  
   138  .PHONY: docker-push
   139  docker-push: ## Push docker image with the manager.
   140  	docker push ${IMG}
   141  
   142  # PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
   143  # architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
   144  # - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
   145  # - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
   146  # - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
   147  # To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
   148  PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
   149  .PHONY: docker-buildx
   150  docker-buildx: test ## Build and push docker image for the manager for cross-platform support
   151  	# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
   152  	sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
   153  	- docker buildx create --name project-v3-builder
   154  	docker buildx use project-v3-builder
   155  	- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
   156  	- docker buildx rm project-v3-builder
   157  	rm Dockerfile.cross
   158  
   159  ##@ Deployment
   160  
   161  ifndef ignore-not-found
   162    ignore-not-found = false
   163  endif
   164  
   165  .PHONY: install
   166  install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
   167  	$(KUSTOMIZE) build config/crd | kubectl apply -f -
   168  
   169  .PHONY: uninstall
   170  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.
   171  	$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
   172  
   173  .PHONY: deploy
   174  deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
   175  	cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
   176  	$(KUSTOMIZE) build config/default | kubectl apply -f -
   177  
   178  .PHONY: undeploy
   179  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.
   180  	$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
   181  
   182  ##@ Build Dependencies
   183  
   184  ## Location to install dependencies to
   185  LOCALBIN ?= $(shell pwd)/bin
   186  $(LOCALBIN):
   187  	mkdir -p $(LOCALBIN)
   188  
   189  ## Tool Binaries
   190  KUSTOMIZE ?= $(LOCALBIN)/kustomize
   191  CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
   192  ENVTEST ?= $(LOCALBIN)/setup-envtest
   193  
   194  ## Tool Versions
   195  KUSTOMIZE_VERSION ?= {{ .KustomizeVersion }}
   196  CONTROLLER_TOOLS_VERSION ?= {{ .ControllerToolsVersion }}
   197  
   198  KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
   199  .PHONY: kustomize
   200  kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
   201  $(KUSTOMIZE): $(LOCALBIN)
   202  	@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
   203  		echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
   204  		rm -rf $(LOCALBIN)/kustomize; \
   205  	fi
   206  	test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) --output install_kustomize.sh && bash install_kustomize.sh $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); rm install_kustomize.sh; }
   207  
   208  .PHONY: controller-gen
   209  controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
   210  $(CONTROLLER_GEN): $(LOCALBIN)
   211  	test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
   212  	GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
   213  
   214  .PHONY: envtest
   215  envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
   216  $(ENVTEST): $(LOCALBIN)
   217  	test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
   218  `