2018-12-17 05:27:07 -05:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/docker/cli/cli/context"
|
|
|
|
"github.com/docker/cli/cli/context/store"
|
|
|
|
"github.com/docker/cli/kubernetes"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EndpointMeta is a typed wrapper around a context-store generic endpoint describing
|
|
|
|
// a Kubernetes endpoint, without TLS data
|
|
|
|
type EndpointMeta struct {
|
|
|
|
context.EndpointMetaBase
|
2018-11-09 09:10:41 -05:00
|
|
|
DefaultNamespace string `json:",omitempty"`
|
|
|
|
AuthProvider *clientcmdapi.AuthProviderConfig `json:",omitempty"`
|
|
|
|
Exec *clientcmdapi.ExecConfig `json:",omitempty"`
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Endpoint is a typed wrapper around a context-store generic endpoint describing
|
|
|
|
// a Kubernetes endpoint, with TLS data
|
|
|
|
type Endpoint struct {
|
|
|
|
EndpointMeta
|
|
|
|
TLSData *context.TLSData
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTLSData loads TLS materials for the endpoint
|
2019-04-15 06:03:03 -04:00
|
|
|
func (c *EndpointMeta) WithTLSData(s store.Reader, contextName string) (Endpoint, error) {
|
2018-12-17 05:27:07 -05:00
|
|
|
tlsData, err := context.LoadTLSData(s, contextName, KubernetesEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return Endpoint{}, err
|
|
|
|
}
|
|
|
|
return Endpoint{
|
|
|
|
EndpointMeta: *c,
|
|
|
|
TLSData: tlsData,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// KubernetesConfig creates the kubernetes client config from the endpoint
|
|
|
|
func (c *Endpoint) KubernetesConfig() clientcmd.ClientConfig {
|
|
|
|
cfg := clientcmdapi.NewConfig()
|
|
|
|
cluster := clientcmdapi.NewCluster()
|
|
|
|
cluster.Server = c.Host
|
|
|
|
cluster.InsecureSkipTLSVerify = c.SkipTLSVerify
|
|
|
|
authInfo := clientcmdapi.NewAuthInfo()
|
|
|
|
if c.TLSData != nil {
|
|
|
|
cluster.CertificateAuthorityData = c.TLSData.CA
|
|
|
|
authInfo.ClientCertificateData = c.TLSData.Cert
|
|
|
|
authInfo.ClientKeyData = c.TLSData.Key
|
|
|
|
}
|
|
|
|
authInfo.AuthProvider = c.AuthProvider
|
|
|
|
authInfo.Exec = c.Exec
|
|
|
|
cfg.Clusters["cluster"] = cluster
|
|
|
|
cfg.AuthInfos["authInfo"] = authInfo
|
|
|
|
ctx := clientcmdapi.NewContext()
|
|
|
|
ctx.AuthInfo = "authInfo"
|
|
|
|
ctx.Cluster = "cluster"
|
|
|
|
ctx.Namespace = c.DefaultNamespace
|
|
|
|
cfg.Contexts["context"] = ctx
|
|
|
|
cfg.CurrentContext = "context"
|
|
|
|
return clientcmd.NewDefaultClientConfig(*cfg, &clientcmd.ConfigOverrides{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndpointFromContext extracts kubernetes endpoint info from current context
|
2019-04-18 09:12:30 -04:00
|
|
|
func EndpointFromContext(metadata store.Metadata) *EndpointMeta {
|
2018-12-17 05:27:07 -05:00
|
|
|
ep, ok := metadata.Endpoints[KubernetesEndpoint]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
typed, ok := ep.(EndpointMeta)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &typed
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigFromContext resolves a kubernetes client config for the specified context.
|
|
|
|
// If kubeconfigOverride is specified, use this config file instead of the context defaults.ConfigFromContext
|
|
|
|
// if command.ContextDockerHost is specified as the context name, fallsback to the default user's kubeconfig file
|
2019-04-15 06:03:03 -04:00
|
|
|
func ConfigFromContext(name string, s store.Reader) (clientcmd.ClientConfig, error) {
|
2019-04-18 09:12:30 -04:00
|
|
|
ctxMeta, err := s.GetMetadata(name)
|
2018-12-17 05:27:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
epMeta := EndpointFromContext(ctxMeta)
|
|
|
|
if epMeta != nil {
|
|
|
|
ep, err := epMeta.WithTLSData(s, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ep.KubernetesConfig(), nil
|
|
|
|
}
|
|
|
|
// context has no kubernetes endpoint
|
|
|
|
return kubernetes.NewKubernetesConfig(""), nil
|
|
|
|
}
|