Skip to content

Commit 71f9d28

Browse files
authored
Merge pull request #38 from Sh4d1/multi_stage_cloudbuild
add multisarch and cloudbuild
2 parents a0fa98f + a1d5e4d commit 71f9d28

File tree

4 files changed

+159
-33
lines changed

4 files changed

+159
-33
lines changed

Makefile

Lines changed: 139 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,61 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
.PHONY: gen clean certs build docker/proxy-server docker/proxy-agent push-images test
16-
proto/agent/agent.pb.go: proto/agent/agent.proto
17-
protoc -I proto proto/agent/agent.proto --go_out=plugins=grpc:proto
15+
ARCH ?= amd64
16+
ALL_ARCH = amd64 arm arm64 ppc64le s390x
17+
18+
REGISTRY ?= gcr.io/$(shell gcloud config get-value project)
19+
STAGING_REGISTRY := gcr.io/k8s-staging-kas-network-proxy
20+
21+
SERVER_IMAGE_NAME ?= proxy-server
22+
AGENT_IMAGE_NAME ?= proxy-agent
23+
24+
SERVER_FULL_IMAGE ?= $(REGISTRY)/$(SERVER_IMAGE_NAME)
25+
AGENT_FULL_IMAGE ?= $(REGISTRY)/$(AGENT_IMAGE_NAME)
26+
27+
TAG ?= $(shell git rev-parse HEAD)
28+
29+
DOCKER_CLI_EXPERIMENTAL ?= enabled
30+
31+
## --------------------------------------
32+
## Testing
33+
## --------------------------------------
34+
35+
.PHONY: test
36+
test:
37+
go test ./...
38+
39+
## --------------------------------------
40+
## Binaries
41+
## --------------------------------------
1842

1943
bin:
2044
mkdir -p bin
2145

46+
.PHONY: build
47+
build: bin/proxy-agent bin/proxy-server bin/proxy-test-client
48+
49+
bin/proxy-agent: bin cmd/agent/main.go proto/agent/agent.pb.go
50+
go build -o bin/proxy-agent cmd/agent/main.go
51+
52+
bin/proxy-test-client: bin cmd/client/main.go proto/proxy.pb.go
53+
go build -o bin/proxy-test-client cmd/client/main.go
54+
55+
bin/proxy-server: bin cmd/proxy/main.go proto/agent/agent.pb.go proto/proxy.pb.go
56+
go build -o bin/proxy-server cmd/proxy/main.go
57+
58+
## --------------------------------------
59+
## Linting
60+
## --------------------------------------
61+
62+
63+
## --------------------------------------
64+
## Proto
65+
## --------------------------------------
66+
67+
.PHONY: gen
68+
gen: proto/agent/agent.pb.go proto/proxy.pb.go
69+
2270
proto/agent/agent.pb.go: proto/agent/agent.proto
2371
protoc -I proto proto/agent/agent.proto --go_out=plugins=grpc:proto
2472
cat hack/go-license-header.txt proto/agent/agent.pb.go > proto/agent/agent.licensed.go
@@ -29,26 +77,9 @@ proto/proxy.pb.go: proto/proxy.proto
2977
cat hack/go-license-header.txt proto/proxy.pb.go > proto/proxy.licensed.go
3078
mv proto/proxy.licensed.go proto/proxy.pb.go
3179

32-
bin/proxy-agent: bin cmd/agent/main.go proto/agent/agent.pb.go
33-
go build -o bin/proxy-agent cmd/agent/main.go
34-
35-
docker/proxy-agent: cmd/agent/main.go proto/agent/agent.pb.go
36-
@[ "${REGISTRY}" ] || ( echo "REGISTRY is not set"; exit 1 )
37-
@[ "${VERSION}" ] || ( echo "VERSION is not set"; exit 1 )
38-
@[ "${PROJECT_ID}" ] || ( echo "PROJECT_ID is not set"; exit 1 )
39-
docker build . -f artifacts/images/agent-build.Dockerfile -t ${REGISTRY}/${PROJECT_ID}/proxy-agent:${VERSION}
40-
41-
bin/proxy-server: bin cmd/proxy/main.go proto/agent/agent.pb.go proto/proxy.pb.go
42-
go build -o bin/proxy-server cmd/proxy/main.go
43-
44-
docker/proxy-server: cmd/proxy/main.go proto/agent/agent.pb.go proto/proxy.pb.go
45-
@[ "${REGISTRY}" ] || ( echo "REGISTRY is not set"; exit 1 )
46-
@[ "${VERSION}" ] || ( echo "VERSION is not set"; exit 1 )
47-
@[ "${PROJECT_ID}" ] || ( echo "PROJECT_ID is not set"; exit 1 )
48-
docker build . -f artifacts/images/server-build.Dockerfile -t ${REGISTRY}/${PROJECT_ID}/proxy-server:${VERSION}
49-
50-
bin/proxy-test-client: bin cmd/client/main.go proto/proxy.pb.go
51-
go build -o bin/proxy-test-client cmd/client/main.go
80+
## --------------------------------------
81+
## Certs
82+
## --------------------------------------
5283

