2018-11-09 09:10:41 -05:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/context/kubernetes"
|
|
|
|
"github.com/docker/cli/cli/context/store"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
)
|
|
|
|
|
2019-01-21 03:37:20 -05:00
|
|
|
// ExportOptions are the options used for exporting a context
|
|
|
|
type ExportOptions struct {
|
|
|
|
Kubeconfig bool
|
|
|
|
ContextName string
|
|
|
|
Dest string
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func newExportCommand(dockerCli command.Cli) *cobra.Command {
|
2019-01-21 03:37:20 -05:00
|
|
|
opts := &ExportOptions{}
|
2018-11-09 09:10:41 -05:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "export [OPTIONS] CONTEXT [FILE|-]",
|
|
|
|
Short: "Export a context to a tar or kubeconfig file",
|
|
|
|
Args: cli.RequiresRangeArgs(1, 2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2019-01-21 03:37:20 -05:00
|
|
|
opts.ContextName = args[0]
|
2018-11-09 09:10:41 -05:00
|
|
|
if len(args) == 2 {
|
2019-01-21 03:37:20 -05:00
|
|
|
opts.Dest = args[1]
|
2018-11-09 09:10:41 -05:00
|
|
|
} else {
|
2019-01-21 03:37:20 -05:00
|
|
|
opts.Dest = opts.ContextName
|
|
|
|
if opts.Kubeconfig {
|
|
|
|
opts.Dest += ".kubeconfig"
|
2018-11-09 09:10:41 -05:00
|
|
|
} else {
|
2019-01-21 03:37:20 -05:00
|
|
|
opts.Dest += ".dockercontext"
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
}
|
2019-01-21 03:37:20 -05:00
|
|
|
return RunExport(dockerCli, opts)
|
2018-11-09 09:10:41 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2019-01-21 03:37:20 -05:00
|
|
|
flags.BoolVar(&opts.Kubeconfig, "kubeconfig", false, "Export as a kubeconfig file")
|
2018-11-09 09:10:41 -05:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeTo(dockerCli command.Cli, reader io.Reader, dest string) error {
|
|
|
|
var writer io.Writer
|
|
|
|
var printDest bool
|
|
|
|
if dest == "-" {
|
|
|
|
if dockerCli.Out().IsTerminal() {
|
|
|
|
return errors.New("cowardly refusing to export to a terminal, please specify a file path")
|
|
|
|
}
|
|
|
|
writer = dockerCli.Out()
|
|
|
|
} else {
|
|
|
|
f, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
writer = f
|
|
|
|
printDest = true
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(writer, reader); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if printDest {
|
|
|
|
fmt.Fprintf(dockerCli.Err(), "Written file %q\n", dest)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-21 03:37:20 -05:00
|
|
|
// RunExport exports a Docker context
|
|
|
|
func RunExport(dockerCli command.Cli, opts *ExportOptions) error {
|
|
|
|
if err := validateContextName(opts.ContextName); err != nil {
|
2018-11-09 09:10:41 -05:00
|
|
|
return err
|
|
|
|
}
|
2019-01-21 03:37:20 -05:00
|
|
|
ctxMeta, err := dockerCli.ContextStore().GetContextMetadata(opts.ContextName)
|
2018-11-09 09:10:41 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-21 03:37:20 -05:00
|
|
|
if !opts.Kubeconfig {
|
|
|
|
reader := store.Export(opts.ContextName, dockerCli.ContextStore())
|
2018-11-09 09:10:41 -05:00
|
|
|
defer reader.Close()
|
2019-01-21 03:37:20 -05:00
|
|
|
return writeTo(dockerCli, reader, opts.Dest)
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
kubernetesEndpointMeta := kubernetes.EndpointFromContext(ctxMeta)
|
|
|
|
if kubernetesEndpointMeta == nil {
|
2019-01-21 03:37:20 -05:00
|
|
|
return fmt.Errorf("context %q has no kubernetes endpoint", opts.ContextName)
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
2019-01-21 03:37:20 -05:00
|
|
|
kubernetesEndpoint, err := kubernetesEndpointMeta.WithTLSData(dockerCli.ContextStore(), opts.ContextName)
|
2018-11-09 09:10:41 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
kubeConfig := kubernetesEndpoint.KubernetesConfig()
|
|
|
|
rawCfg, err := kubeConfig.RawConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data, err := clientcmd.Write(rawCfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-21 03:37:20 -05:00
|
|
|
return writeTo(dockerCli, bytes.NewBuffer(data), opts.Dest)
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|