overrides.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. Copyright 2014 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 clientcmd
  14. import (
  15. "strconv"
  16. "github.com/spf13/pflag"
  17. clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
  18. )
  19. // ConfigOverrides holds values that should override whatever information is pulled from the actual Config object. You can't
  20. // simply use an actual Config object, because Configs hold maps, but overrides are restricted to "at most one"
  21. type ConfigOverrides struct {
  22. AuthInfo clientcmdapi.AuthInfo
  23. // ClusterDefaults are applied before the configured cluster info is loaded.
  24. ClusterDefaults clientcmdapi.Cluster
  25. ClusterInfo clientcmdapi.Cluster
  26. Context clientcmdapi.Context
  27. CurrentContext string
  28. }
  29. // ConfigOverrideFlags holds the flag names to be used for binding command line flags. Notice that this structure tightly
  30. // corresponds to ConfigOverrides
  31. type ConfigOverrideFlags struct {
  32. AuthOverrideFlags AuthOverrideFlags
  33. ClusterOverrideFlags ClusterOverrideFlags
  34. ContextOverrideFlags ContextOverrideFlags
  35. CurrentContext FlagInfo
  36. }
  37. // AuthOverrideFlags holds the flag names to be used for binding command line flags for AuthInfo objects
  38. type AuthOverrideFlags struct {
  39. ClientCertificate FlagInfo
  40. ClientKey FlagInfo
  41. Token FlagInfo
  42. Impersonate FlagInfo
  43. Username FlagInfo
  44. Password FlagInfo
  45. }
  46. // ContextOverrideFlags holds the flag names to be used for binding command line flags for Cluster objects
  47. type ContextOverrideFlags struct {
  48. ClusterName FlagInfo
  49. AuthInfoName FlagInfo
  50. Namespace FlagInfo
  51. }
  52. // ClusterOverride holds the flag names to be used for binding command line flags for Cluster objects
  53. type ClusterOverrideFlags struct {
  54. APIServer FlagInfo
  55. APIVersion FlagInfo
  56. CertificateAuthority FlagInfo
  57. InsecureSkipTLSVerify FlagInfo
  58. }
  59. // FlagInfo contains information about how to register a flag. This struct is useful if you want to provide a way for an extender to
  60. // get back a set of recommended flag names, descriptions, and defaults, but allow for customization by an extender. This makes for
  61. // coherent extension, without full prescription
  62. type FlagInfo struct {
  63. // LongName is the long string for a flag. If this is empty, then the flag will not be bound
  64. LongName string
  65. // ShortName is the single character for a flag. If this is empty, then there will be no short flag
  66. ShortName string
  67. // Default is the default value for the flag
  68. Default string
  69. // Description is the description for the flag
  70. Description string
  71. }
  72. // BindStringFlag binds the flag based on the provided info. If LongName == "", nothing is registered
  73. func (f FlagInfo) BindStringFlag(flags *pflag.FlagSet, target *string) {
  74. // you can't register a flag without a long name
  75. if len(f.LongName) > 0 {
  76. flags.StringVarP(target, f.LongName, f.ShortName, f.Default, f.Description)
  77. }
  78. }
  79. // BindBoolFlag binds the flag based on the provided info. If LongName == "", nothing is registered
  80. func (f FlagInfo) BindBoolFlag(flags *pflag.FlagSet, target *bool) {
  81. // you can't register a flag without a long name
  82. if len(f.LongName) > 0 {
  83. // try to parse Default as a bool. If it fails, assume false
  84. boolVal, err := strconv.ParseBool(f.Default)
  85. if err != nil {
  86. boolVal = false
  87. }
  88. flags.BoolVarP(target, f.LongName, f.ShortName, boolVal, f.Description)
  89. }
  90. }
  91. const (
  92. FlagClusterName = "cluster"
  93. FlagAuthInfoName = "user"
  94. FlagContext = "context"
  95. FlagNamespace = "namespace"
  96. FlagAPIServer = "server"
  97. FlagAPIVersion = "api-version"
  98. FlagInsecure = "insecure-skip-tls-verify"
  99. FlagCertFile = "client-certificate"
  100. FlagKeyFile = "client-key"
  101. FlagCAFile = "certificate-authority"
  102. FlagEmbedCerts = "embed-certs"
  103. FlagBearerToken = "token"
  104. FlagImpersonate = "as"
  105. FlagUsername = "username"
  106. FlagPassword = "password"
  107. )
  108. // RecommendedAuthOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing
  109. func RecommendedAuthOverrideFlags(prefix string) AuthOverrideFlags {
  110. return AuthOverrideFlags{
  111. ClientCertificate: FlagInfo{prefix + FlagCertFile, "", "", "Path to a client certificate file for TLS"},
  112. ClientKey: FlagInfo{prefix + FlagKeyFile, "", "", "Path to a client key file for TLS"},
  113. Token: FlagInfo{prefix + FlagBearerToken, "", "", "Bearer token for authentication to the API server"},
  114. Impersonate: FlagInfo{prefix + FlagImpersonate, "", "", "Username to impersonate for the operation"},
  115. Username: FlagInfo{prefix + FlagUsername, "", "", "Username for basic authentication to the API server"},
  116. Password: FlagInfo{prefix + FlagPassword, "", "", "Password for basic authentication to the API server"},
  117. }
  118. }
  119. // RecommendedClusterOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing
  120. func RecommendedClusterOverrideFlags(prefix string) ClusterOverrideFlags {
  121. return ClusterOverrideFlags{
  122. APIServer: FlagInfo{prefix + FlagAPIServer, "", "", "The address and port of the Kubernetes API server"},
  123. APIVersion: FlagInfo{prefix + FlagAPIVersion, "", "", "DEPRECATED: The API version to use when talking to the server"},
  124. CertificateAuthority: FlagInfo{prefix + FlagCAFile, "", "", "Path to a cert. file for the certificate authority"},
  125. InsecureSkipTLSVerify: FlagInfo{prefix + FlagInsecure, "", "false", "If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure"},
  126. }
  127. }
  128. // RecommendedConfigOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing
  129. func RecommendedConfigOverrideFlags(prefix string) ConfigOverrideFlags {
  130. return ConfigOverrideFlags{
  131. AuthOverrideFlags: RecommendedAuthOverrideFlags(prefix),
  132. ClusterOverrideFlags: RecommendedClusterOverrideFlags(prefix),
  133. ContextOverrideFlags: RecommendedContextOverrideFlags(prefix),
  134. CurrentContext: FlagInfo{prefix + FlagContext, "", "", "The name of the kubeconfig context to use"},
  135. }
  136. }
  137. // RecommendedContextOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing
  138. func RecommendedContextOverrideFlags(prefix string) ContextOverrideFlags {
  139. return ContextOverrideFlags{
  140. ClusterName: FlagInfo{prefix + FlagClusterName, "", "", "The name of the kubeconfig cluster to use"},
  141. AuthInfoName: FlagInfo{prefix + FlagAuthInfoName, "", "", "The name of the kubeconfig user to use"},
  142. Namespace: FlagInfo{prefix + FlagNamespace, "n", "", "If present, the namespace scope for this CLI request"},
  143. }
  144. }
  145. // BindAuthInfoFlags is a convenience method to bind the specified flags to their associated variables
  146. func BindAuthInfoFlags(authInfo *clientcmdapi.AuthInfo, flags *pflag.FlagSet, flagNames AuthOverrideFlags) {
  147. flagNames.ClientCertificate.BindStringFlag(flags, &authInfo.ClientCertificate)
  148. flagNames.ClientKey.BindStringFlag(flags, &authInfo.ClientKey)
  149. flagNames.Token.BindStringFlag(flags, &authInfo.Token)
  150. flagNames.Impersonate.BindStringFlag(flags, &authInfo.Impersonate)
  151. flagNames.Username.BindStringFlag(flags, &authInfo.Username)
  152. flagNames.Password.BindStringFlag(flags, &authInfo.Password)
  153. }
  154. // BindClusterFlags is a convenience method to bind the specified flags to their associated variables
  155. func BindClusterFlags(clusterInfo *clientcmdapi.Cluster, flags *pflag.FlagSet, flagNames ClusterOverrideFlags) {
  156. flagNames.APIServer.BindStringFlag(flags, &clusterInfo.Server)
  157. // TODO: remove --api-version flag in 1.3.
  158. flagNames.APIVersion.BindStringFlag(flags, &clusterInfo.APIVersion)
  159. flags.MarkDeprecated(FlagAPIVersion, "flag is no longer respected and will be deleted in the next release")
  160. flagNames.CertificateAuthority.BindStringFlag(flags, &clusterInfo.CertificateAuthority)
  161. flagNames.InsecureSkipTLSVerify.BindBoolFlag(flags, &clusterInfo.InsecureSkipTLSVerify)
  162. }
  163. // BindOverrideFlags is a convenience method to bind the specified flags to their associated variables
  164. func BindOverrideFlags(overrides *ConfigOverrides, flags *pflag.FlagSet, flagNames ConfigOverrideFlags) {
  165. BindAuthInfoFlags(&overrides.AuthInfo, flags, flagNames.AuthOverrideFlags)
  166. BindClusterFlags(&overrides.ClusterInfo, flags, flagNames.ClusterOverrideFlags)
  167. BindContextFlags(&overrides.Context, flags, flagNames.ContextOverrideFlags)
  168. flagNames.CurrentContext.BindStringFlag(flags, &overrides.CurrentContext)
  169. }
  170. // BindFlags is a convenience method to bind the specified flags to their associated variables
  171. func BindContextFlags(contextInfo *clientcmdapi.Context, flags *pflag.FlagSet, flagNames ContextOverrideFlags) {
  172. flagNames.ClusterName.BindStringFlag(flags, &contextInfo.Cluster)
  173. flagNames.AuthInfoName.BindStringFlag(flags, &contextInfo.AuthInfo)
  174. flagNames.Namespace.BindStringFlag(flags, &contextInfo.Namespace)
  175. }