From 374d0f88cd101d76b5117946ed36d9f8e2e7d2ba Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 29 Mar 2022 10:41:43 +0200 Subject: [PATCH] cli: initializeFromClient(): detect swarm status from ping (if available) Signed-off-by: Sebastiaan van Stijn --- cli/command/cli.go | 16 ++++++++++++++-- cli/command/cli_test.go | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cli/command/cli.go b/cli/command/cli.go index 6057bc1dd1..2e4c0b55a6 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -25,7 +25,8 @@ import ( dopts "github.com/docker/cli/opts" "github.com/docker/docker/api" "github.com/docker/docker/api/types" - registrytypes "github.com/docker/docker/api/types/registry" + "github.com/docker/docker/api/types/registry" + "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/client" "github.com/docker/go-connections/tlsconfig" "github.com/moby/term" @@ -132,6 +133,7 @@ func (cli *DockerCli) loadConfigFile() { // ServerInfo returns the server version details for the host this client is // connected to func (cli *DockerCli) ServerInfo() ServerInfo { + // TODO(thaJeztah) make ServerInfo() lazily load the info (ping only when needed) return cli.serverInfo } @@ -170,7 +172,7 @@ func (cli *DockerCli) ManifestStore() manifeststore.Store { // RegistryClient returns a client for communicating with a Docker distribution // registry func (cli *DockerCli) RegistryClient(allowInsecure bool) registryclient.RegistryClient { - resolver := func(ctx context.Context, index *registrytypes.IndexInfo) types.AuthConfig { + resolver := func(ctx context.Context, index *registry.IndexInfo) types.AuthConfig { return ResolveAuthConfig(ctx, cli, index) } return registryclient.NewRegistryClient(resolver, UserAgent(), allowInsecure) @@ -336,6 +338,7 @@ func (cli *DockerCli) initializeFromClient() { HasExperimental: ping.Experimental, OSType: ping.OSType, BuildkitVersion: ping.BuilderVersion, + SwarmStatus: ping.SwarmStatus, } cli.client.NegotiateAPIVersionPing(ping) } @@ -376,6 +379,15 @@ type ServerInfo struct { HasExperimental bool OSType string BuildkitVersion types.BuilderVersion + + // SwarmStatus provides information about the current swarm status of the + // engine, obtained from the "Swarm" header in the API response. + // + // It can be a nil struct if the API version does not provide this header + // in the ping response, or if an error occurred, in which case the client + // should use other ways to get the current swarm status, such as the /swarm + // endpoint. + SwarmStatus *swarm.Status } // NewDockerCli returns a DockerCli instance with all operators applied on it. diff --git a/cli/command/cli_test.go b/cli/command/cli_test.go index cc642e5420..ba157ebe4b 100644 --- a/cli/command/cli_test.go +++ b/cli/command/cli_test.go @@ -159,7 +159,7 @@ func TestInitializeFromClient(t *testing.T) { cli := &DockerCli{client: apiclient} cli.initializeFromClient() - assert.DeepEqual(t, cli.serverInfo, testcase.expectedServer) + assert.DeepEqual(t, cli.ServerInfo(), testcase.expectedServer) assert.Equal(t, apiclient.negotiated, testcase.negotiated) }) }