2017-12-04 06:30:39 -05:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2018-01-02 17:56:07 -05:00
|
|
|
"fmt"
|
2017-12-04 06:30:39 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
composev1beta1 "github.com/docker/cli/kubernetes/client/clientset_generated/clientset/typed/compose/v1beta1"
|
|
|
|
"github.com/docker/docker/pkg/homedir"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
restclient "k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
)
|
|
|
|
|
|
|
|
// KubeCli holds kubernetes specifics (client, namespace) with the command.Cli
|
|
|
|
type KubeCli struct {
|
|
|
|
command.Cli
|
|
|
|
kubeConfig *restclient.Config
|
|
|
|
kubeNamespace string
|
|
|
|
}
|
|
|
|
|
2017-12-05 03:35:52 -05:00
|
|
|
// WrapCli wraps command.Cli with kubernetes specifics
|
2017-12-04 06:30:39 -05:00
|
|
|
func WrapCli(dockerCli command.Cli, cmd *cobra.Command) (*KubeCli, error) {
|
|
|
|
var err error
|
|
|
|
cli := &KubeCli{
|
|
|
|
Cli: dockerCli,
|
|
|
|
kubeNamespace: "default",
|
|
|
|
}
|
|
|
|
if cmd.PersistentFlags().Changed("namespace") {
|
|
|
|
cli.kubeNamespace, err = cmd.PersistentFlags().GetString("namespace")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kubeConfig := ""
|
|
|
|
if cmd.PersistentFlags().Changed("kubeconfig") {
|
2017-12-05 03:35:52 -05:00
|
|
|
kubeConfig, err = cmd.PersistentFlags().GetString("kubeconfig")
|
2017-12-04 06:30:39 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if kubeConfig == "" {
|
|
|
|
if config := os.Getenv("KUBECONFIG"); config != "" {
|
|
|
|
kubeConfig = config
|
|
|
|
} else {
|
|
|
|
kubeConfig = filepath.Join(homedir.Get(), ".kube/config")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
|
|
|
|
if err != nil {
|
2018-01-02 17:56:07 -05:00
|
|
|
return nil, fmt.Errorf("Failed to load kubernetes configuration file '%s'", kubeConfig)
|
2017-12-04 06:30:39 -05:00
|
|
|
}
|
|
|
|
cli.kubeConfig = config
|
|
|
|
|
|
|
|
return cli, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *KubeCli) composeClient() (*Factory, error) {
|
|
|
|
return NewFactory(c.kubeNamespace, c.kubeConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *KubeCli) stacks() (composev1beta1.StackInterface, error) {
|
|
|
|
err := APIPresent(c.kubeConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
clientSet, err := composev1beta1.NewForConfig(c.kubeConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return clientSet.Stacks(c.kubeNamespace), nil
|
|
|
|
}
|