1/* 2Copyright 2018 The Kubernetes Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package options 18 19import ( 20 "flag" 21 "fmt" 22 "os" 23 "strings" 24 25 "github.com/spf13/pflag" 26 27 // libs that provide registration functions 28 "k8s.io/component-base/logs" 29 "k8s.io/component-base/version/verflag" 30 "k8s.io/klog/v2" 31 32 // ensure libs have a chance to globally register their flags 33 _ "k8s.io/kubernetes/pkg/credentialprovider/azure" 34 _ "k8s.io/kubernetes/pkg/credentialprovider/gcp" 35) 36 37// AddGlobalFlags explicitly registers flags that libraries (glog, verflag, etc.) register 38// against the global flagsets from "flag" and "github.com/spf13/pflag". 39// We do this in order to prevent unwanted flags from leaking into the Kubelet's flagset. 40func AddGlobalFlags(fs *pflag.FlagSet) { 41 addKlogFlags(fs) 42 addCadvisorFlags(fs) 43 addCredentialProviderFlags(fs) 44 verflag.AddFlags(fs) 45 logs.AddFlags(fs) 46} 47 48// normalize replaces underscores with hyphens 49// we should always use hyphens instead of underscores when registering kubelet flags 50func normalize(s string) string { 51 return strings.Replace(s, "_", "-", -1) 52} 53 54// register adds a flag to local that targets the Value associated with the Flag named globalName in global 55func register(global *flag.FlagSet, local *pflag.FlagSet, globalName string) { 56 if f := global.Lookup(globalName); f != nil { 57 pflagFlag := pflag.PFlagFromGoFlag(f) 58 pflagFlag.Name = normalize(pflagFlag.Name) 59 local.AddFlag(pflagFlag) 60 } else { 61 panic(fmt.Sprintf("failed to find flag in global flagset (flag): %s", globalName)) 62 } 63} 64 65// pflagRegister adds a flag to local that targets the Value associated with the Flag named globalName in global 66func pflagRegister(global, local *pflag.FlagSet, globalName string) { 67 if f := global.Lookup(globalName); f != nil { 68 f.Name = normalize(f.Name) 69 local.AddFlag(f) 70 } else { 71 panic(fmt.Sprintf("failed to find flag in global flagset (pflag): %s", globalName)) 72 } 73} 74 75// registerDeprecated registers the flag with register, and then marks it deprecated 76func registerDeprecated(global *flag.FlagSet, local *pflag.FlagSet, globalName, deprecated string) { 77 register(global, local, globalName) 78 local.Lookup(normalize(globalName)).Deprecated = deprecated 79} 80 81// addCredentialProviderFlags adds flags from k8s.io/kubernetes/pkg/credentialprovider 82func addCredentialProviderFlags(fs *pflag.FlagSet) { 83 // lookup flags in global flag set and re-register the values with our flagset 84 global := pflag.CommandLine 85 local := pflag.NewFlagSet(os.Args[0], pflag.ExitOnError) 86 87 addLegacyCloudProviderCredentialProviderFlags(global, local) 88 89 fs.AddFlagSet(local) 90} 91 92// addKlogFlags adds flags from k8s.io/klog 93func addKlogFlags(fs *pflag.FlagSet) { 94 local := flag.NewFlagSet(os.Args[0], flag.ExitOnError) 95 klog.InitFlags(local) 96 fs.AddGoFlagSet(local) 97} 98