airshipctl/cmd/cluster/get_kubeconfig.go
Sirisha Gopigiri 21f015b502 Updating cmd files for documentation
This PS updates cmd related to root, completion, document and
version subcommands. It also updates minor details in other
subcommands.

The description and examples are updated for the airshipctl
commands, which will be inturn used for generating documentation.
Please ignore the .md file changes in this PS. They are added for zuul
gates to pass. Here is the PS with generated documention
files https://review.opendev.org/c/airship/airshipctl/+/789250

Relates-To: #280
Change-Id: I562c15c0d25b2e9731c0eb03854d1d348eb435f2
2021-06-09 09:56:21 +00:00

68 lines
2.2 KiB
Go

/*
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/cluster"
"opendev.org/airship/airshipctl/pkg/config"
)
const (
getKubeconfigLong = `
Retrieves kubeconfig of the cluster and prints it to stdout.
If you specify CLUSTER_NAME, kubeconfig will have a CurrentContext set to CLUSTER_NAME and
will have its context defined.
If you don't specify CLUSTER_NAME, kubeconfig will have multiple contexts for every cluster
in the airship site. Context names will correspond to cluster names. CurrentContext will be empty.
`
getKubeconfigExample = `
Retrieve target-cluster kubeconfig
# airshipctl cluster get-kubeconfig target-cluster
Retrieve kubeconfig for the entire site; the kubeconfig will have context for every cluster
# airshipctl cluster get-kubeconfig
`
)
// NewGetKubeconfigCommand creates a command which retrieves cluster kubeconfig
func NewGetKubeconfigCommand(cfgFactory config.Factory) *cobra.Command {
opts := &cluster.GetKubeconfigCommand{}
cmd := &cobra.Command{
Use: "get-kubeconfig CLUSTER_NAME",
Short: "Airshipctl command to retrieve kubeconfig for a desired cluster",
Long: getKubeconfigLong[1:],
Args: GetKubeconfArgs(opts),
Example: getKubeconfigExample,
RunE: func(cmd *cobra.Command, args []string) error {
return opts.RunE(cfgFactory, cmd.OutOrStdout())
},
}
return cmd
}
// GetKubeconfArgs extracts one or less arguments from command line, and saves it as name
func GetKubeconfArgs(opts *cluster.GetKubeconfigCommand) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
if len(args) == 1 {
opts.ClusterName = args[0]
}
return cobra.MaximumNArgs(1)(cmd, args)
}
}