client_config.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. "fmt"
  16. "io"
  17. "io/ioutil"
  18. "net/http"
  19. "net/url"
  20. "os"
  21. "strings"
  22. "unicode"
  23. restclient "k8s.io/client-go/rest"
  24. clientauth "k8s.io/client-go/tools/auth"
  25. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  26. "k8s.io/klog/v2"
  27. "github.com/imdario/mergo"
  28. )
  29. var (
  30. // ClusterDefaults has the same behavior as the old EnvVar and DefaultCluster fields
  31. // DEPRECATED will be replaced
  32. ClusterDefaults = clientcmdapi.Cluster{Server: getDefaultServer()}
  33. // DefaultClientConfig represents the legacy behavior of this package for defaulting
  34. // DEPRECATED will be replace
  35. DefaultClientConfig = DirectClientConfig{*clientcmdapi.NewConfig(), "", &ConfigOverrides{
  36. ClusterDefaults: ClusterDefaults,
  37. }, nil, NewDefaultClientConfigLoadingRules(), promptedCredentials{}}
  38. )
  39. // getDefaultServer returns a default setting for DefaultClientConfig
  40. // DEPRECATED
  41. func getDefaultServer() string {
  42. if server := os.Getenv("KUBERNETES_MASTER"); len(server) > 0 {
  43. return server
  44. }
  45. return "http://localhost:8080"
  46. }
  47. // ClientConfig is used to make it easy to get an api server client
  48. type ClientConfig interface {
  49. // RawConfig returns the merged result of all overrides
  50. RawConfig() (clientcmdapi.Config, error)
  51. // ClientConfig returns a complete client config
  52. ClientConfig() (*restclient.Config, error)
  53. // Namespace returns the namespace resulting from the merged
  54. // result of all overrides and a boolean indicating if it was
  55. // overridden
  56. Namespace() (string, bool, error)
  57. // ConfigAccess returns the rules for loading/persisting the config.
  58. ConfigAccess() ConfigAccess
  59. }
  60. type PersistAuthProviderConfigForUser func(user string) restclient.AuthProviderConfigPersister
  61. type promptedCredentials struct {
  62. username string
  63. password string
  64. }
  65. // DirectClientConfig is a ClientConfig interface that is backed by a clientcmdapi.Config, options overrides, and an optional fallbackReader for auth information
  66. type DirectClientConfig struct {
  67. config clientcmdapi.Config
  68. contextName string
  69. overrides *ConfigOverrides
  70. fallbackReader io.Reader
  71. configAccess ConfigAccess
  72. // promptedCredentials store the credentials input by the user
  73. promptedCredentials promptedCredentials
  74. }
  75. // NewDefaultClientConfig creates a DirectClientConfig using the config.CurrentContext as the context name
  76. func NewDefaultClientConfig(config clientcmdapi.Config, overrides *ConfigOverrides) ClientConfig {
  77. return &DirectClientConfig{config, config.CurrentContext, overrides, nil, NewDefaultClientConfigLoadingRules(), promptedCredentials{}}
  78. }
  79. // NewNonInteractiveClientConfig creates a DirectClientConfig using the passed context name and does not have a fallback reader for auth information
  80. func NewNonInteractiveClientConfig(config clientcmdapi.Config, contextName string, overrides *ConfigOverrides, configAccess ConfigAccess) ClientConfig {
  81. return &DirectClientConfig{config, contextName, overrides, nil, configAccess, promptedCredentials{}}
  82. }
  83. // NewInteractiveClientConfig creates a DirectClientConfig using the passed context name and a reader in case auth information is not provided via files or flags
  84. func NewInteractiveClientConfig(config clientcmdapi.Config, contextName string, overrides *ConfigOverrides, fallbackReader io.Reader, configAccess ConfigAccess) ClientConfig {
  85. return &DirectClientConfig{config, contextName, overrides, fallbackReader, configAccess, promptedCredentials{}}
  86. }
  87. // NewClientConfigFromBytes takes your kubeconfig and gives you back a ClientConfig
  88. func NewClientConfigFromBytes(configBytes []byte) (ClientConfig, error) {
  89. config, err := Load(configBytes)
  90. if err != nil {
  91. return nil, err
  92. }
  93. return &DirectClientConfig{*config, "", &ConfigOverrides{}, nil, nil, promptedCredentials{}}, nil
  94. }
  95. // RESTConfigFromKubeConfig is a convenience method to give back a restconfig from your kubeconfig bytes.
  96. // For programmatic access, this is what you want 80% of the time
  97. func RESTConfigFromKubeConfig(configBytes []byte) (*restclient.Config, error) {
  98. clientConfig, err := NewClientConfigFromBytes(configBytes)
  99. if err != nil {
  100. return nil, err
  101. }
  102. return clientConfig.ClientConfig()
  103. }
  104. func (config *DirectClientConfig) RawConfig() (clientcmdapi.Config, error) {
  105. return config.config, nil
  106. }
  107. // ClientConfig implements ClientConfig
  108. func (config *DirectClientConfig) ClientConfig() (*restclient.Config, error) {
  109. // check that getAuthInfo, getContext, and getCluster do not return an error.
  110. // Do this before checking if the current config is usable in the event that an
  111. // AuthInfo, Context, or Cluster config with user-defined names are not found.
  112. // This provides a user with the immediate cause for error if one is found
  113. configAuthInfo, err := config.getAuthInfo()
  114. if err != nil {
  115. return nil, err
  116. }
  117. _, err = config.getContext()
  118. if err != nil {
  119. return nil, err
  120. }
  121. configClusterInfo, err := config.getCluster()
  122. if err != nil {
  123. return nil, err
  124. }
  125. if err := config.ConfirmUsable(); err != nil {
  126. return nil, err
  127. }
  128. clientConfig := &restclient.Config{}
  129. clientConfig.Host = configClusterInfo.Server
  130. if configClusterInfo.ProxyURL != "" {
  131. u, err := parseProxyURL(configClusterInfo.ProxyURL)
  132. if err != nil {
  133. return nil, err
  134. }
  135. clientConfig.Proxy = http.ProxyURL(u)
  136. }
  137. if config.overrides != nil && len(config.overrides.Timeout) > 0 {
  138. timeout, err := ParseTimeout(config.overrides.Timeout)
  139. if err != nil {
  140. return nil, err
  141. }
  142. clientConfig.Timeout = timeout
  143. }
  144. if u, err := url.ParseRequestURI(clientConfig.Host); err == nil && u.Opaque == "" && len(u.Path) > 1 {
  145. u.RawQuery = ""
  146. u.Fragment = ""
  147. clientConfig.Host = u.String()
  148. }
  149. if len(configAuthInfo.Impersonate) > 0 {
  150. clientConfig.Impersonate = restclient.ImpersonationConfig{
  151. UserName: configAuthInfo.Impersonate,
  152. Groups: configAuthInfo.ImpersonateGroups,
  153. Extra: configAuthInfo.ImpersonateUserExtra,
  154. }
  155. }
  156. // only try to read the auth information if we are secure
  157. if restclient.IsConfigTransportTLS(*clientConfig) {
  158. var err error
  159. var persister restclient.AuthProviderConfigPersister
  160. if config.configAccess != nil {
  161. authInfoName, _ := config.getAuthInfoName()
  162. persister = PersisterForUser(config.configAccess, authInfoName)
  163. }
  164. userAuthPartialConfig, err := config.getUserIdentificationPartialConfig(configAuthInfo, config.fallbackReader, persister)
  165. if err != nil {
  166. return nil, err
  167. }
  168. mergo.MergeWithOverwrite(clientConfig, userAuthPartialConfig)
  169. serverAuthPartialConfig, err := getServerIdentificationPartialConfig(configAuthInfo, configClusterInfo)
  170. if err != nil {
  171. return nil, err
  172. }
  173. mergo.MergeWithOverwrite(clientConfig, serverAuthPartialConfig)
  174. }
  175. return clientConfig, nil
  176. }
  177. // clientauth.Info object contain both user identification and server identification. We want different precedence orders for
  178. // both, so we have to split the objects and merge them separately
  179. // we want this order of precedence for the server identification
  180. // 1. configClusterInfo (the final result of command line flags and merged .kubeconfig files)
  181. // 2. configAuthInfo.auth-path (this file can contain information that conflicts with #1, and we want #1 to win the priority)
  182. // 3. load the ~/.kubernetes_auth file as a default
  183. func getServerIdentificationPartialConfig(configAuthInfo clientcmdapi.AuthInfo, configClusterInfo clientcmdapi.Cluster) (*restclient.Config, error) {
  184. mergedConfig := &restclient.Config{}
  185. // configClusterInfo holds the information identify the server provided by .kubeconfig
  186. configClientConfig := &restclient.Config{}
  187. configClientConfig.CAFile = configClusterInfo.CertificateAuthority
  188. configClientConfig.CAData = configClusterInfo.CertificateAuthorityData
  189. configClientConfig.Insecure = configClusterInfo.InsecureSkipTLSVerify
  190. configClientConfig.ServerName = configClusterInfo.TLSServerName
  191. mergo.MergeWithOverwrite(mergedConfig, configClientConfig)
  192. return mergedConfig, nil
  193. }
  194. // clientauth.Info object contain both user identification and server identification. We want different precedence orders for
  195. // both, so we have to split the objects and merge them separately
  196. // we want this order of precedence for user identification
  197. // 1. configAuthInfo minus auth-path (the final result of command line flags and merged .kubeconfig files)
  198. // 2. configAuthInfo.auth-path (this file can contain information that conflicts with #1, and we want #1 to win the priority)
  199. // 3. if there is not enough information to identify the user, load try the ~/.kubernetes_auth file
  200. // 4. if there is not enough information to identify the user, prompt if possible
  201. func (config *DirectClientConfig) getUserIdentificationPartialConfig(configAuthInfo clientcmdapi.AuthInfo, fallbackReader io.Reader, persistAuthConfig restclient.AuthProviderConfigPersister) (*restclient.Config, error) {
  202. mergedConfig := &restclient.Config{}
  203. // blindly overwrite existing values based on precedence
  204. if len(configAuthInfo.Token) > 0 {
  205. mergedConfig.BearerToken = configAuthInfo.Token
  206. mergedConfig.BearerTokenFile = configAuthInfo.TokenFile
  207. } else if len(configAuthInfo.TokenFile) > 0 {
  208. tokenBytes, err := ioutil.ReadFile(configAuthInfo.TokenFile)
  209. if err != nil {
  210. return nil, err
  211. }
  212. mergedConfig.BearerToken = string(tokenBytes)
  213. mergedConfig.BearerTokenFile = configAuthInfo.TokenFile
  214. }
  215. if len(configAuthInfo.Impersonate) > 0 {
  216. mergedConfig.Impersonate = restclient.ImpersonationConfig{
  217. UserName: configAuthInfo.Impersonate,
  218. Groups: configAuthInfo.ImpersonateGroups,
  219. Extra: configAuthInfo.ImpersonateUserExtra,
  220. }
  221. }
  222. if len(configAuthInfo.ClientCertificate) > 0 || len(configAuthInfo.ClientCertificateData) > 0 {
  223. mergedConfig.CertFile = configAuthInfo.ClientCertificate
  224. mergedConfig.CertData = configAuthInfo.ClientCertificateData
  225. mergedConfig.KeyFile = configAuthInfo.ClientKey
  226. mergedConfig.KeyData = configAuthInfo.ClientKeyData
  227. }
  228. if len(configAuthInfo.Username) > 0 || len(configAuthInfo.Password) > 0 {
  229. mergedConfig.Username = configAuthInfo.Username
  230. mergedConfig.Password = configAuthInfo.Password
  231. }
  232. if configAuthInfo.AuthProvider != nil {
  233. mergedConfig.AuthProvider = configAuthInfo.AuthProvider
  234. mergedConfig.AuthConfigPersister = persistAuthConfig
  235. }
  236. if configAuthInfo.Exec != nil {
  237. mergedConfig.ExecProvider = configAuthInfo.Exec
  238. mergedConfig.ExecProvider.InstallHint = cleanANSIEscapeCodes(mergedConfig.ExecProvider.InstallHint)
  239. }
  240. // if there still isn't enough information to authenticate the user, try prompting
  241. if !canIdentifyUser(*mergedConfig) && (fallbackReader != nil) {
  242. if len(config.promptedCredentials.username) > 0 && len(config.promptedCredentials.password) > 0 {
  243. mergedConfig.Username = config.promptedCredentials.username
  244. mergedConfig.Password = config.promptedCredentials.password
  245. return mergedConfig, nil
  246. }
  247. prompter := NewPromptingAuthLoader(fallbackReader)
  248. promptedAuthInfo, err := prompter.Prompt()
  249. if err != nil {
  250. return nil, err
  251. }
  252. promptedConfig := makeUserIdentificationConfig(*promptedAuthInfo)
  253. previouslyMergedConfig := mergedConfig
  254. mergedConfig = &restclient.Config{}
  255. mergo.MergeWithOverwrite(mergedConfig, promptedConfig)
  256. mergo.MergeWithOverwrite(mergedConfig, previouslyMergedConfig)
  257. config.promptedCredentials.username = mergedConfig.Username
  258. config.promptedCredentials.password = mergedConfig.Password
  259. }
  260. return mergedConfig, nil
  261. }
  262. // makeUserIdentificationFieldsConfig returns a client.Config capable of being merged using mergo for only user identification information
  263. func makeUserIdentificationConfig(info clientauth.Info) *restclient.Config {
  264. config := &restclient.Config{}
  265. config.Username = info.User
  266. config.Password = info.Password
  267. config.CertFile = info.CertFile
  268. config.KeyFile = info.KeyFile
  269. config.BearerToken = info.BearerToken
  270. return config
  271. }
  272. func canIdentifyUser(config restclient.Config) bool {
  273. return len(config.Username) > 0 ||
  274. (len(config.CertFile) > 0 || len(config.CertData) > 0) ||
  275. len(config.BearerToken) > 0 ||
  276. config.AuthProvider != nil ||
  277. config.ExecProvider != nil
  278. }
  279. // cleanANSIEscapeCodes takes an arbitrary string and ensures that there are no
  280. // ANSI escape sequences that could put the terminal in a weird state (e.g.,
  281. // "\e[1m" bolds text)
  282. func cleanANSIEscapeCodes(s string) string {
  283. // spaceControlCharacters includes tab, new line, vertical tab, new page, and
  284. // carriage return. These are in the unicode.Cc category, but that category also
  285. // contains ESC (U+001B) which we don't want.
  286. spaceControlCharacters := unicode.RangeTable{
  287. R16: []unicode.Range16{
  288. {Lo: 0x0009, Hi: 0x000D, Stride: 1},
  289. },
  290. }
  291. // Why not make this deny-only (instead of allow-only)? Because unicode.C
  292. // contains newline and tab characters that we want.
  293. allowedRanges := []*unicode.RangeTable{
  294. unicode.L,
  295. unicode.M,
  296. unicode.N,
  297. unicode.P,
  298. unicode.S,
  299. unicode.Z,
  300. &spaceControlCharacters,
  301. }
  302. builder := strings.Builder{}
  303. for _, roon := range s {
  304. if unicode.IsOneOf(allowedRanges, roon) {
  305. builder.WriteRune(roon) // returns nil error, per go doc
  306. } else {
  307. fmt.Fprintf(&builder, "%U", roon)
  308. }
  309. }
  310. return builder.String()
  311. }
  312. // Namespace implements ClientConfig
  313. func (config *DirectClientConfig) Namespace() (string, bool, error) {
  314. if config.overrides != nil && config.overrides.Context.Namespace != "" {
  315. // In the event we have an empty config but we do have a namespace override, we should return
  316. // the namespace override instead of having config.ConfirmUsable() return an error. This allows
  317. // things like in-cluster clients to execute `kubectl get pods --namespace=foo` and have the
  318. // --namespace flag honored instead of being ignored.
  319. return config.overrides.Context.Namespace, true, nil
  320. }
  321. if err := config.ConfirmUsable(); err != nil {
  322. return "", false, err
  323. }
  324. configContext, err := config.getContext()
  325. if err != nil {
  326. return "", false, err
  327. }
  328. if len(configContext.Namespace) == 0 {
  329. return "default", false, nil
  330. }
  331. return configContext.Namespace, false, nil
  332. }
  333. // ConfigAccess implements ClientConfig
  334. func (config *DirectClientConfig) ConfigAccess() ConfigAccess {
  335. return config.configAccess
  336. }
  337. // ConfirmUsable looks a particular context and determines if that particular part of the config is useable. There might still be errors in the config,
  338. // but no errors in the sections requested or referenced. It does not return early so that it can find as many errors as possible.
  339. func (config *DirectClientConfig) ConfirmUsable() error {
  340. validationErrors := make([]error, 0)
  341. var contextName string
  342. if len(config.contextName) != 0 {
  343. contextName = config.contextName
  344. } else {
  345. contextName = config.config.CurrentContext
  346. }
  347. if len(contextName) > 0 {
  348. _, exists := config.config.Contexts[contextName]
  349. if !exists {
  350. validationErrors = append(validationErrors, &errContextNotFound{contextName})
  351. }
  352. }
  353. authInfoName, _ := config.getAuthInfoName()
  354. authInfo, _ := config.getAuthInfo()
  355. validationErrors = append(validationErrors, validateAuthInfo(authInfoName, authInfo)...)
  356. clusterName, _ := config.getClusterName()
  357. cluster, _ := config.getCluster()
  358. validationErrors = append(validationErrors, validateClusterInfo(clusterName, cluster)...)
  359. // when direct client config is specified, and our only error is that no server is defined, we should
  360. // return a standard "no config" error
  361. if len(validationErrors) == 1 && validationErrors[0] == ErrEmptyCluster {
  362. return newErrConfigurationInvalid([]error{ErrEmptyConfig})
  363. }
  364. return newErrConfigurationInvalid(validationErrors)
  365. }
  366. // getContextName returns the default, or user-set context name, and a boolean that indicates
  367. // whether the default context name has been overwritten by a user-set flag, or left as its default value
  368. func (config *DirectClientConfig) getContextName() (string, bool) {
  369. if config.overrides != nil && len(config.overrides.CurrentContext) != 0 {
  370. return config.overrides.CurrentContext, true
  371. }
  372. if len(config.contextName) != 0 {
  373. return config.contextName, false
  374. }
  375. return config.config.CurrentContext, false
  376. }
  377. // getAuthInfoName returns a string containing the current authinfo name for the current context,
  378. // and a boolean indicating whether the default authInfo name is overwritten by a user-set flag, or
  379. // left as its default value
  380. func (config *DirectClientConfig) getAuthInfoName() (string, bool) {
  381. if config.overrides != nil && len(config.overrides.Context.AuthInfo) != 0 {
  382. return config.overrides.Context.AuthInfo, true
  383. }
  384. context, _ := config.getContext()
  385. return context.AuthInfo, false
  386. }
  387. // getClusterName returns a string containing the default, or user-set cluster name, and a boolean
  388. // indicating whether the default clusterName has been overwritten by a user-set flag, or left as
  389. // its default value
  390. func (config *DirectClientConfig) getClusterName() (string, bool) {
  391. if config.overrides != nil && len(config.overrides.Context.Cluster) != 0 {
  392. return config.overrides.Context.Cluster, true
  393. }
  394. context, _ := config.getContext()
  395. return context.Cluster, false
  396. }
  397. // getContext returns the clientcmdapi.Context, or an error if a required context is not found.
  398. func (config *DirectClientConfig) getContext() (clientcmdapi.Context, error) {
  399. contexts := config.config.Contexts
  400. contextName, required := config.getContextName()
  401. mergedContext := clientcmdapi.NewContext()
  402. if configContext, exists := contexts[contextName]; exists {
  403. mergo.MergeWithOverwrite(mergedContext, configContext)
  404. } else if required {
  405. return clientcmdapi.Context{}, fmt.Errorf("context %q does not exist", contextName)
  406. }
  407. if config.overrides != nil {
  408. mergo.MergeWithOverwrite(mergedContext, config.overrides.Context)
  409. }
  410. return *mergedContext, nil
  411. }
  412. // getAuthInfo returns the clientcmdapi.AuthInfo, or an error if a required auth info is not found.
  413. func (config *DirectClientConfig) getAuthInfo() (clientcmdapi.AuthInfo, error) {
  414. authInfos := config.config.AuthInfos
  415. authInfoName, required := config.getAuthInfoName()
  416. mergedAuthInfo := clientcmdapi.NewAuthInfo()
  417. if configAuthInfo, exists := authInfos[authInfoName]; exists {
  418. mergo.MergeWithOverwrite(mergedAuthInfo, configAuthInfo)
  419. } else if required {
  420. return clientcmdapi.AuthInfo{}, fmt.Errorf("auth info %q does not exist", authInfoName)
  421. }
  422. if config.overrides != nil {
  423. mergo.MergeWithOverwrite(mergedAuthInfo, config.overrides.AuthInfo)
  424. }
  425. return *mergedAuthInfo, nil
  426. }
  427. // getCluster returns the clientcmdapi.Cluster, or an error if a required cluster is not found.
  428. func (config *DirectClientConfig) getCluster() (clientcmdapi.Cluster, error) {
  429. clusterInfos := config.config.Clusters
  430. clusterInfoName, required := config.getClusterName()
  431. mergedClusterInfo := clientcmdapi.NewCluster()
  432. if config.overrides != nil {
  433. mergo.MergeWithOverwrite(mergedClusterInfo, config.overrides.ClusterDefaults)
  434. }
  435. if configClusterInfo, exists := clusterInfos[clusterInfoName]; exists {
  436. mergo.MergeWithOverwrite(mergedClusterInfo, configClusterInfo)
  437. } else if required {
  438. return clientcmdapi.Cluster{}, fmt.Errorf("cluster %q does not exist", clusterInfoName)
  439. }
  440. if config.overrides != nil {
  441. mergo.MergeWithOverwrite(mergedClusterInfo, config.overrides.ClusterInfo)
  442. }
  443. // * An override of --insecure-skip-tls-verify=true and no accompanying CA/CA data should clear already-set CA/CA data
  444. // otherwise, a kubeconfig containing a CA reference would return an error that "CA and insecure-skip-tls-verify couldn't both be set".
  445. // * An override of --certificate-authority should also override TLS skip settings and CA data, otherwise existing CA data will take precedence.
  446. if config.overrides != nil {
  447. caLen := len(config.overrides.ClusterInfo.CertificateAuthority)
  448. caDataLen := len(config.overrides.ClusterInfo.CertificateAuthorityData)
  449. if config.overrides.ClusterInfo.InsecureSkipTLSVerify || caLen > 0 || caDataLen > 0 {
  450. mergedClusterInfo.InsecureSkipTLSVerify = config.overrides.ClusterInfo.InsecureSkipTLSVerify
  451. mergedClusterInfo.CertificateAuthority = config.overrides.ClusterInfo.CertificateAuthority
  452. mergedClusterInfo.CertificateAuthorityData = config.overrides.ClusterInfo.CertificateAuthorityData
  453. }
  454. // if the --tls-server-name has been set in overrides, use that value.
  455. // if the --server has been set in overrides, then use the value of --tls-server-name specified on the CLI too. This gives the property
  456. // that setting a --server will effectively clear the KUBECONFIG value of tls-server-name if it is specified on the command line which is
  457. // usually correct.
  458. if config.overrides.ClusterInfo.TLSServerName != "" || config.overrides.ClusterInfo.Server != "" {
  459. mergedClusterInfo.TLSServerName = config.overrides.ClusterInfo.TLSServerName
  460. }
  461. }
  462. return *mergedClusterInfo, nil
  463. }
  464. // inClusterClientConfig makes a config that will work from within a kubernetes cluster container environment.
  465. // Can take options overrides for flags explicitly provided to the command inside the cluster container.
  466. type inClusterClientConfig struct {
  467. overrides *ConfigOverrides
  468. inClusterConfigProvider func() (*restclient.Config, error)
  469. }
  470. var _ ClientConfig = &inClusterClientConfig{}
  471. func (config *inClusterClientConfig) RawConfig() (clientcmdapi.Config, error) {
  472. return clientcmdapi.Config{}, fmt.Errorf("inCluster environment config doesn't support multiple clusters")
  473. }
  474. func (config *inClusterClientConfig) ClientConfig() (*restclient.Config, error) {
  475. if config.inClusterConfigProvider == nil {
  476. config.inClusterConfigProvider = restclient.InClusterConfig
  477. }
  478. icc, err := config.inClusterConfigProvider()
  479. if err != nil {
  480. return nil, err
  481. }
  482. // in-cluster configs only takes a host, token, or CA file
  483. // if any of them were individually provided, overwrite anything else
  484. if config.overrides != nil {
  485. if server := config.overrides.ClusterInfo.Server; len(server) > 0 {
  486. icc.Host = server
  487. }
  488. if len(config.overrides.AuthInfo.Token) > 0 || len(config.overrides.AuthInfo.TokenFile) > 0 {
  489. icc.BearerToken = config.overrides.AuthInfo.Token
  490. icc.BearerTokenFile = config.overrides.AuthInfo.TokenFile
  491. }
  492. if certificateAuthorityFile := config.overrides.ClusterInfo.CertificateAuthority; len(certificateAuthorityFile) > 0 {
  493. icc.TLSClientConfig.CAFile = certificateAuthorityFile
  494. }
  495. }
  496. return icc, err
  497. }
  498. func (config *inClusterClientConfig) Namespace() (string, bool, error) {
  499. // This way assumes you've set the POD_NAMESPACE environment variable using the downward API.
  500. // This check has to be done first for backwards compatibility with the way InClusterConfig was originally set up
  501. if ns := os.Getenv("POD_NAMESPACE"); ns != "" {
  502. return ns, false, nil
  503. }
  504. // Fall back to the namespace associated with the service account token, if available
  505. if data, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
  506. if ns := strings.TrimSpace(string(data)); len(ns) > 0 {
  507. return ns, false, nil
  508. }
  509. }
  510. return "default", false, nil
  511. }
  512. func (config *inClusterClientConfig) ConfigAccess() ConfigAccess {
  513. return NewDefaultClientConfigLoadingRules()
  514. }
  515. // Possible returns true if loading an inside-kubernetes-cluster is possible.
  516. func (config *inClusterClientConfig) Possible() bool {
  517. fi, err := os.Stat("/var/run/secrets/kubernetes.io/serviceaccount/token")
  518. return os.Getenv("KUBERNETES_SERVICE_HOST") != "" &&
  519. os.Getenv("KUBERNETES_SERVICE_PORT") != "" &&
  520. err == nil && !fi.IsDir()
  521. }
  522. // BuildConfigFromFlags is a helper function that builds configs from a master
  523. // url or a kubeconfig filepath. These are passed in as command line flags for cluster
  524. // components. Warnings should reflect this usage. If neither masterUrl or kubeconfigPath
  525. // are passed in we fallback to inClusterConfig. If inClusterConfig fails, we fallback
  526. // to the default config.
  527. func BuildConfigFromFlags(masterUrl, kubeconfigPath string) (*restclient.Config, error) {
  528. if kubeconfigPath == "" && masterUrl == "" {
  529. klog.Warningf("Neither --kubeconfig nor --master was specified. Using the inClusterConfig. This might not work.")
  530. kubeconfig, err := restclient.InClusterConfig()
  531. if err == nil {
  532. return kubeconfig, nil
  533. }
  534. klog.Warning("error creating inClusterConfig, falling back to default config: ", err)
  535. }
  536. return NewNonInteractiveDeferredLoadingClientConfig(
  537. &ClientConfigLoadingRules{ExplicitPath: kubeconfigPath},
  538. &ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: masterUrl}}).ClientConfig()
  539. }
  540. // BuildConfigFromKubeconfigGetter is a helper function that builds configs from a master
  541. // url and a kubeconfigGetter.
  542. func BuildConfigFromKubeconfigGetter(masterUrl string, kubeconfigGetter KubeconfigGetter) (*restclient.Config, error) {
  543. // TODO: We do not need a DeferredLoader here. Refactor code and see if we can use DirectClientConfig here.
  544. cc := NewNonInteractiveDeferredLoadingClientConfig(
  545. &ClientConfigGetter{kubeconfigGetter: kubeconfigGetter},
  546. &ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: masterUrl}})
  547. return cc.ClientConfig()
  548. }