Debian Codegraph @

Makefile

raw
1 # Copyright 2019 The Kubernetes Authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # ARCH specifies the golang target platform, for docker build
16 # targetes. It is supported for backward compatibility, but prefer
17 # BUILDARCH and TARGETARCH.
18 ARCH ?= amd64
19 # BUILDARCH specifies the golang build platform, for docker build targets.
20 BUILDARCH ?= amd64
21 # TARGETARCH specifies the golang target platform, for docker build targets.
22 TARGETARCH ?= $(ARCH)
23 ALL_ARCH ?= amd64 arm arm64 ppc64le s390x
24 # The output type could either be docker (local), or registry.
25 OUTPUT_TYPE ?= docker
26 GO_TOOLCHAIN ?= golang
27 GO_VERSION ?= 1.26.2
28 GOTOOLCHAIN ?= go$(GO_VERSION)+auto
29 export GOTOOLCHAIN
30 BASEIMAGE ?= gcr.io/distroless/static-debian12:nonroot
31
32 ifeq ($(GOPATH),)
33 export GOPATH := $(shell go env GOPATH)
34 endif
35 GOOS ?= $(shell go env GOOS)
36 GOARCH ?= $(shell go env GOARCH)
37 INSTALL_LOCATION:=$(shell go env GOPATH)/bin
38 GOLANGCI_LINT_VERSION ?= 2.9.0
39 GOSEC_VERSION ?= 2.13.1
40
41 REGISTRY ?= gcr.io/$(shell gcloud config get-value project)
42 STAGING_REGISTRY := gcr.io/k8s-staging-kas-network-proxy
43
44 SERVER_IMAGE_NAME ?= proxy-server
45 AGENT_IMAGE_NAME ?= proxy-agent
46 TEST_CLIENT_IMAGE_NAME ?= proxy-test-client
47 TEST_SERVER_IMAGE_NAME ?= http-test-server
48
49 SERVER_FULL_IMAGE ?= $(REGISTRY)/$(SERVER_IMAGE_NAME)
50 AGENT_FULL_IMAGE ?= $(REGISTRY)/$(AGENT_IMAGE_NAME)
51 TEST_CLIENT_FULL_IMAGE ?= $(REGISTRY)/$(TEST_CLIENT_IMAGE_NAME)
52 TEST_SERVER_FULL_IMAGE ?= $(REGISTRY)/$(TEST_SERVER_IMAGE_NAME)
53
54 TAG ?= $(shell git rev-parse HEAD)
55
56 DOCKER_CMD ?= docker
57 DOCKER_CLI_EXPERIMENTAL ?= enabled
58 PROXY_SERVER_IP ?= 127.0.0.1
59
60 KIND_IMAGE ?= kindest/node:v1.30.2
61 CONNECTION_MODE ?= grpc
62
63 MOCKGEN_VERSION := $(shell mockgen -version)
64 DESIRED_MOCKGEN := "v0.5.2"
65
66 ## --------------------------------------
67 ## Testing
68 ## --------------------------------------
69 .PHONY: mock_gen
70 mock_gen:
71 echo "Mock gen is set to $(MOCKGEN_VERSION)"
72 if [ "$(MOCKGEN_VERSION)" != $(DESIRED_MOCKGEN) ]; then echo "Error need mockgen version $(DESIRED_VERSION)"; exit 1; fi
73 mkdir -p proto/agent/mocks
74 mockgen --build_flags=--mod=mod sigs.k8s.io/apiserver-network-proxy/proto/agent AgentService_ConnectServer > proto/agent/mocks/agent_mock.go
75 cat hack/go-license-header.txt proto/agent/mocks/agent_mock.go > proto/agent/mocks/agent_mock.licensed.go
76 mv proto/agent/mocks/agent_mock.licensed.go proto/agent/mocks/agent_mock.go
77
78 # Unit tests with faster execution (nicer for development).
79 .PHONY: fast-test
80 fast-test:
81 go test -mod=vendor -race $(shell go list ./... | grep -v -e "/e2e$$" -e "/e2e/.*")
82 cd konnectivity-client && go test -race ./...
83
84 # Unit tests with fuller coverage, invoked by CI system.
85 .PHONY: test
86 test:
87 $(eval TEST_LIST := $(shell go list -test ./... | egrep " \[.*\]" | cut -d' ' -f1 | grep -v -e "/e2e$$" -e "/e2e/.*"))
88 echo "Running tests on $(TEST_LIST)"
89 $(info GOTOOLCHAIN is $(GOTOOLCHAIN))
90 go test -v -mod=vendor -race -covermode=atomic -coverprofile=konnectivity.out $(TEST_LIST) && go tool cover -html=konnectivity.out -o=konnectivity.html
91 cd konnectivity-client && go test -race -covermode=atomic -coverprofile=client.out ./... && go tool cover -html=client.out -o=client.html
92
93 .PHONY: test-integration
94 test-integration: build
95 go test -mod=vendor -race ./tests -agent-path $(PWD)/bin/proxy-agent
96
97 .PHONY: test-e2e
98 test-e2e: docker-build
99 go test -mod=vendor ./e2e -race -agent-image ${AGENT_FULL_IMAGE}-$(TARGETARCH):${TAG} -server-image ${SERVER_FULL_IMAGE}-$(TARGETARCH):${TAG} -kind-image ${KIND_IMAGE} -mode ${CONNECTION_MODE}
100
101 # e2e test runner for continuous integration that does not build a new image.
102 .PHONY: test-e2e-ci
103 test-e2e-ci:
104 go test -mod=vendor ./e2e -race -agent-image ${AGENT_FULL_IMAGE}-$(TARGETARCH):${TAG} -server-image ${SERVER_FULL_IMAGE}-$(TARGETARCH):${TAG} -kind-image ${KIND_IMAGE} -mode ${CONNECTION_MODE}
105
106 ## --------------------------------------
107 ## Binaries
108 ## --------------------------------------
109
110 bin:
111 mkdir -p bin
112
113 .PHONY: build
114 build: bin/proxy-agent bin/proxy-server bin/proxy-test-client bin/http-test-server
115
116 .PHONY: bin/proxy-agent
117 bin/proxy-agent:
118 GO111MODULE=on go build -mod=vendor -o bin/proxy-agent cmd/agent/main.go
119
120 .PHONY: bin/proxy-test-client
121 bin/proxy-test-client:
122 GO111MODULE=on go build -mod=vendor -o bin/proxy-test-client cmd/test-client/main.go
123
124 .PHONY: bin/http-test-server
125 bin/http-test-server:
126 GO111MODULE=on go build -mod=vendor -o bin/http-test-server cmd/test-server/main.go
127
128 .PHONY: bin/proxy-server
129 bin/proxy-server:
130 GO111MODULE=on go build -mod=vendor -o bin/proxy-server cmd/server/main.go
131
132 ## --------------------------------------
133 ## Linting
134 ## --------------------------------------
135
136 .PHONY: lint
137 lint:
138 curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(INSTALL_LOCATION) v$(GOLANGCI_LINT_VERSION)
139 $(INSTALL_LOCATION)/golangci-lint run --config ./.golangci.yaml --verbose
140
141 ## --------------------------------------
142 ## Go
143 ## --------------------------------------
144
145 .PHONY: mod-download
146 mod-download:
147 go mod download
148
149 ## --------------------------------------
150 ## Proto
151 ## --------------------------------------
152
153 .PHONY: gen
154 gen: mod-download gen-proto mock_gen
155
156 .PHONY: gen-proto
157 gen-proto:
158 protoc -I . konnectivity-client/proto/client/client.proto --go_out=. --go_opt=paths=source_relative --go-grpc_out=require_unimplemented_servers=false:. --go-grpc_opt=paths=source_relative
159 cat hack/go-license-header.txt konnectivity-client/proto/client/client_grpc.pb.go > konnectivity-client/proto/client/client_grpc.licensed.go
160 mv konnectivity-client/proto/client/client_grpc.licensed.go konnectivity-client/proto/client/client_grpc.pb.go
161 protoc -I . proto/agent/agent.proto --go_out=. --go_opt=paths=source_relative --go-grpc_out=require_unimplemented_servers=false:. --go-grpc_opt=paths=source_relative
162 cat hack/go-license-header.txt proto/agent/agent_grpc.pb.go > proto/agent/agent_grpc.licensed.go
163 mv proto/agent/agent_grpc.licensed.go proto/agent/agent_grpc.pb.go
164
165 ## --------------------------------------
166 ## Certs
167 ## --------------------------------------
168
169 easy-rsa.tar.gz:
170 curl -L -o easy-rsa.tar.gz --connect-timeout 20 --retry 6 --retry-delay 2 https://dl.k8s.io/easy-rsa/easy-rsa.tar.gz
171
172 easy-rsa: easy-rsa.tar.gz
173 tar xvf easy-rsa.tar.gz
174 mv easy-rsa-master easy-rsa
175
176 cfssl:
177 @if ! command -v cfssl &> /dev/null; then \
178 curl --retry 10 -L -o cfssl https://github.com/cloudflare/cfssl/releases/download/v1.6.5/cfssl_1.6.5_$(GOOS)_$(GOARCH); \
179 chmod +x cfssl; \
180 fi
181
182 cfssljson:
183 @if ! command -v cfssljson &> /dev/null; then \
184 curl --retry 10 -L -o cfssljson https://github.com/cloudflare/cfssl/releases/download/v1.6.5/cfssljson_1.6.5_$(GOOS)_$(GOARCH); \
185 chmod +x cfssljson; \
186 fi
187
188 .PHONY: certs
189 certs: export PATH := $(shell pwd):$(PATH)
190 certs: easy-rsa cfssl cfssljson
191 # set up easy-rsa
192 cp -rf easy-rsa/easyrsa3 easy-rsa/frontend
193 cp -rf easy-rsa/easyrsa3 easy-rsa/agent
194 # create the client <-> server-proxy connection certs
195 cd easy-rsa/frontend; \
196 ./easyrsa init-pki; \
197 ./easyrsa --batch "--req-cn=127.0.0.1@$(date +%s)" build-ca nopass; \
198 ./easyrsa --subject-alt-name="DNS:kubernetes,DNS:localhost,IP:127.0.0.1" build-server-full "proxy-frontend" nopass; \
199 ./easyrsa build-client-full proxy-client nopass; \
200 echo '{"signing":{"default":{"expiry":"43800h","usages":["signing","key encipherment","client auth"]}}}' > "ca-config.json"; \
201 echo '{"CN":"proxy","names":[{"O":"system:nodes"}],"hosts":[""],"key":{"algo":"rsa","size":2048}}' | cfssl gencert -ca=pki/ca.crt -ca-key=pki/private/ca.key -config=ca-config.json - | cfssljson -bare proxy
202 mkdir -p certs/frontend
203 cp -r easy-rsa/frontend/pki/private certs/frontend
204 cp -r easy-rsa/frontend/pki/issued certs/frontend
205 cp easy-rsa/frontend/pki/ca.crt certs/frontend/issued
206 # create the agent <-> server-proxy connection certs
207 cd easy-rsa/agent; \
208 ./easyrsa init-pki; \
209 ./easyrsa --batch "--req-cn=$(PROXY_SERVER_IP)@$(date +%s)" build-ca nopass; \
210 ./easyrsa --subject-alt-name="DNS:kubernetes,DNS:localhost,IP:$(PROXY_SERVER_IP)" build-server-full "proxy-frontend" nopass; \
211 ./easyrsa build-client-full proxy-agent nopass; \
212 echo '{"signing":{"default":{"expiry":"43800h","usages":["signing","key encipherment","agent auth"]}}}' > "ca-config.json"; \
213 echo '{"CN":"proxy","names":[{"O":"system:nodes"}],"hosts":[""],"key":{"algo":"rsa","size":2048}}' | cfssl gencert -ca=pki/ca.crt -ca-key=pki/private/ca.key -config=ca-config.json - | cfssljson -bare proxy
214 mkdir -p certs/agent
215 cp -r easy-rsa/agent/pki/private certs/agent
216 cp -r easy-rsa/agent/pki/issued certs/agent
217 cp easy-rsa/agent/pki/ca.crt certs/agent/issued
218
219 ## --------------------------------------
220 ## Docker
221 ## --------------------------------------
222
223 .PHONY: buildx-setup
224 buildx-setup:
225 ${DOCKER_CMD} buildx inspect img-builder > /dev/null || docker buildx create --name img-builder --use
226
227 # Does not include test images
228 .PHONY: docker-build
229 docker-build: docker-build/proxy-agent docker-build/proxy-server
230
231 .PHONY: docker-build-test
232 docker-build-test: docker-build/proxy-test-client docker-build/http-test-server
233
234 .PHONY: docker-push
235 docker-push: docker-push/proxy-agent docker-push/proxy-server
236
237 .PHONY: docker-build/proxy-agent
238 docker-build/proxy-agent: cmd/agent/main.go proto/agent/agent.pb.go buildx-setup
239 @[ "${TAG}" ] || ( echo "TAG is not set"; exit 1 )
240 echo "Building proxy-agent with ${BUILDARCH} for ${TARGETARCH}"
241 ${DOCKER_CMD} buildx build . --pull --output=type=$(OUTPUT_TYPE) --platform linux/$(TARGETARCH) --build-arg GO_TOOLCHAIN=$(GO_TOOLCHAIN) --build-arg GO_VERSION=$(GO_VERSION) --build-arg BUILDARCH=$(BUILDARCH) --build-arg TARGETARCH=$(TARGETARCH) --build-arg BASEIMAGE=$(BASEIMAGE) -f artifacts/images/agent-build.Dockerfile -t ${AGENT_FULL_IMAGE}-$(TARGETARCH):${TAG}
242
243 .PHONY: docker-push/proxy-agent
244 docker-push/proxy-agent: docker-build/proxy-agent
245 @[ "${DOCKER_CMD}" ] || ( echo "DOCKER_CMD is not set"; exit 1 )
246 ${DOCKER_CMD} push ${AGENT_FULL_IMAGE}-$(TARGETARCH):${TAG}
247
248 .PHONY: docker-build/proxy-server
249 docker-build/proxy-server: cmd/server/main.go proto/agent/agent.pb.go buildx-setup
250 @[ "${TAG}" ] || ( echo "TAG is not set"; exit 1 )
251 echo "Building proxy-server with ${BUILDARCH} for ${TARGETARCH}"
252 ${DOCKER_CMD} buildx build . --pull --output=type=$(OUTPUT_TYPE) --platform linux/$(TARGETARCH) --build-arg GO_TOOLCHAIN=$(GO_TOOLCHAIN) --build-arg GO_VERSION=$(GO_VERSION) --build-arg BUILDARCH=$(BUILDARCH) --build-arg TARGETARCH=$(TARGETARCH) --build-arg BASEIMAGE=$(BASEIMAGE) -f artifacts/images/server-build.Dockerfile -t ${SERVER_FULL_IMAGE}-$(TARGETARCH):${TAG}
253
254 .PHONY: docker-push/proxy-server
255 docker-push/proxy-server: docker-build/proxy-server
256 @[ "${DOCKER_CMD}" ] || ( echo "DOCKER_CMD is not set"; exit 1 )
257 ${DOCKER_CMD} push ${SERVER_FULL_IMAGE}-$(TARGETARCH):${TAG}
258
259 .PHONY: docker-build/proxy-test-client
260 docker-build/proxy-test-client: cmd/test-client/main.go proto/agent/agent.pb.go buildx-setup
261 @[ "${TAG}" ] || ( echo "TAG is not set"; exit 1 )
262 echo "Building proxy-test-client with ${BUILDARCH} for ${TARGETARCH}"
263 ${DOCKER_CMD} buildx build . --pull --output=type=$(OUTPUT_TYPE) --platform linux/$(TARGETARCH) --build-arg GO_TOOLCHAIN=$(GO_TOOLCHAIN) --build-arg GO_VERSION=$(GO_VERSION) --build-arg BUILDARCH=$(BUILDARCH) --build-arg TARGETARCH=$(TARGETARCH) --build-arg BASEIMAGE=$(BASEIMAGE) -f artifacts/images/test-client-build.Dockerfile -t ${TEST_CLIENT_FULL_IMAGE}-$(TARGETARCH):${TAG}
264
265 .PHONY: docker-push/proxy-test-client
266 docker-push/proxy-test-client: docker-build/proxy-test-client
267 @[ "${DOCKER_CMD}" ] || ( echo "DOCKER_CMD is not set"; exit 1 )
268 ${DOCKER_CMD} push ${TEST_CLIENT_FULL_IMAGE}-$(TARGETARCH):${TAG}
269
270 .PHONY: docker-build/http-test-server
271 docker-build/http-test-server: cmd/test-server/main.go buildx-setup
272 @[ "${TAG}" ] || ( echo "TAG is not set"; exit 1 )
273 echo "Building http-test-server with ${BUILDARCH} for ${TARGETARCH}"
274 ${DOCKER_CMD} buildx build . --pull --output=type=$(OUTPUT_TYPE) --platform linux/$(TARGETARCH) --build-arg GO_TOOLCHAIN=$(GO_TOOLCHAIN) --build-arg GO_VERSION=$(GO_VERSION) --build-arg BUILDARCH=$(BUILDARCH) --build-arg TARGETARCH=$(TARGETARCH) --build-arg BASEIMAGE=$(BASEIMAGE) -f artifacts/images/test-server-build.Dockerfile -t ${TEST_SERVER_FULL_IMAGE}-$(TARGETARCH):${TAG}
275
276 .PHONY: docker-push/http-test-server
277 docker-push/http-test-server: docker-build/http-test-server
278 @[ "${DOCKER_CMD}" ] || ( echo "DOCKER_CMD is not set"; exit 1 )
279 ${DOCKER_CMD} push ${TEST_SERVER_FULL_IMAGE}-$(TARGETARCH):${TAG}
280
281 ## --------------------------------------
282 ## Docker — All ARCH
283 ## --------------------------------------
284
285 # As `docker buildx` is time and resource consuming, if not necessary, building specific arch images,
286 # like `make docker-build-arch-amd64`, is recommended.
287 .PHONY: docker-build-all
288 docker-build-all: $(addprefix docker-build-arch-,$(ALL_ARCH))
289
290 .PHONY: docker-push-all
291 docker-push-all: $(addprefix docker-push/proxy-agent-,$(ALL_ARCH)) $(addprefix docker-push/proxy-server-,$(ALL_ARCH))
292 $(MAKE) docker-push-manifest/proxy-agent
293 $(MAKE) docker-push-manifest/proxy-server
294
295 docker-build-arch-%:
296 $(MAKE) docker-build/proxy-agent-$*
297 $(MAKE) docker-build/proxy-server-$*
298
299 docker-build/proxy-agent-%:
300 $(MAKE) TARGETARCH=$* docker-build/proxy-agent
301
302 docker-push/proxy-agent-%:
303 $(MAKE) TARGETARCH=$* docker-push/proxy-agent
304
305 docker-build/proxy-server-%:
306 $(MAKE) TARGETARCH=$* docker-build/proxy-server
307
308 docker-push/proxy-server-%:
309 $(MAKE) TARGETARCH=$* docker-push/proxy-server
310
311 .PHONY: docker-push-manifest/proxy-agent
312 docker-push-manifest/proxy-agent: ## Push the fat manifest docker image.
313 ## Minimum docker version 18.06.0 is required for creating and pushing manifest images.
314 ${DOCKER_CMD} manifest create --amend $(AGENT_FULL_IMAGE):$(TAG) $(shell echo $(ALL_ARCH) | sed -e "s~[^ ]*~$(AGENT_FULL_IMAGE)\-&:$(TAG)~g")
315 @for arch in $(ALL_ARCH); do ${DOCKER_CMD} manifest annotate --arch $${arch} ${AGENT_FULL_IMAGE}:${TAG} ${AGENT_FULL_IMAGE}-$${arch}:${TAG}; done
316 ${DOCKER_CMD} manifest push --purge $(AGENT_FULL_IMAGE):$(TAG)
317
318 .PHONY: docker-push-manifest/proxy-server
319 docker-push-manifest/proxy-server: ## Push the fat manifest docker image.
320 ## Minimum docker version 18.06.0 is required for creating and pushing manifest images.
321 ${DOCKER_CMD} manifest create --amend $(SERVER_FULL_IMAGE):$(TAG) $(shell echo $(ALL_ARCH) | sed -e "s~[^ ]*~$(SERVER_FULL_IMAGE)\-&:$(TAG)~g")
322 @for arch in $(ALL_ARCH); do ${DOCKER_CMD} manifest annotate --arch $${arch} ${SERVER_FULL_IMAGE}:${TAG} ${SERVER_FULL_IMAGE}-$${arch}:${TAG}; done
323 ${DOCKER_CMD} manifest push --purge $(SERVER_FULL_IMAGE):$(TAG)
324
325 ## --------------------------------------
326 ## Release
327 ## --------------------------------------
328
329 .PHONY: release-staging
330 release-staging: ## Builds and push container images to the staging bucket.
331 REGISTRY=$(STAGING_REGISTRY) $(MAKE) docker-push-all release-alias-tag
332
333 .PHONY: release-alias-tag
334 release-alias-tag: # Adds the tag to the last build tag. BASE_REF comes from the cloudbuild.yaml
335 gcloud container images add-tag $(AGENT_FULL_IMAGE):$(TAG) $(AGENT_FULL_IMAGE):$(BASE_REF)
336 gcloud container images add-tag $(SERVER_FULL_IMAGE):$(TAG) $(SERVER_FULL_IMAGE):$(BASE_REF)
337
338 ## --------------------------------------
339 ## Cleanup / Verification
340 ## --------------------------------------
341
342 .PHONY: clean
343 clean:
344 go clean -testcache
345 rm -rf proto/agent/agent.pb.go proto/agent/agent_grpc.pb.go konnectivity-client/proto/client/client.pb.go konnectivity-client/proto/client/client_grpc.pb.go konnectivity-client/proto/client/client_grpc.licensed.go proto/agent/agent_grpc.licensed.go easy-rsa.tar.gz easy-rsa cfssl cfssljson certs bin proto/agent/mocks konnectivity.html konnectivity.out konnectivity-client/client.html konnectivity-client/client.out