diff --git a/README.md b/README.md index 1f0543c847..19982d7e79 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ _If you are interested in contributing to kaniko, see [DEVELOPMENT.md](DEVELOPME - [--cache-ttl duration](#--cache-ttl-duration) - [--cleanup](#--cleanup) - [--context-sub-path](#--context-sub-path) + - [--customPlatform](#--customPlatform) - [--digest-file](#--digest-file) - [--force](#--force) - [--git](#--git) @@ -577,6 +578,13 @@ Set a sub path within the given `--context`. Its particularly useful when your context is, for example, a git repository, and you want to build one of its subfolders instead of the root folder. +#### --customPlatform + +Allows to build with another default platform than the host, similarly to docker build --platform xxx +the value has to be on the form `--customPlatform=linux/arm` , with acceptable values listed here: [GOOS/GOARCH](https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63) + +_This is not virtualization and cannot help to build an architecture not natively supported by the build host. This is used to build i386 on an amd64 Host for example, or arm32 on an arm64 host._ + #### --digest-file Set this flag to specify a file in the container. This file will @@ -819,4 +827,4 @@ file are made and when the `mtime` is updated. This means: which will still be correct, but it does affect the number of layers. _Note that these issues are currently theoretical only. If you see this issue occur, please -[open an issue](https://github.com/GoogleContainerTools/kaniko/issues)._ +[open an issue](https://github.com/GoogleContainerTools/kaniko/issues)._ \ No newline at end of file diff --git a/cmd/executor/cmd/root.go b/cmd/executor/cmd/root.go index 68b552da0e..68a0540b23 100644 --- a/cmd/executor/cmd/root.go +++ b/cmd/executor/cmd/root.go @@ -150,6 +150,7 @@ func addKanikoOptionsFlags() { RootCmd.PersistentFlags().StringVarP(&opts.Bucket, "bucket", "b", "", "Name of the GCS bucket from which to access build context as tarball.") RootCmd.PersistentFlags().VarP(&opts.Destinations, "destination", "d", "Registry the final image should be pushed to. Set it repeatedly for multiple destinations.") RootCmd.PersistentFlags().StringVarP(&opts.SnapshotMode, "snapshotMode", "", "full", "Change the file attributes inspected during snapshotting") + RootCmd.PersistentFlags().StringVarP(&opts.CustomPlatform, "customPlatform", "", "", "Specify the build platform if different from the current host") RootCmd.PersistentFlags().VarP(&opts.BuildArgs, "build-arg", "", "This flag allows you to pass in ARG values at build time. Set it repeatedly for multiple values.") RootCmd.PersistentFlags().BoolVarP(&opts.Insecure, "insecure", "", false, "Push to insecure registry using plain HTTP") RootCmd.PersistentFlags().BoolVarP(&opts.SkipTLSVerify, "skip-tls-verify", "", false, "Push to insecure registry ignoring TLS verify") diff --git a/pkg/config/options.go b/pkg/config/options.go index 669b84f588..a9e799b515 100644 --- a/pkg/config/options.go +++ b/pkg/config/options.go @@ -36,6 +36,7 @@ type KanikoOptions struct { DockerfilePath string SrcContext string SnapshotMode string + CustomPlatform string Bucket string TarPath string Target string diff --git a/pkg/executor/build.go b/pkg/executor/build.go index 3de971cc99..e50b6674fb 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -607,8 +607,13 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) { if err != nil { return nil, err } - configFile.OS = runtime.GOOS - configFile.Architecture = runtime.GOARCH + if opts.CustomPlatform == "" { + configFile.OS = runtime.GOOS + configFile.Architecture = runtime.GOARCH + } else { + configFile.OS = strings.Split(opts.CustomPlatform, "/")[0] + configFile.Architecture = strings.Split(opts.CustomPlatform, "/")[1] + } sourceImage, err = mutate.ConfigFile(sourceImage, configFile) if err != nil { return nil, err diff --git a/pkg/image/image_util.go b/pkg/image/image_util.go index 44f2f399f4..4faeba01c9 100644 --- a/pkg/image/image_util.go +++ b/pkg/image/image_util.go @@ -169,7 +169,7 @@ func remoteOptions(registryName string, opts *config.KanikoOptions) []remote.Opt tr := util.MakeTransport(opts, registryName) // on which v1.Platform is this currently running? - platform := currentPlatform() + platform := currentPlatform(opts) return []remote.Option{remote.WithTransport(tr), remote.WithAuthFromKeychain(creds.GetKeychain()), remote.WithPlatform(platform)} } @@ -199,7 +199,13 @@ func cachedImage(opts *config.KanikoOptions, image string) (v1.Image, error) { } // CurrentPlatform returns the v1.Platform on which the code runs -func currentPlatform() v1.Platform { +func currentPlatform(opts *config.KanikoOptions) v1.Platform { + if opts.CustomPlatform != "" { + return v1.Platform{ + OS: strings.Split(opts.CustomPlatform, "/")[0], + Architecture: strings.Split(opts.CustomPlatform, "/")[1], + } + } return v1.Platform{ OS: runtime.GOOS, Architecture: runtime.GOARCH,