From e12c834284b53a6866e4862dec1a486c878a8759 Mon Sep 17 00:00:00 2001 From: Ompragash Viswanathan Date: Fri, 21 Nov 2025 11:21:42 +0530 Subject: [PATCH] fix: Consolidate platform flags to use --custom-platform and eliminate deprecation warning --- cmd/kaniko-acr/main.go | 10 ++---- cmd/kaniko-docker/main.go | 10 ++---- cmd/kaniko-docker/main_test.go | 58 ++++++++++++++++++++++++++++++++++ cmd/kaniko-ecr/main.go | 10 ++---- cmd/kaniko-gar/main.go | 10 ++---- cmd/kaniko-gcr/main.go | 10 ++---- kaniko.go | 5 --- kaniko_test.go | 51 ++++++++++++++++++++++++++++++ 8 files changed, 119 insertions(+), 45 deletions(-) diff --git a/cmd/kaniko-acr/main.go b/cmd/kaniko-acr/main.go index e862caf..0ba3ec5 100644 --- a/cmd/kaniko-acr/main.go +++ b/cmd/kaniko-acr/main.go @@ -254,7 +254,7 @@ func main() { cli.StringFlag{ Name: "platform", Usage: "Allows to build with another default platform than the host, similarly to docker build --platform", - EnvVar: "PLUGIN_PLATFORM", + EnvVar: "PLUGIN_PLATFORM,PLUGIN_CUSTOM_PLATFORM", }, cli.BoolFlag{ Name: "skip-unused-stages", @@ -292,11 +292,6 @@ func main() { Usage: "Sub-path within the context to build.", EnvVar: "PLUGIN_CONTEXT_SUB_PATH", }, - cli.StringFlag{ - Name: "custom-platform", - Usage: "Platform to use for building.", - EnvVar: "PLUGIN_CUSTOM_PLATFORM", - }, cli.BoolFlag{ Name: "force", Usage: "Force building the image even if it already exists.", @@ -477,14 +472,13 @@ func run(c *cli.Context) error { DigestFile: defaultDigestFile, NoPush: noPush, Verbosity: c.String("verbosity"), - Platform: c.String("platform"), + CustomPlatform: c.String("platform"), SkipUnusedStages: c.Bool("skip-unused-stages"), CacheDir: c.String("cache-dir"), CacheCopyLayers: c.Bool("cache-copy-layers"), CacheRunLayers: c.Bool("cache-run-layers"), Cleanup: c.Bool("cleanup"), ContextSubPath: c.String("context-sub-path"), - CustomPlatform: c.String("custom-platform"), Force: c.Bool("force"), ImageNameWithDigestFile: c.String("image-name-with-digest-file"), ImageNameTagWithDigestFile: c.String("image-name-tag-with-digest-file"), diff --git a/cmd/kaniko-docker/main.go b/cmd/kaniko-docker/main.go index 4360912..23b850c 100644 --- a/cmd/kaniko-docker/main.go +++ b/cmd/kaniko-docker/main.go @@ -213,7 +213,7 @@ func main() { cli.StringFlag{ Name: "platform", Usage: "Allows to build with another default platform than the host, similarly to docker build --platform", - EnvVar: "PLUGIN_PLATFORM", + EnvVar: "PLUGIN_PLATFORM,PLUGIN_CUSTOM_PLATFORM", }, cli.BoolFlag{ Name: "skip-unused-stages", @@ -257,11 +257,6 @@ func main() { Usage: "Sub-path within the context to build.", EnvVar: "PLUGIN_CONTEXT_SUB_PATH", }, - cli.StringFlag{ - Name: "custom-platform", - Usage: "Platform to use for building.", - EnvVar: "PLUGIN_CUSTOM_PLATFORM", - }, cli.BoolFlag{ Name: "force", Usage: "Force building the image even if it already exists.", @@ -449,7 +444,7 @@ func run(c *cli.Context) error { NoPush: noPush, TarPath: c.String("tar-path"), Verbosity: c.String("verbosity"), - Platform: c.String("platform"), + CustomPlatform: c.String("platform"), PushOnly: c.Bool("push-only"), SkipUnusedStages: c.Bool("skip-unused-stages"), CacheDir: c.String("cache-dir"), @@ -457,7 +452,6 @@ func run(c *cli.Context) error { CacheRunLayers: c.Bool("cache-run-layers"), Cleanup: c.Bool("cleanup"), ContextSubPath: c.String("context-sub-path"), - CustomPlatform: c.String("custom-platform"), Force: c.Bool("force"), ImageNameWithDigestFile: c.String("image-name-with-digest-file"), ImageNameTagWithDigestFile: c.String("image-name-tag-with-digest-file"), diff --git a/cmd/kaniko-docker/main_test.go b/cmd/kaniko-docker/main_test.go index fbb4a70..19cb7c3 100644 --- a/cmd/kaniko-docker/main_test.go +++ b/cmd/kaniko-docker/main_test.go @@ -206,6 +206,64 @@ func TestDockerBuildArgsProcessing(t *testing.T) { } } +func TestPlatformEnvVarMapping(t *testing.T) { + tests := []struct { + name string + envVar string + envValue string + expectedValue string + }{ + { + name: "PLUGIN_PLATFORM env var", + envVar: "PLUGIN_PLATFORM", + envValue: "linux/amd64", + expectedValue: "linux/amd64", + }, + { + name: "PLUGIN_CUSTOM_PLATFORM env var", + envVar: "PLUGIN_CUSTOM_PLATFORM", + envValue: "linux/arm64", + expectedValue: "linux/arm64", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Set the environment variable + os.Setenv(tt.envVar, tt.envValue) + defer os.Unsetenv(tt.envVar) + + app := cli.NewApp() + app.Name = "kaniko-docker-test" + + var capturedPlatform string + + app.Flags = []cli.Flag{ + cli.StringFlag{ + Name: "platform", + Usage: "Allows to build with another default platform than the host", + EnvVar: "PLUGIN_PLATFORM,PLUGIN_CUSTOM_PLATFORM", + }, + } + + app.Action = func(c *cli.Context) error { + capturedPlatform = c.String("platform") + return nil + } + + err := app.Run([]string{"kaniko-docker-test"}) + if err != nil { + t.Errorf("CLI run error = %v, want nil", err) + return + } + + if capturedPlatform != tt.expectedValue { + t.Errorf("Got platform = %v, want %v", capturedPlatform, tt.expectedValue) + } + }) + } +} + func TestCreateDockerConfig(t *testing.T) { config := docker.NewConfig() tempDir, err := ioutil.TempDir("", "docker-config-test") diff --git a/cmd/kaniko-ecr/main.go b/cmd/kaniko-ecr/main.go index bde087f..0ee0066 100644 --- a/cmd/kaniko-ecr/main.go +++ b/cmd/kaniko-ecr/main.go @@ -244,7 +244,7 @@ func main() { cli.StringFlag{ Name: "platform", Usage: "Allows to build with another default platform than the host, similarly to docker build --platform", - EnvVar: "PLUGIN_PLATFORM", + EnvVar: "PLUGIN_PLATFORM,PLUGIN_CUSTOM_PLATFORM", }, cli.BoolFlag{ Name: "skip-unused-stages", @@ -282,11 +282,6 @@ func main() { Usage: "Sub-path within the context to build.", EnvVar: "PLUGIN_CONTEXT_SUB_PATH", }, - cli.StringFlag{ - Name: "custom-platform", - Usage: "Platform to use for building.", - EnvVar: "PLUGIN_CUSTOM_PLATFORM", - }, cli.BoolFlag{ Name: "force", Usage: "Force building the image even if it already exists.", @@ -518,14 +513,13 @@ func run(c *cli.Context) error { DigestFile: defaultDigestFile, NoPush: noPush, Verbosity: c.String("verbosity"), - Platform: c.String("platform"), + CustomPlatform: c.String("platform"), SkipUnusedStages: c.Bool("skip-unused-stages"), CacheDir: c.String("cache-dir"), CacheCopyLayers: c.Bool("cache-copy-layers"), CacheRunLayers: c.Bool("cache-run-layers"), Cleanup: c.Bool("cleanup"), ContextSubPath: c.String("context-sub-path"), - CustomPlatform: c.String("custom-platform"), Force: c.Bool("force"), ImageNameWithDigestFile: c.String("image-name-with-digest-file"), ImageNameTagWithDigestFile: c.String("image-name-tag-with-digest-file"), diff --git a/cmd/kaniko-gar/main.go b/cmd/kaniko-gar/main.go index d8d2cbf..d6e754f 100644 --- a/cmd/kaniko-gar/main.go +++ b/cmd/kaniko-gar/main.go @@ -207,7 +207,7 @@ func main() { cli.StringFlag{ Name: "platform", Usage: "Allows to build with another default platform than the host, similarly to docker build --platform", - EnvVar: "PLUGIN_PLATFORM", + EnvVar: "PLUGIN_PLATFORM,PLUGIN_CUSTOM_PLATFORM", }, cli.BoolFlag{ Name: "skip-unused-stages", @@ -245,11 +245,6 @@ func main() { Usage: "Sub-path within the context to build.", EnvVar: "PLUGIN_CONTEXT_SUB_PATH", }, - cli.StringFlag{ - Name: "custom-platform", - Usage: "Platform to use for building.", - EnvVar: "PLUGIN_CUSTOM_PLATFORM", - }, cli.BoolFlag{ Name: "force", Usage: "Force building the image even if it already exists.", @@ -430,14 +425,13 @@ func run(c *cli.Context) error { SourceTarPath: c.String("source-tar-path"), TarPath: c.String("tar-path"), Verbosity: c.String("verbosity"), - Platform: c.String("platform"), + CustomPlatform: c.String("platform"), SkipUnusedStages: c.Bool("skip-unused-stages"), CacheDir: c.String("cache-dir"), CacheCopyLayers: c.Bool("cache-copy-layers"), CacheRunLayers: c.Bool("cache-run-layers"), Cleanup: c.Bool("cleanup"), ContextSubPath: c.String("context-sub-path"), - CustomPlatform: c.String("custom-platform"), Force: c.Bool("force"), ImageNameWithDigestFile: c.String("image-name-with-digest-file"), ImageNameTagWithDigestFile: c.String("image-name-tag-with-digest-file"), diff --git a/cmd/kaniko-gcr/main.go b/cmd/kaniko-gcr/main.go index 675f85d..19dab7f 100644 --- a/cmd/kaniko-gcr/main.go +++ b/cmd/kaniko-gcr/main.go @@ -176,7 +176,7 @@ func main() { cli.StringFlag{ Name: "platform", Usage: "Allows to build with another default platform than the host, similarly to docker build --platform", - EnvVar: "PLUGIN_PLATFORM", + EnvVar: "PLUGIN_PLATFORM,PLUGIN_CUSTOM_PLATFORM", }, cli.BoolFlag{ Name: "skip-unused-stages", @@ -214,11 +214,6 @@ func main() { Usage: "Sub-path within the context to build.", EnvVar: "PLUGIN_CONTEXT_SUB_PATH", }, - cli.StringFlag{ - Name: "custom-platform", - Usage: "Platform to use for building.", - EnvVar: "PLUGIN_CUSTOM_PLATFORM", - }, cli.BoolFlag{ Name: "force", Usage: "Force building the image even if it already exists.", @@ -404,14 +399,13 @@ func run(c *cli.Context) error { DigestFile: defaultDigestFile, NoPush: noPush, Verbosity: c.String("verbosity"), - Platform: c.String("platform"), + CustomPlatform: c.String("platform"), SkipUnusedStages: c.Bool("skip-unused-stages"), CacheDir: c.String("cache-dir"), CacheCopyLayers: c.Bool("cache-copy-layers"), CacheRunLayers: c.Bool("cache-run-layers"), Cleanup: c.Bool("cleanup"), ContextSubPath: c.String("context-sub-path"), - CustomPlatform: c.String("custom-platform"), Force: c.Bool("force"), ImageNameWithDigestFile: c.String("image-name-with-digest-file"), ImageNameTagWithDigestFile: c.String("image-name-tag-with-digest-file"), diff --git a/kaniko.go b/kaniko.go index 4eb0448..51f3dd6 100644 --- a/kaniko.go +++ b/kaniko.go @@ -37,7 +37,6 @@ type ( Labels []string // Label map Mirrors []string // Docker repository mirrors NoPush bool // Set this flag if you only want to build the image, without pushing to a registry - Platform string // Allows to build with another default platform than the host, similarly to docker build --platform PushOnly bool // Specify if the operation is push-only. Repo string // Docker build repository SkipTlsVerify bool // Docker skip tls certificate verify for registry @@ -311,10 +310,6 @@ func (p Plugin) Exec() error { cmdArgs = append(cmdArgs, fmt.Sprintf("--verbosity=%s", p.Build.Verbosity)) } - if p.Build.Platform != "" { - cmdArgs = append(cmdArgs, fmt.Sprintf("--customPlatform=%s", p.Build.Platform)) - } - if p.Build.SkipUnusedStages { cmdArgs = append(cmdArgs, "--skip-unused-stages") } diff --git a/kaniko_test.go b/kaniko_test.go index 0e97196..7c8509a 100644 --- a/kaniko_test.go +++ b/kaniko_test.go @@ -283,6 +283,57 @@ func TestTarPathValidation(t *testing.T) { } } +func TestCustomPlatformFlag(t *testing.T) { + tests := []struct { + name string + customPlatform string + expectFlag bool + }{ + { + name: "with_custom_platform", + customPlatform: "linux/amd64", + expectFlag: true, + }, + { + name: "with_custom_platform_arm", + customPlatform: "linux/arm64", + expectFlag: true, + }, + { + name: "empty_custom_platform", + customPlatform: "", + expectFlag: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := Plugin{ + Build: Build{ + Dockerfile: "Dockerfile", + Context: ".", + Repo: "test/repo", + Tags: []string{"latest"}, + CustomPlatform: tt.customPlatform, + NoPush: true, // Don't actually push + }, + } + + // We can't actually run Exec() without kaniko installed, + // but we can verify the logic by checking the field is set correctly + if tt.expectFlag && p.Build.CustomPlatform == "" { + t.Errorf("Expected CustomPlatform to be set to %q, but got empty string", tt.customPlatform) + } + if !tt.expectFlag && p.Build.CustomPlatform != "" { + t.Errorf("Expected CustomPlatform to be empty, but got %q", p.Build.CustomPlatform) + } + if tt.expectFlag && p.Build.CustomPlatform != tt.customPlatform { + t.Errorf("Expected CustomPlatform to be %q, but got %q", tt.customPlatform, p.Build.CustomPlatform) + } + }) + } +} + func TestSourceTarballPush(t *testing.T) { tests := []struct { name string