-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMakefile
More file actions
247 lines (205 loc) · 9.59 KB
/
Copy pathMakefile
File metadata and controls
247 lines (205 loc) · 9.59 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
PROJECT_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
include $(PROJECT_DIR)/Makefile.versions
CONTROLLER_TOOLS_VERSION = 0.20.1
BIN_DIR := $(PROJECT_DIR)/bin
CRD_DIR := $(PROJECT_DIR)/config/crd/third
WORKFLOWS_DIR := $(PROJECT_DIR)/.github/workflows
KUSTOMIZE := $(BIN_DIR)/kustomize
CONTROLLER_GEN := $(BIN_DIR)/controller-gen
SETUP_ENVTEST := $(BIN_DIR)/setup-envtest
STATICCHECK := $(BIN_DIR)/staticcheck
CUSTOMCHECKER := $(BIN_DIR)/custom-checker
GOIMPORTS := $(BIN_DIR)/goimports
KIND := $(BIN_DIR)/kind
GH := $(BIN_DIR)/gh
YQ := $(BIN_DIR)/yq
KUBECTL := $(BIN_DIR)/kubectl
HELM := $(BIN_DIR)/helm
# Image URL to use all building/pushing image targets
IMG ?= ghcr.io/cybozu-go/contour-plus:latest
# Set the shell used to bash for better error handling.
SHELL = /bin/bash
.SHELLFLAGS = -e -o pipefail -c
.PHONY: all
all: help
##@ Basic
.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: setup
setup: download-tools download-crds ## Setup
.PHONY: download-tools
download-tools: $(GH) $(YQ) $(KUBECTL) $(HELM)
GOBIN=$(BIN_DIR) go install sigs.k8s.io/controller-tools/cmd/controller-gen@v$(CONTROLLER_TOOLS_VERSION)
GOBIN=$(BIN_DIR) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@v$(SETUP_ENVTEST_VERSION)
GOBIN=$(BIN_DIR) go install sigs.k8s.io/kustomize/kustomize/v5@v$(KUSTOMIZE_VERSION)
GOBIN=$(BIN_DIR) go install github.com/cybozu-go/golang-custom-analyzer/cmd/custom-checker@v$(CUSTOM_CHECKER_VERSION)
GOBIN=$(BIN_DIR) go install honnef.co/go/tools/cmd/staticcheck@v$(STATICCHECK_VERSION)
GOBIN=$(BIN_DIR) go install golang.org/x/tools/cmd/goimports@v$(GOIMPORTS_VERSION)
GOBIN=$(BIN_DIR) go install sigs.k8s.io/kind@v$(KIND_CLI_VERSION)
.PHONY: download-crds
download-crds:
curl -fsL -o $(CRD_DIR)/certmanager.yml -sLf https://github.com/jetstack/cert-manager/releases/download/$(call upstream-tag,$(CERT_MANAGER_VERSION))/cert-manager.crds.yaml
echo "$(CERTMANAGER_CRD_SHA256) $(CRD_DIR)/certmanager.yml" | sha256sum --check
curl -fsL -o $(CRD_DIR)/dnsendpoint.yml -sLf https://github.com/kubernetes-sigs/external-dns/raw/$(call upstream-tag,$(EXTERNAL_DNS_VERSION))/config/crd/standard/dnsendpoints.externaldns.k8s.io.yaml
echo "$(EXTERNALDNS_CRD_SHA256) $(CRD_DIR)/dnsendpoint.yml" | sha256sum --check
curl -fsL -o $(CRD_DIR)/httpproxy.yml -sLf https://github.com/projectcontour/contour/raw/$(call upstream-tag,$(CONTOUR_VERSION))/examples/contour/01-crds.yaml
echo "$(CONTOUR_CRD_SHA256) $(CRD_DIR)/httpproxy.yml" | sha256sum --check
$(GH):
mkdir -p $(BIN_DIR)
wget -qO $(BIN_DIR)/gh.tar.gz https://github.com/cli/cli/releases/download/v$(GH_VERSION)/gh_$(GH_VERSION)_linux_amd64.tar.gz
echo "$(GH_SHA256) $(BIN_DIR)/gh.tar.gz" | sha256sum --check
tar -zx -O -f $(BIN_DIR)/gh.tar.gz gh_$(GH_VERSION)_linux_amd64/bin/gh > $@
chmod +x $@
rm $(BIN_DIR)/gh.tar.gz
$(YQ):
mkdir -p $(BIN_DIR)
wget -qO $@ https://github.com/mikefarah/yq/releases/download/v$(YQ_VERSION)/yq_linux_amd64
echo "$(YQ_SHA256) $@" | sha256sum --check
chmod +x $@
$(KUBECTL):
mkdir -p $(BIN_DIR)
wget -qO $@ https://dl.k8s.io/release/v$(ENVTEST_K8S_VERSION)/bin/linux/amd64/kubectl
echo "$(KUBECTL_SHA256) $@" | sha256sum --check
chmod +x $@
$(HELM):
mkdir -p $(BIN_DIR)
wget -qO $(BIN_DIR)/helm.tar.gz https://get.helm.sh/helm-v$(HELM_VERSION)-linux-amd64.tar.gz
echo "$(HELM_SHA256) $(BIN_DIR)/helm.tar.gz" | sha256sum --check
tar -xzf $(BIN_DIR)/helm.tar.gz -C $(BIN_DIR) linux-amd64/helm
mv $(BIN_DIR)/linux-amd64/helm $@
chmod +x $@
rm -rf $(BIN_DIR)/linux-amd64 $(BIN_DIR)/helm.tar.gz
.PHONY: clean
clean: ## Clean files
rm -rf $(BIN_DIR)/* $(CRD_DIR)/*
##@ Build
.PHONY: manifests
manifests: ## Generate manifests e.g. CRD, RBAC etc.
$(CONTROLLER_GEN) rbac:roleName=contour-plus webhook paths="./..."
.PHONY: generate
generate: ## Generate code
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
.PHONY: build
build: ## Build manager binary
CGO_ENABLED=0 go build -o bin/contour-plus -ldflags="-w -s" main.go
.PHONY: docker-build
docker-build: build ## Build the docker image
docker build . -t ${IMG}
##@ Maintenance
.PHONY: login-gh
login-gh: ## Login to GitHub
if ! $(GH) auth status 2>/dev/null; then \
echo; \
echo '!! You need login to GitHub to proceed. Please follow the next command with "Authenticate Git with your GitHub credentials? (Y)".'; \
echo; \
$(GH) auth login -h github.com -p HTTPS -w; \
fi
.PHONY: logout-gh
logout-gh: ## Logout from GitHub
$(GH) auth logout
.PHONY: update-contour
update-contour: ## Update Contour and Kubernetes in go.mod
$(call get-latest-gh-package-tag,contour)
go get github.com/projectcontour/contour@$(call upstream-tag,$(latest_tag))
K8S_MINOR_VERSION="0."$$(go list -m -f '{{.Version}}' k8s.io/api | cut -d'.' -f2); \
K8S_PACKAGE_VERSION="$$(go list -m -versions k8s.io/api | tr ' ' '\n' | grep $${K8S_MINOR_VERSION} | sort -V | tail -n 1)"; \
go get k8s.io/api@$${K8S_PACKAGE_VERSION}; \
go get k8s.io/apimachinery@$${K8S_PACKAGE_VERSION}; \
go get k8s.io/client-go@$${K8S_PACKAGE_VERSION}; \
go mod tidy
.PHONY: version
version: login-gh ## Update dependent versions
$(call update-version,actions/checkout,ACTIONS_CHECKOUT_VERSION,1)
$(call update-version,actions/create-release,ACTIONS_CREATE_RELEASE_VERSION,1)
$(call update-version,actions/setup-go,ACTIONS_SETUP_GO_VERSION,1)
$(call update-version-ghcr,cert-manager,CERT_MANAGER_VERSION)
$(call update-version-ghcr,contour,CONTOUR_VERSION)
$(call update-version-ghcr,external-dns,EXTERNAL_DNS_VERSION)
$(call update-version-ghcr,envoy,ENVOY_VERSION)
$(call update-version-ghcr,etcd,ETCD_VERSION)
$(call update-version-ghcr,coredns,COREDNS_VERSION)
$(call get-latest-gh-package-tag,argocd)
NEW_VERSION=$$(docker run ghcr.io/cybozu/argocd:$(latest_tag) kustomize version | cut -c2-); \
sed -i -e "s/KUSTOMIZE_VERSION := .*/KUSTOMIZE_VERSION := $${NEW_VERSION}/g" Makefile.versions
K8S_MINOR_VERSION="1."$$(go list -m -f '{{.Version}}' k8s.io/api | cut -d'.' -f2); \
NEW_VERSION=$$($(SETUP_ENVTEST) list | tr -s ' ' | cut -d' ' -f2 | fgrep $${K8S_MINOR_VERSION} | sort -V | tail -n 1 | cut -c2-); \
sed -i -e "s/ENVTEST_K8S_VERSION := .*/ENVTEST_K8S_VERSION := $${NEW_VERSION}/g" Makefile.versions
# update kindest node version
K8S_MINOR_VERSION="1."$$(go list -m -f '{{.Version}}' k8s.io/api | cut -d'.' -f2); \
NEW_VERSION=$$( \
curl -fsSL "https://hub.docker.com/v2/repositories/kindest/node/tags?page_size=50&name=v$${K8S_MINOR_VERSION}." \
| jq -r '.results[].name' \
| grep -E "^v$${K8S_MINOR_VERSION}\.[0-9]+$$" \
| sort -V \
| tail -n 1 \
); \
echo "Updating kindest/node to $$NEW_KINDEST_TAG"; \
sed -i -e "s/KINDEST_NODE_VERSION := .*/KINDEST_NODE_VERSION := $${NEW_VERSION#v}/g" Makefile.versions
.PHONY: update-actions
update-actions:
$(call update-trusted-action,actions/checkout,$(ACTIONS_CHECKOUT_VERSION))
$(call update-trusted-action,actions/create-release,$(ACTIONS_CREATE_RELEASE_VERSION))
$(call update-trusted-action,actions/setup-go,$(ACTIONS_SETUP_GO_VERSION))
.PHONY: maintenance
maintenance: ## Update dependent manifests
$(MAKE) update-actions
$(MAKE) download-crds
.PHONY: list-actions
list-actions: ## List used GitHub Actions
@{ for i in $(shell ls $(WORKFLOWS_DIR)); do \
$(YQ) '.. | select(has("uses")).uses' $(WORKFLOWS_DIR)/$$i; \
done } | sort | uniq
##@ Test
.PHONY: check-generate
check-generate: ## Check for commit omissions of auto-generated files
$(MAKE) manifests
$(MAKE) generate
$(GOIMPORTS) -w -local github.com/cybozu-go/contour-plus .
go mod tidy
git diff --exit-code --name-only
.PHONY: lint
lint: ## Run lint tools
test -z "$$(gofmt -s -l . | tee /dev/stderr)"
$(STATICCHECK) ./...
test -z "$$($(CUSTOMCHECKER) -restrictpkg.packages=html/template,log $$(go list -tags='$(GOTAGS)' ./... ) 2>&1 | tee /dev/stderr)"
go vet ./...
.PHONY: test
test: ## Run unit tests
source <($(SETUP_ENVTEST) use -p env $(ENVTEST_K8S_VERSION)) && \
go test -race -v -count 1 ./controllers/...
# usage get-latest-gh OWNER/REPO
define get-latest-gh
$(eval latest_gh := $(shell $(GH) release list --repo $1 | grep Latest | cut -f3))
endef
# usage: get-latest-gh-release OWNER/REPO VARNAMESUFFIXopt
# get the latest release from github.com/OWNER/REPO
define get-latest-gh-release
$(eval latest_gh$2 := $(shell curl -sSf https://api.github.com/repos/$1/releases/latest | jq -r '.tag_name'))
endef
# usage: get-latest-gh-package-tag NAME
define get-latest-gh-package-tag
$(eval latest_tag := $(shell curl -sSf -H "Authorization: Bearer $(shell curl -sSf "https://ghcr.io/token?scope=repository%3Acybozu%2F$1%3Apull&service=ghcr.io" | jq -r .token)" https://ghcr.io/v2/cybozu/$1/tags/list | jq -r '.tags[]' | sort -Vr | head -n 1))
endef
# usage: upstream-tag 1.2.3.4
# do not indent because it appears on output
define upstream-tag
$(shell echo $1 | sed -E 's/^(.*)\.[[:digit:]]+$$/v\1/')
endef
# usage update-version OWNER/REPO VAR MAJOR
define update-version
$(call get-latest-gh,$1)
NEW_VERSION=$$(echo $(latest_gh) | if [ -z "$3" ]; then cut -b 2-; else cut -b 2; fi); \
sed -i -e "s/$2 := .*/$2 := $${NEW_VERSION}/g" Makefile.versions
endef
# usage update-version-ghcr NAME VAR
define update-version-ghcr
$(call get-latest-gh-package-tag,$1)
sed -i -e "s/$2 := .*/$2 := $(latest_tag)/g" Makefile.versions
endef
# usage update-trusted-action OWNER/REPO VERSION
define update-trusted-action
for i in $(shell ls $(WORKFLOWS_DIR)); do \
$(YQ) -i '(.. | select(has("uses")) | select(.uses | contains("$1"))).uses = "$1@v$2"' $(WORKFLOWS_DIR)/$$i; \
done
endef