mirror of
https://github.com/drone/drone-kaniko.git
synced 2026-06-04 18:23:49 +08:00
fix: Consolidate platform flags to use --custom-platform and eliminate deprecation warning
This commit is contained in:
@@ -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"),
|
||||
|
||||
@@ -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"),
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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"),
|
||||
|
||||
@@ -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"),
|
||||
|
||||
@@ -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"),
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user