forked from vmware-tanzu/sonobuoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
268 lines (226 loc) · 8.98 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
# Copyright 2017 Heptio Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
EMPTY :=
SPACE := $(EMPTY) $(EMPTY)
COMMA := $(EMPTY),$(EMPTY)
BINARY = sonobuoy
TARGET = sonobuoy
GOTARGET = github.com/vmware-tanzu/$(TARGET)
GOPATH = $(shell go env GOPATH)
REGISTRY ?= sonobuoy
IMAGE = $(REGISTRY)/$(TARGET)
DIR := ${CURDIR}
DOCKER ?= docker
LINUX_ARCH := amd64 arm64
WIN_ARCH := amd64
DOCKERFILE :=
KIND_CLUSTER = kind
# Not used for pushing images, just for local building on other GOOS. Defaults to
# grabbing from the local go env but can be set manually to avoid that requirement.
HOST_GOOS ?= $(shell go env GOOS)
HOST_GOARCH ?= $(shell go env GOARCH)
GO_SYSTEM_FLAGS ?= GOOS=$(HOST_GOOS) GOARCH=$(HOST_GOARCH)
# --tags allows detecting non-annotated tags as well as annotated ones
GIT_VERSION ?= $(shell git describe --always --dirty --tags)
IMAGE_VERSION ?= $(shell git describe --always --dirty --tags)
IMAGE_TAG := $(shell echo $(IMAGE_VERSION) | cut -d. -f1,2)
IMAGE_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD | sed 's/\///g')
GIT_REF_SHORT = $(shell git rev-parse --short=8 --verify HEAD)
GIT_REF_LONG = $(shell git rev-parse --verify HEAD)
ifneq ($(VERBOSE),)
VERBOSE_FLAG = -v
endif
BUILDMNT = /go/src/$(GOTARGET)
BUILD_IMAGE ?= golang:1.14-stretch
AMD_IMAGE ?= debian:stretch-slim
ARM_IMAGE ?= arm64v8/ubuntu:16.04
WIN_IMAGE ?= mcr.microsoft.com/windows/servercore:1809
TESTARGS ?= $(VERBOSE_FLAG) -timeout 60s
COVERARGS ?= -coverprofile=coverage.txt -covermode=atomic
TEST_PKGS ?= $(GOTARGET)/cmd/... $(GOTARGET)/pkg/...
TEST_CMD = go test $(TESTARGS)
TEST = $(TEST_CMD) $(COVERARGS) $(TEST_PKGS)
INT_TEST_PKGS ?= $(GOTARGET)/test/integration/...
INT_TEST= $(TEST_CMD) $(INT_TEST_PKGS) -tags=integration
STRESS_TEST_PKGS ?= $(GOTARGET)/test/stress/...
STRESS_TEST= $(TEST_CMD) $(STRESS_TEST_PKGS)
VET = go vet $(TEST_PKGS)
# Vendor this someday
GOLINT_FLAGS ?= -set_exit_status
LINT = golint $(GOLINT_FLAGS) $(TEST_PKGS)
DOCKER_FLAGS =
DOCKER_BUILD ?= $(DOCKER) run --rm -v $(DIR):$(BUILDMNT) $(DOCKER_FLAGS) -w $(BUILDMNT) $(BUILD_IMAGE) /bin/sh -c
GO_BUILD ?= CGO_ENABLED=0 $(GO_SYSTEM_FLAGS) go build -o $(BINARY) $(VERBOSE_FLAG) -ldflags="-s -w -X $(GOTARGET)/pkg/buildinfo.Version=$(GIT_VERSION) -X $(GOTARGET)/pkg/buildinfo.GitSHA=$(GIT_REF_LONG)" $(GOTARGET)
PUSH_WINDOWS ?= false
# Kind images
K8S_PATH ?= $(GOPATH)/src/github.com/kubernetes/kubernetes
KIND_K8S_TAG ?= $(shell cd $(K8S_PATH) && git describe)
.PHONY: all build_container linux_containers windows_containers build_sonobuoy push push_images push_manifest clean clean_image test local-test local int lint stress vet pre native deploy_kind kind_images push_kind_images check-kind-env
all: linux_containers
local-test:
$(TEST)
# Unit tests
test:
$(DOCKER_BUILD) 'CGO_ENABLED=0 $(TEST)'
# Stress tests
stress:
$(DOCKER_BUILD) 'CGO_ENABLED=0 $(STRESS_TEST)'
# Integration tests
int: DOCKER_FLAGS=-v $(KUBECONFIG):/root/.kube/kubeconfig -v /tmp/artifacts:/tmp/artifacts --env ARTIFACTS_DIR=/tmp/artifacts --env KUBECONFIG=/root/.kube/kubeconfig --network host --env SONOBUOY_CLI=$(SONOBUOY_CLI)
int: TESTARGS= $(VERBOSE_FLAG) -timeout 3m
int:
$(DOCKER_BUILD) 'CGO_ENABLED=0 $(INT_TEST)'
lint:
$(DOCKER_BUILD) '$(LINT)'
vet:
$(DOCKER_BUILD) 'CGO_ENABLED=0 $(VET)'
pre:
wget https://github.com/estesp/manifest-tool/releases/download/v1.0.1/manifest-tool-linux-amd64 \
-O manifest-tool && \
chmod +x ./manifest-tool
echo $(DOCKERHUB_TOKEN) | docker login --username sonobuoybot --password-stdin
build_container:
$(DOCKER) build \
-t $(REGISTRY)/$(TARGET):$(IMAGE_VERSION) \
-t $(REGISTRY)/$(TARGET):$(IMAGE_TAG) \
-t $(REGISTRY)/$(TARGET):$(IMAGE_BRANCH) \
-t $(REGISTRY)/$(TARGET):$(GIT_REF_SHORT) \
-f $(DOCKERFILE) \
.
linux_containers: build/linux/arm64/sonobuoy build/linux/amd64/sonobuoy
for arch in $(LINUX_ARCH); do \
if [ $$arch = amd64 ]; then \
sed -e 's|BASEIMAGE|$(AMD_IMAGE)|g' \
-e 's|CMD1||g' \
-e 's|BINARY|build/linux/amd64/sonobuoy|g' Dockerfile > Dockerfile-$$arch; \
$(MAKE) build_container DOCKERFILE=Dockerfile-$$arch; \
$(MAKE) build_container DOCKERFILE="Dockerfile-$$arch" TARGET="sonobuoy-$$arch"; \
elif [ $$arch = arm64 ]; then \
sed -e 's|BASEIMAGE|$(ARM_IMAGE)|g' \
-e 's|CMD1||g' \
-e 's|BINARY|build/linux/arm64/sonobuoy|g' Dockerfile > Dockerfile-$$arch; \
$(MAKE) build_container DOCKERFILE="Dockerfile-$$arch" TARGET="sonobuoy-$$arch"; \
else \
echo "Linux ARCH unknown"; \
fi \
done
windows_containers: build/windows/amd64/sonobuoy.exe
@echo container_win
for arch in $(WIN_ARCH); do \
if [ $$arch = amd64 ]; then \
sed -e 's|BASEIMAGE|$(WIN_IMAGE)|g' \
-e 's|BINARY|build/windows/amd64/sonobuoy.exe|g' DockerfileWindows > DockerfileWindows-$$arch; \
$(MAKE) build_container DOCKERFILE=DockerfileWindows-$$arch; \
$(MAKE) build_container DOCKERFILE="DockerfileWindows-$$arch" TARGET="sonobuoy-win-$$arch"; \
else \
echo "Windows ARCH unknown"; \
fi \
done
build_sonobuoy:
$(DOCKER_BUILD) '$(GO_BUILD)'
build/linux/arm64/sonobuoy:
echo Building: linux/arm64
mkdir -p build/linux/arm64
$(MAKE) build_sonobuoy GO_SYSTEM_FLAGS="GOOS=linux GOARCH=arm64" BINARY=$@
build/linux/amd64/sonobuoy:
echo Building: linux/amd64
mkdir -p build/linux/amd64
$(MAKE) build_sonobuoy GO_SYSTEM_FLAGS="GOOS=linux GOARCH=amd64" BINARY=$@
build/windows/amd64/sonobuoy.exe:
echo Building: windows/amd64
mkdir -p build/windows/amd64
$(MAKE) build_sonobuoy GO_SYSTEM_FLAGS="GOOS=windows GOARCH=amd64" BINARY=$@
native:
$(GO_BUILD)
push_images:
$(DOCKER) push $(REGISTRY)/$(TARGET):$(IMAGE_BRANCH)
$(DOCKER) push $(REGISTRY)/$(TARGET):$(GIT_REF_SHORT)
if git describe --tags --exact-match >/dev/null 2>&1; \
then \
$(DOCKER) tag $(REGISTRY)/$(TARGET):$(IMAGE_VERSION) $(REGISTRY)/$(TARGET):$(IMAGE_TAG); \
$(DOCKER) tag $(REGISTRY)/$(TARGET):$(IMAGE_VERSION) $(REGISTRY)/$(TARGET):latest; \
$(DOCKER) push $(REGISTRY)/$(TARGET):$(IMAGE_VERSION); \
$(DOCKER) push $(REGISTRY)/$(TARGET):$(IMAGE_TAG); \
$(DOCKER) push $(REGISTRY)/$(TARGET):latest; \
fi
gen_manifest:
mkdir -p build
ifeq ($(PUSH_WINDOWS),true)
sed -e 's|TAG|$(VERSION)|g' \
-e 's|REGISTRY|$(REGISTRY)|g' \
-e 's/WIN_ONLY//g' \
manifest_spec.yaml.tmpl > ./build/manifest_spec.yaml;
else
echo '$$PUSH_WINDOWS not set, not including Windows in manifest'
sed -e 's|TAG|$(VERSION)|g' \
-e 's|REGISTRY|$(REGISTRY)|g' \
-e '/^WIN_ONLY/d' \
manifest_spec.yaml.tmpl > ./build/manifest_spec.yaml;
endif
push_manifest: gen_manifest
./manifest-tool push from-spec ./build/manifest_spec.yaml
push: pre
# Assumes you have the images built or loaded already. Not
# added as dependency due to having both Linux/Windows
# prereqs which can't be done on the same machine.
ifeq ($(PUSH_WINDOWS),true)
for arch in $(WIN_ARCH); do \
$(MAKE) push_images TARGET="sonobuoy-win-$$arch"; \
done
else
echo '$$PUSH_WINDOWS not set, not pushing Windows images'
for arch in $(LINUX_ARCH); do \
$(MAKE) push_images TARGET="sonobuoy-$$arch"; \
done
endif
$(MAKE) push_manifest VERSION=$(IMAGE_BRANCH) TARGET="sonobuoy"
$(MAKE) push_manifest VERSION=$(GIT_REF_SHORT) TARGET="sonobuoy"
if git describe --tags --exact-match >/dev/null 2>&1; \
then \
$(MAKE) push_manifest VERSION=$(IMAGE_VERSION) TARGET="sonobuoy"; \
$(MAKE) push_manifest VERSION=$(IMAGE_TAG) TARGET="sonobuoy"; \
$(MAKE) push_manifest VERSION=latest TARGET="sonobuoy"; \
fi
clean_image:
$(DOCKER) rmi -f `$(DOCKER) images $(REGISTRY)/$(TARGET) -a -q` || true
clean:
rm -f $(TARGET); \
rm Dockerfile-*; \
rm DockerfileWindows-*; \
rm -rf build
for arch in $(LINUX_ARCH); do \
$(MAKE) clean_image TARGET=$(TARGET)-$$arch; \
done
for arch in $(WIN_ARCH); do \
$(MAKE) clean_image TARGET=$(TARGET)-win-$$arch; \
done
deploy_kind: linux_containers
kind load docker-image --name $(KIND_CLUSTER) $(REGISTRY)/$(TARGET):$(IMAGE_VERSION) || true
# kind_images will build the kind-node image. Generally building the base image is not necessary
# and we can use the upstream kindest/base image.
kind_images: check-kind-env
kind build node-image --kube-root=$(K8S_PATH) --image $(REGISTRY)/kind-node:$(KIND_K8S_TAG)
# push_kind_images will push the same image kind_images just built our registry.
push_kind_images:
docker push $(REGISTRY)/kind-node:$(KIND_K8S_TAG)
# check-kind-env will show you what will be built/tagged before doing so with kind_images
check-kind-env:
ifndef K8S_PATH
$(error K8S_PATH is undefined)
endif
ifndef KIND_K8S_TAG
$(error KIND_K8S_TAG is undefined)
endif
echo --kube-root=$(K8S_PATH) tagging as --image $(REGISTRY)/kind-node:$(KIND_K8S_TAG)