forked from k0sproject/k0s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
297 lines (249 loc) · 12.1 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
294
295
296
297
include embedded-bins/Makefile.variables
include inttest/Makefile.variables
include hack/tools/Makefile.variables
ifndef HOST_ARCH
HOST_HARDWARE := $(shell uname -m)
ifneq (, $(filter $(HOST_HARDWARE), aarch64 arm64 armv8l))
HOST_ARCH := arm64
else ifneq (, $(filter $(HOST_HARDWARE), armv7l arm))
HOST_ARCH := arm
else
ifeq (, $(filter $(HOST_HARDWARE), x86_64 amd64 x64))
$(warning unknown machine hardware name $(HOST_HARDWARE), assuming amd64)
endif
HOST_ARCH := amd64
endif
endif
K0S_GO_BUILD_CACHE ?= build/cache
GO_SRCS := $(shell find . -type f -name '*.go' -not -path './$(K0S_GO_BUILD_CACHE)/*' -not -path './inttest/*' -not -name '*_test.go' -not -name 'zz_generated*')
GO_DIRS := . ./cmd/... ./pkg/... ./internal/... ./static/... ./hack/...
# EMBEDDED_BINS_BUILDMODE can be either:
# docker builds the binaries in docker
# none does not embed any binaries
EMBEDDED_BINS_BUILDMODE ?= docker
# k0s runs on linux even if it's built on mac or windows
TARGET_OS ?= linux
BUILD_UID ?= $(shell id -u)
BUILD_GID ?= $(shell id -g)
BUILD_GO_FLAGS := -tags osusergo
BUILD_GO_LDFLAGS_EXTRA :=
DEBUG ?= false
VERSION ?= $(shell git describe --tags)
ifeq ($(DEBUG), false)
LD_FLAGS ?= -w -s
endif
# https://reproducible-builds.org/docs/source-date-epoch/#makefile
# https://reproducible-builds.org/docs/source-date-epoch/#git
# https://stackoverflow.com/a/15103333
BUILD_DATE_FMT = %Y-%m-%dT%H:%M:%SZ
ifdef SOURCE_DATE_EPOCH
BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "+$(BUILD_DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "+$(BUILD_DATE_FMT)" 2>/dev/null || date -u "+$(BUILD_DATE_FMT)")
else
BUILD_DATE ?= $(shell TZ=UTC git log -1 --pretty=%cd --date='format-local:$(BUILD_DATE_FMT)' || date -u +$(BUILD_DATE_FMT))
endif
LD_FLAGS += -X github.com/k0sproject/k0s/pkg/build.Version=$(VERSION)
LD_FLAGS += -X github.com/k0sproject/k0s/pkg/build.RuncVersion=$(runc_version)
LD_FLAGS += -X github.com/k0sproject/k0s/pkg/build.ContainerdVersion=$(containerd_version)
LD_FLAGS += -X github.com/k0sproject/k0s/pkg/build.KubernetesVersion=$(kubernetes_version)
LD_FLAGS += -X github.com/k0sproject/k0s/pkg/build.KineVersion=$(kine_version)
LD_FLAGS += -X github.com/k0sproject/k0s/pkg/build.EtcdVersion=$(etcd_version)
LD_FLAGS += -X github.com/k0sproject/k0s/pkg/build.KonnectivityVersion=$(konnectivity_version)
LD_FLAGS += -X "github.com/k0sproject/k0s/pkg/build.EulaNotice=$(EULA_NOTICE)"
LD_FLAGS += -X github.com/k0sproject/k0s/pkg/telemetry.segmentToken=$(SEGMENT_TOKEN)
LD_FLAGS += -X k8s.io/component-base/version.gitVersion=v$(kubernetes_version)
LD_FLAGS += -X k8s.io/component-base/version.gitMajor=$(shell echo '$(kubernetes_version)' | cut -d. -f1)
LD_FLAGS += -X k8s.io/component-base/version.gitMinor=$(shell echo '$(kubernetes_version)' | cut -d. -f2)
LD_FLAGS += -X k8s.io/component-base/version.buildDate=$(BUILD_DATE)
LD_FLAGS += -X k8s.io/component-base/version.gitCommit=not_available
LD_FLAGS += -X github.com/containerd/containerd/version.Version=$(containerd_version)
ifeq ($(EMBEDDED_BINS_BUILDMODE), docker)
LD_FLAGS += -X github.com/containerd/containerd/version.Revision=$(shell ./embedded-bins/staging/linux/bin/containerd --version | awk '{print $$4}')
endif
LD_FLAGS += $(BUILD_GO_LDFLAGS_EXTRA)
GOLANG_IMAGE ?= golang:$(go_version)-alpine3.16
K0S_GO_BUILD_CACHE_VOLUME_PATH=$(shell realpath $(K0S_GO_BUILD_CACHE))
GO_ENV ?= docker run --rm \
-v '$(K0S_GO_BUILD_CACHE_VOLUME_PATH)':/run/k0s-build \
-v '$(CURDIR)':/go/src/github.com/k0sproject/k0s \
-w /go/src/github.com/k0sproject/k0s \
-e GOOS \
-e CGO_ENABLED \
-e GOARCH \
--user $(BUILD_UID):$(BUILD_GID) \
k0sbuild.docker-image.k0s
GO ?= $(GO_ENV) go
.PHONY: build
ifeq ($(TARGET_OS),windows)
build: k0s.exe
else
BUILD_GO_LDFLAGS_EXTRA = -extldflags=-static
build: k0s
endif
.PHONY: all
all: k0s k0s.exe
$(K0S_GO_BUILD_CACHE):
mkdir -p -- '$@'
.k0sbuild.docker-image.k0s: build/Dockerfile embedded-bins/Makefile.variables | $(K0S_GO_BUILD_CACHE)
docker build --rm \
--build-arg BUILDIMAGE=$(GOLANG_IMAGE) \
-f build/Dockerfile \
-t k0sbuild.docker-image.k0s build/
touch $@
go.sum: go.mod .k0sbuild.docker-image.k0s
$(GO) mod tidy && touch -c -- '$@'
codegen_targets += pkg/apis/helm.k0sproject.io/v1beta1/.controller-gen.stamp
pkg/apis/helm.k0sproject.io/v1beta1/.controller-gen.stamp: $(shell find pkg/apis/helm.k0sproject.io/v1beta1/ -maxdepth 1 -type f -name \*.go)
pkg/apis/helm.k0sproject.io/v1beta1/.controller-gen.stamp: gen_output_dir = helm
codegen_targets += pkg/apis/k0s.k0sproject.io/v1beta1/.controller-gen.stamp
pkg/apis/k0s.k0sproject.io/v1beta1/.controller-gen.stamp: $(shell find pkg/apis/k0s.k0sproject.io/v1beta1/ -maxdepth 1 -type f -name \*.go)
pkg/apis/k0s.k0sproject.io/v1beta1/.controller-gen.stamp: gen_output_dir = v1beta1
codegen_targets += pkg/apis/autopilot.k0sproject.io/v1beta2/.controller-gen.stamp
pkg/apis/autopilot.k0sproject.io/v1beta2/.controller-gen.stamp: $(shell find pkg/apis/autopilot.k0sproject.io/v1beta2/ -maxdepth 1 -type f -name \*.go)
pkg/apis/autopilot.k0sproject.io/v1beta2/.controller-gen.stamp: gen_output_dir = autopilot
pkg/apis/%/.controller-gen.stamp: .k0sbuild.docker-image.k0s hack/tools/Makefile.variables
rm -rf 'static/manifests/$(gen_output_dir)/CustomResourceDefinition'
rm -f -- '$(dir $@)'zz_*.go
CGO_ENABLED=0 $(GO) install sigs.k8s.io/controller-tools/cmd/controller-gen@v$(controller-gen_version)
$(GO_ENV) controller-gen \
crd \
paths="./$(dir $@)..." \
output:crd:artifacts:config=./static/manifests/$(gen_output_dir)/CustomResourceDefinition \
object
touch -- '$@'
# Note: k0s.k0sproject.io omits the version from the output directory, so this needs
# special handling until this versioning layout is fixed.
codegen_targets += pkg/apis/k0s.k0sproject.io/v1beta1/.client-gen.stamp
pkg/apis/k0s.k0sproject.io/v1beta1/.client-gen.stamp: $(shell find pkg/apis/k0s.k0sproject.io/v1beta1/ -maxdepth 1 -type f -name \*.go -not -name zz_\*.go)
pkg/apis/k0s.k0sproject.io/v1beta1/.client-gen.stamp: groupver = k0s.k0sproject.io/v1beta1
pkg/apis/k0s.k0sproject.io/v1beta1/.client-gen.stamp: gen_output_dir = k0s.k0sproject.io
codegen_targets += pkg/apis/autopilot.k0sproject.io/v1beta2/.client-gen.stamp
pkg/apis/autopilot.k0sproject.io/v1beta2/.client-gen.stamp: $(shell find pkg/apis/autopilot.k0sproject.io/v1beta2/ -maxdepth 1 -type f -name \*.go -not -name zz_\*.go)
pkg/apis/autopilot.k0sproject.io/v1beta2/.client-gen.stamp: groupver = autopilot.k0sproject.io/v1beta2
pkg/apis/autopilot.k0sproject.io/v1beta2/.client-gen.stamp: gen_output_dir = $(groupver)
pkg/apis/%/.client-gen.stamp: .k0sbuild.docker-image.k0s hack/tools/boilerplate.go.txt embedded-bins/Makefile.variables
rm -rf 'pkg/apis/$(gen_output_dir)/clientset/'
CGO_ENABLED=0 $(GO) install k8s.io/code-generator/cmd/[email protected]
$(GO_ENV) client-gen \
--go-header-file hack/tools/boilerplate.go.txt \
--input=$(groupver) \
--input-base github.com/k0sproject/k0s/pkg/apis \
--clientset-name=clientset \
--output-package=github.com/k0sproject/k0s/pkg/apis/$(gen_output_dir)/
touch -- '$@'
codegen_targets += static/gen_manifests.go
static/gen_manifests.go: .k0sbuild.docker-image.k0s hack/tools/Makefile.variables
static/gen_manifests.go: $(shell find static/manifests -type f)
-rm -f -- '$@'
CGO_ENABLED=0 $(GO) install github.com/kevinburke/go-bindata/go-bindata@v$(go-bindata_version)
$(GO_ENV) go-bindata -o static/gen_manifests.go -pkg static -prefix static static/...
codegen_targets += pkg/assets/zz_generated_offsets_$(TARGET_OS).go
zz_os = $(patsubst pkg/assets/zz_generated_offsets_%.go,%,$@)
print_empty_generated_offsets = printf "%s\n\n%s\n%s\n" \
"package assets" \
"var BinData = map[string]struct{ offset, size, originalSize int64 }{}" \
"var BinDataSize int64"
ifeq ($(EMBEDDED_BINS_BUILDMODE),none)
pkg/assets/zz_generated_offsets_linux.go pkg/assets/zz_generated_offsets_windows.go:
rm -f bindata_$(zz_os) && touch bindata_$(zz_os)
$(print_empty_generated_offsets) > $@
else
pkg/assets/zz_generated_offsets_linux.go: .bins.linux.stamp
pkg/assets/zz_generated_offsets_windows.go: .bins.windows.stamp
pkg/assets/zz_generated_offsets_linux.go pkg/assets/zz_generated_offsets_windows.go: .k0sbuild.docker-image.k0s go.sum
GOOS=${GOHOSTOS} $(GO) run -tags=hack hack/gen-bindata/cmd/main.go -o bindata_$(zz_os) -pkg assets \
-gofile pkg/assets/zz_generated_offsets_$(zz_os).go \
-prefix embedded-bins/staging/$(zz_os)/ embedded-bins/staging/$(zz_os)/bin
endif
# needed for unit tests on macos
pkg/assets/zz_generated_offsets_darwin.go:
$(print_empty_generated_offsets) > $@
k0s: TARGET_OS = linux
k0s: BUILD_GO_CGO_ENABLED = 1
k0s: .k0sbuild.docker-image.k0s
k0s.exe: TARGET_OS = windows
k0s.exe: BUILD_GO_CGO_ENABLED = 0
k0s.exe k0s: $(GO_SRCS) $(codegen_targets) go.sum
CGO_ENABLED=$(BUILD_GO_CGO_ENABLED) GOOS=$(TARGET_OS) $(GO) build $(BUILD_GO_FLAGS) -ldflags='$(LD_FLAGS)' -o [email protected] main.go
cat [email protected] bindata_$(TARGET_OS) > [email protected] \
&& rm -f [email protected] \
&& chmod +x [email protected] \
&& mv [email protected] $@
.bins.windows.stamp .bins.linux.stamp: embedded-bins/Makefile.variables
$(MAKE) -C embedded-bins buildmode=$(EMBEDDED_BINS_BUILDMODE) TARGET_OS=$(patsubst .bins.%.stamp,%,$@)
touch $@
.PHONY: codegen
codegen: $(codegen_targets)
# bindata contains the parts of codegen which aren't version controlled.
.PHONY: bindata
bindata: static/gen_manifests.go pkg/assets/zz_generated_offsets_$(TARGET_OS).go
.PHONY: lint
lint: GOLANGCI_LINT_FLAGS ?=
lint: .k0sbuild.docker-image.k0s go.sum codegen
CGO_ENABLED=0 $(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@v$(golangci-lint_version)
$(GO_ENV) golangci-lint run --verbose $(GOLANGCI_LINT_FLAGS) $(GO_DIRS)
airgap-images.txt: k0s
./k0s airgap list-images > '$@' || { \
code=$$? && \
rm -f -- '$@' && \
exit $$code ; \
}
airgap-image-bundle-linux-amd64.tar: TARGET_PLATFORM := linux/amd64
airgap-image-bundle-linux-arm64.tar: TARGET_PLATFORM := linux/arm64
airgap-image-bundle-linux-arm.tar: TARGET_PLATFORM := linux/arm/v7
airgap-image-bundle-linux-amd64.tar \
airgap-image-bundle-linux-arm64.tar \
airgap-image-bundle-linux-arm.tar: .k0sbuild.image-bundler.stamp airgap-images.txt
docker run --rm -i --privileged \
-e TARGET_PLATFORM='$(TARGET_PLATFORM)' \
k0sbuild.image-bundler < airgap-images.txt > '$@' || { \
code=$$? && rm -f -- '$@' && exit $$code ; \
}
.k0sbuild.image-bundler.stamp: hack/image-bundler/*
docker build -t k0sbuild.image-bundler hack/image-bundler
touch -- '$@'
.PHONY: $(smoketests)
check-airgap check-ap-airgap: airgap-image-bundle-linux-$(HOST_ARCH).tar
$(smoketests): k0s
$(MAKE) -C inttest K0S_IMAGES_BUNDLE='$(CURDIR)/airgap-image-bundle-linux-$(HOST_ARCH).tar' $@
.PHONY: smoketests
smoketests: $(smoketests)
.PHONY: check-unit
check-unit: GO_TEST_RACE ?= -race
check-unit: go.sum codegen
$(GO) test -tags=hack $(GO_TEST_RACE) -ldflags='$(LD_FLAGS)' `$(GO) list -tags=hack $(GO_DIRS)`
.PHONY: check-image-validity
check-image-validity: go.sum
$(GO) run -tags=hack hack/validate-images/main.go -architectures amd64,arm64,arm
.PHONY: clean-gocache
clean-gocache:
-find $(K0S_GO_BUILD_CACHE)/go/mod -type d -exec chmod u+w '{}' \;
rm -rf $(K0S_GO_BUILD_CACHE)/go
clean-docker-image:
-docker rmi k0sbuild.docker-image.k0s -f
-rm -f .k0sbuild.docker-image.k0s
clean-airgap-image-bundles:
-docker rmi -f k0sbuild.image-bundler.k0s
-rm airgap-images.txt .k0sbuild.image-bundler.stamp
-rm airgap-image-bundle-linux-amd64.tar airgap-image-bundle-linux-arm64.tar airgap-image-bundle-linux-arm.tar
.PHONY: clean
clean: clean-gocache clean-docker-image clean-airgap-image-bundles
-rm -f pkg/assets/zz_generated_offsets_*.go k0s k0s.exe .bins.*stamp bindata* static/gen_manifests.go
-rm -rf $(K0S_GO_BUILD_CACHE)
-find pkg/apis -type f \( -name .client-gen.stamp -or -name .controller-gen.stamp \) -delete
-$(MAKE) -C docs clean
-$(MAKE) -C embedded-bins clean
-$(MAKE) -C inttest clean
.PHONY: manifests
manifests: .helmCRD .cfgCRD
.PHONY: .helmCRD
.PHONY: docs
docs:
$(MAKE) -C docs
.PHONY: docs-serve-dev
docs-serve-dev: DOCS_DEV_PORT ?= 8000
docs-serve-dev:
$(MAKE) -C docs .docker-image.serve-dev.stamp
docker run --rm \
-v "$(CURDIR):/k0s:ro" \
-p '$(DOCS_DEV_PORT):8000' \
k0sdocs.docker-image.serve-dev