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 <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2023-05-03 15:33:06 +02:00
parent f149d49335
commit df8cc7af33
2 changed files with 21 additions and 7 deletions

View File

@ -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")
}
}

View File

@ -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