From f0970ff732b1b2bed0cd8d402212d5fd335c1890 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 30 Mar 2022 18:50:15 +0200 Subject: [PATCH 1/2] cli/command: remove unused EndpointDefaultResolver interface this was added as part of 520be05c494940dfd70f1c43765c52735570f212, but is not used anywhere, so we may as well simplify things a bit. Signed-off-by: Sebastiaan van Stijn --- cli/command/cli.go | 8 ++- cli/command/defaultcontextstore.go | 67 ++++++------------------- cli/command/defaultcontextstore_test.go | 2 +- 3 files changed, 20 insertions(+), 57 deletions(-) diff --git a/cli/command/cli.go b/cli/command/cli.go index dfc22b701f9d..78f010fd12f9 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -233,7 +233,7 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize cli.contextStore = &ContextStoreWithDefault{ Store: store.New(config.ContextStoreDir(), cli.contextStoreConfig), Resolver: func() (*DefaultContext, error) { - return ResolveDefaultContext(cli.options, cli.contextStoreConfig) + return ResolveDefaultContext(cli.options) }, } return nil @@ -244,12 +244,10 @@ func NewAPIClientFromFlags(opts *cliflags.ClientOptions, configFile *configfile. if opts.Context != "" && len(opts.Hosts) > 0 { return nil, errors.New("conflicting options: either specify --host or --context, not both") } - - storeConfig := DefaultContextStoreConfig() contextStore := &ContextStoreWithDefault{ - Store: store.New(config.ContextStoreDir(), storeConfig), + Store: store.New(config.ContextStoreDir(), DefaultContextStoreConfig()), Resolver: func() (*DefaultContext, error) { - return ResolveDefaultContext(opts, storeConfig) + return ResolveDefaultContext(opts) }, } endpoint, err := resolveDockerEndpoint(contextStore, resolveContextName(opts, configFile)) diff --git a/cli/command/defaultcontextstore.go b/cli/command/defaultcontextstore.go index 203900e2fc02..66e011e79a9f 100644 --- a/cli/command/defaultcontextstore.go +++ b/cli/command/defaultcontextstore.go @@ -34,66 +34,31 @@ type ContextStoreWithDefault struct { Resolver DefaultContextResolver } -// EndpointDefaultResolver is implemented by any EndpointMeta object -// which wants to be able to populate the store with whatever their default is. -type EndpointDefaultResolver interface { - // ResolveDefault returns values suitable for storing in store.Metadata.Endpoints - // and store.ContextTLSData.Endpoints. - // - // An error is only returned for something fatal, not simply - // the lack of a default (e.g. because the config file which - // would contain it is missing). If there is no default then - // returns nil, nil, nil. - ResolveDefault() (interface{}, *store.EndpointTLSData, error) -} - // ResolveDefaultContext creates a Metadata for the current CLI invocation parameters -func ResolveDefaultContext(opts *cliflags.ClientOptions, config store.Config) (*DefaultContext, error) { - contextTLSData := store.ContextTLSData{ - Endpoints: make(map[string]store.EndpointTLSData), - } - contextMetadata := store.Metadata{ - Endpoints: make(map[string]interface{}), - Metadata: DockerContext{ - Description: "", - }, - Name: DefaultContextName, - } - +func ResolveDefaultContext(opts *cliflags.ClientOptions) (*DefaultContext, error) { dockerEP, err := resolveDefaultDockerEndpoint(opts) if err != nil { return nil, err } - contextMetadata.Endpoints[docker.DockerEndpoint] = dockerEP.EndpointMeta + contextTLSData := store.ContextTLSData{} if dockerEP.TLSData != nil { - contextTLSData.Endpoints[docker.DockerEndpoint] = *dockerEP.TLSData.ToStoreTLSData() - } - - if err := config.ForeachEndpointType(func(n string, get store.TypeGetter) error { - if n == docker.DockerEndpoint { // handled above - return nil + contextTLSData.Endpoints = map[string]store.EndpointTLSData{ + docker.DockerEndpoint: *dockerEP.TLSData.ToStoreTLSData(), } - ep := get() - if i, ok := ep.(EndpointDefaultResolver); ok { - meta, tls, err := i.ResolveDefault() - if err != nil { - return err - } - if meta == nil { - return nil - } - contextMetadata.Endpoints[n] = meta - if tls != nil { - contextTLSData.Endpoints[n] = *tls - } - } - // Nothing to be done - return nil - }); err != nil { - return nil, err } - return &DefaultContext{Meta: contextMetadata, TLS: contextTLSData}, nil + return &DefaultContext{ + Meta: store.Metadata{ + Endpoints: map[string]interface{}{ + docker.DockerEndpoint: dockerEP.EndpointMeta, + }, + Metadata: DockerContext{ + Description: "", + }, + Name: DefaultContextName, + }, + TLS: contextTLSData, + }, nil } // List implements store.Store's List diff --git a/cli/command/defaultcontextstore_test.go b/cli/command/defaultcontextstore_test.go index a368135ff7fc..c0f8e7927575 100644 --- a/cli/command/defaultcontextstore_test.go +++ b/cli/command/defaultcontextstore_test.go @@ -61,7 +61,7 @@ func TestDefaultContextInitializer(t *testing.T) { TLSOptions: &tlsconfig.Options{ CAFile: "./testdata/ca.pem", }, - }, DefaultContextStoreConfig()) + }) assert.NilError(t, err) assert.Equal(t, "default", ctx.Meta.Name) assert.DeepEqual(t, "ssh://someswarmserver", ctx.Meta.Endpoints[docker.DockerEndpoint].(docker.EndpointMeta).Host) From 13bccd363860bae08bf816c5ac796fdd61d4133d Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 30 Mar 2022 19:15:55 +0200 Subject: [PATCH 2/2] cli/context/store: remove unused Store.ForeachEndpointType() This was added in 4f14c4995ea5d96d91caeb03bb2e700572c97660, in preparation of 1433e274204f6bd239f59d14eaf103dfa859ad8c, but no longer used, so we can remove it. Signed-off-by: Sebastiaan van Stijn --- cli/context/store/storeconfig.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/cli/context/store/storeconfig.go b/cli/context/store/storeconfig.go index 9c93ecbab2b1..e1b54238f9ff 100644 --- a/cli/context/store/storeconfig.go +++ b/cli/context/store/storeconfig.go @@ -30,16 +30,6 @@ func (c Config) SetEndpoint(name string, getter TypeGetter) { c.endpointTypes[name] = getter } -// ForeachEndpointType calls cb on every endpoint type registered with the Config -func (c Config) ForeachEndpointType(cb func(string, TypeGetter) error) error { - for n, ep := range c.endpointTypes { - if err := cb(n, ep); err != nil { - return err - } - } - return nil -} - // NewConfig creates a config object func NewConfig(contextType TypeGetter, endpoints ...NamedTypeGetter) Config { res := Config{