forked from kloeckner-i/db-operator
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Makefile
153 lines (133 loc) · 6.85 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
# ---------------------------------------------------------------------
# -- Image URL to use all building/pushing image targets
# ---------------------------------------------------------------------
IMAGE_TAG ?= my-db-operator:v1.0.0-dev
# ---------------------------------------------------------------------
# -- ENVTEST_K8S_VERSION refers to the version of kubebuilder assets
# -- to be downloaded by envtest binary.
# ---------------------------------------------------------------------
ENVTEST_K8S_VERSION = 1.28.0
# ---------------------------------------------------------------------
# -- CONTROLLER_GEN_VERSION a version of the controller-get tool
# ---------------------------------------------------------------------
CONTROLLER_GEN_VERSION = v0.14.0
# ---------------------------------------------------------------------
# -- K8s version to start a local kubernetes
# ---------------------------------------------------------------------
K8S_VERSION ?= v1.22.3
# ---------------------------------------------------------------------
# -- Helper tools version
# ---------------------------------------------------------------------
GOLANGCI_LINT_VERSION ?= v1.59.1
# ---------------------------------------------------------------------
# -- Get the currently used golang install path
# -- (in GOPATH/bin, unless GOBIN is set)
# ---------------------------------------------------------------------
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif
# ---------------------------------------------------------------------
# -- Which container tool to use
# ---------------------------------------------------------------------
CONTAINER_TOOL ?= docker
# ---------------------------------------------------------------------
# -- Which compose tool to use
# -- For example
# -- $ export COMPOSE_TOOL="nerdctl compose"
# ---------------------------------------------------------------------
COMPOSE_TOOL ?= docker-compose
# ---------------------------------------------------------------------
# -- It's required when you want to use k3s and nerdctl
# -- $ export CONTAINER_TOOL_NAMESPACE_ARG="--namespace k8s.io"
# ---------------------------------------------------------------------
CONTAINER_TOOL_NAMESPACE_ARG ?=
# -- Set additional argumets to container tool
# -- To use, set an environment variable:
# -- $ export CONTAINER_TOOL_ARGS="--build-arg GOARCH=arm64 --platform=linux/arm64"
# ---------------------------------------------------------------------
CONTAINER_TOOL_ARGS ?=
# ---------------------------------------------------------------------
# -- A path to store binaries that are used in the Makefile
# ---------------------------------------------------------------------
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec
# ---------------------------------------------------------------------
# -- Rules
# ---------------------------------------------------------------------
help: ## show this help
@echo 'usage: make [target] ...'
@echo ''
@echo 'targets:'
@grep -E '^(.+)\:\ .*##\ (.+)' ${MAKEFILE_LIST} | sort | sed 's/:.*##/#/' | column -t -c 2 -s '#'
.PHONY: build
build: ## Build a container
$(CONTAINER_TOOL) build ${CONTAINER_TOOL_ARGS} -t ${IMAGE_TAG} . ${CONTAINER_TOOL_NAMESPACE_ARG} --build-arg="OPERATOR_VERSION=v1.0.0-dev"
$(CONTAINER_TOOL) save ${CONTAINER_TOOL_NAMESPACE_ARG} ${IMAGE_TAG} -o my-image.tar
# ---------------------------------------------------------------------
# -- Go related rules
# ---------------------------------------------------------------------
lint: ## lint go code
echo "desired golangci-lint version is ${GOLANGCI_LINT_VERSION}"
@go get -u all
@go mod tidy
test -s $(LOCALBIN)/golangci-lint || GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@${GOLANGCI_LINT_VERSION}
$(LOCALBIN)/golangci-lint --version
$(LOCALBIN)/golangci-lint run ./... --timeout 240s
fmt: ## Format go code
@test -s $(LOCALBIN)/gofumpt || GOBIN=$(LOCALBIN) go install mvdan.cc/gofumpt@latest
@$(LOCALBIN)/gofumpt -l -w .
vet: ## go vet to find issues
@go vet ./...
# ---------------------------------------------------------------------
# -- Tests are not passing when GOARCH is not amd64, so it's hardcoded
# ---------------------------------------------------------------------
unit: ## run go unit tests
GOARCH=amd64 go test -tags tests -run "TestUnit" ./... -v
test: ## run go all test
$(COMPOSE_TOOL) down
$(COMPOSE_TOOL) up -d
$(COMPOSE_TOOL) restart sqladmin
sleep 10
GOARCH=amd64 go test -count=1 -tags tests ./... -v -cover
$(COMPOSE_TOOL) down
# ---------------------------------------------------------------------
# -- Kubebuilder realted rule
# ---------------------------------------------------------------------
addexamples: ## add examples via kubectl create -f examples/
cd ./examples/; ls | while read line; do kubectl apply -f $$line; done
manifests: controller-gen ## generate custom resource definitions
$(LOCALBIN)/controller-gen crd rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
generate: controller-gen ## generate supporting code for custom resource types
$(LOCALBIN)/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
.PHONY: controller-gen
controller-gen: ## Download controller-gen locally if necessary.
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@${CONTROLLER_GEN_VERSION}
.PHONY: envtest
envtest: ## Download envtest-setup locally if necessary.
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
${LOCALBIN}/setup-envtest use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path
# ---------------------------------------------------------------------
# -- Additional helpers
# ---------------------------------------------------------------------
k3s_mac_lima_create: ## create local k8s using lima
limactl start --tty=false ./resources/lima/k3s.yaml
k3s_mac_lima_start: ## start local lima k8s
limactl start k3s
k3s_mac_deploy: build k3s_mac_image ## build image and import image to local lima k8s
k3s_mac_image: ## import built image to local lima k8s
limactl copy my-image.tar k3s:/tmp/db.tar
limactl shell k3s sudo k3s ctr images import --all-platforms /tmp/db.tar
limactl shell k3s rm -f /tmp/db.tar
k3d_setup: k3d_install k3d_image ## install k3d and import image to your k3d cluster
k3d_install: ## install k3d cluster locally
@curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash
@k3d cluster create myk3s -i rancher/k3s:$(K8S_VERSION)-k3s1
@kubectl get pod
k3d_image: build ## rebuild the docker images and upload into your k3d cluster
@k3d image import my-image.tar -c myk3s