From e756ae522a08f0ba1492977ec7bb2d855a39fc8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Apayd=C4=B1n?= Date: Wed, 1 Jun 2022 15:35:02 +0300 Subject: [PATCH] chore(dockerfile): use buildx features for cross-compilation (#4023) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore(dockerfile): use buildx features for cross-compilation Signed-off-by: Batuhan Apaydın * feat(kyverno): main container image Signed-off-by: Batuhan Apaydın Co-authored-by: shuting --- cmd/cli/kubectl-kyverno/Dockerfile | 26 ++++++++++++++++---------- cmd/initContainer/Dockerfile | 26 ++++++++++++++++---------- cmd/kyverno/Dockerfile | 28 +++++++++++++++++----------- 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/cmd/cli/kubectl-kyverno/Dockerfile b/cmd/cli/kubectl-kyverno/Dockerfile index 802e222bb5..66fc1b7958 100644 --- a/cmd/cli/kubectl-kyverno/Dockerfile +++ b/cmd/cli/kubectl-kyverno/Dockerfile @@ -1,22 +1,28 @@ # Multi-stage docker build # Build stage -FROM golang@sha256:724abf4dd44985d060f7aa91af5211eb2052491424bd497ba3ddc31f7cee969d AS builder - +FROM --platform=${BUILDPLATFORM} golang@sha256:724abf4dd44985d060f7aa91af5211eb2052491424bd497ba3ddc31f7cee969d AS base +WORKDIR /src LABEL maintainer="Kyverno" +COPY go.* . + +RUN --mount=type=cache,target=/go/pkg/mod \ + go mod download + +FROM --platform=${BUILDPLATFORM} tonistiigi/xx:1.1.1@sha256:23ca08d120366b31d1d7fad29283181f063b0b43879e1f93c045ca5b548868e9 AS xx + +FROM base AS builder + # LD_FLAGS is passed as argument from Makefile. It will be empty, if no argument passed ARG LD_FLAGS ARG TARGETPLATFORM -ADD . /kyverno -WORKDIR /kyverno +COPY --from=xx / / -RUN export GOOS=$(echo ${TARGETPLATFORM} | cut -d / -f1) && \ - export GOARCH=$(echo ${TARGETPLATFORM} | cut -d / -f2) - -RUN go env - -RUN CGO_ENABLED=0 go build -o /output/kyverno -ldflags="${LD_FLAGS}" -v ./cmd/cli/kubectl-kyverno/ +RUN --mount=type=bind,target=. \ + --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg/mod \ + CGO_ENABLED=0 xx-go build -o /output/kyverno -ldflags="${LD_FLAGS}" -v ./cmd/cli/kubectl-kyverno/ # Packaging stage FROM scratch diff --git a/cmd/initContainer/Dockerfile b/cmd/initContainer/Dockerfile index e063a5a4db..20c7918543 100644 --- a/cmd/initContainer/Dockerfile +++ b/cmd/initContainer/Dockerfile @@ -1,22 +1,28 @@ # Multi-stage docker build # Build stage -FROM golang@sha256:724abf4dd44985d060f7aa91af5211eb2052491424bd497ba3ddc31f7cee969d AS builder - +FROM --platform=${BUILDPLATFORM} golang@sha256:724abf4dd44985d060f7aa91af5211eb2052491424bd497ba3ddc31f7cee969d AS base +WORKDIR /src LABEL maintainer="Kyverno" +COPY go.* . + +RUN --mount=type=cache,target=/go/pkg/mod \ + go mod download + +FROM --platform=${BUILDPLATFORM} tonistiigi/xx:1.1.1@sha256:23ca08d120366b31d1d7fad29283181f063b0b43879e1f93c045ca5b548868e9 AS xx + +FROM base AS builder + # LD_FLAGS is passed as argument from Makefile. It will be empty, if no argument passed ARG LD_FLAGS ARG TARGETPLATFORM -ADD . /kyverno -WORKDIR /kyverno +COPY --from=xx / / -RUN export GOOS=$(echo ${TARGETPLATFORM} | cut -d / -f1) && \ - export GOARCH=$(echo ${TARGETPLATFORM} | cut -d / -f2) - -RUN go env - -RUN CGO_ENABLED=0 go build -o /output/kyvernopre -ldflags="${LD_FLAGS}" -v ./cmd/initContainer/ +RUN --mount=type=bind,target=. \ + --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg/mod \ + CGO_ENABLED=0 xx-go build -o /output/kyvernopre -ldflags="${LD_FLAGS}" -v ./cmd/initContainer/ # Packaging stage FROM scratch diff --git a/cmd/kyverno/Dockerfile b/cmd/kyverno/Dockerfile index 71b58e72ca..d533091be3 100644 --- a/cmd/kyverno/Dockerfile +++ b/cmd/kyverno/Dockerfile @@ -1,26 +1,32 @@ -FROM golang:alpine AS certs +FROM --platform=${BUILDPLATFORM} golang:alpine AS certs LABEL maintainer="Kyverno" RUN apk add --no-cache ca-certificates -FROM golang@sha256:724abf4dd44985d060f7aa91af5211eb2052491424bd497ba3ddc31f7cee969d AS builder - +FROM --platform=${BUILDPLATFORM} golang@sha256:724abf4dd44985d060f7aa91af5211eb2052491424bd497ba3ddc31f7cee969d AS base +WORKDIR /src LABEL maintainer="Kyverno" +COPY go.* . + +RUN --mount=type=cache,target=/go/pkg/mod \ + go mod download + +FROM --platform=${BUILDPLATFORM} tonistiigi/xx:1.1.1@sha256:23ca08d120366b31d1d7fad29283181f063b0b43879e1f93c045ca5b548868e9 AS xx + +FROM base AS builder + # LD_FLAGS is passed as argument from Makefile. It will be empty, if no argument passed ARG LD_FLAGS ARG TARGETPLATFORM -ADD . /kyverno -WORKDIR /kyverno +COPY --from=xx / / -RUN export GOOS=$(echo ${TARGETPLATFORM} | cut -d / -f1) && \ - export GOARCH=$(echo ${TARGETPLATFORM} | cut -d / -f2) - -RUN go env - -RUN CGO_ENABLED=0 go build -o /output/kyverno -ldflags="${LD_FLAGS}" -v ./cmd/kyverno/ +RUN --mount=type=bind,target=. \ + --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg/mod \ + CGO_ENABLED=0 xx-go build -o /output/kyverno -ldflags="${LD_FLAGS}" -v ./cmd/kyverno/ # Packaging stage FROM scratch