From df8cc7af338c9e21d8ecf3c4321cfafceacf5cd6 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Wed, 3 May 2023 15:33:06 +0200 Subject: [PATCH] remote: return better connect error We have a spacial logic to create a better user error that hints at podman machine, however because we string matched it missed the case of the ssh connection. Stop doing string comparison and return a proper error and match it with errors.As() [NO NEW TESTS NEEDED] see https://github.com/containers/podman/discussions/18426 Signed-off-by: Paul Holzinger --- cmd/podman/root.go | 3 ++- pkg/bindings/connection.go | 25 +++++++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/cmd/podman/root.go b/cmd/podman/root.go index d4de64f4b5..cc440ca8f1 100644 --- a/cmd/podman/root.go +++ b/cmd/podman/root.go @@ -17,6 +17,7 @@ import ( "github.com/containers/podman/v4/cmd/podman/registry" "github.com/containers/podman/v4/cmd/podman/validate" "github.com/containers/podman/v4/libpod/define" + "github.com/containers/podman/v4/pkg/bindings" "github.com/containers/podman/v4/pkg/checkpoint/crutils" "github.com/containers/podman/v4/pkg/domain/entities" "github.com/containers/podman/v4/pkg/parallel" @@ -109,7 +110,7 @@ func Execute() { registry.SetExitCode(define.ExecErrorCodeGeneric) } if registry.IsRemote() { - if strings.Contains(err.Error(), "unable to connect to Podman") { + if errors.As(err, &bindings.ConnectError{}) { fmt.Fprintln(os.Stderr, "Cannot connect to Podman. Please verify your connection to the Linux system using `podman system connection list`, or try `podman machine init` and `podman machine start` to manage a new Linux VM") } } diff --git a/pkg/bindings/connection.go b/pkg/bindings/connection.go index 7bd1ff4849..d3bd5e8a8e 100644 --- a/pkg/bindings/connection.go +++ b/pkg/bindings/connection.go @@ -37,6 +37,22 @@ const ( versionKey = valueKey("ServiceVersion") ) +type ConnectError struct { + Err error +} + +func (c ConnectError) Error() string { + return "unable to connect to Podman socket: " + c.Err.Error() +} + +func (c ConnectError) Unwrap() error { + return c.Err +} + +func newConnectError(err error) error { + return ConnectError{Err: err} +} + // GetClient from context build by NewConnection() func GetClient(ctx context.Context) (*Connection, error) { if c, ok := ctx.Value(clientKey).(*Connection); ok { @@ -107,7 +123,7 @@ func NewConnectionWithIdentity(ctx context.Context, uri string, identity string, InsecureIsMachineConnection: machine, }, "golang") if err != nil { - return nil, err + return nil, newConnectError(err) } connection = Connection{URI: _url} connection.Client = &http.Client{ @@ -129,20 +145,17 @@ func NewConnectionWithIdentity(ctx context.Context, uri string, identity string, } conn, err := tcpClient(_url) if err != nil { - return nil, err + return nil, newConnectError(err) } connection = conn default: return nil, fmt.Errorf("unable to create connection. %q is not a supported schema", _url.Scheme) } - if err != nil { - return nil, fmt.Errorf("unable to connect to Podman. failed to create %sClient: %w", _url.Scheme, err) - } ctx = context.WithValue(ctx, clientKey, &connection) serviceVersion, err := pingNewConnection(ctx) if err != nil { - return nil, fmt.Errorf("unable to connect to Podman socket: %w", err) + return nil, newConnectError(err) } ctx = context.WithValue(ctx, versionKey, serviceVersion) return ctx, nil