Skip to content

Commit 9c766d9

Browse files
Add multi-arch and automatic Go version build support (#391)
Extends #358 Description of changes: Adds support for `GOARCH` in the controller image Dockerfile and relevant scripts. Updates the Dockerfile to use the Go version specified in `go.mod` file (upgraded to 1.19). By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 61247b8 commit 9c766d9

File tree

5 files changed

+43
-15
lines changed

5 files changed

+43
-15
lines changed

Dockerfile

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
# Base image to use for the final stage
1+
# Base image to use at runtime
22
ARG base_image=public.ecr.aws/eks-distro-build-tooling/eks-distro-minimal-base-nonroot:2021-12-01-1638322424
3+
4+
# Golang image to use for compiling the manager
5+
ARG builder_image=public.ecr.aws/docker/library/golang
6+
7+
# Version of Golang
8+
ARG golang_version
9+
310
# Build the manager binary
4-
# TODO(vijtrip2) move this builder image to public.ecr.aws/eks-distro-build-tooling/builder-base, when builder-base
5-
# supports golang 1.17
6-
FROM public.ecr.aws/bitnami/golang:1.17 as builder
11+
FROM $builder_image:$golang_version as builder
712

813
ARG service_alias
914
# The tuple of controller image version information
1015
ARG service_controller_git_version
1116
ARG service_controller_git_commit
1217
ARG build_date
18+
ARG go_arch=amd64
1319
# The directory within the builder container into which we will copy our
1420
# service controller code.
1521
ARG work_dir=/github.com/aws-controllers-k8s/$service_alias-controller
1622
WORKDIR $work_dir
1723
ENV GOPROXY=https://proxy.golang.org|direct
1824
ENV GO111MODULE=on
19-
ENV GOARCH=amd64
25+
ENV GOARCH=$go_arch
2026
ENV GOOS=linux
2127
ENV CGO_ENABLED=0
2228
ENV VERSION_PKG=github.com/aws-controllers-k8s/$service_alias-controller/pkg/version

Dockerfile.local

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
# Base image to use at runtime
22
ARG base_image=public.ecr.aws/eks-distro-build-tooling/eks-distro-minimal-base-nonroot:2021-12-01-1638322424
3+
4+
# Golang image to use for compiling the manager
5+
ARG builder_image=public.ecr.aws/docker/library/golang
6+
7+
# Version of Golang
8+
ARG golang_version
9+
310
# Build the manager binary
4-
# TODO(vijtrip2) move this builder image to public.ecr.aws/eks-distro-build-tooling/builder-base, when builder-base
5-
# supports golang 1.17
6-
FROM public.ecr.aws/bitnami/golang:1.17 as builder
11+
FROM $builder_image:$golang_version as builder
712

813
ARG service_alias
914
# The tuple of controller image version information
1015
ARG service_controller_git_version
1116
ARG service_controller_git_commit
1217
ARG build_date
18+
ARG go_arch=amd64
1319
# The directory within the builder container into which we will copy our
1420
# service controller code.
1521
ARG work_dir=/github.com/aws-controllers-k8s/$service_alias-controller
1622
WORKDIR $work_dir
1723
ENV GOPROXY=https://proxy.golang.org|direct
1824
ENV GO111MODULE=on
19-
ENV GOARCH=amd64
25+
ENV GOARCH=$go_arch
2026
ENV GOOS=linux
2127
ENV CGO_ENABLED=0
2228
ENV VERSION_PKG=github.com/aws-controllers-k8s/$service_alias-controller/pkg/version

go.local.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/aws-controllers-k8s/code-generator
22

3-
go 1.17
3+
go 1.19
44

55
replace github.com/aws-controllers-k8s/runtime => ../runtime
66

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/aws-controllers-k8s/code-generator
22

3-
go 1.17
3+
go 1.19
44

55
require (
66
github.com/aws-controllers-k8s/pkg v0.0.0-20230111194700-e8a36ef99b23

scripts/build-controller-image.sh

100755100644
Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ LOCAL_MODULES=${LOCAL_MODULES:-"false"}
1212
BUILD_DATE=$(date +%Y-%m-%dT%H:%M)
1313
QUIET=${QUIET:-"false"}
1414

15+
HARDWARE_PLATFORM=${HARDWARE_PLATFORM:-$(uname -i)}
16+
GOARCH=${GOARCH:-""}
17+
if [ "$HARDWARE_PLATFORM" = "aarch64" ]; then
18+
GOARCH="arm64"
19+
elif [ "$HARDWARE_PLATFORM" = "x86_64" ]; then
20+
GOARCH="amd64"
21+
else
22+
echo "HARDWARE_PLATFORM is not supported: $HARDWARE_PLATFORM. Defaulting to amd64"
23+
GOARCH="amd64"
24+
fi
25+
1526
export DOCKER_BUILDKIT=${DOCKER_BUILDKIT:-1}
1627

1728
source "$SCRIPTS_DIR/lib/common.sh"
@@ -60,10 +71,8 @@ if [[ ! -d $SERVICE_CONTROLLER_SOURCE_PATH ]]; then
6071
fi
6172

6273
pushd "$SERVICE_CONTROLLER_SOURCE_PATH" 1>/dev/null
63-
64-
SERVICE_CONTROLLER_GIT_VERSION=$(git describe --tags --always --dirty || echo "unknown")
65-
SERVICE_CONTROLLER_GIT_COMMIT=$(git rev-parse HEAD)
66-
74+
SERVICE_CONTROLLER_GIT_VERSION=$(git describe --tags --always --dirty || echo "unknown")
75+
SERVICE_CONTROLLER_GIT_COMMIT=$(git rev-parse HEAD)
6776
popd 1>/dev/null
6877

6978
DEFAULT_AWS_SERVICE_DOCKER_IMG="aws-controllers-k8s:$AWS_SERVICE-$SERVICE_CONTROLLER_GIT_VERSION"
@@ -79,6 +88,11 @@ if ! is_public_ecr_logged_in; then
7988
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws
8089
fi
8190

91+
pushd "$ROOT_DIR" 1>/dev/null
92+
# Get the golang version from the code-generator
93+
GOLANG_VERSION=${GOLANG_VERSION:-"$(go list -f {{.GoVersion}} -m)"}
94+
popd 1>/dev/null
95+
8296
# if local build
8397
# then use Dockerfile which allows references to local modules from service controller
8498
DOCKER_BUILD_CONTEXT="$ACK_DIR"
@@ -94,6 +108,8 @@ if ! docker build \
94108
--build-arg service_controller_git_version="$SERVICE_CONTROLLER_GIT_VERSION" \
95109
--build-arg service_controller_git_commit="$SERVICE_CONTROLLER_GIT_COMMIT" \
96110
--build-arg build_date="$BUILD_DATE" \
111+
--build-arg golang_version="${GOLANG_VERSION}" \
112+
--build-arg go_arch="$GOARCH" \
97113
"${DOCKER_BUILD_CONTEXT}"; then
98114
exit 2
99115
fi

0 commit comments

Comments
 (0)