From ff385a972813c79bbd5fc89357ff2cefe3e5b43c Mon Sep 17 00:00:00 2001 From: Josh W Lewis Date: Sun, 5 May 2024 10:49:00 -0500 Subject: [PATCH] feat: mutate platform (#1919) Signed-off-by: Josh W Lewis --- cmd/crane/cmd/mutate.go | 17 ++++++++++++++++- cmd/crane/doc/crane_mutate.md | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cmd/crane/cmd/mutate.go b/cmd/crane/cmd/mutate.go index db484e5c7..4fc9b1dc0 100644 --- a/cmd/crane/cmd/mutate.go +++ b/cmd/crane/cmd/mutate.go @@ -39,6 +39,7 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command { var user string var workdir string var ports []string + var newPlatform string mutateCmd := &cobra.Command{ Use: "mutate", @@ -130,8 +131,20 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command { cfg.Config.ExposedPorts = portMap } + // Set platform + if len(newPlatform) > 0 { + platform, err := parsePlatform(newPlatform) + if err != nil { + return err + } + cfg.OS = platform.OS + cfg.Architecture = platform.Architecture + cfg.Variant = platform.Variant + cfg.OSVersion = platform.OSVersion + } + // Mutate and write image. - img, err = mutate.Config(img, cfg.Config) + img, err = mutate.ConfigFile(img, cfg) if err != nil { return fmt.Errorf("mutating config: %w", err) } @@ -183,6 +196,8 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command { mutateCmd.Flags().StringVarP(&user, "user", "u", "", "New user to set") mutateCmd.Flags().StringVarP(&workdir, "workdir", "w", "", "New working dir to set") mutateCmd.Flags().StringSliceVar(&ports, "exposed-ports", nil, "New ports to expose") + // Using "set-platform" to avoid clobbering "platform" persistent flag. + mutateCmd.Flags().StringVar(&newPlatform, "set-platform", "", "New platform to set in the form os/arch[/variant][:osversion] (e.g. linux/amd64)") return mutateCmd } diff --git a/cmd/crane/doc/crane_mutate.md b/cmd/crane/doc/crane_mutate.md index 367410450..f97d33df1 100644 --- a/cmd/crane/doc/crane_mutate.md +++ b/cmd/crane/doc/crane_mutate.md @@ -19,6 +19,7 @@ crane mutate [flags] -l, --label stringToString New labels to add (default []) -o, --output string Path to new tarball of resulting image --repo string Repository to push the mutated image to. If provided, push by digest to this repository. + --set-platform string New platform to set in the form os/arch[/variant][:osversion] (e.g. linux/amd64) -t, --tag string New tag reference to apply to mutated image. If not provided, push by digest to the original image repository. -u, --user string New user to set -w, --workdir string New working dir to set