Skip to content

Commit 1433e27

Browse files
author
Ian Campbell
committed
Allow dynamically registered context endpoint to provide their defaults.
Previously an endpoint registered using `RegisterDefaultStoreEndpoints` would not be taken into consideration by `resolveDefaultContext` and so could not provide any details. Resolve this by passing a `store.Config` to `resolveDefaultContext` and using it to iterate over all registered endpoints. Any endpoint can ensure that their type implements the new `EndpointDefaultResolver` in order to provide a default. The Docker and Kubernetes endpoints are special cased, shortly the Kubernetes one will be refactored to be dynamically registered. Signed-off-by: Ian Campbell <ijc@docker.com>
1 parent 4f14c49 commit 1433e27

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

cli/command/cli.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize
214214
cli.contextStore = &ContextStoreWithDefault{
215215
Store: baseContextStore,
216216
Resolver: func() (*DefaultContext, error) {
217-
return resolveDefaultContext(opts.Common, cli.ConfigFile(), cli.Err())
217+
return resolveDefaultContext(opts.Common, cli.ConfigFile(), cli.contextStoreConfig, cli.Err())
218218
},
219219
}
220220
cli.currentContext, err = resolveContextName(opts.Common, cli.configFile, cli.contextStore)
@@ -259,10 +259,11 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize
259259

260260
// NewAPIClientFromFlags creates a new APIClient from command line flags
261261
func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
262+
storeConfig := defaultContextStoreConfig()
262263
store := &ContextStoreWithDefault{
263-
Store: store.New(cliconfig.ContextStoreDir(), defaultContextStoreConfig()),
264+
Store: store.New(cliconfig.ContextStoreDir(), storeConfig),
264265
Resolver: func() (*DefaultContext, error) {
265-
return resolveDefaultContext(opts, configFile, ioutil.Discard)
266+
return resolveDefaultContext(opts, configFile, storeConfig, ioutil.Discard)
266267
},
267268
}
268269
contextName, err := resolveContextName(opts, configFile, store)

cli/command/defaultcontextstore.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,16 @@ type ContextStoreWithDefault struct {
3535
Resolver DefaultContextResolver
3636
}
3737

38+
// EndpointDefaultResolver is implemented by any EndpointMeta object
39+
// which wants to be able to populate the store with whatever their default is.
40+
type EndpointDefaultResolver interface {
41+
// ResolveDefault returns values suitable for storing in store.Metadata.Endpoints
42+
// and store.ContextTLSData.Endpoints. If there is no default then returns nil, nil.
43+
ResolveDefault() (interface{}, *store.EndpointTLSData)
44+
}
45+
3846
// resolveDefaultContext creates a Metadata for the current CLI invocation parameters
39-
func resolveDefaultContext(opts *cliflags.CommonOptions, config *configfile.ConfigFile, stderr io.Writer) (*DefaultContext, error) {
47+
func resolveDefaultContext(opts *cliflags.CommonOptions, config *configfile.ConfigFile, storeconfig store.Config, stderr io.Writer) (*DefaultContext, error) {
4048
stackOrchestrator, err := GetStackOrchestrator("", "", config.StackOrchestrator, stderr)
4149
if err != nil {
4250
return nil, err
@@ -78,6 +86,27 @@ func resolveDefaultContext(opts *cliflags.CommonOptions, config *configfile.Conf
7886
}
7987
}
8088

89+
if err := storeconfig.ForeachEndpointType(func(n string, get store.TypeGetter) error {
90+
if n == docker.DockerEndpoint || n == kubernetes.KubernetesEndpoint { // handled above
91+
return nil
92+
}
93+
ep := get()
94+
if i, ok := ep.(EndpointDefaultResolver); ok {
95+
meta, tls := i.ResolveDefault()
96+
if meta == nil {
97+
return nil
98+
}
99+
contextMetadata.Endpoints[n] = meta
100+
if tls != nil {
101+
contextTLSData.Endpoints[n] = *tls
102+
}
103+
}
104+
// Nothing to be done
105+
return nil
106+
}); err != nil {
107+
return nil, err
108+
}
109+
81110
return &DefaultContext{Meta: contextMetadata, TLS: contextTLSData}, nil
82111
}
83112

cli/command/defaultcontextstore_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestDefaultContextInitializer(t *testing.T) {
7272
TLSOptions: &tlsconfig.Options{
7373
CAFile: "./testdata/ca.pem",
7474
},
75-
}, cli.ConfigFile(), cli.Err())
75+
}, cli.ConfigFile(), defaultContextStoreConfig(), cli.Err())
7676
assert.NilError(t, err)
7777
assert.Equal(t, "default", ctx.Meta.Name)
7878
assert.Equal(t, OrchestratorAll, ctx.Meta.Metadata.(DockerContext).StackOrchestrator)

0 commit comments

Comments
 (0)