GoogleContainerTools / kaniko

Build Container Images In Kubernetes
Apache License 2.0
14.88k stars 1.44k forks source link

Remote caching does not respect architecture #2567

Open jamil-s opened 1 year ago

jamil-s commented 1 year ago

Actual behavior With caching enabled, we have seen arm64 builds fetch artifacts for amd64 architecture builds.

Expected behavior Cache is architecture aware.

To Reproduce Steps to reproduce the behavior:

  1. Enable caching using --snapshot-mode=redo --cache=true --customPlatform=linux/amd64 and similarly for --customPlatform=linux/arm64
  2. Notice cache hits for different architecture

    Triage Notes for the Maintainers

    Description Yes/No
    Please check if this a new feature you are proposing
    • - [ ]
    Please check if the build works in docker but not in kaniko
    • - [ ]
    Please check if this error is seen when you use --cache flag
    • - [ ]
    Please check if your dockerfile is a multistage dockerfile
    • - [ ]
aaron-prindle commented 1 year ago

Related: https://github.com/GoogleContainerTools/kaniko/issues/1525

liiight commented 6 months ago

@aaron-prindle I'm not so sure this is related to #1525, this seems like a very specific bug.

It looks to me that this is the part that is responsible on creating the id used for the cache layer: https://github.com/GoogleContainerTools/kaniko/blob/98df8ebfc7834a720c83b81bd0b1d54f4f480477/pkg/executor/build.go#L254

Note that it takes into consideration the command, files, build args and environment, but not other implicit options, like architecture in this case. I imagine this issue can be reproduced with other conditions as well, differences between build runs that are not part of the aforementioned components.

There is however an easy workaround for this issue, I just passed the architechture as a build arg and set it as an ENV command, which is being taken into consideration when creating the cache layer:

ARG TARGETARCH
ENV ARCH=$TARGETARCH

I chose TARGETARCH to make it compatible with buildkit syntax as well, which passes this arguments implicitly: https://docs.docker.com/reference/dockerfile/#automatic-platform-args-in-the-global-scope