2018-12-17 05:27:07 -05:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2019-05-16 10:06:27 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/command"
|
2018-12-17 05:27:07 -05:00
|
|
|
"github.com/docker/cli/cli/context"
|
|
|
|
"github.com/docker/cli/cli/context/store"
|
2019-05-14 10:12:06 -04:00
|
|
|
api "github.com/docker/compose-on-kubernetes/api"
|
2019-05-16 10:06:27 -04:00
|
|
|
"github.com/docker/docker/pkg/homedir"
|
2018-12-17 05:27:07 -05:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
2019-05-16 10:06:27 -04:00
|
|
|
var _ command.EndpointDefaultResolver = &EndpointMeta{}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-05-16 10:06:27 -04:00
|
|
|
func init() {
|
|
|
|
command.RegisterDefaultStoreEndpoints(
|
|
|
|
store.EndpointTypeGetter(KubernetesEndpoint, func() interface{} { return &EndpointMeta{} }),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-12-17 05:27:07 -05:00
|
|
|
// 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{})
|
|
|
|
}
|
|
|
|
|
2019-05-16 10:06:27 -04:00
|
|
|
// ResolveDefault returns endpoint metadata for the default Kubernetes
|
|
|
|
// endpoint, which is derived from the env-based kubeconfig.
|
|
|
|
func (c *EndpointMeta) ResolveDefault() (interface{}, *store.EndpointTLSData) {
|
|
|
|
kubeconfig := os.Getenv("KUBECONFIG")
|
|
|
|
if kubeconfig == "" {
|
|
|
|
kubeconfig = filepath.Join(homedir.Get(), ".kube/config")
|
|
|
|
}
|
|
|
|
kubeEP, err := FromKubeConfig(kubeconfig, "", "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var tls *store.EndpointTLSData
|
|
|
|
if kubeEP.TLSData != nil {
|
|
|
|
tls = kubeEP.TLSData.ToStoreTLSData()
|
|
|
|
}
|
|
|
|
return kubeEP.EndpointMeta, tls
|
|
|
|
}
|
|
|
|
|
2018-12-17 05:27:07 -05:00
|
|
|
// 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
|
2019-05-14 10:12:06 -04:00
|
|
|
return api.NewKubernetesConfig(""), nil
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|