Extend cluster get-kubeconfig cmd functionality

This PS will allow us to retrieve kubeconfig from different
kind of sources.

Change-Id: I31381cf466c58373efda40d06587e34bef411c68
Signed-off-by: Ruslan Aliev <raliev@mirantis.com>
This commit is contained in:
Ruslan Aliev 2021-02-09 12:27:32 -06:00
parent e2af947337
commit 68e76a9059
5 changed files with 52 additions and 65 deletions

View File

@ -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())
}
}

View File

@ -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")

View File

@ -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")
```
### Options inherited from parent commands

View File

@ -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)
}

View File

@ -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)
}