Skip to content

Commit 1e32cb2

Browse files
committedMar 8, 2025
cli/trust: TagTrusted: remove, and inline code
Commit e37d814 moved the image.TagTrusted function to the trust package, but changed the signature slightly to accept an API client, instead of requiring the command.Cli. However, this could result in situations where the Client obtained from the CLI was not correctly initialized, resulting in failures in our e2e test; === FAIL: e2e/global TestPromptExitCode/plugin_upgrade (9.14s) cli_test.go:203: assertion failed: Command: docker plugin push registry:5000/plugin-content-trust-upgrade:next ExitCode: 1 Error: exit status 1 Stdout: The push refers to repository [registry:5000/plugin-content-trust-upgrade] 24ec5b45d59b: Preparing 6a594992d358: Preparing 224414d1b129: Preparing 24ec5b45d59b: Preparing 6a594992d358: Preparing 224414d1b129: Preparing Stderr: error pushing plugin: failed to do request: Head "https://registry:5000/v2/plugin-content-trust-upgrade/blobs/sha256:6a594992d358facbbc4ab134bbbba77cb91e0adee6ff0d6103403ff94a9b796c": http: server gave HTTP response to HTTPS client Failures: ExitCode was 1 expected 0 Expected no error This patch changes the signature to accept an "APIClientProvider" so that the Client is obtained the moment when used. We should look what exactly causes this situation, and if we can make sure that requesting the `Client()` will always produce the client with the expected configuration. While looking at the code, I also noticed that Client.ImageTag already parses and normalizes tags given, so we don't need to convert them to their "familiar" form, other than for printing the message; https://github.com/moby/moby/blob/b4bdf12daec84caaf809a639f923f7370d4926ad/client/image_tag.go#L11-L37 With that taken into account, trust.TrustedPush has no real value, other than printing an informational message, so removing the function and inlining it in the locations where it's used. WARNING: looks like the test is still flaky after this change, so it may just be a bad test, or tests affecting each-other (same port, but different config?). That said; these changes may still be ok to include. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 2eec746 commit 1e32cb2

File tree

5 files changed

+35
-18
lines changed

5 files changed

+35
-18
lines changed
 

‎cli/command/container/create.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/docker/cli/cli/command/image"
1616
"github.com/docker/cli/cli/internal/jsonstream"
1717
"github.com/docker/cli/cli/streams"
18-
"github.com/docker/cli/cli/trust"
1918
"github.com/docker/cli/opts"
2019
"github.com/docker/docker/api/types/container"
2120
imagetypes "github.com/docker/docker/api/types/image"
@@ -243,7 +242,7 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
243242
return err
244243
}
245244
if taggedRef, ok := namedRef.(reference.NamedTagged); ok && trustedRef != nil {
246-
return trust.TagTrusted(ctx, dockerCli.Client(), dockerCli.Err(), trustedRef, taggedRef)
245+
return tagTrusted(ctx, dockerCli, trustedRef, taggedRef)
247246
}
248247
return nil
249248
}

‎cli/command/container/trust.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package container
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/distribution/reference"
8+
"github.com/docker/cli/cli/command"
9+
)
10+
11+
// tagTrusted tags a trusted ref. It is a shallow wrapper around APIClient.ImageTag
12+
// that updates the given image references to their familiar format for printing.
13+
func tagTrusted(ctx context.Context, cli command.Cli, trustedRef reference.Canonical, ref reference.NamedTagged) error {
14+
_, _ = fmt.Fprintf(cli.Err(), "Tagging %s as %s\n", reference.FamiliarString(trustedRef), reference.FamiliarString(ref))
15+
return cli.Client().ImageTag(ctx, trustedRef.String(), ref.String())
16+
}

