Skip to content

Commit 47e2011

Browse files
committed
cli/trust: TagTrusted: lazily request APIClient
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. 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 47e2011

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
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

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package container
2+
3+
import (
4+
"context"
5+
6+
"github.com/distribution/reference"
7+
"github.com/docker/cli/cli/command"
8+
"github.com/docker/cli/cli/trust"
9+
)
10+
11+
func tagTrusted(ctx context.Context, cli command.Cli, trustedRef reference.Canonical, ref reference.NamedTagged) error {
12+
return trust.TagTrusted(ctx, cli, cli.Err(), trustedRef, ref)
13+
}

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-7
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
}
@@ -233,9 +229,16 @@ func convertTarget(t client.Target) (target, error) {
233229
// that updates the given image references to their familiar format for tagging
234230
// and printing.
235231
//
236-
// Deprecated: this function was only used internally, and will be removed in the next release.
232+
// Deprecated: use [trust.TagTrusted] instead. This function was only used internally, and will be removed in the next release.
237233
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)
234+
return tagTrusted(ctx, cli, trustedRef, ref)
235+
}
236+
237+
// tagTrusted tags a trusted ref. It is a shallow wrapper around APIClient.ImageTag
238+
// that updates the given image references to their familiar format for tagging
239+
// and printing.
240+
func tagTrusted(ctx context.Context, cli command.Cli, trustedRef reference.Canonical, ref reference.NamedTagged) error {
241+
return trust.TagTrusted(ctx, cli, cli.Err(), trustedRef, ref)
239242
}
240243

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

cli/trust/trust_tag.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ 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
1721
familiarRef := reference.FamiliarString(ref)
1822
trustedFamiliarRef := reference.FamiliarString(trustedRef)
1923

2024
_, _ = fmt.Fprintf(out, "Tagging %s as %s\n", trustedFamiliarRef, familiarRef)
21-
return apiClient.ImageTag(ctx, trustedFamiliarRef, familiarRef)
25+
return cli.Client().ImageTag(ctx, trustedFamiliarRef, familiarRef)
2226
}

0 commit comments

Comments
 (0)