overrides.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. "strings"
  17. "github.com/spf13/pflag"
  18. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  19. )
  20. // ConfigOverrides holds values that should override whatever information is pulled from the actual Config object. You can't
  21. // simply use an actual Config object, because Configs hold maps, but overrides are restricted to "at most one"
  22. type ConfigOverrides struct {
  23. AuthInfo clientcmdapi.AuthInfo
  24. // ClusterDefaults are applied before the configured cluster info is loaded.
  25. ClusterDefaults clientcmdapi.Cluster
  26. ClusterInfo clientcmdapi.Cluster
  27. Context clientcmdapi.Context
  28. CurrentContext string
  29. Timeout string
  30. }
  31. // ConfigOverrideFlags holds the flag names to be used for binding command line flags. Notice that this structure tightly
  32. // corresponds to ConfigOverrides
  33. type ConfigOverrideFlags struct {
  34. AuthOverrideFlags AuthOverrideFlags
  35. ClusterOverrideFlags ClusterOverrideFlags
  36. ContextOverrideFlags ContextOverrideFlags
  37. CurrentContext FlagInfo
  38. Timeout FlagInfo
  39. }
  40. // AuthOverrideFlags holds the flag names to be used for binding command line flags for AuthInfo objects
  41. type AuthOverrideFlags struct {
  42. ClientCertificate FlagInfo
  43. ClientKey FlagInfo
  44. Token FlagInfo
  45. Impersonate FlagInfo
  46. ImpersonateUID FlagInfo
  47. ImpersonateGroups FlagInfo
  48. Username FlagInfo
  49. Password FlagInfo
  50. }
  51. // ContextOverrideFlags holds the flag names to be used for binding command line flags for Cluster objects
  52. type ContextOverrideFlags struct {
  53. ClusterName FlagInfo
  54. AuthInfoName FlagInfo
  55. Namespace FlagInfo
  56. }
  57. // ClusterOverride holds the flag names to be used for binding command line flags for Cluster objects
  58. type ClusterOverrideFlags struct {
  59. APIServer FlagInfo
  60. APIVersion FlagInfo
  61. CertificateAuthority FlagInfo
  62. InsecureSkipTLSVerify FlagInfo
  63. TLSServerName FlagInfo
  64. ProxyURL FlagInfo
  65. DisableCompression FlagInfo
  66. }
  67. // FlagInfo contains information about how to register a flag. This struct is useful if you want to provide a way for an extender to
  68. // get back a set of recommended flag names, descriptions, and defaults, but allow for customization by an extender. This makes for
  69. // coherent extension, without full prescription
  70. type FlagInfo struct {
  71. // LongName is the long string for a flag. If this is empty, then the flag will not be bound
  72. LongName string
  73. // ShortName is the single character for a flag. If this is empty, then there will be no short flag
  74. ShortName string
  75. // Default is the default value for the flag
  76. Default string
  77. // Description is the description for the flag
  78. Description string
  79. }
  80. // AddSecretAnnotation add secret flag to Annotation.
  81. func (f FlagInfo) AddSecretAnnotation(flags *pflag.FlagSet) FlagInfo {
  82. flags.SetAnnotation(f.LongName, "classified", []string{"true"})
  83. return f
  84. }
  85. // BindStringFlag binds the flag based on the provided info. If LongName == "", nothing is registered
  86. func (f FlagInfo) BindStringFlag(flags *pflag.FlagSet, target *string) FlagInfo {
  87. // you can't register a flag without a long name
  88. if len(f.LongName) > 0 {
  89. flags.StringVarP(target, f.LongName, f.ShortName, f.Default, f.Description)
  90. }
  91. return f
  92. }
  93. // BindTransformingStringFlag binds the flag based on the provided info. If LongName == "", nothing is registered
  94. func (f FlagInfo) BindTransformingStringFlag(flags *pflag.FlagSet, target *string, transformer func(string) (string, error)) FlagInfo {
  95. // you can't register a flag without a long name
  96. if len(f.LongName) > 0 {
  97. flags.VarP(newTransformingStringValue(f.Default, target, transformer), f.LongName, f.ShortName, f.Description)
  98. }
  99. return f
  100. }
  101. // BindStringSliceFlag binds the flag based on the provided info. If LongName == "", nothing is registered
  102. func (f FlagInfo) BindStringArrayFlag(flags *pflag.FlagSet, target *[]string) FlagInfo {
  103. // you can't register a flag without a long name
  104. if len(f.LongName) > 0 {
  105. sliceVal := []string{}
  106. if len(f.Default) > 0 {
  107. sliceVal = []string{f.Default}
  108. }
  109. flags.StringArrayVarP(target, f.LongName, f.ShortName, sliceVal, f.Description)
  110. }
  111. return f
  112. }
  113. // BindBoolFlag binds the flag based on the provided info. If LongName == "", nothing is registered
  114. func (f FlagInfo) BindBoolFlag(flags *pflag.FlagSet, target *bool) FlagInfo {
  115. // you can't register a flag without a long name
  116. if len(f.LongName) > 0 {
  117. // try to parse Default as a bool. If it fails, assume false
  118. boolVal, err := strconv.ParseBool(f.Default)
  119. if err != nil {
  120. boolVal = false
  121. }
  122. flags.BoolVarP(target, f.LongName, f.ShortName, boolVal, f.Description)
  123. }
  124. return f
  125. }
  126. const (
  127. FlagClusterName = "cluster"
  128. FlagAuthInfoName = "user"
  129. FlagContext = "context"
  130. FlagNamespace = "namespace"
  131. FlagAPIServer = "server"
  132. FlagTLSServerName = "tls-server-name"
  133. FlagInsecure = "insecure-skip-tls-verify"
  134. FlagCertFile = "client-certificate"
  135. FlagKeyFile = "client-key"
  136. FlagCAFile = "certificate-authority"
  137. FlagEmbedCerts = "embed-certs"
  138. FlagBearerToken = "token"
  139. FlagImpersonate = "as"
  140. FlagImpersonateUID = "as-uid"
  141. FlagImpersonateGroup = "as-group"
  142. FlagUsername = "username"
  143. FlagPassword = "password"
  144. FlagTimeout = "request-timeout"
  145. FlagProxyURL = "proxy-url"
  146. FlagDisableCompression = "disable-compression"
  147. )
  148. // RecommendedConfigOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing
  149. func RecommendedConfigOverrideFlags(prefix string) ConfigOverrideFlags {
  150. return ConfigOverrideFlags{
  151. AuthOverrideFlags: RecommendedAuthOverrideFlags(prefix),
  152. ClusterOverrideFlags: RecommendedClusterOverrideFlags(prefix),
  153. ContextOverrideFlags: RecommendedContextOverrideFlags(prefix),
  154. CurrentContext: FlagInfo{prefix + FlagContext, "", "", "The name of the kubeconfig context to use"},
  155. 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."},
  156. }
  157. }
  158. // RecommendedAuthOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing
  159. func RecommendedAuthOverrideFlags(prefix string) AuthOverrideFlags {
  160. return AuthOverrideFlags{
  161. ClientCertificate: FlagInfo{prefix + FlagCertFile, "", "", "Path to a client certificate file for TLS"},
  162. ClientKey: FlagInfo{prefix + FlagKeyFile, "", "", "Path to a client key file for TLS"},
  163. Token: FlagInfo{prefix + FlagBearerToken, "", "", "Bearer token for authentication to the API server"},
  164. Impersonate: FlagInfo{prefix + FlagImpersonate, "", "", "Username to impersonate for the operation"},
  165. ImpersonateUID: FlagInfo{prefix + FlagImpersonateUID, "", "", "UID to impersonate for the operation"},
  166. ImpersonateGroups: FlagInfo{prefix + FlagImpersonateGroup, "", "", "Group to impersonate for the operation, this flag can be repeated to specify multiple groups."},
  167. Username: FlagInfo{prefix + FlagUsername, "", "", "Username for basic authentication to the API server"},
  168. Password: FlagInfo{prefix + FlagPassword, "", "", "Password for basic authentication to the API server"},
  169. }
  170. }
  171. // RecommendedClusterOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing
  172. func RecommendedClusterOverrideFlags(prefix string) ClusterOverrideFlags {
  173. return ClusterOverrideFlags{
  174. APIServer: FlagInfo{prefix + FlagAPIServer, "", "", "The address and port of the Kubernetes API server"},
  175. CertificateAuthority: FlagInfo{prefix + FlagCAFile, "", "", "Path to a cert file for the certificate authority"},
  176. InsecureSkipTLSVerify: FlagInfo{prefix + FlagInsecure, "", "false", "If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure"},
  177. TLSServerName: FlagInfo{prefix + FlagTLSServerName, "", "", "If provided, this name will be used to validate server certificate. If this is not provided, hostname used to contact the server is used."},
  178. ProxyURL: FlagInfo{prefix + FlagProxyURL, "", "", "If provided, this URL will be used to connect via proxy"},
  179. DisableCompression: FlagInfo{prefix + FlagDisableCompression, "", "", "If true, opt-out of response compression for all requests to the server"},
  180. }
  181. }
  182. // RecommendedContextOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing
  183. func RecommendedContextOverrideFlags(prefix string) ContextOverrideFlags {
  184. return ContextOverrideFlags{
  185. ClusterName: FlagInfo{prefix + FlagClusterName, "", "", "The name of the kubeconfig cluster to use"},
  186. AuthInfoName: FlagInfo{prefix + FlagAuthInfoName, "", "", "The name of the kubeconfig user to use"},
  187. Namespace: FlagInfo{prefix + FlagNamespace, "n", "", "If present, the namespace scope for this CLI request"},
  188. }
  189. }
  190. // BindOverrideFlags is a convenience method to bind the specified flags to their associated variables
  191. func BindOverrideFlags(overrides *ConfigOverrides, flags *pflag.FlagSet, flagNames ConfigOverrideFlags) {
  192. BindAuthInfoFlags(&overrides.AuthInfo, flags, flagNames.AuthOverrideFlags)
  193. BindClusterFlags(&overrides.ClusterInfo, flags, flagNames.ClusterOverrideFlags)
  194. BindContextFlags(&overrides.Context, flags, flagNames.ContextOverrideFlags)
  195. flagNames.CurrentContext.BindStringFlag(flags, &overrides.CurrentContext)
  196. flagNames.Timeout.BindStringFlag(flags, &overrides.Timeout)
  197. }
  198. // BindAuthInfoFlags is a convenience method to bind the specified flags to their associated variables
  199. func BindAuthInfoFlags(authInfo *clientcmdapi.AuthInfo, flags *pflag.FlagSet, flagNames AuthOverrideFlags) {
  200. flagNames.ClientCertificate.BindStringFlag(flags, &authInfo.ClientCertificate).AddSecretAnnotation(flags)
  201. flagNames.ClientKey.BindStringFlag(flags, &authInfo.ClientKey).AddSecretAnnotation(flags)
  202. flagNames.Token.BindStringFlag(flags, &authInfo.Token).AddSecretAnnotation(flags)
  203. flagNames.Impersonate.BindStringFlag(flags, &authInfo.Impersonate).AddSecretAnnotation(flags)
  204. flagNames.ImpersonateUID.BindStringFlag(flags, &authInfo.ImpersonateUID).AddSecretAnnotation(flags)
  205. flagNames.ImpersonateGroups.BindStringArrayFlag(flags, &authInfo.ImpersonateGroups).AddSecretAnnotation(flags)
  206. flagNames.Username.BindStringFlag(flags, &authInfo.Username).AddSecretAnnotation(flags)
  207. flagNames.Password.BindStringFlag(flags, &authInfo.Password).AddSecretAnnotation(flags)
  208. }
  209. // BindClusterFlags is a convenience method to bind the specified flags to their associated variables
  210. func BindClusterFlags(clusterInfo *clientcmdapi.Cluster, flags *pflag.FlagSet, flagNames ClusterOverrideFlags) {
  211. flagNames.APIServer.BindStringFlag(flags, &clusterInfo.Server)
  212. flagNames.CertificateAuthority.BindStringFlag(flags, &clusterInfo.CertificateAuthority)
  213. flagNames.InsecureSkipTLSVerify.BindBoolFlag(flags, &clusterInfo.InsecureSkipTLSVerify)
  214. flagNames.TLSServerName.BindStringFlag(flags, &clusterInfo.TLSServerName)
  215. flagNames.ProxyURL.BindStringFlag(flags, &clusterInfo.ProxyURL)
  216. flagNames.DisableCompression.BindBoolFlag(flags, &clusterInfo.DisableCompression)
  217. }
  218. // BindFlags is a convenience method to bind the specified flags to their associated variables
  219. func BindContextFlags(contextInfo *clientcmdapi.Context, flags *pflag.FlagSet, flagNames ContextOverrideFlags) {
  220. flagNames.ClusterName.BindStringFlag(flags, &contextInfo.Cluster)
  221. flagNames.AuthInfoName.BindStringFlag(flags, &contextInfo.AuthInfo)
  222. flagNames.Namespace.BindTransformingStringFlag(flags, &contextInfo.Namespace, RemoveNamespacesPrefix)
  223. }
  224. // RemoveNamespacesPrefix is a transformer that strips "ns/", "namespace/" and "namespaces/" prefixes case-insensitively
  225. func RemoveNamespacesPrefix(value string) (string, error) {
  226. for _, prefix := range []string{"namespaces/", "namespace/", "ns/"} {
  227. if len(value) > len(prefix) && strings.EqualFold(value[0:len(prefix)], prefix) {
  228. value = value[len(prefix):]
  229. break
  230. }
  231. }
  232. return value, nil
  233. }