-
Notifications
You must be signed in to change notification settings - Fork 754
/
Copy pathMakefile
235 lines (206 loc) · 8.22 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
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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.
#
.PHONY: all dist check clean \
lint format check-format vet docker-vet \
build-linux docker \
unit-test unit-test-race build-docker-test docker-func-test \
build-metrics docker-metrics \
metrics-unit-test docker-metrics-test
# VERSION is the source revision that executables and images are built from.
VERSION = $(shell git describe --tags --always --dirty || echo "unknown")
# DESTDIR is where distribution output (container images) is placed.
DESTDIR = .
# IMAGE is the primary AWS VPC CNI plugin container image.
IMAGE = amazon/amazon-k8s-cni
IMAGE_NAME = $(IMAGE)$(IMAGE_ARCH_SUFFIX):$(VERSION)
IMAGE_DIST = $(DESTDIR)/$(subst /,_,$(IMAGE_NAME)).tar.gz
# METRICS_IMAGE is the CNI metrics publisher sidecar container image.
METRICS_IMAGE = amazon/cni-metrics-helper
METRICS_IMAGE_NAME = $(METRICS_IMAGE)$(IMAGE_ARCH_SUFFIX):$(VERSION)
METRICS_IMAGE_DIST = $(DESTDIR)/$(subst /,_,$(METRICS_IMAGE_NAME)).tar.gz
# TEST_IMAGE is the testing environment container image.
TEST_IMAGE = amazon-k8s-cni-test
TEST_IMAGE_NAME = $(TEST_IMAGE)$(IMAGE_ARCH_SUFFIX):$(VERSION)
# These values derive ARCH and DOCKER_ARCH which are needed by dependencies in
# image build defaulting to system's architecture when not specified.
#
# UNAME_ARCH is the runtime architecture of the building host.
UNAME_ARCH = $(shell uname -m)
# ARCH is the target architecture which is being built for.
#
# These are pairs of input_arch to derived_arch separated by colons:
ARCH = $(lastword $(subst :, ,$(filter $(UNAME_ARCH):%,x86_64:amd64 aarch64:arm64)))
# DOCKER_ARCH is the docker specific architecture specifier used for building on
# multiarch container images.
DOCKER_ARCH = $(lastword $(subst :, ,$(filter $(ARCH):%,amd64:amd64 arm64:arm64v8)))
# IMAGE_ARCH_SUFFIX is the `-arch` suffix included in the container image name.
#
# This is only applied to the arm64 container image by default. Override to
# provide an alternate suffix or to omit.
IMAGE_ARCH_SUFFIX = $(addprefix -,$(filter $(ARCH),arm64))
# GOLANG_IMAGE is the building golang container image used.
GOLANG_IMAGE = golang:1.13-stretch
# For the requseted build, these are the set of Go specific build environment
# variables.
export GOARCH ?= $(ARCH)
export GOOS = linux
export CGO_ENABLED = 0
# NOTE: Provided for local toolchains that require explicit module feature flag.
export GO111MODULE = on
export GOPROXY = direct
# LDFLAGS is the set of flags used when building golang executables.
LDFLAGS = -X main.version=$(VERSION)
# ALLPKGS is the set of packages provided in source.
ALLPKGS = $(shell go list ./...)
# BINS is the set of built command executables.
BINS = aws-k8s-agent aws-cni grpc-health-probe cni-metrics-helper
# DOCKER_ARGS is extra arguments passed during container image build.
DOCKER_ARGS =
# DOCKER_RUN_FLAGS is set the flags passed during runs of containers.
DOCKER_RUN_FLAGS = --rm -ti $(DOCKER_ARGS)
# DOCKER_BUILD_FLAGS is the set of flags passed during container image builds
# based on the requested build.
DOCKER_BUILD_FLAGS = --build-arg GOARCH="$(ARCH)" \
--build-arg docker_arch="$(DOCKER_ARCH)" \
--build-arg golang_image="$(GOLANG_IMAGE)" \
--network=host \
$(DOCKER_ARGS)
# Default to building an executable using the host's Go toolchain.
.DEFAULT_GOAL = build-linux
# Build both CNI and metrics helper container images.
all: docker docker-metrics
dist: all
mkdir -p $(DESTDIR)
docker save $(IMAGE_NAME) | gzip > $(IMAGE_DIST)
docker save $(METRICS_IMAGE_NAME) | gzip > $(METRICS_IMAGE_DIST)
# Build the VPC CNI plugin agent using the host's Go toolchain.
build-linux: BUILD_FLAGS = -ldflags '-s -w $(LDFLAGS)'
build-linux:
go build $(BUILD_FLAGS) -o aws-k8s-agent ./cmd/aws-k8s-agent
go build $(BUILD_FLAGS) -o aws-cni ./cmd/routed-eni-cni-plugin
go build $(BUILD_FLAGS) -o grpc-health-probe ./cmd/grpc-health-probe
# Build VPC CNI plugin & agent container image.
docker:
docker build $(DOCKER_BUILD_FLAGS) \
-f scripts/dockerfiles/Dockerfile.release \
-t "$(IMAGE_NAME)" \
.
@echo "Built Docker image \"$(IMAGE_NAME)\""
# Run the built cni container image to use in functional testing
docker-func-test: docker
docker run $(DOCKER_RUN_FLAGS) \
"$(IMAGE_NAME)"
# Run unit tests
unit-test:
go test -v -cover $(ALLPKGS)
# Run unit tests with race detection (can only be run natively)
unit-test-race: CGO_ENABLED=1
unit-test-race: GOARCH=
unit-test-race:
go test -v -cover -race -timeout 10s ./cmd/...
go test -v -cover -race -timeout 150s ./pkg/awsutils/...
go test -v -cover -race -timeout 10s ./pkg/k8sapi/...
go test -v -cover -race -timeout 10s ./pkg/networkutils/...
go test -v -cover -race -timeout 10s ./pkg/utils/...
go test -v -cover -race -timeout 10s ./pkg/eniconfig/...
go test -v -cover -race -timeout 10s ./pkg/ipamd/...
# Build the unit test driver container image.
build-docker-test:
docker build $(DOCKER_BUILD_FLAGS) \
-f scripts/dockerfiles/Dockerfile.test \
-t $(TEST_IMAGE_NAME) \
.
# Run unit tests inside of the testing container image.
docker-unit-test: build-docker-test
docker run $(DOCKER_RUN_ARGS) \
$(TEST_IMAGE_NAME) \
make unit-test
# Build metrics helper agent.
build-metrics:
go build -ldflags="-s -w" -o cni-metrics-helper ./cmd/cni-metrics-helper
# Build metrics helper agent Docker image.
docker-metrics:
docker build $(DOCKER_BUILD_FLAGS) \
-f scripts/dockerfiles/Dockerfile.metrics \
-t "$(METRICS_IMAGE_NAME)" \
.
@echo "Built Docker image \"amazon/cni-metrics-helper:$(VERSION)\""
# Run metrics helper unit test suite (must be run natively).
metrics-unit-test: CGO_ENABLED=1
metrics-unit-test: GOARCH=
metrics-unit-test:
go test -v -cover -race -timeout 10s \
./cmd/cni-metrics-helper/metrics/...
# Run metrics helper unit test suite in a container.
docker-metrics-test:
docker run $(DOCKER_RUN_FLAGS) \
-v $(shell pwd -P):/src --workdir=/src \
-e GOARCH -e GOOS -e GO111MODULE \
$(GOLANG_IMAGE) \
make metrics-unit-test
generate:
go generate -x ./...
$(MAKE) format
# Generate descriptors for supported ENI configurations.
generate-limits:
go run pkg/awsutils/gen_vpc_ip_limits.go
# Fetch portmap the port-forwarding management CNI plugin
portmap: FETCH_VERSION=0.7.5
portmap: FETCH_URL=https://github.com/containernetworking/plugins/releases/download/v$(FETCH_VERSION)/cni-plugins-$(GOARCH)-v$(FETCH_VERSION).tgz
portmap: VISIT_URL=https://github.com/containernetworking/plugins/tree/v$(FETCH_VERSION)/plugins/meta/portmap
portmap:
@echo "Fetching portmap CNI plugin v$(FETCH_VERSION) from upstream release"
@echo
@echo "Visit upstream project for portmap plugin details:"
@echo "$(VISIT_URL)"
@echo
curl -L $(FETCH_URL) | tar -z -x ./portmap
# Run all source code checks.
check: check-format lint vet
# Run golint on source code.
#
# To install:
#
# go get -u golang.org/x/lint/golint
#
lint: LINT_FLAGS = -set_exit_status
lint:
@command -v golint >/dev/null || { echo "ERROR: golint not installed"; exit 1; }
find . \
-type f -name '*.go' \
-not -name 'mock_*' -not -name 'mocks_*' \
-print0 | sort -z | xargs -0 -L1 -- golint $(LINT_FLAGS) 2>/dev/null
# Run go vet on source code.
vet:
go vet ./...
# Run go vet inside of a container.
docker-vet: build-docker-test
docker run $(DOCKER_RUN_FLAGS) \
$(TEST_IMAGE_NAME) make vet
# Format all Go source code files.
format:
@command -v goimports >/dev/null || { echo "ERROR: goimports not installed"; exit 1; }
find ./* \
-type f \
-not -name 'mock_publisher.go' \
-not -name 'rpc.pb.go' \
-name '*.go' \
-print0 | sort -z | xargs -0 -- goimports $(or $(FORMAT_FLAGS),-w)
# Check formatting of source code files without modification.
check-format: FORMAT_FLAGS = -l
check-format: format
# Clean temporary files and build artifacts from the project.
clean:
@rm -f -- $(BINS)
@rm -f -- portmap