Add option customPlatform (#1500)

* Add option customPlatform

* fix formatting

* fix No newline at end of file
This commit is contained in:
mickkael 2020-12-09 08:11:18 +08:00 committed by GitHub
parent ea59504b9b
commit 275cc9a7e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 5 deletions

View File

@ -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)._

View File

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

View File

@ -36,6 +36,7 @@ type KanikoOptions struct {
DockerfilePath string
SrcContext string
SnapshotMode string
CustomPlatform string
Bucket string
TarPath string
Target string

View File

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

View File

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