‎cli/command/image/build.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/docker/cli/cli/command/image/build"
2323
"github.com/docker/cli/cli/internal/jsonstream"
2424
"github.com/docker/cli/cli/streams"
25-
"github.com/docker/cli/cli/trust"
2625
"github.com/docker/cli/opts"
2726
"github.com/docker/docker/api"
2827
"github.com/docker/docker/api/types"
@@ -407,7 +406,7 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
407406
// Since the build was successful, now we must tag any of the resolved
408407
// images from the above Dockerfile rewrite.
409408
for _, resolved := range resolvedTags {
410-
if err := trust.TagTrusted(ctx, dockerCli.Client(), dockerCli.Err(), resolved.digestRef, resolved.tagRef); err != nil {
409+
if err := tagTrusted(ctx, dockerCli, resolved.digestRef, resolved.tagRef); err != nil {
411410
return err
412411
}
413412
}

‎cli/command/image/trust.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,7 @@ func trustedPull(ctx context.Context, cli command.Cli, imgRefAndAuth trust.Image
104104
return err
105105
}
106106

107-
// Use familiar references when interacting with client and output
108-
familiarRef := reference.FamiliarString(tagged)
109-
trustedFamiliarRef := reference.FamiliarString(trustedRef)
110-
_, _ = fmt.Fprintf(cli.Err(), "Tagging %s as %s\n", trustedFamiliarRef, familiarRef)
111-
if err := cli.Client().ImageTag(ctx, trustedFamiliarRef, familiarRef); err != nil {
107+
if err := tagTrusted(ctx, cli, trustedRef, tagged); err != nil {
112108
return err
113109
}
114110
}
@@ -230,12 +226,18 @@ func convertTarget(t client.Target) (target, error) {
230226
}
231227

232228
// TagTrusted tags a trusted ref. It is a shallow wrapper around APIClient.ImageTag
233-
// that updates the given image references to their familiar format for tagging
234-
// and printing.
229+
// that updates the given image references to their familiar format for printing.
235230
//
236231
// Deprecated: this function was only used internally, and will be removed in the next release.
237232
func TagTrusted(ctx context.Context, cli command.Cli, trustedRef reference.Canonical, ref reference.NamedTagged) error {
238-
return trust.TagTrusted(ctx, cli.Client(), cli.Err(), trustedRef, ref)
233+
return tagTrusted(ctx, cli, trustedRef, ref)
234+
}
235+
236+
// tagTrusted tags a trusted ref. It is a shallow wrapper around APIClient.ImageTag
237+
// that updates the given image references to their familiar format for printing.
238+
func tagTrusted(ctx context.Context, cli command.Cli, trustedRef reference.Canonical, ref reference.NamedTagged) error {
239+
_, _ = fmt.Fprintf(cli.Err(), "Tagging %s as %s\n", reference.FamiliarString(trustedRef), reference.FamiliarString(ref))
240+
return cli.Client().ImageTag(ctx, trustedRef.String(), ref.String())
239241
}
240242

241243
// AuthResolver returns an auth resolver function from a command.Cli

‎cli/trust/trust_tag.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import (
99
"github.com/docker/docker/client"
1010
)
1111

12+
type APIClientProvider interface {
13+
Client() client.APIClient
14+
}
15+
1216
// TagTrusted tags a trusted ref. It is a shallow wrapper around [client.Client.ImageTag]
1317
// that updates the given image references to their familiar format for tagging
1418
// and printing.
15-
func TagTrusted(ctx context.Context, apiClient client.ImageAPIClient, out io.Writer, trustedRef reference.Canonical, ref reference.NamedTagged) error {
19+
func TagTrusted(ctx context.Context, cli APIClientProvider, out io.Writer, trustedRef reference.Canonical, ref reference.NamedTagged) error {
1620
// Use familiar references when interacting with client and output
17-
familiarRef := reference.FamiliarString(ref)
18-
trustedFamiliarRef := reference.FamiliarString(trustedRef)
19-
20-
_, _ = fmt.Fprintf(out, "Tagging %s as %s\n", trustedFamiliarRef, familiarRef)
21-
return apiClient.ImageTag(ctx, trustedFamiliarRef, familiarRef)
21+
_, _ = fmt.Fprintf(out, "Tagging %s as %s\n", reference.FamiliarString(trustedRef), reference.FamiliarString(ref))
22+
return cli.Client().ImageTag(ctx, trustedRef.String(), ref.String())
2223
}

0 commit comments

Comments
 (0)