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

feat: support pre-defined build args of multi-platform build arguments #3392

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions integration/dockerfiles/Dockerfile_test_pre_defined_build_args
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# ingegration test for pre-defined build args; see pkg/dockerfile/buildargs.go
FROM alpine:3 AS builder
ARG BUILDPLATFORM
ARG BUILDOS
ARG BUILDARCH
ARG BUILDVARIANT
ARG TARGETPLATFORM
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT
ARG TARGETSTAGE
RUN echo "BUILDPLATFORM: ${BUILDPLATFORM}" && \
echo "BUILDOS: ${BUILDOS}" && \
echo "BUILDARCH: ${BUILDARCH}" && \
echo "BUILDVARIANT: ${BUILDVARIANT}" && \
echo "TARGETPLATFORM: ${TARGETPLATFORM}" && \
echo "TARGETOS: ${TARGETOS}" && \
echo "TARGETARCH: ${TARGETARCH}" && \
echo "TARGETVARIANT: ${TARGETVARIANT}" && \
echo "TARGETSTAGE: ${TARGETSTAGE}"
RUN uname -a
COPY foo /app/foo
RUN echo 'add line' >> /app/foo

FROM alpine:3
COPY --from=builder /app/foo /app/foo
ARG BUILDPLATFORM
ARG BUILDOS
ARG BUILDARCH
ARG BUILDVARIANT
ARG TARGETPLATFORM
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT
ARG TARGETSTAGE
RUN echo "BUILDPLATFORM: ${BUILDPLATFORM}" && \
echo "BUILDOS: ${BUILDOS}" && \
echo "BUILDARCH: ${BUILDARCH}" && \
echo "BUILDVARIANT: ${BUILDVARIANT}" && \
echo "TARGETPLATFORM: ${TARGETPLATFORM}" && \
echo "TARGETOS: ${TARGETOS}" && \
echo "TARGETARCH: ${TARGETARCH}" && \
echo "TARGETVARIANT: ${TARGETVARIANT}" && \
echo "TARGETSTAGE: ${TARGETSTAGE}"
RUN uname -a
RUN cat /app/foo
51 changes: 51 additions & 0 deletions pkg/dockerfile/buildargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ limitations under the License.
package dockerfile

import (
"fmt"
"strings"
"sync"

"github.com/GoogleContainerTools/kaniko/pkg/config"
"github.com/containerd/platforms"
d "github.com/docker/docker/builder/dockerfile"
containerregistryV1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/sirupsen/logrus"
)

var defaultArgsOnce sync.Once
var defaultArgs []string

type BuildArgs struct {
d.BuildArgs
}
Expand Down Expand Up @@ -68,3 +77,45 @@ func (b *BuildArgs) AddMetaArgs(metaArgs []instructions.ArgCommand) {
}
}
}

// DefaultArgs get default args.
// include pre-defined build args: TARGETOS, TARGETARCH, BUILDPLATFORM, TARGETPLATFORM ...
func DefaultArgs(opt config.KanikoOptions) []string {
defaultArgsOnce.Do(func() {
// build info
buildPlatform := platforms.Format(platforms.Normalize(platforms.DefaultSpec()))
buildPlatformSpec, err := containerregistryV1.ParsePlatform(buildPlatform)
if err != nil {
logrus.Fatalf("Parse build platform %q: %v", buildPlatform, err)
}

// target info
var targetPlatform string
if opt.CustomPlatform != "" {
targetPlatform = opt.CustomPlatform
} else {
targetPlatform = buildPlatform
}
targetPlatformSpec, err := containerregistryV1.ParsePlatform(opt.CustomPlatform)
if err != nil {
logrus.Fatalf("Invalid platform %q: %v", opt.CustomPlatform, err)
}

// pre-defined build args
defaultArgs = []string{
fmt.Sprintf("%s=%s", "BUILDPLATFORM", buildPlatform),
fmt.Sprintf("%s=%s", "BUILDOS", buildPlatformSpec.OS),
fmt.Sprintf("%s=%s", "BUILDOSVERSION", buildPlatformSpec.OSVersion),
fmt.Sprintf("%s=%s", "BUILDARCH", buildPlatformSpec.Architecture),
fmt.Sprintf("%s=%s", "BUILDVARIANT", buildPlatformSpec.Variant),
fmt.Sprintf("%s=%s", "TARGETPLATFORM", targetPlatform),
fmt.Sprintf("%s=%s", "TARGETOS", targetPlatformSpec.OS),
fmt.Sprintf("%s=%s", "TARGETOSVERSION", targetPlatformSpec.OSVersion),
fmt.Sprintf("%s=%s", "TARGETARCH", targetPlatformSpec.Architecture),
fmt.Sprintf("%s=%s", "TARGETVARIANT", targetPlatformSpec.Variant),
fmt.Sprintf("%s=%s", "TARGETSTAGE", opt.Target),
}
logrus.Infof("init default args: %s", defaultArgs)
})
return defaultArgs
}
4 changes: 3 additions & 1 deletion pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ func newStageBuilder(args *dockerfile.BuildArgs, opts *config.KanikoOptions, sta
if args != nil {
s.args = args.Clone()
} else {
s.args = dockerfile.NewBuildArgs(s.opts.BuildArgs)
defaultArgs := dockerfile.DefaultArgs(*opts)
mergedArgs := append(defaultArgs, s.opts.BuildArgs...)
s.args = dockerfile.NewBuildArgs(mergedArgs)
}
s.args.AddMetaArgs(s.stage.MetaArgs)
return s, nil
Expand Down