github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/utils/k8s_client.go (about)

     1  /*
     2  Copyright 2017 Mirantis
     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 utils
    18  
    19  import (
    20  	"flag"
    21  	"os"
    22  	"strings"
    23  
    24  	"github.com/spf13/pflag"
    25  	"k8s.io/client-go/kubernetes"
    26  	"k8s.io/client-go/rest"
    27  	"k8s.io/client-go/tools/clientcmd"
    28  )
    29  
    30  // GetK8sClientConfig returns config that is needed to access k8s
    31  func GetK8sClientConfig(host string) (*rest.Config, error) {
    32  	if host == "" {
    33  		url, exists := os.LookupEnv("KUBERNETES_CLUSTER_URL")
    34  		if !exists {
    35  			return rest.InClusterConfig()
    36  		}
    37  		host = url
    38  	}
    39  	return &rest.Config{Host: host}, nil
    40  }
    41  
    42  // GetK8sClientset returns clientset for standard k8s APIs
    43  func GetK8sClientset(config *rest.Config) (*kubernetes.Clientset, error) {
    44  	if config == nil {
    45  		var err error
    46  		config, err = GetK8sClientConfig("")
    47  		if err != nil {
    48  			return nil, err
    49  		}
    50  	}
    51  	return kubernetes.NewForConfig(config)
    52  }
    53  
    54  // wordSepNormalizeFunc change "_" to "-" in the flags.
    55  func wordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
    56  	if strings.Contains(name, "_") {
    57  		return pflag.NormalizedName(strings.Replace(name, "_", "-", -1))
    58  	}
    59  	return pflag.NormalizedName(name)
    60  }
    61  
    62  // defaultClientConfig builds a default Kubernetes client config based
    63  // on Cobra flags. It's based on kubelet code.
    64  func defaultClientConfig(flags *pflag.FlagSet) clientcmd.ClientConfig {
    65  	loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
    66  	// use the standard defaults for this client command
    67  	// DEPRECATED: remove and replace with something more accurate
    68  	loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
    69  
    70  	flags.StringVar(&loadingRules.ExplicitPath, "kubeconfig", "", "Path to the kubeconfig file to use for CLI requests.")
    71  
    72  	overrides := &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults}
    73  
    74  	flagNames := clientcmd.RecommendedConfigOverrideFlags("")
    75  	// short flagnames are disabled by default.  These are here for compatibility with existing scripts
    76  	flagNames.ClusterOverrideFlags.APIServer.ShortName = "s"
    77  
    78  	clientcmd.BindOverrideFlags(overrides, flags, flagNames)
    79  	clientConfig := clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, os.Stdin)
    80  
    81  	return clientConfig
    82  }
    83  
    84  // BindFlags applies standard Go flags and the flags used by kubeclient
    85  // to the specified FlagSet.
    86  func BindFlags(flags *pflag.FlagSet) clientcmd.ClientConfig {
    87  	flags.AddGoFlagSet(flag.CommandLine)
    88  	flags.SetNormalizeFunc(wordSepNormalizeFunc)
    89  	return defaultClientConfig(flags)
    90  }