From 9cde986a315320b1a1531de58974ba3b1355eb89 Mon Sep 17 00:00:00 2001 From: Ruslan Aliev Date: Thu, 19 Nov 2020 19:01:46 -0600 Subject: [PATCH] Introduce cluster get-kubeconfig command This command will be used to retrieve kubeconfig from from leveraging cluster or from document model. Particular implementation will be added in further commits to make it easier for review. Change-Id: If9e35dd90fdfcad6fa60a3cc4fab76ec1cb57f62 Signed-off-by: Ruslan Aliev Related-To: #374 --- cmd/cluster/cluster.go | 1 + cmd/cluster/get_kubeconfig.go | 52 +++++++++++++++++++ cmd/cluster/get_kubeconfig_test.go | 35 +++++++++++++ .../cluster-cmd-with-help.golden | 1 + ...luster-get-kubeconfig-cmd-with-help.golden | 12 +++++ docs/source/cli/airshipctl_cluster.md | 1 + .../cli/airshipctl_cluster_get-kubeconfig.md | 38 ++++++++++++++ 7 files changed, 140 insertions(+) create mode 100644 cmd/cluster/get_kubeconfig.go create mode 100644 cmd/cluster/get_kubeconfig_test.go create mode 100644 cmd/cluster/testdata/TestNewKubeConfigCommandCmdGoldenOutput/cluster-get-kubeconfig-cmd-with-help.golden create mode 100644 docs/source/cli/airshipctl_cluster_get-kubeconfig.md diff --git a/cmd/cluster/cluster.go b/cmd/cluster/cluster.go index 6633e8b77..57f9e6fdc 100644 --- a/cmd/cluster/cluster.go +++ b/cmd/cluster/cluster.go @@ -43,6 +43,7 @@ func NewClusterCommand(cfgFactory config.Factory) *cobra.Command { clusterRootCmd.AddCommand(NewStatusCommand(cfgFactory)) clusterRootCmd.AddCommand(resetsatoken.NewResetCommand(cfgFactory)) clusterRootCmd.AddCommand(checkexpiration.NewCheckCommand(cfgFactory)) + clusterRootCmd.AddCommand(NewGetKubeconfigCommand()) return clusterRootCmd } diff --git a/cmd/cluster/get_kubeconfig.go b/cmd/cluster/get_kubeconfig.go new file mode 100644 index 000000000..2c579ba0e --- /dev/null +++ b/cmd/cluster/get_kubeconfig.go @@ -0,0 +1,52 @@ +/* + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package cluster + +import ( + "github.com/spf13/cobra" + + "opendev.org/airship/airshipctl/pkg/errors" +) + +const ( + getKubeconfigLong = ` +Retrieve cluster kubeconfig and save it to file or stdout. +` + getKubeconfigExample = ` +# Retrieve target-cluster kubeconfig and print it to stdout +airshipctl cluster get-kubeconfig target-cluster +` +) + +// NewGetKubeconfigCommand creates a command which retrieves cluster kubeconfig +func NewGetKubeconfigCommand() *cobra.Command { + 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(), + } + + return cmd +} + +// getKubeconfigRunE returns a function to cobra command to be executed in runtime +func getKubeconfigRunE() func(cmd *cobra.Command, args []string) error { + return func(cmd *cobra.Command, args []string) error { + return errors.ErrNotImplemented{What: "cluster get-kubeconfig is not implemented yet"} + } +} diff --git a/cmd/cluster/get_kubeconfig_test.go b/cmd/cluster/get_kubeconfig_test.go new file mode 100644 index 000000000..aacef11cf --- /dev/null +++ b/cmd/cluster/get_kubeconfig_test.go @@ -0,0 +1,35 @@ +/* + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package cluster_test + +import ( + "testing" + + "opendev.org/airship/airshipctl/cmd/cluster" + "opendev.org/airship/airshipctl/testutil" +) + +func TestNewKubeConfigCommandCmd(t *testing.T) { + tests := []*testutil.CmdTest{ + { + Name: "cluster-get-kubeconfig-cmd-with-help", + CmdLine: "--help", + Cmd: cluster.NewGetKubeconfigCommand(), + }, + } + for _, testcase := range tests { + testutil.RunTest(t, testcase) + } +} diff --git a/cmd/cluster/testdata/TestNewClusterCommandGoldenOutput/cluster-cmd-with-help.golden b/cmd/cluster/testdata/TestNewClusterCommandGoldenOutput/cluster-cmd-with-help.golden index 32190e478..c4f83dfa0 100644 --- a/cmd/cluster/testdata/TestNewClusterCommandGoldenOutput/cluster-cmd-with-help.golden +++ b/cmd/cluster/testdata/TestNewClusterCommandGoldenOutput/cluster-cmd-with-help.golden @@ -6,6 +6,7 @@ Usage: Available Commands: check-certificate-expiration Check for expiring TLS certificates, secrets and kubeconfigs in the kubernetes cluster + get-kubeconfig Retrieve kubeconfig for a desired cluster help Help about any command init Deploy cluster-api provider components move Move Cluster API objects, provider specific objects and all dependencies to the target cluster 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 new file mode 100644 index 000000000..5aa351486 --- /dev/null +++ b/cmd/cluster/testdata/TestNewKubeConfigCommandCmdGoldenOutput/cluster-get-kubeconfig-cmd-with-help.golden @@ -0,0 +1,12 @@ +Retrieve cluster kubeconfig and save it to file or stdout. + +Usage: + get-kubeconfig [cluster_name] [flags] + +Examples: +# Retrieve target-cluster kubeconfig and print it to stdout +airshipctl cluster get-kubeconfig target-cluster + + +Flags: + -h, --help help for get-kubeconfig diff --git a/docs/source/cli/airshipctl_cluster.md b/docs/source/cli/airshipctl_cluster.md index 62d4c9641..5138e84a2 100644 --- a/docs/source/cli/airshipctl_cluster.md +++ b/docs/source/cli/airshipctl_cluster.md @@ -25,6 +25,7 @@ such as getting status and deploying initial infrastructure. * [airshipctl](airshipctl.md) - A unified entrypoint to various airship components * [airshipctl cluster check-certificate-expiration](airshipctl_cluster_check-certificate-expiration.md) - Check for expiring TLS certificates, secrets and kubeconfigs in the kubernetes cluster +* [airshipctl cluster get-kubeconfig](airshipctl_cluster_get-kubeconfig.md) - Retrieve kubeconfig for a desired cluster * [airshipctl cluster init](airshipctl_cluster_init.md) - Deploy cluster-api provider components * [airshipctl cluster move](airshipctl_cluster_move.md) - Move Cluster API objects, provider specific objects and all dependencies to the target cluster * [airshipctl cluster rotate-sa-token](airshipctl_cluster_rotate-sa-token.md) - Rotate tokens of Service Accounts diff --git a/docs/source/cli/airshipctl_cluster_get-kubeconfig.md b/docs/source/cli/airshipctl_cluster_get-kubeconfig.md new file mode 100644 index 000000000..0c7906184 --- /dev/null +++ b/docs/source/cli/airshipctl_cluster_get-kubeconfig.md @@ -0,0 +1,38 @@ +## airshipctl cluster get-kubeconfig + +Retrieve kubeconfig for a desired cluster + +### Synopsis + +Retrieve cluster kubeconfig and save it to file or stdout. + + +``` +airshipctl cluster get-kubeconfig [cluster_name] [flags] +``` + +### Examples + +``` +# Retrieve target-cluster kubeconfig and print it to stdout +airshipctl cluster get-kubeconfig target-cluster + +``` + +### Options + +``` + -h, --help help for get-kubeconfig +``` + +### Options inherited from parent commands + +``` + --airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config") + --debug enable verbose output +``` + +### SEE ALSO + +* [airshipctl cluster](airshipctl_cluster.md) - Manage Kubernetes clusters +