feature: support pre-defined build args of multi-platform build arguments

This commit is contained in:
k7kit 2025-01-22 20:45:27 +08:00
parent e328007bc1
commit 8218bd7386
2 changed files with 42 additions and 0 deletions

View File

@ -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"
)
@ -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)
}

View File

@ -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
}