Skip to content

Commit

Permalink
Add option customPlatform (#1500)
Browse files Browse the repository at this point in the history
* Add option customPlatform

* fix formatting

* fix No newline at end of file
  • Loading branch information
mickkael authored Dec 9, 2020
1 parent ea59504 commit 275cc9a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)._
1 change: 1 addition & 0 deletions cmd/executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type KanikoOptions struct {
DockerfilePath string
SrcContext string
SnapshotMode string
CustomPlatform string
Bucket string
TarPath string
Target string
Expand Down
9 changes: 7 additions & 2 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions pkg/image/image_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 275cc9a

Please sign in to comment.