From 9cb93b5eec9e2f0bda7bd95da333ac7f3a156b67 Mon Sep 17 00:00:00 2001 From: Bennett Sala Date: Fri, 2 Jun 2023 16:46:29 -0700 Subject: [PATCH] Adds multiarch builds for k8s http2 example This modifies the images used for the k8s http2 example to default support arm64 in addition to amd64. This should gracefully downgrade to existing behavior if run from an environment that does not support cross-building. This is done on this example as it's the "main" k8s setup for walkthroughs for demoing multiarch k8s abilities. Further walkthroughs may be adapted in the future. --- .../howto-k8s-http2/color_client/Dockerfile | 24 +++++++------------ .../howto-k8s-http2/color_server/Dockerfile | 24 +++++++------------ walkthroughs/howto-k8s-http2/deploy.sh | 21 ++++++++++++---- 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/walkthroughs/howto-k8s-http2/color_client/Dockerfile b/walkthroughs/howto-k8s-http2/color_client/Dockerfile index 83125cf3..570d0851 100644 --- a/walkthroughs/howto-k8s-http2/color_client/Dockerfile +++ b/walkthroughs/howto-k8s-http2/color_client/Dockerfile @@ -1,32 +1,24 @@ -FROM public.ecr.aws/amazonlinux/amazonlinux:2 AS builder -RUN yum update -y && \ - yum install -y ca-certificates unzip tar gzip git && \ - yum clean all && \ - rm -rf /var/cache/yum +FROM --platform=${BUILDPLATFORM} golang:1.20 as builder -RUN curl -LO https://golang.org/dl/go1.17.1.linux-amd64.tar.gz && \ - tar -C /usr/local -xzvf go1.17.1.linux-amd64.tar.gz - -ENV PATH="${PATH}:/usr/local/go/bin" -ENV GOPATH="${HOME}/go" -ENV PATH="${PATH}:${GOPATH}/bin" +WORKDIR /go/src/github.com/aws/aws-app-mesh-examples/walkthroughs/howto-http2/color_client # Use the default go proxy ARG GO_PROXY=https://proxy.golang.org -WORKDIR /go/src/github.com/aws/aws-app-mesh-examples/walkthroughs/howto-http2/color_client - # Set the proxies for the go compiler. ENV GOPROXY=$GO_PROXY # go.mod and go.sum go into their own layers. # This ensures `go mod download` happens only when go.mod and go.sum change. -COPY go.mod . -COPY go.sum . +COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /color_client . + +ARG TARGETOS +ARG TARGETARCH + +RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -installsuffix nocgo -o /color_client . FROM public.ecr.aws/amazonlinux/amazonlinux:2 RUN yum update -y && \ diff --git a/walkthroughs/howto-k8s-http2/color_server/Dockerfile b/walkthroughs/howto-k8s-http2/color_server/Dockerfile index a804e800..1f98ef5e 100644 --- a/walkthroughs/howto-k8s-http2/color_server/Dockerfile +++ b/walkthroughs/howto-k8s-http2/color_server/Dockerfile @@ -1,32 +1,24 @@ -FROM public.ecr.aws/amazonlinux/amazonlinux:2 AS builder -RUN yum update -y && \ - yum install -y ca-certificates unzip tar gzip git && \ - yum clean all && \ - rm -rf /var/cache/yum +FROM --platform=${BUILDPLATFORM} golang:1.20 as builder -RUN curl -LO https://golang.org/dl/go1.17.1.linux-amd64.tar.gz && \ - tar -C /usr/local -xzvf go1.17.1.linux-amd64.tar.gz - -ENV PATH="${PATH}:/usr/local/go/bin" -ENV GOPATH="${HOME}/go" -ENV PATH="${PATH}:${GOPATH}/bin" +WORKDIR /go/src/github.com/aws/aws-app-mesh-examples/walkthroughs/howto-http2/color_server # Use the default go proxy ARG GO_PROXY=https://proxy.golang.org -WORKDIR /go/src/github.com/aws/aws-app-mesh-examples/walkthroughs/howto-http2/color_server - # Set the proxies for the go compiler. ENV GOPROXY=$GO_PROXY # go.mod and go.sum go into their own layers. # This ensures `go mod download` happens only when go.mod and go.sum change. -COPY go.mod . -COPY go.sum . +COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /color_server . + +ARG TARGETOS +ARG TARGETARCH + +RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -installsuffix nocgo -o /color_server . FROM public.ecr.aws/amazonlinux/amazonlinux:2 RUN yum update -y && \ diff --git a/walkthroughs/howto-k8s-http2/deploy.sh b/walkthroughs/howto-k8s-http2/deploy.sh index bdc2544a..d3d0c823 100755 --- a/walkthroughs/howto-k8s-http2/deploy.sh +++ b/walkthroughs/howto-k8s-http2/deploy.sh @@ -19,8 +19,8 @@ MESH_NAME=${PROJECT_NAME} CLOUDMAP_NAMESPACE="${APP_NAMESPACE}.svc.cluster.local" ECR_URL="${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com" ECR_IMAGE_PREFIX="${ECR_URL}/${PROJECT_NAME}" -CLIENT_APP_IMAGE="${ECR_IMAGE_PREFIX}/color_client" -COLOR_APP_IMAGE="${ECR_IMAGE_PREFIX}/color_server" +CLIENT_APP_IMAGE="${ECR_IMAGE_PREFIX}/color_client:latest" +COLOR_APP_IMAGE="${ECR_IMAGE_PREFIX}/color_server:latest" MANIFEST_VERSION="${1:-v1beta2}" AWS_CLI_VERSION=$(aws --version 2>&1 | cut -d/ -f2 | cut -d. -f1) @@ -83,8 +83,21 @@ deploy_images() { ecr_login for app in color_client color_server; do aws ecr describe-repositories --repository-name $PROJECT_NAME/$app >/dev/null 2>&1 || aws ecr create-repository --repository-name $PROJECT_NAME/$app >/dev/null - docker build -t ${ECR_IMAGE_PREFIX}/${app} ${DIR}/${app} --build-arg GO_PROXY=${GO_PROXY:-"https://proxy.golang.org"} - docker push ${ECR_IMAGE_PREFIX}/${app} + + # performs a multiarch build and deploy if cross-build tools are + # available. Otherwise, builds on the current architecture. + docker image rm "${ECR_IMAGE_PREFIX}/${app}:latest" 1>/dev/null 2>/dev/null || : + missing_buildx=`docker buildx version 1>/dev/null 2>/dev/null; echo $?` + if [[ "$missing_buildx" -eq "0" ]]; then + docker manifest rm "${ECR_IMAGE_PREFIX}/${app}:latest" 1>/dev/null 2>/dev/null || : + docker buildx build --platform linux/amd64 -t "${ECR_IMAGE_PREFIX}/${app}:latest-linux_amd64" ${DIR}/${app} --build-arg GO_PROXY=${GO_PROXY:-"https://proxy.golang.org"} --push + docker buildx build --platform linux/arm64 -t "${ECR_IMAGE_PREFIX}/${app}:latest-linux_arm64" ${DIR}/${app} --build-arg GO_PROXY=${GO_PROXY:-"https://proxy.golang.org"} --push + docker manifest create "${ECR_IMAGE_PREFIX}/${app}:latest" "${ECR_IMAGE_PREFIX}/${app}:latest-linux_amd64" "${ECR_IMAGE_PREFIX}/${app}:latest-linux_arm64" + docker manifest push "${ECR_IMAGE_PREFIX}/${app}:latest" + else + docker build -t "${ECR_IMAGE_PREFIX}/${app}:latest" ${DIR}/${app} --build-arg GO_PROXY=${GO_PROXY:-"https://proxy.golang.org"} + docker push "${ECR_IMAGE_PREFIX}/${app}:latest" + fi done }