diff --git a/cmd/cluster/get_kubeconfig.go b/cmd/cluster/get_kubeconfig.go index b8d2fec9e..c67673f88 100644 --- a/cmd/cluster/get_kubeconfig.go +++ b/cmd/cluster/get_kubeconfig.go @@ -17,8 +17,7 @@ package cluster import ( "github.com/spf13/cobra" - "opendev.org/airship/airshipctl/pkg/clusterctl/client" - clusterctlcmd "opendev.org/airship/airshipctl/pkg/clusterctl/cmd" + "opendev.org/airship/airshipctl/pkg/cluster" "opendev.org/airship/airshipctl/pkg/config" ) @@ -28,55 +27,28 @@ Retrieve cluster kubeconfig and print it to stdout ` getKubeconfigExample = ` # Retrieve target-cluster kubeconfig -airshipctl cluster get-kubeconfig target-cluster --kubeconfig /tmp/kubeconfig +airshipctl cluster get-kubeconfig target-cluster ` ) // NewGetKubeconfigCommand creates a command which retrieves cluster kubeconfig func NewGetKubeconfigCommand(cfgFactory config.Factory) *cobra.Command { - o := &client.GetKubeconfigOptions{} cmd := &cobra.Command{ Use: "get-kubeconfig [cluster_name]", Short: "Retrieve kubeconfig for a desired cluster", Long: getKubeconfigLong[1:], Example: getKubeconfigExample[1:], Args: cobra.ExactArgs(1), - RunE: getKubeconfigRunE(cfgFactory, o), + RunE: getKubeconfigRunE(cfgFactory), } - initFlags(o, cmd) - return cmd } -func initFlags(o *client.GetKubeconfigOptions, cmd *cobra.Command) { - flags := cmd.Flags() - - flags.StringVar( - &o.ParentKubeconfigPath, - "kubeconfig", - "", - "path to kubeconfig associated with parental cluster") - - flags.StringVarP( - &o.ManagedClusterNamespace, - "namespace", - "n", - "default", - "namespace where cluster is located, if not specified default one will be used") - - flags.StringVar( - &o.ParentKubeconfigContext, - "context", - "", - "specify context within the kubeconfig file") -} - // getKubeconfigRunE returns a function to cobra command to be executed in runtime -func getKubeconfigRunE(cfgFactory config.Factory, o *client.GetKubeconfigOptions) func( +func getKubeconfigRunE(cfgFactory config.Factory) func( cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { - o.ManagedClusterName = args[0] - return clusterctlcmd.GetKubeconfig(cfgFactory, o, cmd.OutOrStdout()) + return cluster.GetKubeconfig(cfgFactory, args[0], cmd.OutOrStdout()) } } diff --git a/cmd/cluster/testdata/TestNewKubeConfigCommandCmdGoldenOutput/cluster-get-kubeconfig-cmd-with-help.golden b/cmd/cluster/testdata/TestNewKubeConfigCommandCmdGoldenOutput/cluster-get-kubeconfig-cmd-with-help.golden index 0ee32b3a4..6b7af28a2 100644 --- a/cmd/cluster/testdata/TestNewKubeConfigCommandCmdGoldenOutput/cluster-get-kubeconfig-cmd-with-help.golden +++ b/cmd/cluster/testdata/TestNewKubeConfigCommandCmdGoldenOutput/cluster-get-kubeconfig-cmd-with-help.golden @@ -5,11 +5,8 @@ Usage: Examples: # Retrieve target-cluster kubeconfig -airshipctl cluster get-kubeconfig target-cluster --kubeconfig /tmp/kubeconfig +airshipctl cluster get-kubeconfig target-cluster Flags: - --context string specify context within the kubeconfig file - -h, --help help for get-kubeconfig - --kubeconfig string path to kubeconfig associated with parental cluster - -n, --namespace string namespace where cluster is located, if not specified default one will be used (default "default") + -h, --help help for get-kubeconfig diff --git a/docs/source/cli/airshipctl_cluster_get-kubeconfig.md b/docs/source/cli/airshipctl_cluster_get-kubeconfig.md index 3a33477e4..641f76e83 100644 --- a/docs/source/cli/airshipctl_cluster_get-kubeconfig.md +++ b/docs/source/cli/airshipctl_cluster_get-kubeconfig.md @@ -15,17 +15,14 @@ airshipctl cluster get-kubeconfig [cluster_name] [flags] ``` # Retrieve target-cluster kubeconfig -airshipctl cluster get-kubeconfig target-cluster --kubeconfig /tmp/kubeconfig +airshipctl cluster get-kubeconfig target-cluster ``` ### Options ``` - --context string specify context within the kubeconfig file - -h, --help help for get-kubeconfig - --kubeconfig string path to kubeconfig associated with parental cluster - -n, --namespace string namespace where cluster is located, if not specified default one will be used (default "default") + -h, --help help for get-kubeconfig ``` ### Options inherited from parent commands diff --git a/pkg/cluster/command.go b/pkg/cluster/command.go index 55bfa5f02..5cfab2f07 100755 --- a/pkg/cluster/command.go +++ b/pkg/cluster/command.go @@ -18,7 +18,12 @@ import ( "fmt" "io" + airshipv1 "opendev.org/airship/airshipctl/pkg/api/v1alpha1" + "opendev.org/airship/airshipctl/pkg/clusterctl/client" + "opendev.org/airship/airshipctl/pkg/config" + "opendev.org/airship/airshipctl/pkg/k8s/kubeconfig" "opendev.org/airship/airshipctl/pkg/log" + "opendev.org/airship/airshipctl/pkg/phase" "opendev.org/airship/airshipctl/pkg/util" ) @@ -50,3 +55,41 @@ func StatusRunner(o StatusOptions, w io.Writer) error { } return nil } + +// GetKubeconfig creates new kubeconfig interface object from secret and prints its content to writer +func GetKubeconfig(cfgFactory config.Factory, clusterName string, writer io.Writer) error { + cfg, err := cfgFactory() + if err != nil { + return err + } + + helper, err := phase.NewHelper(cfg) + if err != nil { + return err + } + + cMap, err := helper.ClusterMap() + if err != nil { + return err + } + + wd, err := helper.WorkDir() + if err != nil { + return err + } + + client, err := client.NewClient(helper.TargetPath(), log.DebugEnabled(), airshipv1.DefaultClusterctl()) + if err != nil { + return err + } + + kubeconf := kubeconfig.NewBuilder(). + WithBundle(helper.PhaseBundleRoot()). + WithClusterctClient(client). + WithClusterMap(cMap). + WithClusterName(clusterName). + WithTempRoot(wd). + Build() + + return kubeconf.Write(writer) +} diff --git a/pkg/clusterctl/cmd/command.go b/pkg/clusterctl/cmd/command.go index c26ec1bfb..a3b653f2c 100644 --- a/pkg/clusterctl/cmd/command.go +++ b/pkg/clusterctl/cmd/command.go @@ -15,13 +15,10 @@ package cmd import ( - "io" - airshipv1 "opendev.org/airship/airshipctl/pkg/api/v1alpha1" "opendev.org/airship/airshipctl/pkg/clusterctl/client" "opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/document" - "opendev.org/airship/airshipctl/pkg/k8s/kubeconfig" "opendev.org/airship/airshipctl/pkg/log" "opendev.org/airship/airshipctl/pkg/phase" ) @@ -108,22 +105,3 @@ func (c *Command) Move(toKubeconfigContext string) error { } return c.client.Move(c.kubeconfigPath, c.kubeconfigContext, c.kubeconfigPath, toKubeconfigContext, "") } - -// GetKubeconfig creates new kubeconfig interface object from secret and prints its content to writer -func GetKubeconfig(cfgFactory config.Factory, options *client.GetKubeconfigOptions, writer io.Writer) error { - cfg, err := cfgFactory() - if err != nil { - return err - } - - targetPath, err := cfg.CurrentContextTargetPath() - if err != nil { - return err - } - - client, err := client.NewClient(targetPath, log.DebugEnabled(), &airshipv1.Clusterctl{}) - if err != nil { - return err - } - return kubeconfig.NewKubeConfig(kubeconfig.FromSecret(client, options)).Write(writer) -}