mirror of https://github.com/containers/podman.git
Merge pull request #18441 from Luap99/remote-connect-err
remote: return better connect error
This commit is contained in:
commit
b98960d1cb
|
@ -17,6 +17,7 @@ import (
|
||||||
"github.com/containers/podman/v4/cmd/podman/registry"
|
"github.com/containers/podman/v4/cmd/podman/registry"
|
||||||
"github.com/containers/podman/v4/cmd/podman/validate"
|
"github.com/containers/podman/v4/cmd/podman/validate"
|
||||||
"github.com/containers/podman/v4/libpod/define"
|
"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/checkpoint/crutils"
|
||||||
"github.com/containers/podman/v4/pkg/domain/entities"
|
"github.com/containers/podman/v4/pkg/domain/entities"
|
||||||
"github.com/containers/podman/v4/pkg/parallel"
|
"github.com/containers/podman/v4/pkg/parallel"
|
||||||
|
@ -109,7 +110,7 @@ func Execute() {
|
||||||
registry.SetExitCode(define.ExecErrorCodeGeneric)
|
registry.SetExitCode(define.ExecErrorCodeGeneric)
|
||||||
}
|
}
|
||||||
if registry.IsRemote() {
|
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")
|
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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,22 @@ const (
|
||||||
versionKey = valueKey("ServiceVersion")
|
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()
|
// GetClient from context build by NewConnection()
|
||||||
func GetClient(ctx context.Context) (*Connection, error) {
|
func GetClient(ctx context.Context) (*Connection, error) {
|
||||||
if c, ok := ctx.Value(clientKey).(*Connection); ok {
|
if c, ok := ctx.Value(clientKey).(*Connection); ok {
|
||||||
|
@ -107,7 +123,7 @@ func NewConnectionWithIdentity(ctx context.Context, uri string, identity string,
|
||||||
InsecureIsMachineConnection: machine,
|
InsecureIsMachineConnection: machine,
|
||||||
}, "golang")
|
}, "golang")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, newConnectError(err)
|
||||||
}
|
}
|
||||||
connection = Connection{URI: _url}
|
connection = Connection{URI: _url}
|
||||||
connection.Client = &http.Client{
|
connection.Client = &http.Client{
|
||||||
|
@ -129,20 +145,17 @@ func NewConnectionWithIdentity(ctx context.Context, uri string, identity string,
|
||||||
}
|
}
|
||||||
conn, err := tcpClient(_url)
|
conn, err := tcpClient(_url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, newConnectError(err)
|
||||||
}
|
}
|
||||||
connection = conn
|
connection = conn
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unable to create connection. %q is not a supported schema", _url.Scheme)
|
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)
|
ctx = context.WithValue(ctx, clientKey, &connection)
|
||||||
serviceVersion, err := pingNewConnection(ctx)
|
serviceVersion, err := pingNewConnection(ctx)
|
||||||
if err != nil {
|
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)
|
ctx = context.WithValue(ctx, versionKey, serviceVersion)
|
||||||
return ctx, nil
|
return ctx, nil
|
||||||
|
|
Loading…
Reference in New Issue