airshipctl/cmd/cluster/get_kubeconfig.go
Kostiantyn Kalynovskyi f24bf00d17 Get-kubeconfig return all cluster contexts
Now if we run `airshipctl cluster get-kubeconfig` without args
it will return kubeconfig for the entire site, which will have
contexts for every cluster defined in cluster map.

Relates-To: #460
Closes: #460

Change-Id: Icf1f09724a5c60ac520b1dbcbda69c3280ac4c7e
2021-03-17 19:27:14 +00:00

66 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 = `
Retrieve cluster kubeconfig and print it to stdout
If you specify clusterName, kubeconfig will have a CurrentContext set to clusterName and
will have this context defined
If you don't specify clusterName, 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 [clusterName]",
Short: "Retrieve kubeconfig for a desired cluster",
Long: getKubeconfigLong[1:],
Args: GetKubeconfArgs(opts),
Example: getKubeconfigExample[1:],
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)
}
}