generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMakefile
151 lines (120 loc) · 5.6 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Copyright 2024 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
.DEFAULT_GOAL := help
.SUFFIXES: # remove legacy builtin suffixes to allow easier make debugging
SHELL = /usr/bin/env bash
# If GOARCH is not set in the env, find it
GOARCH ?= $(shell go env GOARCH)
## ==== ARGS =====
DOCKER ?= docker # Container build tool compatible with `docker` API
PLATFORM ?= linux/$(GOARCH) # Platform for 'build'
BUILD_ARGS ?= # Additional args for 'build'
SAMPLE_DRIVER_TAG ?= cosi-driver-sample:latest # Image tag for controller image build
##@ General
.PHONY: help
help: ## Display this help.
@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)
.PHONY: clean
clean:
rm -rf $(LOCALBIN)
.PHONY: all
all: clean lint test
##@ Image
.PHONY: build
build: ## Build only the controller container image
$(DOCKER) build --file Dockerfile --platform $(PLATFORM) $(BUILD_ARGS) --tag $(SAMPLE_DRIVER_TAG) .
##@ Release
.PHONY: release-tag
release-tag: ## Tag the repository for release.
git tag $(GIT_TAG)
git push origin $(GIT_TAG)
##@ Development
.PHONY: test
test: test-unit ## Run all tests.
.PHONY: test-unit
test-unit: ## Run unit tests.
GO111MODULE=on GOARCH=$(ARCH) go test -cover -race ./pkg/... ./cmd/...
.PHONY: lint
lint: golangci-lint ## Run golangci-lint linter.
$(GOLANGCI_LINT) run
.PHONY: lint-fix
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes.
$(GOLANGCI_LINT) run --fix
.PHONY: lint-logging
lint-logging: logcheck ## Run logcheck linter to verify the logging practices.
$(LOGCHECK) ./... || (echo 'Fix structured logging' && exit 1)
.PHONY: lint-manifests
lint-manifests: kustomize kube-linter ## Run kube-linter on Kubernetes manifests.
$(KUSTOMIZE) build config/default |\
$(KUBE_LINTER) lint --config=./config/.kube-linter.yaml -
.PHONY: verify-licenses
verify-licenses: addlicense ## Run addlicense to verify if files have license headers.
find -type f -name "*.go" ! -path "*/vendor/*" | xargs $(ADDLICENSE) -check || (echo 'Run "make update"' && exit 1)
.PHONY: add-licenses
add-licenses: addlicense ## Run addlicense to append license headers to files missing one.
find -type f -name "*.go" ! -path "*/vendor/*" | xargs $(ADDLICENSE) -c "The Kubernetes Authors."
##@ Dependencies
## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)
## Tool Binaries
ADDLICENSE ?= $(LOCALBIN)/addlicense
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
KUBE_LINTER ?= $(LOCALBIN)/kube-linter
KUSTOMIZE ?= $(LOCALBIN)/kustomize
LOGCHECK ?= $(LOCALBIN)/logcheck
## Tool Versions
ADDLICENSE_VERSION ?= $(shell grep 'github.com/google/addlicense ' ./go.mod | cut -d ' ' -f 2)
GOLANGCI_LINT_VERSION ?= $(shell grep 'github.com/golangci/golangci-lint ' ./go.mod | cut -d ' ' -f 2)
KUBE_LINTER_VERSION ?= $(shell grep 'golang.stackrox.io/kube-linter ' ./go.mod | cut -d ' ' -f 2)
KUSTOMIZE_VERSION ?= $(shell grep 'sigs.k8s.io/kustomize/kustomize/v5 ' ./go.mod | cut -d ' ' -f 2)
LOGCHECK_VERSION ?= $(shell grep 'sigs.k8s.io/logtools ' ./go.mod | cut -d ' ' -f 2)
.PHONY: tools
tools: addlicense golangci-lint kube-linter kustomize logcheck
.PHONY: addlicense
addlicense: $(ADDLICENSE)$(ADDLICENSE_VERSION) ## Download addlicense locally if necessary.
$(ADDLICENSE)$(ADDLICENSE_VERSION): $(LOCALBIN)
$(call go-install-tool,$(ADDLICENSE),github.com/google/addlicense,$(ADDLICENSE_VERSION))
.PHONY: golangci-lint
golangci-lint: $(GOLANGCI_LINT)$(GOLANGCI_LINT_VERSION) ## Download golangci-lint locally if necessary.
$(GOLANGCI_LINT)$(GOLANGCI_LINT_VERSION): $(LOCALBIN)
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
.PHONY: kube-linter
kube-linter: $(KUBE_LINTER)$(KUBE_LINTER_VERSION)
$(KUBE_LINTER)$(KUBE_LINTER_VERSION): $(LOCALBIN) ## Download kube-linter locally if necessary.
$(call go-install-tool,$(KUBE_LINTER),golang.stackrox.io/kube-linter/cmd/kube-linter,$(KUBE_LINTER_VERSION))
.PHONY: kustomize
kustomize: $(KUSTOMIZE)$(KUSTOMIZE_VERSION) ## Download kustomize locally if necessary.
$(KUSTOMIZE)$(KUSTOMIZE_VERSION): $(LOCALBIN)
$(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION))
.PHONY: logcheck
logcheck: $(LOGCHECK)$(LOGCHECK_VERSION) ## Download logcheck locally if necessary.
$(LOGCHECK)$(LOGCHECK_VERSION): $(LOCALBIN)
$(call go-install-tool,$(LOGCHECK),sigs.k8s.io/logtools/logcheck,$(LOGCHECK_VERSION))
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
# $1 - target path with name of binary
# $2 - package url which can be installed
# $3 - specific version of package
define go-install-tool
@[ -f "$(1)-$(3)" ] || { \
set -e; \
package=$(2)@$(3) ;\
echo "Downloading $${package}" ;\
rm -f $(1) || true ;\
GOBIN=$(LOCALBIN) go install $${package} ;\
mv $(1) $(1)-$(3) ;\
} ;\
ln -sf $(1)-$(3) $(1)
endef