forked from cert-manager/approver-policy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
293 lines (236 loc) · 11.2 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# Copyright 2021 The cert-manager 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.
MAKEFLAGS += --warn-undefined-variables --no-builtin-rules
SHELL := /usr/bin/env bash
.SHELLFLAGS := -uo pipefail -c
.DEFAULT_GOAL := help
.DELETE_ON_ERROR:
.SUFFIXES:
FORCE:
# The version of approver-policy
VERSION ?= $(shell git describe --tags --always --dirty --match='v*' --abbrev=14)
BINDIR := $(CURDIR)/_bin
.PHONY: all
all: help
ARCH ?= $(shell go env GOARCH)
OS ?= $(shell go env GOOS)
# Check https://github.com/helm/helm/releases for latest available release
HELM_VERSION ?= 3.11.3
# Check https://github.com/kubernetes-sigs/kubebuilder/blob/tools-releases/build/cloudbuild_tools.yaml
# for latest available tag
KUBEBUILDER_TOOLS_VERSION ?= 1.27.1
# Check https://github.com/kyverno/kyverno/releases for latest available release
KYVERNO_VERSION ?= v1.10.0
K8S_CLUSTER_NAME ?= approver-policy
IMAGE_REGISTRY ?= quay.io/jetstack
IMAGE_NAME := cert-manager-approver-policy
IMAGE_TAG := $(VERSION)
IMAGE := $(IMAGE_REGISTRY)/$(IMAGE_NAME):$(IMAGE_TAG)
GOMARKDOC_FLAGS=--format github --repository.url "https://github.com/cert-manager/approver-policy" --repository.default-branch master --repository.path /
# An OCI Helm chart registry where the Helm package will be uploaded on release.
# Empty by default to prevent accidental publication of the Helm chart.
HELM_CHART_REGISTRY ?=
helm_chart_archive := $(BINDIR)/charts/cert-manager-approver-policy-$(VERSION).tgz
##@ General
# The help target prints out all targets with their descriptions organized
# beneath their categories. The categories are represented by '##@' and the
# target descriptions by '##'. The awk commands is responsible for reading the
# entire set of makefiles included in this invocation, looking for lines of the
# file as xyz: ## something, and then pretty-format the target and help. Then,
# if there's a line with ##@ something, that gets pretty-printed as a category.
# More info on the usage of ANSI control characters for terminal formatting:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php
.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)
##@ Utilities
.PHONY: clean
clean: ## clean up created files
rm -rf \
$(BINDIR) \
_artifacts
##@ Build
.PHONY: build
build: ## Build manager binary.
CGO_ENABLED=0 go build -o bin/approver-policy ./cmd/
.PHONY: image
image: ## build docker image
docker build --tag ${IMAGE} --build-arg VERSION=$(VERSION) .
##@ Development
.PHONY: generate-manifests
generate-manifests: ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
generate-manifests: | $(BINDIR)/controller-gen
$(BINDIR)/controller-gen rbac:roleName=manager-role crd webhook paths="./..." \
output:crd:artifacts:config=$(helm_chart_source_dir)/templates/crds
.PHONY: generate-deepcopy
generate-deepcopy: ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
generate-deepcopy: | $(BINDIR)/controller-gen
$(BINDIR)/controller-gen object:headerFile="hack/boilerplate/boilerplate.go.txt" paths="./..."
.PHONY: generate-protos
generate-protos: ## Generate protocol buffer Go code.
generate-protos: | $(BINDIR)/protoc-gen-go
$(BINDIR)/protoc --plugin=$(BINDIR)/protoc-gen-go --proto_path=. --go_out=. --go_opt=paths=source_relative \
pkg/internal/approver/validation/certificaterequest.proto
.PHONY: generate-helm-docs
generate-helm-docs: ## Generate helm docs
generate-helm-docs: | $(BINDIR)/helm-docs
$(BINDIR)/helm-docs deploy/charts/approver-policy
.PHONY: generate-api-docs
generate-api-docs: | $(BINDIR)/gomarkdoc
mkdir -p docs/api
$(BINDIR)/gomarkdoc $(GOMARKDOC_FLAGS) --output docs/api/api.md github.com/cert-manager/approver-policy/pkg/apis/policy/v1alpha1
.PHONY: generate
generate: ## generate code and documentation
generate: fmt
generate: generate-manifests
generate: generate-deepcopy
generate: generate-helm-docs
generate: generate-api-docs
generate: generate-protos
.PHONY: fmt
fmt: ## Run go fmt against code.
go fmt ./...
.PHONY: vet
vet: ## Run go vet against code.
go vet ./...
##@ Testing
.PHONY: verify-pod-security-standards
verify-pod-security-standards: $(helm_chart_archive) | $(BINDIR)/kyverno $(BINDIR)/kustomize $(BINDIR)/helm
$(BINDIR)/kyverno apply <($(BINDIR)/kustomize build https://github.com/kyverno/policies/pod-security/enforce) \
--resource <($(BINDIR)/helm template $(helm_chart_archive)) 2>/dev/null
# Run both `helm lint` and `helm template`, to check that the templates can be rendered.
.PHONY: verify-helm-lint
verify-helm-lint: $(helm_chart_archive) | $(BINDIR)/helm
$(BINDIR)/helm lint $(helm_chart_archive)
$(BINDIR)/helm template $(helm_chart_archive) --values hack/helm/sample-chart-values.yaml
# instead of running verify-generate-api-docs, this target uses the gomarkdoc --check flag to verify that the docs are up to date
.PHONY: verify-api-docs
verify-api-docs: $(BINDIR)/gomarkdoc
@$(BINDIR)/gomarkdoc \
--check $(GOMARKDOC_FLAGS) \
--output docs/api/api.md github.com/cert-manager/approver-policy/pkg/apis/policy/v1alpha1 \
|| (echo "docs are not up to date; run 'make generate' and commit the result" && exit 1)
# Run the supplied make target argument in a temporary workspace and diff the results.
verify-%: FORCE
./hack/util/verify.sh $(MAKE) -s $*
.PHONY: verify
verify: ## Verify code and generate targets.
verify: vet
verify: verify-generate-manifests
verify: verify-generate-deepcopy
verify: verify-generate-helm-docs
verify: verify-api-docs
@echo "The following targets create temporary files in the current directory, that is why they have to be run last:"
$(MAKE) \
verify-helm-lint \
verify-pod-security-standards
cert_manager_crds: $(BINDIR)/cert-manager/crds.yaml
$(BINDIR)/cert-manager/crds.yaml: | $(BINDIR)
mkdir -p $(BINDIR)/cert-manager
curl -sSLo $(BINDIR)/cert-manager/crds.yaml https://github.com/cert-manager/cert-manager/releases/download/$(shell curl --silent "https://api.github.com/repos/cert-manager/cert-manager/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')/cert-manager.crds.yaml
.PHONY: test
test: cert_manager_crds tools ## Test approver-policy
KUBEBUILDER_ASSETS=$(BINDIR)/kubebuilder/bin \
CERT_MANAGER_CRDS=$(BINDIR)/cert-manager/crds.yaml \
APPROVER_POLICY_CRDS=$(CURDIR)/deploy/charts/approver-policy/templates/crds/policy.cert-manager.io_certificaterequestpolicies.yaml \
$(BINDIR)/ginkgo -procs=1 -v $(TEST_ARGS) ./cmd/... ./pkg/...
.PHONY: demo
demo: cert_manager_crds tools ## create cluster and deploy approver-policy
REPO_ROOT=$(shell pwd) ./hack/ci/create-cluster.sh
.PHONY: smoke
smoke: demo ## create cluster, deploy approver-policy, run smoke tests
REPO_ROOT=$(shell pwd) ./hack/ci/run-smoke-test.sh
REPO_ROOT=$(shell pwd) ./hack/ci/delete-cluster.sh
##@ Helm Chart
helm_chart_source_dir := deploy/charts/approver-policy
helm_chart_sources := $(shell find $(helm_chart_source_dir) -maxdepth 1 -type f) $(shell find $(helm_chart_source_dir)/templates -type f)
$(helm_chart_archive): $(helm_chart_sources) | $(BINDIR)/helm
$(eval helm_chart_source_dir_versioned := $(BINDIR)/charts/approver-policy-$(VERSION))
rm -rf $(helm_chart_source_dir_versioned)
mkdir -p $(dir $(helm_chart_source_dir_versioned))
cp -a $(helm_chart_source_dir) $(helm_chart_source_dir_versioned)
mkdir -p $(dir $@)
$(BINDIR)/helm package $(helm_chart_source_dir_versioned) \
--app-version $(VERSION) \
--version $(VERSION) \
--destination $(dir $@)
# Allow target to create GitHub outputs when run via GitHub Actions
GITHUB_OUTPUT ?= /dev/null
.PHONY: helm-chart
helm-chart: ## Create a helm chart
helm-chart: $(helm_chart_archive)
@echo path=$(helm_chart_archive) >> $(GITHUB_OUTPUT)
##@ Tools
-include osvars.mk
#
# protocol compiler
# ------------------
PROTOC_VERSION := 25.0
PROTOC_RELEASES_PATH := https://github.com/protocolbuffers/protobuf/releases/download
PROTOC_ZIP := protoc-$(PROTOC_VERSION)-$(PROTOC_PLATFORM).zip
PROTOC_DOWNLOAD := $(PROTOC_RELEASES_PATH)/v$(PROTOC_VERSION)/$(PROTOC_ZIP)
$(BINDIR)/protoc: | $(BINDIR)
curl --location $(PROTOC_DOWNLOAD) --output $(BINDIR)/$(PROTOC_ZIP)
unzip -o -d "$(BINDIR)" $(BINDIR)/$(PROTOC_ZIP) && mv $(BINDIR)/bin/protoc $@
$(BINDIR)/protoc-gen-go: | $(BINDIR)/protoc
cd hack/tools && go build -o $@ google.golang.org/protobuf/cmd/protoc-gen-go
$(BINDIR):
mkdir -p $@
$(BINDIR)/deepcopy-gen: | $(BINDIR)
cd hack/tools && go build -o $@ k8s.io/code-generator/cmd/deepcopy-gen
$(BINDIR)/controller-gen: | $(BINDIR)
cd hack/tools && go build -o $@ sigs.k8s.io/controller-tools/cmd/controller-gen
$(BINDIR)/ginkgo: | $(BINDIR)
cd hack/tools && go build -o $@ github.com/onsi/ginkgo/v2/ginkgo
$(BINDIR)/kind: | $(BINDIR)
cd hack/tools && go build -o $@ sigs.k8s.io/kind
$(BINDIR)/kustomize: | $(BINDIR)
cd hack/tools && go build -o $@ sigs.k8s.io/kustomize/kustomize/v5
$(BINDIR)/helm: | $(BINDIR)
curl -o $(BINDIR)/helm.tar.gz -sSL "https://get.helm.sh/helm-v$(HELM_VERSION)-$(OS)-$(ARCH).tar.gz"
tar -C $(BINDIR) -xzf $(BINDIR)/helm.tar.gz
cp $(BINDIR)/$(OS)-$(ARCH)/helm $@
rm -r $(BINDIR)/$(OS)-$(ARCH) $(BINDIR)/helm.tar.gz
$(BINDIR)/helm repo add jetstack https://charts.jetstack.io --force-update
$(BINDIR)/kubectl: | $(BINDIR)
curl -o $@ -LO "https://storage.googleapis.com/kubernetes-release/release/$(shell curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/$(OS)/$(ARCH)/kubectl"
chmod +x $@
$(BINDIR)/kubebuilder/bin/kube-apiserver: | $(BINDIR)
curl -sSLo $(BINDIR)/envtest-bins.tar.gz "https://storage.googleapis.com/kubebuilder-tools/kubebuilder-tools-$(KUBEBUILDER_TOOLS_VERSION)-$(OS)-$(ARCH).tar.gz"
mkdir -p $(BINDIR)/kubebuilder
tar -C $(BINDIR)/kubebuilder --strip-components=1 -zvxf $(BINDIR)/envtest-bins.tar.gz
$(BINDIR)/gomarkdoc: | $(BINDIR)
cd hack/tools && go build -o $@ github.com/princjef/gomarkdoc/cmd/gomarkdoc
$(BINDIR)/helm-docs: | $(BINDIR)
cd hack/tools && go build -o $@ github.com/norwoodj/helm-docs/cmd/helm-docs
$(BINDIR)/kyverno: | $(BINDIR)
curl https://github.com/kyverno/kyverno/releases/download/$(KYVERNO_VERSION)/kyverno-cli_$(KYVERNO_VERSION)_$(OS)_$(subst amd64,x86_64,${ARCH}).tar.gz -fsSL -o [email protected]
@# O writes the specified file to stdout
tar xfO [email protected] kyverno > $@
chmod +x $@
rm -f [email protected]
.PHONY: tools
tools: ## Download and setup all tools
tools: $(BINDIR)/deepcopy-gen
tools: $(BINDIR)/controller-gen
tools: $(BINDIR)/ginkgo
tools: $(BINDIR)/kind
tools: $(BINDIR)/helm
tools: $(BINDIR)/kubectl
tools: $(BINDIR)/kubebuilder/bin/kube-apiserver
tools: $(BINDIR)/gomarkdoc
tools: $(BINDIR)/helm-docs
tools: $(BINDIR)/protoc-gen-go