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",
|
|
|
|
}
|
2018-01-24 12:17:08 -05:00
|
|
|
if cmd.Flags().Changed("namespace") {
|
|
|
|
cli.kubeNamespace, err = cmd.Flags().GetString("namespace")
|
2017-12-04 06:30:39 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kubeConfig := ""
|
2018-01-24 12:17:08 -05:00
|
|
|
if cmd.Flags().Changed("kubeconfig") {
|
|
|
|
kubeConfig, err = cmd.Flags().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
|
|
|
|
}
|