...

Source file src/k8s.io/kubectl/pkg/cmd/config/get_clusters.go

Documentation: k8s.io/kubectl/pkg/cmd/config

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package config
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  
    23  	"github.com/spf13/cobra"
    24  	"k8s.io/client-go/tools/clientcmd"
    25  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    26  	"k8s.io/kubectl/pkg/util/i18n"
    27  	"k8s.io/kubectl/pkg/util/templates"
    28  )
    29  
    30  var (
    31  	getClustersExample = templates.Examples(`
    32  		# List the clusters that kubectl knows about
    33  		kubectl config get-clusters`)
    34  )
    35  
    36  // NewCmdConfigGetClusters creates a command object for the "get-clusters" action, which
    37  // lists all clusters defined in the kubeconfig.
    38  func NewCmdConfigGetClusters(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
    39  	cmd := &cobra.Command{
    40  		Use:     "get-clusters",
    41  		Short:   i18n.T("Display clusters defined in the kubeconfig"),
    42  		Long:    i18n.T("Display clusters defined in the kubeconfig."),
    43  		Example: getClustersExample,
    44  		Run: func(cmd *cobra.Command, args []string) {
    45  			cmdutil.CheckErr(runGetClusters(out, configAccess))
    46  		},
    47  	}
    48  
    49  	return cmd
    50  }
    51  
    52  func runGetClusters(out io.Writer, configAccess clientcmd.ConfigAccess) error {
    53  	config, err := configAccess.GetStartingConfig()
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	fmt.Fprintf(out, "NAME\n")
    59  	for name := range config.Clusters {
    60  		fmt.Fprintf(out, "%s\n", name)
    61  	}
    62  
    63  	return nil
    64  }
    65  

View as plain text