Merge pull request #3715 from thaJeztah/context_cleanup_part1a

cli/command: remove unused args from ResolveDefaultContext()
This commit is contained in:
Sebastiaan van Stijn 2022-07-27 23:02:53 +02:00 committed by GitHub
commit a445d97c25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 39 deletions

View File

@ -216,7 +216,7 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize
cli.contextStore = &ContextStoreWithDefault{ cli.contextStore = &ContextStoreWithDefault{
Store: baseContextStore, Store: baseContextStore,
Resolver: func() (*DefaultContext, error) { Resolver: func() (*DefaultContext, error) {
return ResolveDefaultContext(opts.Common, cli.ConfigFile(), cli.contextStoreConfig, cli.Err()) return ResolveDefaultContext(opts.Common, cli.contextStoreConfig)
}, },
} }
cli.currentContext, err = resolveContextName(opts.Common, cli.configFile, cli.contextStore) cli.currentContext, err = resolveContextName(opts.Common, cli.configFile, cli.contextStore)
@ -244,7 +244,7 @@ func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.
contextStore := &ContextStoreWithDefault{ contextStore := &ContextStoreWithDefault{
Store: store.New(config.ContextStoreDir(), storeConfig), Store: store.New(config.ContextStoreDir(), storeConfig),
Resolver: func() (*DefaultContext, error) { Resolver: func() (*DefaultContext, error) {
return ResolveDefaultContext(opts, configFile, storeConfig, io.Discard) return ResolveDefaultContext(opts, storeConfig)
}, },
} }
contextName, err := resolveContextName(opts, configFile, contextStore) contextName, err := resolveContextName(opts, configFile, contextStore)

View File

@ -1,13 +1,10 @@
package command package command
import ( import (
"fmt"
"io" "io"
"os" "os"
"strconv" "strconv"
"github.com/docker/cli/cli/context/docker"
"github.com/docker/cli/cli/context/store"
"github.com/docker/cli/cli/streams" "github.com/docker/cli/cli/streams"
"github.com/moby/term" "github.com/moby/term"
) )
@ -82,19 +79,6 @@ func WithContentTrust(enabled bool) DockerCliOption {
} }
} }
// WithContextEndpointType add support for an additional typed endpoint in the context store
// Plugins should use this to store additional endpoints configuration in the context store
func WithContextEndpointType(endpointName string, endpointType store.TypeGetter) DockerCliOption {
return func(cli *DockerCli) error {
switch endpointName {
case docker.DockerEndpoint:
return fmt.Errorf("cannot change %q endpoint type", endpointName)
}
cli.contextStoreConfig.SetEndpoint(endpointName, endpointType)
return nil
}
}
// WithDefaultContextStoreConfig configures the cli to use the default context store configuration. // WithDefaultContextStoreConfig configures the cli to use the default context store configuration.
func WithDefaultContextStoreConfig() DockerCliOption { func WithDefaultContextStoreConfig() DockerCliOption {
return func(cli *DockerCli) error { return func(cli *DockerCli) error {

View File

@ -92,17 +92,24 @@ func createNewContext(o *CreateOptions, cli command.Cli, s store.Writer) error {
if o.Docker == nil { if o.Docker == nil {
return errors.New("docker endpoint configuration is required") return errors.New("docker endpoint configuration is required")
} }
contextMetadata := newContextMetadata(o)
contextTLSData := store.ContextTLSData{
Endpoints: make(map[string]store.EndpointTLSData),
}
dockerEP, dockerTLS, err := getDockerEndpointMetadataAndTLS(cli, o.Docker) dockerEP, dockerTLS, err := getDockerEndpointMetadataAndTLS(cli, o.Docker)
if err != nil { if err != nil {
return errors.Wrap(err, "unable to create docker endpoint config") return errors.Wrap(err, "unable to create docker endpoint config")
} }
contextMetadata.Endpoints[docker.DockerEndpoint] = dockerEP contextMetadata := store.Metadata{
Endpoints: map[string]interface{}{
docker.DockerEndpoint: dockerEP,
},
Metadata: command.DockerContext{
Description: o.Description,
},
Name: o.Name,
}
contextTLSData := store.ContextTLSData{}
if dockerTLS != nil { if dockerTLS != nil {
contextTLSData.Endpoints[docker.DockerEndpoint] = *dockerTLS contextTLSData.Endpoints = map[string]store.EndpointTLSData{
docker.DockerEndpoint: *dockerTLS,
}
} }
if err := validateEndpoints(contextMetadata); err != nil { if err := validateEndpoints(contextMetadata); err != nil {
return err return err
@ -161,13 +168,3 @@ func (d *descriptionDecorator) GetMetadata(name string) (store.Metadata, error)
c.Metadata = typedContext c.Metadata = typedContext
return c, nil return c, nil
} }
func newContextMetadata(o *CreateOptions) store.Metadata {
return store.Metadata{
Endpoints: make(map[string]interface{}),
Metadata: command.DockerContext{
Description: o.Description,
},
Name: o.Name,
}
}

View File

@ -2,9 +2,7 @@ package command
import ( import (
"fmt" "fmt"
"io"
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/context/docker" "github.com/docker/cli/cli/context/docker"
"github.com/docker/cli/cli/context/store" "github.com/docker/cli/cli/context/store"
cliflags "github.com/docker/cli/cli/flags" cliflags "github.com/docker/cli/cli/flags"
@ -45,7 +43,7 @@ type EndpointDefaultResolver interface {
} }
// ResolveDefaultContext creates a Metadata for the current CLI invocation parameters // ResolveDefaultContext creates a Metadata for the current CLI invocation parameters
func ResolveDefaultContext(opts *cliflags.CommonOptions, config *configfile.ConfigFile, storeconfig store.Config, stderr io.Writer) (*DefaultContext, error) { func ResolveDefaultContext(opts *cliflags.CommonOptions, config store.Config) (*DefaultContext, error) {
contextTLSData := store.ContextTLSData{ contextTLSData := store.ContextTLSData{
Endpoints: make(map[string]store.EndpointTLSData), Endpoints: make(map[string]store.EndpointTLSData),
} }
@ -66,7 +64,7 @@ func ResolveDefaultContext(opts *cliflags.CommonOptions, config *configfile.Conf
contextTLSData.Endpoints[docker.DockerEndpoint] = *dockerEP.TLSData.ToStoreTLSData() contextTLSData.Endpoints[docker.DockerEndpoint] = *dockerEP.TLSData.ToStoreTLSData()
} }
if err := storeconfig.ForeachEndpointType(func(n string, get store.TypeGetter) error { if err := config.ForeachEndpointType(func(n string, get store.TypeGetter) error {
if n == docker.DockerEndpoint { // handled above if n == docker.DockerEndpoint { // handled above
return nil return nil
} }

View File

@ -60,7 +60,7 @@ func TestDefaultContextInitializer(t *testing.T) {
TLSOptions: &tlsconfig.Options{ TLSOptions: &tlsconfig.Options{
CAFile: "./testdata/ca.pem", CAFile: "./testdata/ca.pem",
}, },
}, cli.ConfigFile(), DefaultContextStoreConfig(), cli.Err()) }, DefaultContextStoreConfig())
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, "default", ctx.Meta.Name) assert.Equal(t, "default", ctx.Meta.Name)
assert.DeepEqual(t, "ssh://someswarmserver", ctx.Meta.Endpoints[docker.DockerEndpoint].(docker.EndpointMeta).Host) assert.DeepEqual(t, "ssh://someswarmserver", ctx.Meta.Endpoints[docker.DockerEndpoint].(docker.EndpointMeta).Host)