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.23.6
28 BASEIMAGE ?= gcr.io/distroless/static-debian12:nonroot
29
30 ifeq ($(GOPATH),)
31 export GOPATH := $(shell go env GOPATH)
32 endif
33 GOOS ?= $(shell go env GOOS)
34 GOARCH ?= $(shell go env GOARCH)
35 INSTALL_LOCATION:=$(shell go env GOPATH)/bin
36 GOLANGCI_LINT_VERSION ?= 2.1.6
37 GOSEC_VERSION ?= 2.13.1
38
39 REGISTRY ?= gcr.io/$(shell gcloud config get-value project)
40 STAGING_REGISTRY := gcr.io/k8s-staging-kas-network-proxy
41
42 SERVER_IMAGE_NAME ?= proxy-server
43 AGENT_IMAGE_NAME ?= proxy-agent
44 TEST_CLIENT_IMAGE_NAME ?= proxy-test-client
45 TEST_SERVER_IMAGE_NAME ?= http-test-server
46
47 SERVER_FULL_IMAGE ?= $(REGISTRY)/$(SERVER_IMAGE_NAME)
48 AGENT_FULL_IMAGE ?= $(REGISTRY)/$(AGENT_IMAGE_NAME)
49 TEST_CLIENT_FULL_IMAGE ?= $(REGISTRY)/$(TEST_CLIENT_IMAGE_NAME)
50 TEST_SERVER_FULL_IMAGE ?= $(REGISTRY)/$(TEST_SERVER_IMAGE_NAME)
51
52 TAG ?= $(shell git rev-parse HEAD)
53
54 DOCKER_CMD ?= docker
55 DOCKER_CLI_EXPERIMENTAL ?= enabled
56 PROXY_SERVER_IP ?= 127.0.0.1
57
58 KIND_IMAGE ?= kindest/node:v1.30.2
59 CONNECTION_MODE ?= grpc
60 ## --------------------------------------
61 ## Testing
62 ## --------------------------------------
63 .PHONY: mock_gen
64 mock_gen:
65 mkdir -p proto/agent/mocks
66 mockgen --build_flags=--mod=mod sigs.k8s.io/apiserver-network-proxy/proto/agent AgentService_ConnectServer > proto/agent/mocks/agent_mock.go
67 cat hack/go-license-header.txt proto/agent/mocks/agent_mock.go > proto/agent/mocks/agent_mock.licensed.go
68 mv proto/agent/mocks/agent_mock.licensed.go proto/agent/mocks/agent_mock.go
69
70 # Unit tests with faster execution (nicer for development).
71 .PHONY: fast-test
72 fast-test:
73 go test -mod=vendor -race $(shell go list ./... | grep -v -e "/e2e$$" -e "/e2e/.*")
74 cd konnectivity-client && go test -race ./...
75
76 # Unit tests with fuller coverage, invoked by CI system.
77 .PHONY: test
78 test:
79 go test -v -mod=vendor -race -covermode=atomic -coverprofile=konnectivity.out $(shell go list ./... | grep -v -e "/e2e$$" -e "/e2e/.*") && go tool cover -html=konnectivity.out -o=konnectivity.html
80 cd konnectivity-client && go test -race -covermode=atomic -coverprofile=client.out ./... && go tool cover -html=client.out -o=client.html
81
82 .PHONY: test-integration
83 test-integration: build
84 go test -mod=vendor -race ./tests -agent-path $(PWD)/bin/proxy-agent
85
86 .PHONY: test-e2e
87 test-e2e: docker-build
88 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}
89
90 # e2e test runner for continuous integration that does not build a new image.
91 .PHONY: test-e2e-ci
92 test-e2e-ci:
93 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}
94
95 ## --------------------------------------
96 ## Binaries
97 ## --------------------------------------
98
99 bin:
100 mkdir -p bin
101
102 .PHONY: build
103 build: bin/proxy-agent bin/proxy-server bin/proxy-test-client bin/http-test-server
104
105 .PHONY: bin/proxy-agent
106 bin/proxy-agent:
107 GO111MODULE=on go build -mod=vendor -o bin/proxy-agent cmd/agent/main.go
108
109 .PHONY: bin/proxy-test-client
110 bin/proxy-test-client:
111 GO111MODULE=on go build -mod=vendor -o bin/proxy-test-client cmd/test-client/main.go
112
113 .PHONY: bin/http-test-server
114 bin/http-test-server:
115 GO111MODULE=on go build -mod=vendor -o bin/http-test-server cmd/test-server/main.go
116
117 .PHONY: bin/proxy-server
118 bin/proxy-server:
119 GO111MODULE=on go build -mod=vendor -o bin/proxy-server cmd/server/main.go
120
121 ## --------------------------------------
122 ## Linting
123 ## --------------------------------------
124
125 .PHONY: lint
126 lint:
127 curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(INSTALL_LOCATION) v$(GOLANGCI_LINT_VERSION)
128 $(INSTALL_LOCATION)/golangci-lint run --config ./.golangci.yaml --verbose
129
130 ## --------------------------------------
131 ## Go
132 ## --------------------------------------
133
134 .PHONY: mod-download
135 mod-download:
136 go mod download
137
138 ## --------------------------------------
139 ## Proto
140 ## --------------------------------------
141
142 .PHONY: gen
143 gen: mod-download gen-proto mock_gen
144
145 .PHONY: gen-proto
146 gen-proto:
147 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
148 cat hack/go-license-header.txt konnectivity-client/proto/client/client_grpc.pb.go > konnectivity-client/proto/client/client_grpc.licensed.go
149 mv konnectivity-client/proto/client/client_grpc.licensed.go konnectivity-client/proto/client/client_grpc.pb.go
150 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
151 cat hack/go-license-header.txt proto/agent/agent_grpc.pb.go > proto/agent/agent_grpc.licensed.go
152 mv proto/agent/agent_grpc.licensed.go proto/agent/agent_grpc.pb.go
153
154 ## --------------------------------------
155 ## Certs
156 ## --------------------------------------
157
158 easy-rsa.tar.gz:
159 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
160
161 easy-rsa: easy-rsa.tar.gz
162 tar xvf easy-rsa.tar.gz
163 mv easy-rsa-master easy-rsa
164
165 cfssl:
166 @if ! command -v cfssl &> /dev/null; then \
167 curl --retry 10 -L -o cfssl https://github.com/cloudflare/cfssl/releases/download/v1.6.5/cfssl_1.6.5_$(GOOS)_$(GOARCH); \
168 chmod +x cfssl; \
169 fi
170
171 cfssljson:
172 @if ! command -v cfssljson &> /dev/null; then \
173 curl --retry 10 -L -o cfssljson https://github.com/cloudflare/cfssl/releases/download/v1.6.5/cfssljson_1.6.5_$(GOOS)_$(GOARCH); \
174 chmod +x cfssljson; \
175 fi
176
177 .PHONY: certs
178 certs: export PATH := $(shell pwd):$(PATH)
179 certs: easy-rsa cfssl cfssljson
180 # set up easy-rsa
181 cp -rf easy-rsa/easyrsa3 easy-rsa/frontend
182 cp -rf easy-rsa/easyrsa3 easy-rsa/agent
183 # create the client <-> server-proxy connection certs
184 cd easy-rsa/frontend; \
185 ./easyrsa init-pki; \
186 ./easyrsa --batch "--req-cn=127.0.0.1@$(date +%s)" build-ca nopass; \
187 ./easyrsa --subject-alt-name="DNS:kubernetes,DNS:localhost,IP:127.0.0.1" build-server-full "proxy-frontend" nopass; \
188 ./easyrsa build-client-full proxy-client nopass; \
189 echo '{"signing":{"default":{"expiry":"43800h","usages":["signing","key encipherment","client auth"]}}}' > "ca-config.json"; \
190 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
191 mkdir -p certs/frontend
192 cp -r easy-rsa/frontend/pki/private certs/frontend
193 cp -r easy-rsa/frontend/pki/issued certs/frontend
194 cp easy-rsa/frontend/pki/ca.crt certs/frontend/issued
195 # create the agent <-> server-proxy connection certs
196 cd easy-rsa/agent; \
197 ./easyrsa init-pki; \
198 ./easyrsa --batch "--req-cn=$(PROXY_SERVER_IP)@$(date +%s)" build-ca nopass; \
199 ./easyrsa --subject-alt-name="DNS:kubernetes,DNS:localhost,IP:$(PROXY_SERVER_IP)" build-server-full "proxy-frontend" nopass; \
200 ./easyrsa build-client-full proxy-agent nopass; \
201 echo '{"signing":{"default":{"expiry":"43800h","usages":["signing","key encipherment","agent auth"]}}}' > "ca-config.json"; \
202 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
203 mkdir -p certs/agent
204 cp -r easy-rsa/agent/pki/private certs/agent
205 cp -r easy-rsa/agent/pki/issued certs/agent
206 cp easy-rsa/agent/pki/ca.crt certs/agent/issued
207
208 ## --------------------------------------
209 ## Docker
210 ## --------------------------------------
211
212 .PHONY: buildx-setup
213 buildx-setup:
214 ${DOCKER_CMD} buildx inspect img-builder > /dev/null || docker buildx create --name img-builder --use
215
216 # Does not include test images
217 .PHONY: docker-build
218 docker-build: docker-build/proxy-agent docker-build/proxy-server
219
220 .PHONY: docker-build-test
221 docker-build-test: docker-build/proxy-test-client docker-build/http-test-server
222
223 .PHONY: docker-push
224 docker-push: docker-push/proxy-agent docker-push/proxy-server
225
226 .PHONY: docker-build/proxy-agent
227 docker-build/proxy-agent: cmd/agent/main.go proto/agent/agent.pb.go buildx-setup
228 @[ "${TAG}" ] || ( echo "TAG is not set"; exit 1 )
229 echo "Building proxy-agent with ${BUILDARCH} for ${TARGETARCH}"
230 ${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}
231
232 .PHONY: docker-push/proxy-agent
233 docker-push/proxy-agent: docker-build/proxy-agent
234 @[ "${DOCKER_CMD}" ] || ( echo "DOCKER_CMD is not set"; exit 1 )
235 ${DOCKER_CMD} push ${AGENT_FULL_IMAGE}-$(TARGETARCH):${TAG}
236
237 .PHONY: docker-build/proxy-server
238 docker-build/proxy-server: cmd/server/main.go proto/agent/agent.pb.go buildx-setup
239 @[ "${TAG}" ] || ( echo "TAG is not set"; exit 1 )
240 echo "Building proxy-server 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/server-build.Dockerfile -t ${SERVER_FULL_IMAGE}-$(TARGETARCH):${TAG}
242
243 .PHONY: docker-push/proxy-server
244 docker-push/proxy-server: docker-build/proxy-server
245 @[ "${DOCKER_CMD}" ] || ( echo "DOCKER_CMD is not set"; exit 1 )
246 ${DOCKER_CMD} push ${SERVER_FULL_IMAGE}-$(TARGETARCH):${TAG}
247
248 .PHONY: docker-build/proxy-test-client
249 docker-build/proxy-test-client: cmd/test-client/main.go proto/agent/agent.pb.go buildx-setup
250 @[ "${TAG}" ] || ( echo "TAG is not set"; exit 1 )
251 echo "Building proxy-test-client 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/test-client-build.Dockerfile -t ${TEST_CLIENT_FULL_IMAGE}-$(TARGETARCH):${TAG}
253
254 .PHONY: docker-push/proxy-test-client
255 docker-push/proxy-test-client: docker-build/proxy-test-client
256 @[ "${DOCKER_CMD}" ] || ( echo "DOCKER_CMD is not set"; exit 1 )
257 ${DOCKER_CMD} push ${TEST_CLIENT_FULL_IMAGE}-$(TARGETARCH):${TAG}
258
259 .PHONY: docker-build/http-test-server
260 docker-build/http-test-server: cmd/test-server/main.go buildx-setup
261 @[ "${TAG}" ] || ( echo "TAG is not set"; exit 1 )
262 echo "Building http-test-server 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-server-build.Dockerfile -t ${TEST_SERVER_FULL_IMAGE}-$(TARGETARCH):${TAG}
264
265 .PHONY: docker-push/http-test-server
266 docker-push/http-test-server: docker-build/http-test-server
267 @[ "${DOCKER_CMD}" ] || ( echo "DOCKER_CMD is not set"; exit 1 )
268 ${DOCKER_CMD} push ${TEST_SERVER_FULL_IMAGE}-$(TARGETARCH):${TAG}
269
270 ## --------------------------------------
271 ## Docker — All ARCH
272 ## --------------------------------------
273
274 # As `docker buildx` is time and resource consuming, if not necessary, building specific arch images,
275 # like `make docker-build-arch-amd64`, is recommended.
276 .PHONY: docker-build-all
277 docker-build-all: $(addprefix docker-build-arch-,$(ALL_ARCH))
278
279 .PHONY: docker-push-all
280 docker-push-all: $(addprefix docker-push/proxy-agent-,$(ALL_ARCH)) $(addprefix docker-push/proxy-server-,$(ALL_ARCH))
281 $(MAKE) docker-push-manifest/proxy-agent
282 $(MAKE) docker-push-manifest/proxy-server
283
284 docker-build-arch-%:
285 $(MAKE) docker-build/proxy-agent-$*
286 $(MAKE) docker-build/proxy-server-$*
287
288 docker-build/proxy-agent-%:
289 $(MAKE) TARGETARCH=$* docker-build/proxy-agent
290
291 docker-push/proxy-agent-%:
292 $(MAKE) TARGETARCH=$* docker-push/proxy-agent
293
294 docker-build/proxy-server-%:
295 $(MAKE) TARGETARCH=$* docker-build/proxy-server
296
297 docker-push/proxy-server-%:
298 $(MAKE) TARGETARCH=$* docker-push/proxy-server
299
300 .PHONY: docker-push-manifest/proxy-agent
301 docker-push-manifest/proxy-agent: ## Push the fat manifest docker image.
302 ## Minimum docker version 18.06.0 is required for creating and pushing manifest images.
303 ${DOCKER_CMD} manifest create --amend $(AGENT_FULL_IMAGE):$(TAG) $(shell echo $(ALL_ARCH) | sed -e "s~[^ ]*~$(AGENT_FULL_IMAGE)\-&:$(TAG)~g")
304 @for arch in $(ALL_ARCH); do ${DOCKER_CMD} manifest annotate --arch $${arch} ${AGENT_FULL_IMAGE}:${TAG} ${AGENT_FULL_IMAGE}-$${arch}:${TAG}; done
305 ${DOCKER_CMD} manifest push --purge $(AGENT_FULL_IMAGE):$(TAG)
306
307 .PHONY: docker-push-manifest/proxy-server
308 docker-push-manifest/proxy-server: ## Push the fat manifest docker image.
309 ## Minimum docker version 18.06.0 is required for creating and pushing manifest images.
310 ${DOCKER_CMD} manifest create --amend $(SERVER_FULL_IMAGE):$(TAG) $(shell echo $(ALL_ARCH) | sed -e "s~[^ ]*~$(SERVER_FULL_IMAGE)\-&:$(TAG)~g")
311 @for arch in $(ALL_ARCH); do ${DOCKER_CMD} manifest annotate --arch $${arch} ${SERVER_FULL_IMAGE}:${TAG} ${SERVER_FULL_IMAGE}-$${arch}:${TAG}; done
312 ${DOCKER_CMD} manifest push --purge $(SERVER_FULL_IMAGE):$(TAG)
313
314 ## --------------------------------------
315 ## Release
316 ## --------------------------------------
317
318 .PHONY: release-staging
319 release-staging: ## Builds and push container images to the staging bucket.
320 REGISTRY=$(STAGING_REGISTRY) $(MAKE) docker-push-all release-alias-tag
321
322 .PHONY: release-alias-tag
323 release-alias-tag: # Adds the tag to the last build tag. BASE_REF comes from the cloudbuild.yaml
324 gcloud container images add-tag $(AGENT_FULL_IMAGE):$(TAG) $(AGENT_FULL_IMAGE):$(BASE_REF)
325 gcloud container images add-tag $(SERVER_FULL_IMAGE):$(TAG) $(SERVER_FULL_IMAGE):$(BASE_REF)
326
327 ## --------------------------------------
328 ## Cleanup / Verification
329 ## --------------------------------------
330
331 .PHONY: clean
332 clean:
333 go clean -testcache
334 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