Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

system/info: failure to connect to docker socket should propagate error #5918

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions cli/command/system/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/docker/cli/templates"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/api/types/system"
"github.com/docker/docker/client"
"github.com/docker/docker/registry"
"github.com/docker/go-units"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -97,30 +98,46 @@ func runInfo(ctx context.Context, cmd *cobra.Command, dockerCli command.Cli, opt
info.ClientErrors = append(info.ClientErrors, err.Error())
}

var serverConnErr error
if needsServerInfo(opts.format, info) {
if dinfo, err := dockerCli.Client().Info(ctx); err == nil {
info.Info = &dinfo
} else {
serverConnErr = addServerInfo(ctx, dockerCli, opts.format, &info)
}

if opts.format == "" {
info.UserName = dockerCli.ConfigFile().AuthConfigs[registry.IndexServer].Username
info.ClientInfo.APIVersion = dockerCli.CurrentVersion()
return errors.Join(prettyPrintInfo(dockerCli, info), serverConnErr)
}

return errors.Join(serverConnErr, formatInfo(dockerCli.Out(), info, opts.format))
}

// addServerInfo retrieves the server information and adds it to the dockerInfo struct.
// if a connection error occurs, it will be returned as an error.
// other errors are appended to the info.ServerErrors field.
func addServerInfo(ctx context.Context, dockerCli command.Cli, format string, info *dockerInfo) error {
dinfo, err := dockerCli.Client().Info(ctx)
if err != nil {
// if no format is provided and we have an error, don't print the server info
if format == "" {
info.Info = nil
}
if !client.IsErrConnectionFailed(err) {
info.ServerErrors = append(info.ServerErrors, err.Error())
if opts.format == "" {
// reset the server info to prevent printing "empty" Server info
// and warnings, but don't reset it if a custom format was specified
// to prevent errors from Go's template parsing during format.
info.Info = nil
} else {
if format != "" {
// if a format is provided, print the error, as it may be hidden
// otherwise if the template doesn't include the ServerErrors field.
// if the format template doesn't include the ServerErrors field.
fprintln(dockerCli.Err(), err)
}
return nil
}
// on a server connection error, we want to return an error
return err
}

if opts.format == "" {
info.UserName = dockerCli.ConfigFile().AuthConfigs[registry.IndexServer].Username
info.ClientInfo.APIVersion = dockerCli.CurrentVersion()
return prettyPrintInfo(dockerCli, info)
}
return formatInfo(dockerCli.Out(), info, opts.format)
// only assign the server info if we have no error
info.Info = &dinfo
return nil
}

// placeHolders does a rudimentary match for possible placeholders in a
Expand Down
Loading