fix: allow for client creation on default context
Some checks failed
continuous-integration/drone/push Build is failing

See coop-cloud/organising#206.
This commit is contained in:
decentral1se 2021-10-17 23:50:44 +02:00
parent 5be190e110
commit 6d42e72f16
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
3 changed files with 32 additions and 30 deletions

View File

@ -11,30 +11,33 @@ import (
// New initiates a new Docker client. // New initiates a new Docker client.
func New(contextName string) (*client.Client, error) { func New(contextName string) (*client.Client, error) {
context, err := GetContext(contextName)
if err != nil {
return nil, err
}
ctxEndpoint, err := GetContextEndpoint(context)
if err != nil {
return nil, err
}
helper := newConnectionHelper(ctxEndpoint)
httpClient := &http.Client{
// No tls, no proxy
Transport: &http.Transport{
DialContext: helper.Dialer,
},
}
var clientOpts []client.Opt var clientOpts []client.Opt
clientOpts = append(clientOpts,
client.WithHTTPClient(httpClient), if contextName != "default" {
client.WithHost(helper.Host), context, err := GetContext(contextName)
client.WithDialContext(helper.Dialer), if err != nil {
) return nil, err
}
ctxEndpoint, err := GetContextEndpoint(context)
if err != nil {
return nil, err
}
helper := newConnectionHelper(ctxEndpoint)
httpClient := &http.Client{
// No tls, no proxy
Transport: &http.Transport{
DialContext: helper.Dialer,
},
}
clientOpts = append(clientOpts,
client.WithHTTPClient(httpClient),
client.WithHost(helper.Host),
client.WithDialContext(helper.Dialer),
)
}
version := os.Getenv("DOCKER_API_VERSION") version := os.Getenv("DOCKER_API_VERSION")
if version != "" { if version != "" {

View File

@ -10,10 +10,10 @@ import (
func newConnectionHelper(daemonURL string) *connhelper.ConnectionHelper { func newConnectionHelper(daemonURL string) *connhelper.ConnectionHelper {
helper, err := connhelper.GetConnectionHelper(daemonURL) helper, err := connhelper.GetConnectionHelper(daemonURL)
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
return helper return helper
} }

View File

@ -9,6 +9,7 @@ import (
context "github.com/docker/cli/cli/context" context "github.com/docker/cli/cli/context"
"github.com/docker/cli/cli/context/docker" "github.com/docker/cli/cli/context/docker"
contextStore "github.com/docker/cli/cli/context/store" contextStore "github.com/docker/cli/cli/context/store"
cliflags "github.com/docker/cli/cli/flags"
"github.com/moby/term" "github.com/moby/term"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -94,7 +95,6 @@ func GetContext(contextName string) (contextStore.Metadata, error) {
} }
func GetContextEndpoint(ctx contextStore.Metadata) (string, error) { func GetContextEndpoint(ctx contextStore.Metadata) (string, error) {
// safe to use docker key hardcoded since abra doesn't use k8s... yet...
endpointmeta, ok := ctx.Endpoints["docker"].(context.EndpointMetaBase) endpointmeta, ok := ctx.Endpoints["docker"].(context.EndpointMetaBase)
if !ok { if !ok {
err := errors.New("context lacks Docker endpoint") err := errors.New("context lacks Docker endpoint")
@ -108,21 +108,20 @@ func newContextStore(dir string, config contextStore.Config) contextStore.Store
} }
func NewDefaultDockerContextStore() *command.ContextStoreWithDefault { func NewDefaultDockerContextStore() *command.ContextStoreWithDefault {
// Grabbing the stderr from Docker commands
// Much easier to fit this into the code we are using to replicate docker cli commands
_, _, stderr := term.StdStreams() _, _, stderr := term.StdStreams()
// TODO: Look into custom docker configs in case users want that
dockerConfig := dConfig.LoadDefaultConfigFile(stderr) dockerConfig := dConfig.LoadDefaultConfigFile(stderr)
contextDir := dConfig.ContextStoreDir() contextDir := dConfig.ContextStoreDir()
storeConfig := command.DefaultContextStoreConfig() storeConfig := command.DefaultContextStoreConfig()
store := newContextStore(contextDir, storeConfig) store := newContextStore(contextDir, storeConfig)
opts := &cliflags.CommonOptions{Context: "default"}
dockerContextStore := &command.ContextStoreWithDefault{ dockerContextStore := &command.ContextStoreWithDefault{
Store: store, Store: store,
Resolver: func() (*command.DefaultContext, error) { Resolver: func() (*command.DefaultContext, error) {
// nil for the Opts because it works without it and its a cli thing return command.ResolveDefaultContext(opts, dockerConfig, storeConfig, stderr)
return command.ResolveDefaultContext(nil, dockerConfig, storeConfig, stderr)
}, },
} }
return dockerContextStore return dockerContextStore
} }