Skip to content

Commit

Permalink
feature: support pre-defined build args of multi-platform build argum…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
kit101 committed Jan 22, 2025
1 parent e328007 commit 8218bd7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/dockerfile/buildargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ limitations under the License.
package dockerfile

import (
"runtime"
"strings"

"github.com/GoogleContainerTools/kaniko/pkg/config"
d "github.com/docker/docker/builder/dockerfile"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
)
Expand Down Expand Up @@ -68,3 +70,42 @@ func (b *BuildArgs) AddMetaArgs(metaArgs []instructions.ArgCommand) {
}
}
}

// AddPreDefinedBuildArgs adds pre-defined build args. Such as TARGETOS, TARGETARCH, BUILDPLATFORM, TARGETPLATFORM
func (b *BuildArgs) AddPreDefinedBuildArgs(opts *config.KanikoOptions) {
buildPlatform := runtime.GOOS + "/" + runtime.GOARCH
buildOs := runtime.GOOS
buildArch := runtime.GOARCH

targetPlatform := ""
targetOs := ""
targetArch := ""
targetVariant := ""

if opts.CustomPlatform == "" {
targetPlatform = buildPlatform
targetOs = buildOs
targetArch = buildArch
} else {
targetPlatform = opts.CustomPlatform
platformParts := strings.Split(opts.CustomPlatform, "/")
if len(platformParts) > 0 {
targetOs = platformParts[0]
}
if len(platformParts) > 1 {
targetArch = platformParts[1]
}
if len(platformParts) > 2 {
targetVariant = platformParts[2]
}
}

b.AddArg("BUILDPLATFORM", &buildPlatform)
b.AddArg("BUILDOS", &buildOs)
b.AddArg("BUILDARCH", &buildArch)

b.AddArg("TARGETPLATFORM", &targetPlatform)
b.AddArg("TARGETOS", &targetOs)
b.AddArg("TARGETARCH", &targetArch)
b.AddArg("TARGETVARIANT", &targetVariant)
}
1 change: 1 addition & 0 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func newStageBuilder(args *dockerfile.BuildArgs, opts *config.KanikoOptions, sta
s.args = dockerfile.NewBuildArgs(s.opts.BuildArgs)
}
s.args.AddMetaArgs(s.stage.MetaArgs)
s.args.AddPreDefinedBuildArgs(opts)
return s, nil
}

Expand Down

0 comments on commit 8218bd7

Please sign in to comment.