globalflags.go 3.4 KB

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