overrides.go 12 KB

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