This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Makefile
237 lines (188 loc) · 10.6 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
# Defines defaults for building, tagging and pushing our Docker images. You can
# override any of these with environment variables. Most notably, when working
# on shipper, you'll probably want to override DOCKER_REGISTRY to point to a
# private registry available to you.
DOCKER_REGISTRY ?= docker.io
IMAGE_TAG ?= latest
SHIPPER_IMAGE ?= $(DOCKER_REGISTRY)/bookingcom/shipper:$(IMAGE_TAG)
SHIPPER_STATE_METRICS_IMAGE ?= $(DOCKER_REGISTRY)/bookingcom/shipper-state-metrics:$(IMAGE_TAG)
# Defines the directory of the kustomizable yaml files.
# Defaults to test directory.
# When releasing shipper, this should be set to release directory.
OVERLAY_PATH ?= kubernetes/overlays/test
# Defines the namespace where you want shipper to run.
SHIPPER_NAMESPACE ?= shipper-system
# Defines the path to a shipper clusters definition to be used by shipperctl in
# `make setup`. See ci/clusters.yaml for an example, if you need to override
# this in your development environment.
SHIPPER_CLUSTERS_YAML ?= ci/clusters.yaml
# Defines the default application cluster the end-to-end tests will use. To
# find out which clusters you have available to you, `kubectl get clusters`. If
# that errors out, or returns nothing, you probably need to run `make setup`
# first. This value needs to be present in the `applicationClusters` section in
# $(SHIPPER_CLUSTERS_YAML).
SHIPPER_CLUSTER ?= kind-app
# Cluster where to install shipper
SHIPPER_MGMT_CLUSTER ?= kind-mgmt
# User to use when connecting to management cluster
SHIPPER_MGMT_USER ?= kind-mgmt
# Defines optional flags to pass to `build/e2e.test` when running end-to-end
# tests. Useful flags are "-inspectfailed" (keep namespaces used for tests that
# filed) and "--test.v" (outputs information about every test, not only failed
# ones).
E2E_FLAGS ?=
# Defines optional flags to pass to `go test` when running unit tests. Most
# useful flag here is `-v`, for verbose output.
TEST_FLAGS ?=
# Defines optional flags to pass to `shipperctl` when running `make setup`.
SETUP_FLAGS ?=
SETUP_MGMT_FLAGS ?=
JOIN_FLAGS ?=
# When set, deployments generated by `make build-yaml` will refer to the
# image's digest instead of a tag. Very useful in development, but not so much
# when building releases.
USE_IMAGE_NAME_WITH_SHA256 ?= 1
# Defines which version shipper is gonna report itself as. By default, it takes
# the latest tag, and annotates it further with the topmost commit if it doesn't
# match the latest tag, and whether the working copy is dirty.
SHIPPER_VERSION ?= $(shell git describe --tags --dirty)
# All the files (*.go and otherwise) that, when changed, trigger rebuilds of
# binaries in the next run of `make`.
PKG := pkg/**/* vendor/**/*
# The binaries we want to build from `cmd/`.
BINARIES := shipper shipperctl shipper-state-metrics
# The operating systems we support. This gets used by `go build` as the `GOOS`
# environment variable.
OS := linux windows darwin
# The operating system where we're currently running. This is just a shorthand
# for a few targets.
GOOS ?= $(shell go env GOOS)
VERSION_PKG := github.com/bookingcom/shipper/pkg/version
LDFLAGS := -ldflags "-X $(VERSION_PKG).Version=$(SHIPPER_VERSION)"
# Setup go environment variables that we want for every `go build` and `go
# test`, to ensure our environment is consistent for all developers.
export GOFLAGS := -mod=vendor
export GO111MODULE := on
export CGO_ENABLED := 0
# *** Common targets ***
# These are the targets you are most likely to use directly, either when
# working on shipper, or via CI scripts.
KUBECTL ?= kubectl --user $(SHIPPER_MGMT_USER) --cluster $(SHIPPER_MGMT_CLUSTER) -n $(SHIPPER_NAMESPACE)
.PHONY: setup install e2e restart logs lint test vendor verify-codegen update-codegen clean
# Set up shipper clusters with `shipperctl`. This is probably the first thing
# you should do when starting to work on shipper, as most of everything else
# depends on having a management cluster talking to an application cluster.
setup: $(SHIPPER_CLUSTERS_YAML) build/shipperctl.$(GOOS)-amd64
./build/shipperctl.$(GOOS)-amd64 clusters setup management -n $(SHIPPER_NAMESPACE) $(SETUP_FLAGS) $(SETUP_MGMT_FLAGS)
./build/shipperctl.$(GOOS)-amd64 clusters join -f $(SHIPPER_CLUSTERS_YAML) -n $(SHIPPER_NAMESPACE) $(SETUP_FLAGS) $(JOIN_FLAGS)
# Install shipper in kubernetes, by applying all the required deployment yamls.
install: build-images build-yaml install/shipper install/shipper-state-metrics
install/shipper:
$(KUBECTL) apply -f build/shipper.deployment.$(IMAGE_TAG).yaml
install/shipper-state-metrics:
$(KUBECTL) apply -f build/shipper-state-metrics.deployment.$(IMAGE_TAG).yaml
# Run all end-to-end tests. It does all the work necessary to get the current
# version of shipper on your working directory running in kubernetes, so just
# running `make -j e2e` should get you up and running immediately. Do remember
# do setup your clusters with `make setup` though.
e2e: install build/e2e.test
$(KUBECTL) wait --for=condition=Progressing deploy/shipper
$(KUBECTL) wait --for=condition=Ready pod -l app=shipper
./build/e2e.test --e2e --kubeconfig ~/.kube/config \
--appcluster $(SHIPPER_CLUSTER) \
--testcharts $(TEST_HELM_REPO_URL) \
$(E2E_FLAGS)
# Delete all pods in $(SHIPPER_NAMESPACE), to force kubernetes to spawn new
# ones with the latest image (assuming that imagePullPolicy is set to Always).
restart:
$(KUBECTL) delete pods --all
# Tail logs from shipper's pods.
logs:
$(KUBECTL) logs -l app=shipper -f
# Run all linters. It's useful to run this one before pushing commits ;)
lint:
golangci-lint run -v --config .golangci.yml ./pkg/... ./cmd/... ./test/...
# Run all unit tests. It's useful to run this one before pushing commits ;)
test:
go test $(TEST_FLAGS) ./pkg/... ./cmd/...
# Tidy up and vendor dependencies. Run this every time you add or remove
# dependencies, otherwise the CI pipeline will fail to download them, and
# shipper won't build.
vendor:
go mod tidy -v
go mod vendor -v
go mod verify
# Verifies if the auto-generated code in pkg/apis/shipper/v1alpha1 and
# pkg/client is up to date.
verify-codegen:
GOPATH=$(shell go env GOPATH) ./hack/verify-codegen.sh
# Generates code for the types in pkg/apis/shipper/v1alpha1 and pkg/client.
update-codegen:
GOPATH=$(shell go env GOPATH) ./hack/update-codegen.sh
# Remove all build artifacts from the filesystem, and all objects installed in
# kubernetes.
.NOTPARALLEL: clean
clean:
rm -rf build/
# *** build/ targets ***
.PHONY: build-bin build-yaml build-images build-all
SHA = $(if $(shell which sha256sum),sha256sum,shasum -a 256)
build-bin: $(foreach bin,$(BINARIES),build/$(bin).$(GOOS)-amd64)
build-yaml: build/kustomize build/shipper.deployment.$(IMAGE_TAG).yaml build/shipper-state-metrics.deployment.$(IMAGE_TAG).yaml
build-images: build/shipper.image.$(IMAGE_TAG) build/shipper-state-metrics.image.$(IMAGE_TAG)
build-all: $(foreach os,$(OS),build/shipperctl.$(os)-amd64.tar.gz) build/sha256sums.txt build-yaml build-images
build:
mkdir -p build
build/shipper-state-metrics.%-amd64: $(PKG) cmd/shipper-state-metrics/*
GOOS=$* GOARCH=amd64 go build $(LDFLAGS) -o build/shipper-state-metrics.$*-amd64 cmd/shipper-state-metrics/*.go
build/shipper.%-amd64: $(PKG) cmd/shipper/*
GOOS=$* GOARCH=amd64 go build $(LDFLAGS) -o build/shipper.$*-amd64 cmd/shipper/*.go
build/shipperctl.%-amd64: $(PKG) cmd/shipperctl/**/*
GOOS=$* GOARCH=amd64 go build $(LDFLAGS) -o build/shipperctl.$*-amd64 cmd/shipperctl/*.go
build/e2e.test: $(PKG) test/e2e/*
go test -c ./test/e2e/ -o build/e2e.test
SHIPPER_IMAGE_NAME_WITH_SHA256 = $(shell cat build/shipper.image.$(IMAGE_TAG))
SHIPPER_IMAGE_NAME_TO_USE = $(if $(USE_IMAGE_NAME_WITH_SHA256),$(SHIPPER_IMAGE_NAME_WITH_SHA256),$(SHIPPER_IMAGE_NAME_WITH_TAG))
.PHONY: build/shipper.deployment.$(IMAGE_TAG).yaml
build/shipper.deployment.$(IMAGE_TAG).yaml: build/shipper.image.$(IMAGE_TAG) build
sed --in-place -e 's=SHIPPER_IMAGE=$(SHIPPER_IMAGE_NAME_TO_USE)=' build/shipper.deployment.$(IMAGE_TAG).yaml
SHIPPER_STATE_METRICS_IMAGE_NAME_WITH_SHA256 = $(shell cat build/shipper-state-metrics.image.$(IMAGE_TAG))
SHIPPER_STATE_METRICS_IMAGE_NAME_TO_USE = $(if $(USE_IMAGE_NAME_WITH_SHA256),$(SHIPPER_STATE_METRICS_IMAGE_NAME_WITH_SHA256),$(SHIPPER_STATE_METRICS_IMAGE_NAME_WITH_TAG))
.PHONY: build/shipper-state-metrics.deployment.$(IMAGE_TAG).yaml
build/shipper-state-metrics.deployment.$(IMAGE_TAG).yaml: build/shipper-state-metrics.image.$(IMAGE_TAG) build
sed --in-place -e 's=SHIPPER_STATE_METRICS_IMAGE=$(SHIPPER_STATE_METRICS_IMAGE_NAME_TO_USE)=' build/shipper-state-metrics.deployment.$(IMAGE_TAG).yaml
build/kustomize:
kustomize build $(OVERLAY_PATH) -o build
mv build/apps_v1_deployment_shipper.yaml build/shipper.deployment.$(IMAGE_TAG).yaml
mv build/apps_v1_deployment_shipper-state-metrics.yaml build/shipper-state-metrics.deployment.$(IMAGE_TAG).yaml
build/sha256sums.txt: $(foreach os,$(OS),build/shipperctl.$(os)-amd64.tar.gz)
$(SHA) build/*.tar.gz > $@
build/%.tar.gz: build/%
tar -zcvf $@ -C build $*
# *** Docker Image Targets ***
# Take the "%" from a target (that is now in $*) and use that as part of a
# variable name, that then gets evaluated, kind of like a poor man's hash. So
# if you have "shipper" in $*, IMAGE_NAME_WITH_TAG will read from
# $(SHIPPER_IMAGE).
IMAGE_NAME_WITH_TAG = $($(subst -,_,$(shell echo $* | tr '[:lower:]' '[:upper:]'))_IMAGE)
SHIPPER_IMAGE_NAME_WITH_TAG = $($(subst -,_,$(shell echo "shipper" | tr '[:lower:]' '[:upper:]'))_IMAGE)
SHIPPER_STATE_METRICS_IMAGE_NAME_WITH_TAG = $($(subst -,_,$(shell echo "shipper-state-metrics" | tr '[:lower:]' '[:upper:]'))_IMAGE)
# The shipper and shipper-state-metrics targets here are phony and
# supposed to be used directly, as a shorthand. They call their close cousins
# in `build/%.image.$(IMAGE_TAG)`, that are *not* phony, as they output the
# fully qualified name to an image that's immutable to a file. This serves two
# purposes:
#
# - there's no need to manually delete pods from kubernetes to get new images
# running, as we can use the digest in deployments so `make install` always
# deploys the most recent image.
# - when the file with the image name is up to date, it prevents `docker
# build` from being called at all, as it just tells us that all layers have
# already been cached and it didn't generate a new image.
.PHONY: shipper shipper-state-metrics
shipper: build/shipper.image.$(IMAGE_TAG)
shipper-state-metrics: build/shipper-state-metrics.image.$(IMAGE_TAG)
build/%.image.$(IMAGE_TAG): Dockerfile.% build/%.linux-amd64
docker build -f Dockerfile.$* -t $(IMAGE_NAME_WITH_TAG) --build-arg HTTP_PROXY=$(HTTP_PROXY) --build-arg HTTPS_PROXY=$(HTTPS_PROXY) .
docker push $(IMAGE_NAME_WITH_TAG)
docker inspect --format='{{index .RepoDigests 0}}' $(IMAGE_NAME_WITH_TAG) > $@