2017-12-04 06:30:39 -05:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/command"
|
2018-02-22 08:55:42 -05:00
|
|
|
"github.com/docker/cli/kubernetes"
|
2017-12-04 06:30:39 -05:00
|
|
|
composev1beta1 "github.com/docker/cli/kubernetes/client/clientset_generated/clientset/typed/compose/v1beta1"
|
|
|
|
"github.com/docker/docker/pkg/homedir"
|
2018-02-22 08:55:42 -05:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
flag "github.com/spf13/pflag"
|
|
|
|
kubeclient "k8s.io/client-go/kubernetes"
|
2017-12-04 06:30:39 -05:00
|
|
|
restclient "k8s.io/client-go/rest"
|
|
|
|
)
|
|
|
|
|
|
|
|
// KubeCli holds kubernetes specifics (client, namespace) with the command.Cli
|
|
|
|
type KubeCli struct {
|
|
|
|
command.Cli
|
|
|
|
kubeConfig *restclient.Config
|
|
|
|
kubeNamespace string
|
2018-02-22 08:55:42 -05:00
|
|
|
clientSet *kubeclient.Clientset
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options contains resolved parameters to initialize kubernetes clients
|
|
|
|
type Options struct {
|
|
|
|
Namespace string
|
|
|
|
Config string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewOptions returns an Options initialized with command line flags
|
|
|
|
func NewOptions(flags *flag.FlagSet) Options {
|
|
|
|
var opts Options
|
|
|
|
if namespace, err := flags.GetString("namespace"); err == nil {
|
|
|
|
opts.Namespace = namespace
|
|
|
|
}
|
|
|
|
if kubeConfig, err := flags.GetString("kubeconfig"); err == nil {
|
|
|
|
opts.Config = kubeConfig
|
|
|
|
}
|
|
|
|
return opts
|
2017-12-04 06:30:39 -05:00
|
|
|
}
|
|
|
|
|
2017-12-05 03:35:52 -05:00
|
|
|
// WrapCli wraps command.Cli with kubernetes specifics
|
2018-02-22 08:55:42 -05:00
|
|
|
func WrapCli(dockerCli command.Cli, opts Options) (*KubeCli, error) {
|
2017-12-04 06:30:39 -05:00
|
|
|
var err error
|
|
|
|
cli := &KubeCli{
|
|
|
|
Cli: dockerCli,
|
|
|
|
kubeNamespace: "default",
|
|
|
|
}
|
2018-02-22 08:55:42 -05:00
|
|
|
if opts.Namespace != "" {
|
|
|
|
cli.kubeNamespace = opts.Namespace
|
2017-12-04 06:30:39 -05:00
|
|
|
}
|
2018-02-22 08:55:42 -05:00
|
|
|
kubeConfig := opts.Config
|
2017-12-04 06:30:39 -05:00
|
|
|
if kubeConfig == "" {
|
|
|
|
if config := os.Getenv("KUBECONFIG"); config != "" {
|
|
|
|
kubeConfig = config
|
|
|
|
} else {
|
|
|
|
kubeConfig = filepath.Join(homedir.Get(), ".kube/config")
|
|
|
|
}
|
|
|
|
}
|
2018-02-22 08:55:42 -05:00
|
|
|
config, err := kubernetes.NewKubernetesConfig(kubeConfig)
|
2017-12-04 06:30:39 -05:00
|
|
|
if err != nil {
|
2018-02-22 08:55:42 -05:00
|
|
|
return nil, err
|
2017-12-04 06:30:39 -05:00
|
|
|
}
|
|
|
|
cli.kubeConfig = config
|
|
|
|
|
2018-02-22 08:55:42 -05:00
|
|
|
clientSet, err := kubeclient.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cli.clientSet = clientSet
|
|
|
|
|
2017-12-04 06:30:39 -05:00
|
|
|
return cli, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *KubeCli) composeClient() (*Factory, error) {
|
|
|
|
return NewFactory(c.kubeNamespace, c.kubeConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *KubeCli) stacks() (composev1beta1.StackInterface, error) {
|
2018-02-22 08:55:42 -05:00
|
|
|
version, err := kubernetes.GetStackAPIVersion(c.clientSet)
|
2017-12-04 06:30:39 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-22 08:55:42 -05:00
|
|
|
switch version {
|
|
|
|
case kubernetes.StackAPIV1Beta1:
|
|
|
|
clientSet, err := composev1beta1.NewForConfig(c.kubeConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return clientSet.Stacks(c.kubeNamespace), nil
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("no supported Stack API version")
|
|
|
|
}
|
2017-12-04 06:30:39 -05:00
|
|
|
}
|