Skip to content

Commit b089adc

Browse files
authored
Build mult-arch manifest image (#335)
1 parent 77979fb commit b089adc

File tree

3 files changed

+107
-10
lines changed

3 files changed

+107
-10
lines changed

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM golang:1.16 as builder
2+
FROM golang:1.16.7 as builder
33

44
WORKDIR /workspace
55
# Copy the Go Modules manifests
@@ -18,7 +18,8 @@ COPY cloud/ cloud/
1818
COPY pkg/ pkg/
1919

2020
# Build
21-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go
21+
ARG ARCH
22+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} GO111MODULE=on go build -ldflags "-extldflags '-static'" -a -o manager main.go
2223

2324
# Use distroless as minimal base image to package the manager binary
2425
# Refer to https://github.com/GoogleContainerTools/distroless for more details

Makefile

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# Copyright 2021 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+
include versions.mk
116

217
# Image URL to use all building/pushing image targets
318
IMG ?= controller:latest
@@ -10,6 +25,20 @@ GOLANGCI_LINT_VER := v1.31.0
1025
GOLANGCI_LINT_BIN := golangci-lint
1126
GOLANGCI_LINT := $(TOOLS_BIN_DIR)/$(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VER)
1227

28+
STAGING_REGISTRY ?= gcr.io/k8s-staging-capi-ibmcloud
29+
REGISTRY ?= $(STAGING_REGISTRY)
30+
RELEASE_TAG ?= $(shell git describe --abbrev=0 2>/dev/null)
31+
PULL_BASE_REF ?= $(RELEASE_TAG) # PULL_BASE_REF will be provided by Prow
32+
RELEASE_ALIAS_TAG ?= $(PULL_BASE_REF)
33+
34+
TAG ?= dev
35+
ARCH ?= amd64
36+
ALL_ARCH ?= amd64 ppc64le
37+
38+
# main controller
39+
CORE_IMAGE_NAME ?= cluster-api-ibmcloud-controller
40+
CORE_CONTROLLER_IMG ?= $(REGISTRY)/$(CORE_IMAGE_NAME)
41+
1342
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
1443
ifeq (,$(shell go env GOBIN))
1544
GOBIN=$(shell go env GOPATH)/bin
@@ -60,14 +89,6 @@ vet:
6089
generate: controller-gen
6190
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
6291

63-
# Build the docker image
64-
docker-build:
65-
docker build . -t ${IMG}
66-
67-
# Push the docker image
68-
docker-push:
69-
docker push ${IMG}
70-
7192
images: docker-build
7293
# find or download controller-gen
7394
# download controller-gen if necessary
@@ -96,3 +117,63 @@ lint: $(GOLANGCI_LINT) ## Lint codebase
96117

97118
$(GOLANGCI_LINT): ## Build golangci-lint from tools folder.
98119
GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) github.com/golangci/golangci-lint/cmd/golangci-lint $(GOLANGCI_LINT_BIN) $(GOLANGCI_LINT_VER)
120+
121+
## --------------------------------------
122+
## Docker
123+
## --------------------------------------
124+
125+
.PHONY: docker-build
126+
docker-build: docker-pull-prerequisites ## Build the docker image for controller-manager
127+
docker build --build-arg ARCH=$(ARCH) . -t $(CORE_CONTROLLER_IMG)-$(ARCH):$(TAG)
128+
129+
.PHONY: docker-push
130+
docker-push: ## Push the docker image
131+
docker push $(CORE_CONTROLLER_IMG)-$(ARCH):$(TAG)
132+
133+
.PHONY: docker-pull-prerequisites
134+
docker-pull-prerequisites:
135+
docker pull docker.io/docker/dockerfile:1.1-experimental
136+
docker pull docker.io/library/golang:$(GOLANG_VERSION)
137+
docker pull gcr.io/distroless/static:latest
138+
139+
## --------------------------------------
140+
## Docker - All ARCH
141+
## --------------------------------------
142+
143+
.PHONY: docker-build-all ## Build all the architecture docker images
144+
docker-build-all: $(addprefix docker-build-,$(ALL_ARCH))
145+
146+
docker-build-%:
147+
$(MAKE) ARCH=$* docker-build
148+
149+
.PHONY: docker-push-all ## Push all the architecture docker images
150+
docker-push-all: $(addprefix docker-push-,$(ALL_ARCH))
151+
$(MAKE) docker-push-core-manifest
152+
153+
docker-push-%:
154+
$(MAKE) ARCH=$* docker-push
155+
156+
.PHONY: docker-push-core-manifest
157+
docker-push-core-manifest: ## Push the fat manifest docker image.
158+
## Minimum docker version 18.06.0 is required for creating and pushing manifest images.
159+
$(MAKE) docker-push-manifest CONTROLLER_IMG=$(CORE_CONTROLLER_IMG) MANIFEST_FILE=$(CORE_MANIFEST_FILE)
160+
161+
.PHONY: docker-push-manifest
162+
docker-push-manifest:
163+
docker manifest create --amend $(CONTROLLER_IMG):$(TAG) $(shell echo $(ALL_ARCH) | sed -e "s~[^ ]*~$(CONTROLLER_IMG)\-&:$(TAG)~g")
164+
@for arch in $(ALL_ARCH); do docker manifest annotate --arch $${arch} ${CONTROLLER_IMG}:${TAG} ${CONTROLLER_IMG}-$${arch}:${TAG}; done
165+
docker manifest push --purge ${CONTROLLER_IMG}:${TAG}
166+
167+
## --------------------------------------
168+
## Release
169+
## --------------------------------------
170+
171+
.PHONY: release-alias-tag
172+
release-alias-tag: # Adds the tag to the last build tag.
173+
gcloud container images add-tag -q $(CORE_CONTROLLER_IMG):$(TAG) $(CORE_CONTROLLER_IMG):$(RELEASE_ALIAS_TAG)
174+
175+
.PHONY: release-staging
176+
release-staging: ## Builds and push container images to the staging image registry.
177+
$(MAKE) docker-build-all
178+
$(MAKE) docker-push-all
179+
$(MAKE) release-alias-tag

versions.mk

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2021 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+
GOLANG_VERSION := 1.16.7

0 commit comments

Comments
 (0)