forked from toolshed/abra
142 lines
3.8 KiB
Go
142 lines
3.8 KiB
Go
package context
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
|
|
"coopcloud.tech/abra/pkg/i18n"
|
|
"github.com/docker/cli/cli/command"
|
|
dConfig "github.com/docker/cli/cli/config"
|
|
"github.com/docker/cli/cli/context"
|
|
"github.com/docker/cli/cli/context/docker"
|
|
"github.com/docker/cli/cli/context/store"
|
|
contextStore "github.com/docker/cli/cli/context/store"
|
|
cliflags "github.com/docker/cli/cli/flags"
|
|
dopts "github.com/docker/cli/opts"
|
|
"github.com/moby/moby/client"
|
|
)
|
|
|
|
func NewDefaultDockerContextStore() *command.ContextStoreWithDefault {
|
|
contextDir := dConfig.ContextStoreDir()
|
|
storeConfig := command.DefaultContextStoreConfig()
|
|
store := newContextStore(contextDir, storeConfig)
|
|
|
|
opts := &cliflags.ClientOptions{Context: "default"}
|
|
|
|
dockerContextStore := &command.ContextStoreWithDefault{
|
|
Store: store,
|
|
Resolver: func() (*command.DefaultContext, error) {
|
|
return resolveDefaultContext(opts, storeConfig)
|
|
},
|
|
}
|
|
|
|
return dockerContextStore
|
|
}
|
|
|
|
// resolveDefaultContext creates a Metadata for the current CLI invocation parameters
|
|
func resolveDefaultContext(opts *cliflags.ClientOptions, config store.Config) (*command.DefaultContext, error) {
|
|
contextTLSData := store.ContextTLSData{
|
|
Endpoints: make(map[string]store.EndpointTLSData),
|
|
}
|
|
|
|
contextMetadata := store.Metadata{
|
|
Endpoints: make(map[string]any),
|
|
Metadata: command.DockerContext{
|
|
Description: "",
|
|
},
|
|
Name: "default",
|
|
}
|
|
|
|
dockerEP, err := resolveDefaultDockerEndpoint(opts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
contextMetadata.Endpoints[docker.DockerEndpoint] = dockerEP.EndpointMeta
|
|
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
|
|
}
|
|
ep := get()
|
|
if i, ok := ep.(command.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 &command.DefaultContext{Meta: contextMetadata, TLS: contextTLSData}, nil
|
|
}
|
|
|
|
// Resolve the Docker endpoint for the default context (based on config, env vars and CLI flags)
|
|
func resolveDefaultDockerEndpoint(opts *cliflags.ClientOptions) (docker.Endpoint, error) {
|
|
// defaultToTLS determines whether we should use a TLS host as default
|
|
// if nothing was configured by the user.
|
|
defaultToTLS := opts.TLSOptions != nil
|
|
host, err := getServerHost(opts.Hosts, defaultToTLS)
|
|
if err != nil {
|
|
return docker.Endpoint{}, err
|
|
}
|
|
|
|
var (
|
|
skipTLSVerify bool
|
|
tlsData *context.TLSData
|
|
)
|
|
|
|
if opts.TLSOptions != nil {
|
|
skipTLSVerify = opts.TLSOptions.InsecureSkipVerify
|
|
tlsData, err = context.TLSDataFromFiles(opts.TLSOptions.CAFile, opts.TLSOptions.CertFile, opts.TLSOptions.KeyFile)
|
|
if err != nil {
|
|
return docker.Endpoint{}, err
|
|
}
|
|
}
|
|
|
|
return docker.Endpoint{
|
|
EndpointMeta: docker.EndpointMeta{
|
|
Host: host,
|
|
SkipTLSVerify: skipTLSVerify,
|
|
},
|
|
TLSData: tlsData,
|
|
}, nil
|
|
}
|
|
|
|
func getServerHost(hosts []string, defaultToTLS bool) (string, error) {
|
|
switch len(hosts) {
|
|
case 0:
|
|
return dopts.ParseHost(defaultToTLS, os.Getenv(client.EnvOverrideHost))
|
|
case 1:
|
|
return dopts.ParseHost(defaultToTLS, hosts[0])
|
|
default:
|
|
return "", errors.New("specify only one -H")
|
|
}
|
|
}
|
|
|
|
func GetContextEndpoint(ctx contextStore.Metadata) (string, error) {
|
|
endpointmeta, ok := ctx.Endpoints["docker"].(context.EndpointMetaBase)
|
|
if !ok {
|
|
err := errors.New(i18n.G("context lacks Docker endpoint"))
|
|
return "", err
|
|
}
|
|
return endpointmeta.Host, nil
|
|
}
|
|
|
|
func newContextStore(dir string, config contextStore.Config) contextStore.Store {
|
|
return contextStore.New(dir, config)
|
|
}
|