5384
easy-rsa.tar.gz:
5485
curl -L -O --connect-timeout 20 --retry 6 --retry-delay 2 https://storage.googleapis.com/kubernetes-release/easy-rsa/easy-rsa.tar.gz
@@ -64,6 +95,7 @@ cfssljson:
6495
curl --retry 10 -L -o cfssljson https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
6596
chmod +x cfssljson
6697

98+
.PHONY: certs
6799
certs: easy-rsa-master cfssl cfssljson
68100
# set up easy-rsa
69101
cp -rf easy-rsa-master/easyrsa3 easy-rsa-master/master
@@ -93,17 +125,93 @@ certs: easy-rsa-master cfssl cfssljson
93125
cp -r easy-rsa-master/agent/pki/issued certs/agent
94126
cp easy-rsa-master/agent/pki/ca.crt certs/agent/issued
95127

96-
gen: proto/agent/agent.pb.go proto/proxy.pb.go
128+
## --------------------------------------
129+
## Docker
130+
## --------------------------------------
97131

98-
build: bin/proxy-agent bin/proxy-server bin/proxy-test-client
132+
.PHONY: docker-build
133+
docker-build: docker-build/proxy-agent docker-build/proxy-server
134+
135+
.PHONY: docker-push
136+
docker-push: docker-push/proxy-agent docker-push/proxy-server
99137

100-
push-images: docker/proxy-agent docker/proxy-server
138+
.PHONY: docker-build/proxy-agent
139+
docker-build/proxy-agent: cmd/agent/main.go proto/agent/agent.pb.go
140+
@[ "${TAG}" ] || ( echo "TAG is not set"; exit 1 )
141+
echo "Building proxy-agent for ${ARCH}"
142+
docker build . --build-arg ARCH=$(ARCH) -f artifacts/images/agent-build.Dockerfile -t ${AGENT_FULL_IMAGE}-$(ARCH):${TAG}
143+
144+
.PHONY: docker-push/proxy-agent
145+
docker-push/proxy-agent: docker-build/proxy-agent
101146
@[ "${DOCKER_CMD}" ] || ( echo "DOCKER_CMD is not set"; exit 1 )
102-
${DOCKER_CMD} push ${REGISTRY}/${PROJECT_ID}/proxy-agent:${VERSION}
103-
${DOCKER_CMD} push ${REGISTRY}/${PROJECT_ID}/proxy-server:${VERSION}
147+
${DOCKER_CMD} push ${AGENT_FULL_IMAGE}:${TAG}
148+
149+
.PHONY: docker-build/proxy-server
150+
docker-build/proxy-server: cmd/proxy/main.go proto/agent/agent.pb.go proto/proxy.pb.go
151+
@[ "${TAG}" ] || ( echo "TAG is not set"; exit 1 )
152+
echo "Building proxy-server for ${ARCH}"
153+
docker build . --build-arg ARCH=$(ARCH) -f artifacts/images/server-build.Dockerfile -t ${SERVER_FULL_IMAGE}-$(ARCH):${TAG}
154+
155+
.PHONY: docker-push/proxy-server
156+
docker-push/proxy-server: docker-build/proxy-server
157+
@[ "${DOCKER_CMD}" ] || ( echo "DOCKER_CMD is not set"; exit 1 )
158+
${DOCKER_CMD} push ${SERVER_FULL_IMAGE}:${TAG}
159+
160+
## --------------------------------------
161+
## Docker — All ARCH
162+
## --------------------------------------
104163

