overrides.go 10.0 KB

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