-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMakefile
More file actions
311 lines (256 loc) · 8.24 KB
/
Makefile
File metadata and controls
311 lines (256 loc) · 8.24 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
SHELL=/usr/bin/env bash -o pipefail
# 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
#############
# Constants #
#############
# Set build version
VERSION=14.31.0
ARTIFACT_NAME=qubership-logging-operator
# Helm charts directory
HELM_FOLDER=charts/qubership-logging-operator
CRDS_HELM_CRDS_FOLDER=charts/qubership-logging-crds
# Directories and files
BUILD_DIR=build
RESULT_DIR=$(BUILD_DIR)/_result
CRDS_DIR=$(BUILD_DIR)/_crds
HELM_DIR=$(BUILD_DIR)/_helm
# Documents folders
PUBLIC_DOC_FOLDER := docs
CRD_FOLDER=$(HELM_FOLDER)/crds
CRD_PUBLIC_DOC_FOLDER=$(PUBLIC_DOC_FOLDER)/crds
# Directories to generate API documentation
TYPES_V1_TARGET=api/v1/loggingservice_types.go
API_DOC_GEN_BINARY_DIR?=$(shell pwd)/api
# Tools
CONTROLLER_GEN_PACKAGE=sigs.k8s.io/controller-tools/cmd/controller-gen@v0.20.1
GEN_CRD_API_PACKAGE=github.com/ahmetb/gen-crd-api-reference-docs@v0.3.0
HELM_DOCS_PACKAGE=github.com/norwoodj/helm-docs/cmd/helm-docs@v1.14.2
# Detect the build environment, local or Jenkins builder
BUILD_DATE=$(shell date +"%Y%m%d-%T")
ifndef JENKINS_URL
BUILD_USER?=$(USER)
BUILD_BRANCH?=$(shell git branch --show-current)
BUILD_REVISION?=$(shell git rev-parse --short HEAD)
else
BUILD_USER=$(BUILD_USER)
BUILD_BRANCH=$(LOCATION:refs/heads/%=%)
BUILD_REVISION=$(REPO_HASH)
endif
# The Prometheus common library import path
LOGGING_OPERATOR_PKG=github.com/Netcracker/qubership-logging-operator
# The ldflags for the go build process to set the version related data.
GO_BUILD_LDFLAGS=\
-s \
-X $(LOGGING_OPERATOR_PKG)/version.Revision=$(BUILD_REVISION) \
-X $(LOGGING_OPERATOR_PKG)/version.BuildUser=$(BUILD_USER) \
-X $(LOGGING_OPERATOR_PKG)/version.BuildDate=$(BUILD_DATE) \
-X $(LOGGING_OPERATOR_PKG)/version.Branch=$(BUILD_BRANCH) \
-X $(LOGGING_OPERATOR_PKG)/version.Version=$(VERSION)
# Go build flags
GO_BUILD_RECIPE=\
GOOS=$(GOOS) \
GOARCH=$(GOARCH) \
CGO_ENABLED=0 \
go build -ldflags="$(GO_BUILD_LDFLAGS)"
# Default test arguments
TEST_RUN_ARGS=-vet=off --shuffle=on
# List of packages, exclude integration tests that require "envtest"
pkgs = $(shell go list ./... | grep -v /e2e-tests)
# Container name
CONTAINER_CLI?=docker
CONTAINER_NAME="qubership-logging-operator"
DOCKERFILE=Dockerfile
###########
# Generic #
###########
# Default run without arguments
.PHONY: all
all: generate test build-binary image docs archives
# Run only build
.PHONY: build
build: generate build-binary image docs archives
# Run only build inside the Dockerfile
.PHONY: build-image
build-image: generate image docs archives
# Remove all files and directories ignored by git
.PHONY: clean
clean:
echo "=> Cleanup repository ..."
git clean -Xfd .
##############
# Generating #
##############
# Generate code (deepcopy files, CRDs)
generate: controller-gen
echo "=> Generate CRDs and deepcopy ..."
$(CONTROLLER_GEN) crd:crdVersions={v1} \
object:headerFile="tools/boilerplate.go.txt" \
paths="./api/v1" \
output:artifacts:config=charts/qubership-logging-operator/crds/
chmod +x ./scripts/build/append-operator-version.sh
VERSION=$(VERSION) ./scripts/build/append-operator-version.sh
# Find or download controller-gen, download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
go mod init tmp ;\
go install $(CONTROLLER_GEN_PACKAGE) ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
#########
# Build #
#########
# Build manager binary
.PHONY: build-binary
build-binary: generate fmt
# TODO: go vet fail build, need to check why?
# build-binary: generate fmt vet
echo "=> Build binary ..."
$(GO_BUILD_RECIPE) -o build/_binary/manager cmd/operator/main.go
# Run go fmt against code
.PHONY: fmt
fmt:
echo "=> Formatting Golang code ..."
go fmt ./...
# Run go vet against code
.PHONY: vet
vet:
echo "=> Examines Golang code ..."
go vet ./...
###############
# Build image #
###############
.PHONY: image
image:
echo "=> Build image ..."
docker build --pull -t $(CONTAINER_NAME) -f $(DOCKERFILE) .
# Set image tag if build inside the Jenkins
for id in $(DOCKER_NAMES) ; do \
docker tag $(CONTAINER_NAME) "$$id"; \
done
###########
# Testing #
###########
.PHONY: test
test: unit-test
# Run unit tests in all packages
.PHONY: unit-test
unit-test:
echo "=> Run Golang unit-tests ..."
go test -race -cover $(TEST_RUN_ARGS) $(pkgs) -count=1 -v
#################
# Documentation #
#################
# Run document generation
docs: docs/api.md docs/crds update-crds docs/helm
# Generate Helm chart documentation using helm-docs
.PHONY: docs/helm
docs/helm: helm-docs
$(HELM_DOCS) --chart-search-root $(HELM_FOLDER)
# Find or download helm-docs, download if necessary
.PHONY: helm-docs
helm-docs:
ifeq (, $(shell which helm-docs))
@{ \
set -e ;\
HELM_DOCS_TMP_DIR=$$(mktemp -d) ;\
cd $$HELM_DOCS_TMP_DIR ;\
go mod init tmp ;\
go install $(HELM_DOCS_PACKAGE) ;\
rm -rf $$HELM_DOCS_TMP_DIR ;\
}
HELM_DOCS=$(GOBIN)/helm-docs
else
HELM_DOCS=$(shell which helm-docs)
endif
# Run gen-crd-api-reference-docs to generate API documents by operator API
docs/api.md: docs/api/gen $(TYPES_V1_TARGET)
cd $(API_DOC_GEN_BINARY_DIR) \
&& $(API_DOC_GEN_BINARY) -api-dir "./v1/" \
-config "../scripts/docs/config.json" \
-template-dir "../scripts/docs/templates" \
-out-file "../docs/api.md" \
&& rm -rf $(API_DOC_GEN_BINARY)
chmod +x ./scripts/build/append-markdown-linter-comments.sh
./scripts/build/append-markdown-linter-comments.sh
# Find or download gen-crd-api-reference-docs, download gen-crd-api-reference-docs if necessary
docs/api/gen:
ifeq (, $(shell which ./gen-crd-api-reference-docs))
@{ \
set -e ;\
GOBIN=$(API_DOC_GEN_BINARY_DIR) go install $(GEN_CRD_API_PACKAGE) ;\
}
API_DOC_GEN_BINARY=./gen-crd-api-reference-docs
else
API_DOC_GEN_BINARY=$(shell which gen-crd-api-reference-docs)
endif
# Copy CRDs from the Helm chart to documentation directory
docs/crds:
rm -rf $(CRD_PUBLIC_DOC_FOLDER)/*.yaml
cp $(CRD_FOLDER)/* $(CRD_PUBLIC_DOC_FOLDER)/
##########################
# Update CRDs Helm chart #
##########################
# Copy CRDs from documentation to the qubership-monitoring-crds Helm chart
.PHONY: update-crds
update-crds:
echo "=> Update CRDs in dedicated Helm chart ..."
find $(CRD_FOLDER) \( -name "*.yaml" -o -name "*.yml" \) -exec cp {} ${CRDS_HELM_CRDS_FOLDER}/crds/ \;
###################
# Running locally #
###################
# Run against the configured Kubernetes cluster in ~/.kube/config
.PHONY: run
run: generate fmt vet
echo "=> Run ..."
go run ./cmd/operator/main.go
############
# Archives #
############
# Run archives with helm chart and crds creation
.PHONY: archives
archives: cleanup prepare-charts archive-helm-chart archive-crds
# Remove build dir
.PHONY: cleanup
cleanup:
rm -rf $(BUILD_DIR)
# Copy Helm charts to the /helm directory because the builder expect it in this dir
.PHONY: prepare-charts
prepare-charts:
echo "=> Copy Helm charts to contract directory for build ..."
mkdir -p $(RESULT_DIR)
# Copy helm chart in folder
mkdir -p $(HELM_DIR)
cp -R charts $(HELM_DIR)/
# Create directories to copy CRDs
mkdir -p "$(CRDS_DIR)/qubership-logging-operator"
# Archive Helm chart separately from application manifest
.PHONY: archive-helm-chart
archive-helm-chart:
echo "=> Archive Helm charts ..."
# Navigate to dir to avoid unnecessary directories in result archive
# name like: logging-operator-14.20.0-chart.zip
cd "$(HELM_DIR)/charts/" && zip -r "../../../$(RESULT_DIR)/$(ARTIFACT_NAME)-$(VERSION)-chart.zip" ./*
# Archive CRDs separately from helm chart
.PHONY: archive-crds
archive-crds:
echo "=> Archive CRDs ..."
# Copy documentation how to apply CRDS
cp docs/user-guides/manual-create-crds.md "${BUILD_DIR}"/_crds/README.md
# Copy CRDs from different places in helm chart and sub-charts
cp charts/qubership-logging-operator/crds/* "${BUILD_DIR}/_crds/qubership-logging-operator/"
# Navigate to dir to avoid unnecessary directories in result archive\
# name like: logging-operator-14.20.0-crds.zip
cd "$(CRDS_DIR)" && zip -r "../../$(RESULT_DIR)/$(ARTIFACT_NAME)-$(VERSION)-crds.zip" ./*