164+
.PHONY: docker-build-all
165+
docker-build-all: $(addprefix docker-build/proxy-agent-,$(ALL_ARCH)) $(addprefix docker-build/proxy-server-,$(ALL_ARCH))
166+
167+
.PHONY: docker-push-all
168+
docker-push-all: $(addprefix docker-push/proxy-agent-,$(ALL_ARCH)) $(addprefix docker-push/proxy-server-,$(ALL_ARCH))
169+
$(MAKE) docker-push-manifest/proxy-agent
170+
$(MAKE) docker-push-manifest/proxy-server
171+
172+
docker-build/proxy-agent-%:
173+
$(MAKE) ARCH=$* docker-build/proxy-agent
174+
175+
docker-push/proxy-agent-%:
176+
$(MAKE) ARCH=$* docker-push/proxy-agent
177+
178+
docker-build/proxy-server-%:
179+
$(MAKE) ARCH=$* docker-build/proxy-server
180+
181+
docker-push/proxy-server-%:
182+
$(MAKE) ARCH=$* docker-push/proxy-server
183+
184+
.PHONY: docker-push-manifest/proxy-agent
185+
docker-push-manifest/proxy-agent: ## Push the fat manifest docker image.
186+
## Minimum docker version 18.06.0 is required for creating and pushing manifest images.
187+
docker manifest create --amend $(AGENT_FULL_IMAGE):$(TAG) $(shell echo $(ALL_ARCH) | sed -e "s~[^ ]*~$(AGENT_FULL_IMAGE)\-&:$(TAG)~g")
188+
@for arch in $(ALL_ARCH); do docker manifest annotate --arch $${arch} ${AGENT_FULL_IMAGE}:${TAG} ${AGENT_FULL_IMAGE}-$${arch}:${TAG}; done
189+
docker manifest push --purge $(AGENT_FULL_IMAGE):$(TAG)
190+
191+
.PHONY: docker-push-manifest/proxy-server
192+
docker-push-manifest/proxy-server: ## Push the fat manifest docker image.
193+
## Minimum docker version 18.06.0 is required for creating and pushing manifest images.
194+
docker manifest create --amend $(SERVER_FULL_IMAGE):$(TAG) $(shell echo $(ALL_ARCH) | sed -e "s~[^ ]*~$(SERVER_FULL_IMAGE)\-&:$(TAG)~g")
195+
@for arch in $(ALL_ARCH); do docker manifest annotate --arch $${arch} ${SERVER_FULL_IMAGE}:${TAG} ${SERVER_FULL_IMAGE}-$${arch}:${TAG}; done
196+
docker manifest push --purge $(SERVER_FULL_IMAGE):$(TAG)
197+
198+
## --------------------------------------
199+
## Release
200+
## --------------------------------------
201+
202+
.PHONY: release-staging
203+
release-staging: ## Builds and push container images to the staging bucket.
204+
REGISTRY=$(STAGING_REGISTRY) $(MAKE) docker-build-all docker-push-all release-alias-tag
205+
206+
.PHONY: release-alias-tag
207+
release-alias-tag: # Adds the tag to the last build tag. BASE_REF comes from the cloudbuild.yaml
208+
gcloud container images add-tag $(AGENT_FULL_IMAGE):$(TAG) $(AGENT_FULL_IMAGE):$(BASE_REF)
209+
gcloud container images add-tag $(SERVER_FULL_IMAGE):$(TAG) $(SERVER_FULL_IMAGE):$(BASE_REF)
210+
211+
## --------------------------------------
212+
## Cleanup / Verification
213+
## --------------------------------------
214+
215+
.PHONY: clean
105216
clean:
106217
rm -rf proto/agent/agent.pb.go proto/proxy.pb.go easy-rsa.tar.gz easy-rsa-master cfssl cfssljson certs bin
107-
108-
test:
109-
go test ./...

artifacts/images/agent-build.Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ COPY proto/ proto/
99
COPY vendor/ vendor/
1010

1111
# Build
12-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o proxy-agent sigs.k8s.io/apiserver-network-proxy/cmd/agent
12+
ARG ARCH
13+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -a -ldflags '-extldflags "-static"' -o proxy-agent sigs.k8s.io/apiserver-network-proxy/cmd/agent
1314

1415
# Copy the loader into a thin image
1516
FROM scratch

artifacts/images/server-build.Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ COPY proto/ proto/
99
COPY vendor/ vendor/
1010

1111
# Build
12-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o proxy-server sigs.k8s.io/apiserver-network-proxy/cmd/proxy
12+
ARG ARCH
13+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -a -ldflags '-extldflags "-static"' -o proxy-server sigs.k8s.io/apiserver-network-proxy/cmd/proxy
1314

1415
# Copy the loader into a thin image
1516
FROM scratch

cloudbuild.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
timeout: 1200s
2+
options:
3+
substitution_option: ALLOW_LOOSE
4+
steps:
5+
- name: 'gcr.io/k8s-testimages/gcb-docker-gcloud:v20190906-745fed4'
6+
entrypoint: make
7+
env:
8+
- TAG=$_GIT_TAG
9+
- BASE_REF=$_PULL_BASE_REF
10+
args:
11+
- release-staging
12+
substitutions:
13+
# _GIT_TAG will be filled with a git-based tag for the image, of the form vYYYYMMDD-hash, and
14+
# can be used as a substitution
15+
_GIT_TAG: '12345'
16+
_PULL_BASE_REF: 'dev'

0 commit comments

Comments
 (0)