-
Notifications
You must be signed in to change notification settings - Fork 4
/
Makefile
192 lines (148 loc) · 7.79 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
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
# Image URL to use all building/pushing image targets
TAG ?= latest
GHCR_REPO ?= ghcr.io/timebertt/kubernetes-controller-sharding
SHARDER_IMG ?= $(GHCR_REPO)/sharder:$(TAG)
SHARD_IMG ?= $(GHCR_REPO)/shard:$(TAG)
WEBHOSTING_OPERATOR_IMG ?= $(GHCR_REPO)/webhosting-operator:$(TAG)
EXPERIMENT_IMG ?= $(GHCR_REPO)/experiment:$(TAG)
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.28
# Setting SHELL to bash allows bash commands to be executed by recipes.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec
.PHONY: all
all: build
##@ 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)
##@ Tools
include hack/tools.mk
.PHONY: clean-tools-bin
clean-tools-bin: ## Empty the tools binary directory.
rm -rf $(TOOLS_BIN_DIR)/*
##@ Development
.PHONY: tidy
tidy: ## Runs go mod to ensure modules are up to date.
go mod tidy
cd webhosting-operator && go mod tidy
@# regenerate go.work.sum
rm -f go.work.sum
go mod download
.PHONY: generate-fast
generate-fast: $(CONTROLLER_GEN) tidy ## Run all fast code generators for the main module.
$(CONTROLLER_GEN) rbac:roleName=sharder crd paths="./pkg/..." output:rbac:artifacts:config=config/rbac output:crd:artifacts:config=config/crds
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./pkg/..."
.PHONY: generate-fast-webhosting
generate-fast-webhosting: $(CONTROLLER_GEN) tidy ## Run all fast code generators for the webhosting-operator module.
$(CONTROLLER_GEN) rbac:roleName=operator crd paths="./webhosting-operator/..." output:rbac:artifacts:config=webhosting-operator/config/manager/rbac output:crd:artifacts:config=webhosting-operator/config/manager/crds
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./webhosting-operator/..."
.PHONY: generate
generate: $(VGOPATH) generate-fast generate-fast-webhosting tidy ## Run all code generators.
hack/update-codegen.sh
.PHONY: fmt
fmt: ## Run go fmt against code.
go fmt ./...
cd webhosting-operator && go fmt ./...
.PHONY: test
test: $(SETUP_ENVTEST) ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(SETUP_ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -race ./cmd/... ./pkg/... ./webhosting-operator/pkg/...
.PHONY: test-kyverno
test-kyverno: $(KYVERNO) ## Run kyverno policy tests.
$(KYVERNO) test --remove-color -v 4 .
.PHONY: test-e2e
test-e2e: $(GINKGO) ## Run e2e tests.
ginkgo run --timeout=1h --poll-progress-after=60s --poll-progress-interval=30s --randomize-all --randomize-suites --keep-going --vv $(GINKGO_FLAGS) ./test/e2e/...
.PHONY: skaffold-fix
skaffold-fix: $(SKAFFOLD) ## Upgrade skaffold configuration to the latest apiVersion.
$(SKAFFOLD) fix --overwrite
[ ! -f $(SKAFFOLD_FILENAME).v2 ] || rm $(SKAFFOLD_FILENAME).v2
##@ Verification
.PHONY: lint
lint: $(GOLANGCI_LINT) ## Run golangci-lint against code.
$(GOLANGCI_LINT) run ./... ./webhosting-operator/...
.PHONY: check
check: lint test test-kyverno ## Check everything (lint + test + test-kyverno).
.PHONY: verify-fmt
verify-fmt: fmt ## Verify go code is formatted.
@if !(git diff --quiet HEAD); then \
echo "unformatted files detected, please run 'make fmt'"; exit 1; \
fi
.PHONY: verify-generate
verify-generate: generate ## Verify generated files are up to date.
@if !(git diff --quiet HEAD); then \
echo "generated files are out of date, please run 'make generate'"; exit 1; \
fi
.PHONY: verify-tidy
verify-tidy: tidy ## Verify go module files are up to date.
@if !(git diff --quiet HEAD -- go.work.sum go.{mod,sum} webhosting-operator/go.{mod,sum}); then \
echo "go module files are out of date, please run 'make tidy'"; exit 1; \
fi
.PHONY: verify
verify: verify-tidy verify-fmt verify-generate check ## Verify everything (all verify-* rules + check).
.PHONY: ci-e2e-kind
ci-e2e-kind: $(KIND)
./hack/ci-e2e-kind.sh
##@ Build
.PHONY: build
build: ## Build the sharder binary.
go build -o bin/sharder ./cmd/sharder
.PHONY: run
run: $(KUBECTL) generate-fast ## Run the sharder from your host and deploy prerequisites.
$(MAKE) deploy SKAFFOLD_MODULE=cert-manager
$(KUBECTL) apply --server-side --force-conflicts -k config/crds
$(KUBECTL) apply --server-side --force-conflicts -k hack/config/certificates/host
go run ./cmd/sharder --config=hack/config/sharder/host/config.yaml --zap-log-level=debug
SHARD_NAME ?= shard-$(shell tr -dc bcdfghjklmnpqrstvwxz2456789 </dev/urandom | head -c 8)
.PHONY: run-shard
run-shard: $(KUBECTL) ## Run a shard from your host and deploy prerequisites.
$(KUBECTL) apply --server-side --force-conflicts -k hack/config/shard/clusterring
go run ./cmd/shard --shard=$(SHARD_NAME) --lease-namespace=default --zap-log-level=debug
PUSH ?= false
images: export KO_DOCKER_REPO = $(GHCR_REPO)
.PHONY: images
images: $(KO) ## Build and push container images using ko.
$(KO) build --push=$(PUSH) --sbom none --base-import-paths -t $(TAG) --platform linux/amd64,linux/arm64 \
./cmd/sharder ./cmd/shard ./webhosting-operator/cmd/webhosting-operator
##@ Deployment
KIND_KUBECONFIG := $(PROJECT_DIR)/hack/kind_kubeconfig.yaml
kind-up kind-down: export KUBECONFIG = $(KIND_KUBECONFIG)
.PHONY: kind-up
kind-up: $(KIND) $(KUBECTL) ## Launch a kind cluster for local development and testing.
$(KIND) create cluster --name sharding --config hack/config/kind-config.yaml
# workaround https://kind.sigs.k8s.io/docs/user/known-issues/#pod-errors-due-to-too-many-open-files
$(KUBECTL) get nodes -o name | cut -d/ -f2 | xargs -I {} docker exec {} sh -c "sysctl fs.inotify.max_user_instances=8192"
# run `export KUBECONFIG=$$PWD/hack/kind_kubeconfig.yaml` to target the created kind cluster.
.PHONY: kind-down
kind-down: $(KIND) ## Tear down the kind testing cluster.
$(KIND) delete cluster --name sharding
export SKAFFOLD_FILENAME = hack/config/skaffold.yaml
# use static label for skaffold to prevent rolling all components on every skaffold invocation
deploy up dev down: export SKAFFOLD_LABEL = skaffold.dev/run-id=sharding
# use dedicated ghcr repo for dev images to prevent spamming the "production" image repo
up dev: export SKAFFOLD_DEFAULT_REPO ?= ghcr.io/timebertt/dev-images
up dev: export SKAFFOLD_TAIL ?= true
.PHONY: deploy
deploy: $(SKAFFOLD) $(KUBECTL) $(YQ) ## Build all images and deploy everything to K8s cluster specified in $KUBECONFIG.
$(SKAFFOLD) deploy -i $(SHARDER_IMG) -i $(SHARD_IMG) -i $(WEBHOSTING_OPERATOR_IMG) -i $(EXPERIMENT_IMG)
.PHONY: up
up: $(SKAFFOLD) $(KUBECTL) $(YQ) ## Build all images, deploy everything to K8s cluster specified in $KUBECONFIG, start port-forward and tail logs.
$(SKAFFOLD) run
.PHONY: dev
dev: $(SKAFFOLD) $(KUBECTL) $(YQ) ## Start continuous dev loop with skaffold.
$(SKAFFOLD) dev --port-forward=user --cleanup=false --trigger=manual
.PHONY: down
down: $(SKAFFOLD) $(KUBECTL) $(YQ) ## Remove everything from K8s cluster specified in $KUBECONFIG.
$(SKAFFOLD) delete