forked from kubegems/kubegems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
212 lines (166 loc) · 8.04 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
# Setting SHELL to bash allows bash commands to be executed by recipes.
# This is a requirement for 'setup-envtest.sh' in the test target.
# 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
BUILD_DATE?=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
GIT_VERSION?=$(shell git describe --tags --dirty --abbrev=0 2>/dev/null || git symbolic-ref --short HEAD)
GIT_COMMIT?=$(shell git rev-parse HEAD 2>/dev/null)
GIT_BRANCH?=$(shell git symbolic-ref --short HEAD 2>/dev/null)
# semver version
VERSION?=$(shell echo "${GIT_VERSION}" | sed -e 's/^v//')
OS?=linux
ARCH?=amd64
BIN_DIR?=bin
PLATFORM?=linux/amd64,linux/arm64
IMAGE_REGISTRY?=docker.io,registry.cn-beijing.aliyuncs.com
IMAGE_TAG=${GIT_VERSION}
PUSH?=false
GOPACKAGE=$(shell go list -m)
ldflags+=-w -s
ldflags+=-X '${GOPACKAGE}/pkg/version.gitVersion=${GIT_VERSION}'
ldflags+=-X '${GOPACKAGE}/pkg/version.gitCommit=${GIT_COMMIT}'
ldflags+=-X '${GOPACKAGE}/pkg/version.buildDate=${BUILD_DATE}'
# HELM BUILD
CHARTS_DIR?=deploy/plugins
HELM_REPO_USERNAME?=kubegems
HELM_REPO_PASSWORD?=
CHARTMUSEUM_ADDR?=https://${HELM_REPO_USERNAME}:${HELM_REPO_PASSWORD}@charts.kubegems.io/kubegems
KUBEGEM_CHARTS_DIR = ${BIN_DIR}/plugins/charts.kubegems.io/kubegems
##@ All
all: generate build container push helm-push## build all
##@ 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
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)
generate: generate-i18n generate-schema generate-proto generate-apis generate-license generate-versions generate-installer ## Generate all
generate-schema:
helm-schema deploy/plugins/kubegems*
generate-apis:
$(CONTROLLER_GEN) paths="./pkg/apis/plugins/..." crd output:crd:artifacts:config=deploy/plugins/kubegems-installer/crds
$(CONTROLLER_GEN) paths="./pkg/apis/gems/..." crd output:crd:artifacts:config=deploy/plugins/kubegems-local/crds
$(CONTROLLER_GEN) paths="./pkg/apis/models/..." crd output:crd:artifacts:config=deploy/plugins/kubegems-models/crds
$(CONTROLLER_GEN) paths="./pkg/apis/edge/..." crd output:crd:artifacts:config=deploy/plugins/kubegems-edge/crds
$(CONTROLLER_GEN) paths="./pkg/..." object:headerFile="hack/boilerplate.go.txt"
generate-proto:
protoc \
--go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
pkg/edge/tunnel/proto/tunnel.proto
generate-versions:
yq -i 'select(.metadata.name == "kubegems").spec.version="$(VERSION)" | select(.metadata.name == "global").spec.values.kubegemsVersion="$(GIT_VERSION)"' deploy/kubegems.yaml
generate-installer: helm-package
helm template --namespace kubegems-installer --include-crds kubegems-installer ${KUBEGEM_CHARTS_DIR}/kubegems-installer-${VERSION}.tgz \
| kubectl annotate -f - --local -oyaml meta.helm.sh/release-name=kubegems-installer meta.helm.sh/release-namespace=kubegems-installer \
> deploy/installer.yaml
generate-i18n:
go run internal/cmd/i18n/main.go gen
.PHONY: generate-license
generate-license:
./scripts/add_license.sh
SERVER_IP ?= 127.0.0.1
.PHONY: certs
certs:
SERVER_IP=${SERVER_IP} sh scripts/generate-certs.sh
swagger:
go install github.com/swaggo/swag/cmd/swag@v1.8.4
swag f -g cmd/main.go
swag i --parseDependency --parseInternal -g cmd/main.go -o docs/swagger
check: linter ## Static code check.
${LINTER} run ./...
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: generate ## Run tests.
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
collect-i18n:
go run internal/cmd/i18n/main.go collect
define go-build
@echo "Building ${1}/${2}"
@CGO_ENABLED=0 GOOS=${1} GOARCH=$(2) go build -gcflags=all="-N -l" -ldflags="${ldflags}" -o ${BIN_DIR}/kubegems-$(1)-$(2) cmd/main.go
@CGO_ENABLED=0 GOOS=${1} GOARCH=$(2) go build -gcflags=all="-N -l" -ldflags="${ldflags}" -o ${BIN_DIR}/kubegems-edge-agent-$(1)-$(2) pkg/edge/cmd/kubegems-edge-agent/main.go
endef
##@ Build
build: build-files build-binaries-all
build-binaries-all: ## Build binaries.
- mkdir -p ${BIN_DIR}
$(call go-build,linux,amd64)
$(call go-build,linux,arm64)
build-binaries:
$(call go-build,${OS},${ARCH})
- mkdir -p ${BIN_DIR}
@cp ${BIN_DIR}/kubegems-${OS}-${ARCH} ${BIN_DIR}/kubegems
build-files: ## Build around files
go run scripts/offline-plugins/main.go
cp -rf deploy/*.yaml ${BIN_DIR}/plugins/
mkdir -p ${BIN_DIR}/config
cp -f config/promql_tpl.yaml config/system_alert.yaml ${BIN_DIR}/config/
cp -rf config/dashboards/ ${BIN_DIR}/config/dashboards/
CHARTS = kubegems kubegems-local kubegems-installer kubegems-models
helm-generate: readme-generator
$(foreach file,$(dir $(wildcard $(CHARTS_DIR)/*/Chart.yaml)), \
readme-generator -v $(file)values.yaml -r $(file)README.md \
;)
.PHONY: helm-package
helm-package:
$(foreach file, $(dir $(wildcard $(CHARTS_DIR)/*/Chart.yaml)), \
helm package -u -d ${KUBEGEM_CHARTS_DIR} --version ${VERSION} --app-version ${VERSION} $(file) \
;)
.PHONY: helm-push
helm-push: helm-package
$(foreach file, $(wildcard $(KUBEGEM_CHARTS_DIR)/kubegems*-$(VERSION).tgz), \
curl -f --data-binary "@$(file)" ${CHARTMUSEUM_ADDR}/api/charts \
;)
comma = ,
buildxbuild = docker buildx build --platform=${PLATFORM}
buildxbuild += $(foreach n,$(subst $(comma), ,$(strip ${IMAGE_REGISTRY})),--tag $(n)/$1 )
ifeq ($(PUSH),true)
buildxbuild += --push
endif
docker: kubegems-image kubegems-edge-image ## Build container image.
kubegems-image:
$(call buildxbuild,kubegems/kubegems:$(IMAGE_TAG)) -f Dockerfile ${BIN_DIR}
debug-image:
$(call buildxbuild,kubegems/debug-tools:latest) -f Dockerfile.debug ${BIN_DIR}
kubegems-edge-image: kubegems-edge-agent-image
kubegems-edge-agent-image:
$(call buildxbuild,kubegems/kubegems-edge-agent:$(IMAGE_TAG)) -f Dockerfile.edge-agent ${BIN_DIR}
kubectl-image:
$(call buildxbuild,kubegems/kubectl:latest) -f Dockerfile.kubectl ${BIN_DIR}
clean:
- rm -rf ${BIN_DIR}
CONTROLLER_GEN = ${BIN_DIR}/controller-gen
controller-gen: ## Download controller-gen locally if necessary.
GOBIN=$(abspath ${BIN_DIR}) go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.9.0
KUSTOMIZE = ${BIN_DIR}/kustomize
KUSTOMIZE_VERSION = 4.4.1
kustomize: ## Download kustomize locally if necessary.
mkdir -p $(BIN_DIR)
curl -SLf https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv$(KUSTOMIZE_VERSION)/kustomize_v$(KUSTOMIZE_VERSION)_linux_amd64.tar.gz | tar -xz -C $(BIN_DIR)
LINTER = ${BIN_DIR}/golangci-lint
linter: ## Download controller-gen locally if necessary.
GOBIN=$(abspath ${BIN_DIR}) go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.50.0
K8S_VERSION = 1.20.0
setup-envtest: ## setup operator test environment
go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
setup-envtest use ${K8S_VERSION}
.PHONY: readme-generator
readme-generator:
ifeq (, $(shell which readme-generator))
@{ \
set -e ;\
echo 'installing readme-generator-for-helm' ;\
npm install -g readme-generator-for-helm ;\
}
else
echo 'readme-generator-for-helm is already installed'
endif