From f327d38b8ef212eb7226e81240f7e6bcceb6490a Mon Sep 17 00:00:00 2001 From: Jorik Jonker Date: Tue, 18 Apr 2023 09:58:03 +0200 Subject: [PATCH] feat: support for multiarch images This commit alters the Dockerfile for both regular and OpenShift in such a way multiarch images are getting built when using buildkit. Also, the GH workflow actually building the images is prepared for that. Fixes #226 Signed-off-by: Jorik Jonker --- .github/workflows/image-build.yml | 30 +++------------- .github/workflows/image-push-master.yml | 29 +++------------- .github/workflows/image-push-release.yml | 44 ++---------------------- Dockerfile | 9 ++--- Dockerfile.arm64 | 17 --------- Dockerfile.openshift | 7 ++-- hack/build-go.sh | 1 + 7 files changed, 23 insertions(+), 114 deletions(-) delete mode 100644 Dockerfile.arm64 diff --git a/.github/workflows/image-build.yml b/.github/workflows/image-build.yml index 8a3b1634c..68aeca1ec 100644 --- a/.github/workflows/image-build.yml +++ b/.github/workflows/image-build.yml @@ -1,8 +1,8 @@ name: Image build on: [pull_request] jobs: - build-amd64: - name: Image build/amd64 + build-multiarch: + name: Image build runs-on: ubuntu-latest steps: - name: Check out code into the Go module directory @@ -16,8 +16,9 @@ jobs: with: context: . push: false - tags: ghcr.io/${{ github.repository }}:latest-amd64 + tags: ghcr.io/${{ github.repository }}:latest file: Dockerfile + platforms: [linux/arm64, linux/amd64] build-openshift: name: Image build/openshift @@ -36,25 +37,4 @@ jobs: push: false tags: k8snetworkplumbingwg/whereabouts-ocp file: Dockerfile.openshift - - build-arm64: - name: Image build/arm64 - runs-on: ubuntu-latest - steps: - - name: Check out code into the Go module directory - uses: actions/checkout@v2 - - - name: Set up QEMU - uses: docker/setup-qemu-action@v2 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - - - name: Build container image - uses: docker/build-push-action@v2 - with: - context: . - push: false - tags: ghcr.io/${{ github.repository }}:latest-arm64 - file: Dockerfile.arm64 - platforms: linux/arm64 + platforms: [linux/arm64, linux/amd64] diff --git a/.github/workflows/image-push-master.yml b/.github/workflows/image-push-master.yml index 685ca29a6..0a400d188 100644 --- a/.github/workflows/image-push-master.yml +++ b/.github/workflows/image-push-master.yml @@ -6,8 +6,8 @@ on: env: image-push-owner: 'k8snetworkplumbingwg' jobs: - push-amd64: - name: Image push/amd64 + push: + name: Image push runs-on: ubuntu-latest steps: - name: Check out code into the Go module directory @@ -30,8 +30,9 @@ jobs: with: context: . push: true - tags: ghcr.io/${{ github.repository }}:latest-amd64 + tags: ghcr.io/${{ github.repository }}:latest file: Dockerfile + platforms: [linux/arm64, linux/amd64] push-origin: name: Image push/origin @@ -59,24 +60,4 @@ jobs: push: true tags: ghcr.io/${{ github.repository }}:latest-ocp file: Dockerfile.openshift - - push-arm64: - name: Image build/arm64 - runs-on: ubuntu-latest - steps: - - name: Check out code into the Go module directory - uses: actions/checkout@v2 - - - name: Set up QEMU - uses: docker/setup-qemu-action@v2 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - - - name: Build container image - uses: docker/build-push-action@v2 - with: - context: . - push: true - tags: ghcr.io/${{ github.repository }}:latest-arm64 - file: Dockerfile.arm64 + platforms: [linux/arm64, linux/amd64] diff --git a/.github/workflows/image-push-release.yml b/.github/workflows/image-push-release.yml index e3159033f..ee689d93d 100644 --- a/.github/workflows/image-push-release.yml +++ b/.github/workflows/image-push-release.yml @@ -7,7 +7,7 @@ env: image-push-owner: 'k8snetworkplumbingwg' jobs: push-amd64: - name: Image push/amd64 + name: Image push runs-on: ubuntu-latest steps: - name: Check out code into the Go module directory @@ -38,44 +38,6 @@ jobs: context: . push: true tags: | - ${{ steps.docker_meta.outputs.tags }}-amd64 + ${{ steps.docker_meta.outputs.tags }} file: Dockerfile - - push-arm64: - name: Image push/arm64 - runs-on: ubuntu-latest - steps: - - name: Check out code into the Go module directory - uses: actions/checkout@v2 - - - name: Set up QEMU - uses: docker/setup-qemu-action@v2 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - - - name: Login to Container Registry - if: github.repository_owner == 'k8snetworkplumbingwg' - uses: docker/login-action@v1 - with: - registry: ghcr.io - username: ${{ github.repository_owner }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Docker meta - id: docker_meta - uses: crazy-max/ghaction-docker-meta@v1 - with: - images: ghcr.io/${{ github.repository }} - tag-latest: false - - - name: Push container image - if: github.repository_owner == 'k8snetworkplumbingwg' - uses: docker/build-push-action@v2 - with: - context: . - push: true - tags: | - ${{ steps.docker_meta.outputs.tags }}-arm64 - file: Dockerfile - platforms: linux/arm64 + platforms: [linux/arm64, linux/amd64] diff --git a/Dockerfile b/Dockerfile index b44309955..4ababbf23 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,14 @@ -FROM golang:1.19 +FROM --platform=$BUILDPLATFORM golang:1.19 as builder ADD . /usr/src/whereabouts RUN mkdir -p $GOPATH/src/github.com/k8snetworkplumbingwg/whereabouts WORKDIR $GOPATH/src/github.com/k8snetworkplumbingwg/whereabouts COPY . . -RUN ./hack/build-go.sh +ARG TARGETOS TARGETARCH +RUN GOOS=$TARGETOS GOARCH=$TARGETARCH ./hack/build-go.sh FROM alpine:latest LABEL org.opencontainers.image.source https://github.com/k8snetworkplumbingwg/whereabouts -COPY --from=0 /go/src/github.com/k8snetworkplumbingwg/whereabouts/bin/whereabouts . -COPY --from=0 /go/src/github.com/k8snetworkplumbingwg/whereabouts/bin/ip-control-loop . +COPY --from=builder /go/src/github.com/k8snetworkplumbingwg/whereabouts/bin/whereabouts . +COPY --from=builder /go/src/github.com/k8snetworkplumbingwg/whereabouts/bin/ip-control-loop . COPY script/install-cni.sh . CMD ["/install-cni.sh"] diff --git a/Dockerfile.arm64 b/Dockerfile.arm64 deleted file mode 100644 index 0b1c7fd54..000000000 --- a/Dockerfile.arm64 +++ /dev/null @@ -1,17 +0,0 @@ -FROM golang:1.19 -ADD . /usr/src/whereabouts - -ENV GOARCH "arm64" -ENV GOOS "linux" - -RUN mkdir -p $GOPATH/src/github.com/k8snetworkplumbingwg/whereabouts -WORKDIR $GOPATH/src/github.com/k8snetworkplumbingwg/whereabouts -COPY . . -RUN ./hack/build-go.sh - -FROM arm64v8/alpine:latest -LABEL org.opencontainers.image.source https://github.com/k8snetworkplumbingwg/whereabouts -COPY --from=0 /go/src/github.com/k8snetworkplumbingwg/whereabouts/bin/whereabouts . -COPY --from=0 /go/src/github.com/k8snetworkplumbingwg/whereabouts/bin/ip-control-loop . -COPY script/install-cni.sh . -CMD ["/install-cni.sh"] diff --git a/Dockerfile.openshift b/Dockerfile.openshift index b6fa401e7..b4d1954a9 100644 --- a/Dockerfile.openshift +++ b/Dockerfile.openshift @@ -1,11 +1,12 @@ # This dockerfile is used for building for OpenShift -FROM registry.ci.openshift.org/openshift/release:golang-1.19 as builder +FROM --platform=$BUILDPLATFORM registry.ci.openshift.org/openshift/release:golang-1.19 as builder ADD . /go/src/github.com/k8snetworkplumbingwg/whereabouts WORKDIR /go/src/github.com/k8snetworkplumbingwg/whereabouts ENV CGO_ENABLED=1 ENV GO111MODULE=on -RUN go build -mod vendor -o bin/whereabouts cmd/whereabouts.go -RUN go build -mod vendor -o bin/ip-control-loop cmd/controlloop/controlloop.go +ARG TARGETARCH TARGETOS +RUN GOOS=$TARGETOS GOARCH=$TARGETARCH go build -mod vendor -o bin/whereabouts cmd/whereabouts.go +RUN GOOS=$TARGETOS GOARCH=$TARGETARCH go build -mod vendor -o bin/ip-control-loop cmd/controlloop/controlloop.go WORKDIR / FROM openshift/origin-base diff --git a/hack/build-go.sh b/hack/build-go.sh index 0d615169e..f3644f774 100755 --- a/hack/build-go.sh +++ b/hack/build-go.sh @@ -38,6 +38,7 @@ echo "VERSION: ${VERSION}" echo "GIT_SHA: ${GIT_SHA}" echo "GIT_TREE_STATE: ${GIT_TREE_STATE}" echo "RELEASE_STATUS: ${RELEASE_STATUS}" +echo "PLATFORM: ${GOOS}/${GOARCH}" VERSION_LDFLAGS="-X github.com/k8snetworkplumbingwg/whereabouts/pkg/version.Version=${VERSION} \ -X github.com/k8snetworkplumbingwg/whereabouts/pkg/version.GitSHA=${GIT_SHA} \ -X github.com/k8snetworkplumbingwg/whereabouts/pkg/version.GitTreeState=${GIT_TREE_STATE} \