From 0563fa649ee58e88c6ccff7a440b477bb6ea7572 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 22 Aug 2023 10:08:20 +0200 Subject: [PATCH 1/2] cli/command/system/runInfo: set negotiated API version when available The current logic was ignoring (e.g.) `--format=json` formats, and was only setting the negotiated API version if no format was specified. While we do want to avoid calling the API if possible, we do already check if the given template requires a server connection, so let's move updating the API version to that block. Signed-off-by: Sebastiaan van Stijn --- cli/command/system/info.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cli/command/system/info.go b/cli/command/system/info.go index a3669802b835..0bb611bcc108 100644 --- a/cli/command/system/info.go +++ b/cli/command/system/info.go @@ -101,11 +101,14 @@ func runInfo(ctx context.Context, cmd *cobra.Command, dockerCli command.Cli, opt var serverConnErr error if needsServerInfo(opts.format, info) { serverConnErr = addServerInfo(ctx, dockerCli, opts.format, &info) + if serverConnErr == nil { + // Update client API version after it was negotiated. + info.ClientInfo.APIVersion = dockerCli.CurrentVersion() + } } if opts.format == "" { info.UserName = dockerCli.ConfigFile().AuthConfigs[registry.IndexServer].Username - info.ClientInfo.APIVersion = dockerCli.CurrentVersion() return errors.Join(prettyPrintInfo(dockerCli, info), serverConnErr) } From c0482fddd9297e97b40ee9c279d631d8d89d074f Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 22 Aug 2023 10:02:51 +0200 Subject: [PATCH 2/2] cli/command/system/newClientVersion: initialize with default API version Set the APIVersion and DefaultAPIVersion fields to the default version, as that's the version the client assumes without making a API connection to do version negotiation. One change worth mentioning is that this means that the API version will differ, depending on the format: If no server information is fetched: docker info --format='{{ json .ClientInfo }}' | jq .ApiVersion "1.44" If server information is fetched: docker info --format='{{ json .}}' | jq .ClientInfo.ApiVersion "1.43" An alternative could be to leave the ApiVersion field empty if no negotiation took place. Signed-off-by: Sebastiaan van Stijn --- cli/command/system/version.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/command/system/version.go b/cli/command/system/version.go index 3a0ad75c351e..e2e796c3b115 100644 --- a/cli/command/system/version.go +++ b/cli/command/system/version.go @@ -91,6 +91,7 @@ type clientVersion struct { func newClientVersion(contextName string, dockerCli command.Cli) clientVersion { v := clientVersion{ Version: version.Version, + APIVersion: api.DefaultVersion, DefaultAPIVersion: api.DefaultVersion, GoVersion: runtime.Version(), GitCommit: version.GitCommit,