github.com/Mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/tools/settings.go (about)

     1  /*
     2  Copyright 2018 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 tools
    18  
    19  import (
    20  	"os"
    21  	"strings"
    22  
    23  	"github.com/spf13/pflag"
    24  	"k8s.io/client-go/tools/clientcmd"
    25  
    26  	"github.com/Mirantis/virtlet/pkg/utils"
    27  )
    28  
    29  const (
    30  	defaultVirtletRuntimeName = "virtlet.cloud"
    31  )
    32  
    33  var (
    34  	virtletRuntime = defaultVirtletRuntimeName
    35  )
    36  
    37  // from k8s pkg/kubectl/plugins/env.go
    38  func flagToEnvName(flagName, prefix string) string {
    39  	envName := strings.TrimPrefix(flagName, "--")
    40  	envName = strings.ToUpper(envName)
    41  	envName = strings.Replace(envName, "-", "_", -1)
    42  	envName = prefix + envName
    43  	return envName
    44  }
    45  
    46  // BindFlags applies go flags to the specified FlagSet and sets global
    47  // flag values from kubectl plugin environment variables in case if
    48  // virtletctl is running as a kubectl plugin.
    49  func BindFlags(flags *pflag.FlagSet) clientcmd.ClientConfig {
    50  	clientConfig := utils.BindFlags(flags)
    51  	flags.StringVar(&virtletRuntime, "virtlet-runtime", defaultVirtletRuntimeName, "the name of virtlet runtime used in kubernetes.io/target-runtime annotation")
    52  	if InPlugin() {
    53  		for _, flagName := range []string{
    54  			// k8s client flags
    55  			"kubeconfig",
    56  			clientcmd.FlagClusterName,
    57  			clientcmd.FlagAuthInfoName,
    58  			clientcmd.FlagContext,
    59  			clientcmd.FlagNamespace,
    60  			clientcmd.FlagAPIServer,
    61  			clientcmd.FlagInsecure,
    62  			clientcmd.FlagCertFile,
    63  			clientcmd.FlagKeyFile,
    64  			clientcmd.FlagCAFile,
    65  			clientcmd.FlagBearerToken,
    66  			clientcmd.FlagImpersonate,
    67  			clientcmd.FlagImpersonateGroup,
    68  			clientcmd.FlagUsername,
    69  			clientcmd.FlagPassword,
    70  			clientcmd.FlagTimeout,
    71  
    72  			// glog flags
    73  			"alsologtostderr",
    74  			"log-backtrace-at",
    75  			"log-dir",
    76  			"logtostderr",
    77  			"stderrthreshold",
    78  			"v",
    79  			"vmodule",
    80  
    81  			// virtletctl flags
    82  			"virtlet-runtime",
    83  		} {
    84  			v, found := os.LookupEnv(flagToEnvName(flagName, "KUBECTL_PLUGINS_GLOBAL_FLAG_"))
    85  			if found && (flagName != clientcmd.FlagImpersonateGroup || v != "[]") {
    86  				flags.Set(flagName, v)
    87  			}
    88  		}
    89  	}
    90  	return clientConfig
    91  }