From 4c8b0ee3c9d602ae7be03d07809d0018e5a81b50 Mon Sep 17 00:00:00 2001 From: Seth Levine Date: Mon, 10 Jan 2022 09:17:43 -0500 Subject: [PATCH] Support buildx for amd64 and arm64 builds step 1 (#766) --- Makefile | 10 ++++- cmd/awscollector/Dockerfile | 60 +++++++++----------------- cmd/awscollector/copy_docker_binary.sh | 13 ++++++ 3 files changed, 42 insertions(+), 41 deletions(-) create mode 100755 cmd/awscollector/copy_docker_binary.sh diff --git a/Makefile b/Makefile index 62f9655836..7eceedf924 100644 --- a/Makefile +++ b/Makefile @@ -86,6 +86,10 @@ build: install-tools lint multimod-verify amd64-build: install-tools lint multimod-verify GOOS=linux GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o ./build/linux/aoc_linux_x86_64 ./cmd/awscollector +.PHONY: arm64-build +arm64-build: install-tools lint multimod-verify + GOOS=linux GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o ./build/linux/aoc_linux_aarch64 ./cmd/awscollector + # For building container image during development, no lint nor other platforms .PHONY: amd64-build-only amd64-build-only: @@ -107,7 +111,11 @@ package-deb: build .PHONY: docker-build docker-build: amd64-build - docker build -t $(DOCKER_NAMESPACE)/$(COMPONENT):$(VERSION) -f ./cmd/$(COMPONENT)/Dockerfile . + docker buildx build --platform linux/amd64 -t $(DOCKER_NAMESPACE)/$(COMPONENT):$(VERSION) -f ./cmd/$(COMPONENT)/Dockerfile . + +.PHONY: docker-build-arm +docker-build-arm: arm64-build + docker buildx build --platform linux/arm64 -t $(DOCKER_NAMESPACE)/$(COMPONENT):$(VERSION) -f ./cmd/$(COMPONENT)/Dockerfile . .PHONY: docker-push docker-push: diff --git a/cmd/awscollector/Dockerfile b/cmd/awscollector/Dockerfile index 2a4670956b..8bd3450e86 100644 --- a/cmd/awscollector/Dockerfile +++ b/cmd/awscollector/Dockerfile @@ -4,42 +4,22 @@ ################################ FROM alpine:latest as build -ENV GOPROXY=direct - # update cert RUN apk --update add ca-certificates -# install libs -RUN apk add --update bash git make build-base - -# config go -COPY --from=golang:1.17-alpine /usr/local/go/ /usr/local/go/ -ENV PATH="/usr/local/go/bin:${PATH}" - WORKDIR /workspace # copy artifacts COPY cmd /workspace/cmd -COPY config.yaml /workspace/config.yaml -COPY pkg /workspace/pkg -COPY tools /workspace/tools -COPY VERSION /workspace/VERSION -COPY .git /workspace/.git -COPY Makefile /workspace/Makefile -COPY config /workspace/config + +ARG TARGETPLATFORM +RUN echo "Targate platform is $TARGETPLATFORM" #Copy binary if exist else build binary -COPY go.* build/linux/aoc_linux_x86_6* /workspace/ -RUN if [[ -f /workspace/aoc_linux_x86_64 ]] ; \ - then \ - echo Copying binary \ - && mkdir -p /workspace/build/linux \ - && cp /workspace/aoc_linux_x86_64 /workspace/build/linux/aoc_linux_x86_64 ; \ - else \ - echo Building binary \ - && go mod download \ - && make amd64-build ; \ - fi +#Always assume binary is created +COPY go.* build/linux/* /workspace/ +RUN chmod +x cmd/awscollector/copy_docker_binary.sh +RUN cmd/awscollector/copy_docker_binary.sh ################################ # Final Stage # @@ -48,19 +28,19 @@ RUN if [[ -f /workspace/aoc_linux_x86_64 ]] ; \ FROM scratch COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt -COPY --from=build /workspace/build/linux/aoc_linux_x86_64 /awscollector -COPY --from=build /workspace/config.yaml /etc/otel-config.yaml -COPY --from=build /workspace/config/eks/prometheus/config-appmesh.yaml /etc/eks/prometheus/config-appmesh.yaml -COPY --from=build /workspace/config/eks/prometheus/config-haproxy.yaml /etc/eks/prometheus/config-haproxy.yaml -COPY --from=build /workspace/config/eks/prometheus/config-jmx.yaml /etc/eks/prometheus/config-jmx.yaml -COPY --from=build /workspace/config/eks/prometheus/config-memcached.yaml /etc/eks/prometheus/config-memcached.yaml -COPY --from=build /workspace/config/eks/prometheus/config-nginx.yaml /etc/eks/prometheus/config-nginx.yaml -COPY --from=build /workspace/config/eks/prometheus/config-redis.yaml /etc/eks/prometheus/config-redis.yaml -COPY --from=build /workspace/config/eks/prometheus/config-all.yaml /etc/eks/prometheus/config-all.yaml -COPY --from=build /workspace/config/ecs/container-insights/otel-task-metrics-config.yaml /etc/ecs/container-insights/otel-task-metrics-config.yaml -COPY --from=build /workspace/config/ecs/ecs-*.yaml /etc/ecs/ -COPY --from=build /workspace/config/ecs/otel-instance-metrics-config.yaml /etc/ecs/otel-instance-metrics-config.yaml -COPY --from=build /workspace/config/apprunner/apprunner-default-config.yaml /etc/apprunner/apprunner-default-config.yaml +COPY --from=build /workspace/build/linux/aoc_linux /awscollector +COPY config.yaml /etc/otel-config.yaml +COPY config/eks/prometheus/config-appmesh.yaml /etc/eks/prometheus/config-appmesh.yaml +COPY config/eks/prometheus/config-haproxy.yaml /etc/eks/prometheus/config-haproxy.yaml +COPY config/eks/prometheus/config-jmx.yaml /etc/eks/prometheus/config-jmx.yaml +COPY config/eks/prometheus/config-memcached.yaml /etc/eks/prometheus/config-memcached.yaml +COPY config/eks/prometheus/config-nginx.yaml /etc/eks/prometheus/config-nginx.yaml +COPY config/eks/prometheus/config-redis.yaml /etc/eks/prometheus/config-redis.yaml +COPY config/eks/prometheus/config-all.yaml /etc/eks/prometheus/config-all.yaml +COPY config/ecs/container-insights/otel-task-metrics-config.yaml /etc/ecs/container-insights/otel-task-metrics-config.yaml +COPY config/ecs/ecs-*.yaml /etc/ecs/ +COPY config/ecs/otel-instance-metrics-config.yaml /etc/ecs/otel-instance-metrics-config.yaml +COPY config/apprunner/apprunner-default-config.yaml /etc/apprunner/apprunner-default-config.yaml ENV RUN_IN_CONTAINER="True" # aws-sdk-go needs $HOME to look up shared credentials diff --git a/cmd/awscollector/copy_docker_binary.sh b/cmd/awscollector/copy_docker_binary.sh new file mode 100755 index 0000000000..da25c1613d --- /dev/null +++ b/cmd/awscollector/copy_docker_binary.sh @@ -0,0 +1,13 @@ +#!/bin/sh +BINARY=aoc_linux_x86_64 +if [[ $TARGETPLATFORM == "linux/arm64" ]]; then + BINARY=aoc_linux_aarch64 +fi +echo binary name is ${BINARY} +mkdir -p /workspace/build/linux +if [[ ! -f /workspace/${BINARY} ]]; then + echo binary does not exsit + exit 1 +fi +echo copying binary +cp /workspace/${BINARY} /workspace/build/linux/aoc_linux