cli/command: resolveContextName() don't validate if context exists

resolveContextName() is used to find which context to use, based on the
available configuration options. Once resolved, the context name is
used to load the actual context, which will fail if the context doesn't
exist, so there's no need to produce an error at this stage; only
check priority of the configuration options to pick the context
with the highest priority.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-11-04 10:22:40 +01:00
parent de52868abb
commit 2f5698511a
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 8 additions and 9 deletions

View File

@ -28,7 +28,6 @@ import (
"github.com/docker/docker/api/types/registry" "github.com/docker/docker/api/types/registry"
"github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/docker/docker/errdefs"
"github.com/docker/go-connections/tlsconfig" "github.com/docker/go-connections/tlsconfig"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -222,7 +221,7 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize
return ResolveDefaultContext(opts, cli.contextStoreConfig) return ResolveDefaultContext(opts, cli.contextStoreConfig)
}, },
} }
cli.currentContext, err = resolveContextName(opts, cli.configFile, cli.contextStore) cli.currentContext, err = resolveContextName(opts, cli.configFile)
if err != nil { if err != nil {
return err return err
} }
@ -250,7 +249,7 @@ func NewAPIClientFromFlags(opts *cliflags.ClientOptions, configFile *configfile.
return ResolveDefaultContext(opts, storeConfig) return ResolveDefaultContext(opts, storeConfig)
}, },
} }
contextName, err := resolveContextName(opts, configFile, contextStore) contextName, err := resolveContextName(opts, configFile)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -445,7 +444,10 @@ func UserAgent() string {
// - if DOCKER_CONTEXT is set, use this value // - if DOCKER_CONTEXT is set, use this value
// - if Config file has a globally set "CurrentContext", use this value // - if Config file has a globally set "CurrentContext", use this value
// - fallbacks to default HOST, uses TLS config from flags/env vars // - fallbacks to default HOST, uses TLS config from flags/env vars
func resolveContextName(opts *cliflags.ClientOptions, config *configfile.ConfigFile, contextstore store.Reader) (string, error) { //
// resolveContextName does not validate if the given context exists; errors may
// occur when trying to use it.
func resolveContextName(opts *cliflags.ClientOptions, config *configfile.ConfigFile) (string, error) {
if opts.Context != "" && len(opts.Hosts) > 0 { if opts.Context != "" && len(opts.Hosts) > 0 {
return "", errors.New("Conflicting options: either specify --host or --context, not both") return "", errors.New("Conflicting options: either specify --host or --context, not both")
} }
@ -462,11 +464,8 @@ func resolveContextName(opts *cliflags.ClientOptions, config *configfile.ConfigF
return ctxName, nil return ctxName, nil
} }
if config != nil && config.CurrentContext != "" { if config != nil && config.CurrentContext != "" {
_, err := contextstore.GetMetadata(config.CurrentContext) // We don't validate if this context exists: errors may occur when trying to use it.
if errdefs.IsNotFound(err) { return config.CurrentContext, nil
return "", errors.Errorf("current context %q is not found on the file system, please check your config file at %s", config.CurrentContext, config.Filename)
}
return config.CurrentContext, err
} }
return DefaultContextName, nil return DefaultContextName, nil
} }