...

Source file src/k8s.io/kubectl/pkg/cmd/config/delete_cluster.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/completion"
    27  	"k8s.io/kubectl/pkg/util/i18n"
    28  	"k8s.io/kubectl/pkg/util/templates"
    29  )
    30  
    31  var (
    32  	deleteClusterExample = templates.Examples(`
    33  		# Delete the minikube cluster
    34  		kubectl config delete-cluster minikube`)
    35  )
    36  
    37  // NewCmdConfigDeleteCluster returns a Command instance for 'config delete-cluster' sub command
    38  func NewCmdConfigDeleteCluster(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
    39  	cmd := &cobra.Command{
    40  		Use:                   "delete-cluster NAME",
    41  		DisableFlagsInUseLine: true,
    42  		Short:                 i18n.T("Delete the specified cluster from the kubeconfig"),
    43  		Long:                  i18n.T("Delete the specified cluster from the kubeconfig."),
    44  		Example:               deleteClusterExample,
    45  		ValidArgsFunction:     completion.ClusterCompletionFunc,
    46  		Run: func(cmd *cobra.Command, args []string) {
    47  			cmdutil.CheckErr(runDeleteCluster(out, configAccess, cmd))
    48  		},
    49  	}
    50  
    51  	return cmd
    52  }
    53  
    54  func runDeleteCluster(out io.Writer, configAccess clientcmd.ConfigAccess, cmd *cobra.Command) error {
    55  	config, err := configAccess.GetStartingConfig()
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	args := cmd.Flags().Args()
    61  	if len(args) != 1 {
    62  		cmd.Help()
    63  		return nil
    64  	}
    65  
    66  	configFile := configAccess.GetDefaultFilename()
    67  	if configAccess.IsExplicitFile() {
    68  		configFile = configAccess.GetExplicitFile()
    69  	}
    70  
    71  	name := args[0]
    72  	_, ok := config.Clusters[name]
    73  	if !ok {
    74  		return fmt.Errorf("cannot delete cluster %s, not in %s", name, configFile)
    75  	}
    76  
    77  	delete(config.Clusters, name)
    78  
    79  	if err := clientcmd.ModifyConfig(configAccess, *config, true); err != nil {
    80  		return err
    81  	}
    82  
    83  	fmt.Fprintf(out, "deleted cluster %s from %s\n", name, configFile)
    84  
    85  	return nil
    86  }
    87  

View as plain text