From 2f5698511a26e51eaa8a2c51bd91de56f348b699 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 4 Nov 2022 10:22:40 +0100 Subject: [PATCH] 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 --- cli/command/cli.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/cli/command/cli.go b/cli/command/cli.go index b5c1ccd996..ed9cb6cc3c 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -28,7 +28,6 @@ import ( "github.com/docker/docker/api/types/registry" "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/client" - "github.com/docker/docker/errdefs" "github.com/docker/go-connections/tlsconfig" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -222,7 +221,7 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize return ResolveDefaultContext(opts, cli.contextStoreConfig) }, } - cli.currentContext, err = resolveContextName(opts, cli.configFile, cli.contextStore) + cli.currentContext, err = resolveContextName(opts, cli.configFile) if err != nil { return err } @@ -250,7 +249,7 @@ func NewAPIClientFromFlags(opts *cliflags.ClientOptions, configFile *configfile. return ResolveDefaultContext(opts, storeConfig) }, } - contextName, err := resolveContextName(opts, configFile, contextStore) + contextName, err := resolveContextName(opts, configFile) if err != nil { return nil, err } @@ -445,7 +444,10 @@ func UserAgent() string { // - if DOCKER_CONTEXT is set, 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 -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 { 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 } if config != nil && config.CurrentContext != "" { - _, err := contextstore.GetMetadata(config.CurrentContext) - if errdefs.IsNotFound(err) { - 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 + // We don't validate if this context exists: errors may occur when trying to use it. + return config.CurrentContext, nil } return DefaultContextName, nil }