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 +