Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cross-compilation and remove all references to ARCH #40

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,51 @@ ARG BCI_IMAGE=registry.suse.com/bci/bci-busybox
ARG GO_IMAGE=rancher/hardened-build-base:v1.21.10b1
FROM ${BCI_IMAGE} as bci

FROM ${GO_IMAGE} as builder
ARG ARCH="amd64"
# Image that provides cross compilation tooling.
FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.3.0 AS xx

FROM --platform=$BUILDPLATFORM ${GO_IMAGE} as base-builder
# copy xx scripts to your build stage
COPY --from=xx / /
RUN apk add file make git clang lld
ARG TARGETPLATFORM
RUN set -x && \
xx-apk --no-cache add musl-dev gcc

FROM base-builder as whereabouts-builder
ARG TAG=v0.7.0
ARG PKG="github.com/k8snetworkplumbingwg/whereabouts"
ARG SRC="github.com/k8snetworkplumbingwg/whereabouts"
RUN git clone --depth=1 https://${SRC}.git $GOPATH/src/${PKG}
WORKDIR $GOPATH/src/${PKG}
RUN git fetch --all --tags --prune
RUN git checkout tags/${TAG} -b ${TAG}
RUN go mod download
# cross-compilation setup
ARG TARGETARCH

ENV GO111MODULE=on
RUN GIT_SHA=$(git rev-parse --short HEAD) \
GIT_TREE_STATE=$(test -n "`git status --porcelain --untracked-files=no`" && echo "dirty" || echo "clean") && \
export GO_LDFLAGS="-X ${PKG}/pkg/version.Version=${TAG} \
-X ${PKG}/pkg/version.GitSHA=${GIT_SHA} \
-X ${PKG}/pkg/version.GitTreeState=${GIT_TREE_STATE}" && \
xx-go --wrap && \
go-build-static.sh -gcflags=-trimpath=${GOPATH}/src -mod vendor -o bin/whereabouts cmd/whereabouts.go && \
go-build-static.sh -gcflags=-trimpath=${GOPATH}/src -mod vendor -o bin/ip-control-loop cmd/controlloop/*.go
RUN go-assert-boring.sh bin/*
RUN install -s bin/* /usr/local/bin
RUN whereabouts --version
RUN xx-verify --static bin/*
RUN install bin/* /usr/local/bin

FROM ${GO_IMAGE} as strip_binary
#strip needs to run on TARGETPLATFORM, not BUILDPLATFORM
COPY --from=whereabouts-builder /usr/local/bin/whereabouts .
COPY --from=whereabouts-builder /usr/local/bin/ip-control-loop .
RUN strip ./whereabouts ./ip-control-loop

FROM bci
COPY --from=builder /usr/local/bin/whereabouts .
COPY --from=builder /usr/local/bin/ip-control-loop .
COPY --from=strip_binary /go/whereabouts .
COPY --from=strip_binary /go/ip-control-loop .
ARG PKG="github.com/k8snetworkplumbingwg/whereabouts"
COPY --from=builder /go/src/${PKG}/script/install-cni.sh .
COPY --from=whereabouts-builder /go/src/${PKG}/script/install-cni.sh .
CMD ["/install-cni.sh"]
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ image-build:
docker buildx build \
--platform=$(ARCH) \
--pull \
--build-arg ARCH=$(ARCH) \
--build-arg PKG=$(PKG) \
--build-arg SRC=$(SRC) \
--build-arg TAG=$(TAG:$(BUILD_META)=) \
Expand Down