From 192714b00d164c0a28d2ff9d47c16114edad0c0d Mon Sep 17 00:00:00 2001 From: Nimesh Kulkarni <167794819+GitNimay@users.noreply.github.com> Date: Mon, 19 Oct 2026 22:55:06 +0530 Subject: [PATCH] cli/command/manifest: allow unsetting variant and os-version in annotate "docker manifest annotate" ignored the --variant and --os-version flags when passed an empty value, making it impossible to remove a variant or OS version from a manifest entry. Apply these flags whenever they are set, so that an empty value unsets the field. Fixes https://github.com/docker/cli/issues/4352 Signed-off-by: Nimesh Kulkarni <167794819+GitNimay@users.noreply.github.com> --- cli/command/manifest/annotate.go | 11 ++++++-- cli/command/manifest/annotate_test.go | 40 +++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/cli/command/manifest/annotate.go b/cli/command/manifest/annotate.go index a9507deb73f0..46f91c0332b8 100644 --- a/cli/command/manifest/annotate.go +++ b/cli/command/manifest/annotate.go @@ -28,6 +28,11 @@ type annotateOptions struct { arch string osFeatures []string osVersion string + + // variantChanged and osVersionChanged are set if the flags were passed, + // so that an empty value can be used to unset them. + variantChanged bool + osVersionChanged bool } // manifestStoreProvider is used in tests to provide a dummy store. @@ -84,6 +89,8 @@ func newAnnotateCommand(dockerCLI command.Cli) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { opts.target = args[0] opts.image = args[1] + opts.variantChanged = cmd.Flags().Changed("variant") + opts.osVersionChanged = cmd.Flags().Changed("os-version") return runManifestAnnotate(dockerCLI, opts) }, DisableFlagsInUseLine: true, @@ -132,10 +139,10 @@ func runManifestAnnotate(dockerCLI command.Cli, opts annotateOptions) error { for _, osFeature := range opts.osFeatures { imageManifest.Descriptor.Platform.OSFeatures = appendIfUnique(imageManifest.Descriptor.Platform.OSFeatures, osFeature) } - if opts.variant != "" { + if opts.variantChanged { imageManifest.Descriptor.Platform.Variant = opts.variant } - if opts.osVersion != "" { + if opts.osVersionChanged { imageManifest.Descriptor.Platform.OSVersion = opts.osVersion } diff --git a/cli/command/manifest/annotate_test.go b/cli/command/manifest/annotate_test.go index f2c924e45ffc..a3382f020a57 100644 --- a/cli/command/manifest/annotate_test.go +++ b/cli/command/manifest/annotate_test.go @@ -79,3 +79,43 @@ func TestManifestAnnotate(t *testing.T) { expected := golden.Get(t, "inspect-annotate.golden") assert.Check(t, is.Equal(string(expected), actual.String())) } + +func TestManifestAnnotateUnsetFields(t *testing.T) { + manifestStore := store.NewStore(t.TempDir()) + + cli := test.NewFakeCli(nil) + cli.SetManifestStore(manifestStore) + listRef := ref(t, "list:v1") + namedRef := ref(t, "alpine:3.0") + err := manifestStore.Save(listRef, namedRef, fullImageManifest(t, namedRef)) + assert.NilError(t, err) + + annotate := func(args ...string) { + t.Helper() + cmd := newAnnotateCommand(cli) + cmd.SetArgs(append([]string{"example.com/list:v1", "example.com/alpine:3.0"}, args...)) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) + assert.NilError(t, cmd.Execute()) + } + + annotate("--os", "linux", "--arch", "arm", "--variant", "v7", "--os-version", "1") + imageManifest, err := manifestStore.Get(listRef, namedRef) + assert.NilError(t, err) + assert.Check(t, is.Equal(imageManifest.Descriptor.Platform.Variant, "v7")) + assert.Check(t, is.Equal(imageManifest.Descriptor.Platform.OSVersion, "1")) + + // Not passing the flags should preserve the existing values. + annotate() + imageManifest, err = manifestStore.Get(listRef, namedRef) + assert.NilError(t, err) + assert.Check(t, is.Equal(imageManifest.Descriptor.Platform.Variant, "v7")) + assert.Check(t, is.Equal(imageManifest.Descriptor.Platform.OSVersion, "1")) + + // Passing an empty value should unset them. + annotate("--variant", "", "--os-version", "") + imageManifest, err = manifestStore.Get(listRef, namedRef) + assert.NilError(t, err) + assert.Check(t, is.Equal(imageManifest.Descriptor.Platform.Variant, "")) + assert.Check(t, is.Equal(imageManifest.Descriptor.Platform.OSVersion, "")) +}