generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Cross-compile for arm64 and mount go cache #30
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
ARG GOARCH="amd64" | ||
FROM golang:1.22 AS builder | ||
FROM --platform=$BUILDPLATFORM golang:1.22 AS builder | ||
|
||
WORKDIR /src | ||
|
||
COPY go.mod go.sum . | ||
RUN --mount=type=cache,target=/go/pkg \ | ||
go mod download | ||
|
||
COPY . . | ||
# build | ||
RUN go mod download | ||
RUN CGO_ENABLED=0 go build -o /go/bin/netpol ./cmd | ||
|
||
ARG TARGETOS TARGETARCH | ||
RUN --mount=type=cache,target=/root/.cache/go-build \ | ||
--mount=type=cache,target=/go/pkg \ | ||
CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \ | ||
go build -o /go/bin/netpol ./cmd | ||
|
||
# STEP 2: Build small image | ||
FROM registry.k8s.io/build-image/distroless-iptables:v0.5.2 | ||
COPY --from=builder --chown=root:root /go/bin/netpol /bin/netpol | ||
|
||
CMD ["/bin/netpol"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -35,9 +35,15 @@ REGISTRY?=gcr.io/k8s-staging-networking | |||||
TAG?=$(shell echo "$$(date +v%Y%m%d)-$$(git describe --always --dirty)") | ||||||
# the full image tag | ||||||
IMAGE?=$(REGISTRY)/$(IMAGE_NAME):$(TAG) | ||||||
PLATFORMS?=linux/amd64,linux/arm64 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
default only to one platfor, this make it works locally and for multiplatform we can always set the variable |
||||||
|
||||||
# required to enable buildx | ||||||
export DOCKER_CLI_EXPERIMENTAL=enabled | ||||||
image-build: | ||||||
# docker buildx build --platform=${PLATFORMS} $(OUTPUT) --progress=$(PROGRESS) -t ${IMAGE} --pull $(EXTRA_BUILD_OPT) . | ||||||
docker build . -t ${IMAGE} | ||||||
docker buildx build . \ | ||||||
--tag="${IMAGE}" \ | ||||||
--load | ||||||
|
||||||
image-push: | ||||||
docker buildx build . \ | ||||||
--platform="${PLATFORMS}" \ | ||||||
--tag="${IMAGE}" \ | ||||||
--push |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we have defaults for all these variables for amd64 and linux?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None of these arguments need to be set manually. The build platform argument as used in the from line tells docker to always use the native architecture rather than to emulate with qemu as we use cross compilation. The target arguments get set based on the platform flag which is provided in the makefile.
https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation-guide/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I knew I remember something about this but I didn't know what.
Here is the problem, with multiplatform the
--load
flag fails docker/buildx#65This seems to solve the problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vaskozl you resolved this comment but using the
--load
flag with multiple archs fails for me, is not failing for you?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I haven't resolved it since the first comment.
--load
does indeed work for me. Apologies if it's still showing that way, I didn't mean to resolve it in the first place, github ate my comment and made me type it out twice earlier on my phone :/.It's not necessary though if one doesn't plan to use the image locally. Looks like it might not work on older version of
docker
, so I suggest:--load
if we just want to test builds on all architectures without pushing onimage-build
--load
and remove the--platform
flag. It will only build for the host arch in that case.What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commented here https://github.com/kubernetes-sigs/kube-network-policies/pull/30/files#r1616335414 , defaulting to one platform is compatible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you not prefer to keep
PLATFORMS
set to the platforms we push for, and then work around the "local" limitation by not using--platform
inbulild-image
(only inbuild-push
)?That way one wouldn't need to set PLATFORMS even if working locally and
make image-push
would should push upstream without error?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I say that because I personally think the fewer variables one has to juggle the better :D. I've already removed
--platform
from the local build. It's pretty redundant as you would never need a local alternate arch unless you ran that image with qemu.Edit: Also looking at other projects, they seem to keep PLATFORMS populated with everything they push for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not an expert here, I trust you