loader.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. "os"
  19. "path"
  20. "path/filepath"
  21. goruntime "runtime"
  22. "strings"
  23. "github.com/golang/glog"
  24. "github.com/imdario/mergo"
  25. "k8s.io/kubernetes/pkg/api/unversioned"
  26. clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
  27. clientcmdlatest "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/latest"
  28. "k8s.io/kubernetes/pkg/runtime"
  29. utilerrors "k8s.io/kubernetes/pkg/util/errors"
  30. "k8s.io/kubernetes/pkg/util/homedir"
  31. )
  32. const (
  33. RecommendedConfigPathFlag = "kubeconfig"
  34. RecommendedConfigPathEnvVar = "KUBECONFIG"
  35. RecommendedHomeDir = ".kube"
  36. RecommendedFileName = "config"
  37. RecommendedSchemaName = "schema"
  38. )
  39. var RecommendedHomeFile = path.Join(homedir.HomeDir(), RecommendedHomeDir, RecommendedFileName)
  40. var RecommendedSchemaFile = path.Join(homedir.HomeDir(), RecommendedHomeDir, RecommendedSchemaName)
  41. // currentMigrationRules returns a map that holds the history of recommended home directories used in previous versions.
  42. // Any future changes to RecommendedHomeFile and related are expected to add a migration rule here, in order to make
  43. // sure existing config files are migrated to their new locations properly.
  44. func currentMigrationRules() map[string]string {
  45. oldRecommendedHomeFile := path.Join(os.Getenv("HOME"), "/.kube/.kubeconfig")
  46. oldRecommendedWindowsHomeFile := path.Join(os.Getenv("HOME"), RecommendedHomeDir, RecommendedFileName)
  47. migrationRules := map[string]string{}
  48. migrationRules[RecommendedHomeFile] = oldRecommendedHomeFile
  49. if goruntime.GOOS == "windows" {
  50. migrationRules[RecommendedHomeFile] = oldRecommendedWindowsHomeFile
  51. }
  52. return migrationRules
  53. }
  54. type ClientConfigLoader interface {
  55. ConfigAccess
  56. Load() (*clientcmdapi.Config, error)
  57. }
  58. type KubeconfigGetter func() (*clientcmdapi.Config, error)
  59. type ClientConfigGetter struct {
  60. kubeconfigGetter KubeconfigGetter
  61. }
  62. // ClientConfigGetter implements the ClientConfigLoader interface.
  63. var _ ClientConfigLoader = &ClientConfigGetter{}
  64. func (g *ClientConfigGetter) Load() (*clientcmdapi.Config, error) {
  65. return g.kubeconfigGetter()
  66. }
  67. func (g *ClientConfigGetter) GetLoadingPrecedence() []string {
  68. return nil
  69. }
  70. func (g *ClientConfigGetter) GetStartingConfig() (*clientcmdapi.Config, error) {
  71. return g.kubeconfigGetter()
  72. }
  73. func (g *ClientConfigGetter) GetDefaultFilename() string {
  74. return ""
  75. }
  76. func (g *ClientConfigGetter) IsExplicitFile() bool {
  77. return false
  78. }
  79. func (g *ClientConfigGetter) GetExplicitFile() string {
  80. return ""
  81. }
  82. // ClientConfigLoadingRules is an ExplicitPath and string slice of specific locations that are used for merging together a Config
  83. // Callers can put the chain together however they want, but we'd recommend:
  84. // EnvVarPathFiles if set (a list of files if set) OR the HomeDirectoryPath
  85. // ExplicitPath is special, because if a user specifically requests a certain file be used and error is reported if thie file is not present
  86. type ClientConfigLoadingRules struct {
  87. ExplicitPath string
  88. Precedence []string
  89. // MigrationRules is a map of destination files to source files. If a destination file is not present, then the source file is checked.
  90. // If the source file is present, then it is copied to the destination file BEFORE any further loading happens.
  91. MigrationRules map[string]string
  92. // DoNotResolvePaths indicates whether or not to resolve paths with respect to the originating files. This is phrased as a negative so
  93. // that a default object that doesn't set this will usually get the behavior it wants.
  94. DoNotResolvePaths bool
  95. }
  96. // ClientConfigLoadingRules implements the ClientConfigLoader interface.
  97. var _ ClientConfigLoader = &ClientConfigLoadingRules{}
  98. // NewDefaultClientConfigLoadingRules returns a ClientConfigLoadingRules object with default fields filled in. You are not required to
  99. // use this constructor
  100. func NewDefaultClientConfigLoadingRules() *ClientConfigLoadingRules {
  101. chain := []string{}
  102. envVarFiles := os.Getenv(RecommendedConfigPathEnvVar)
  103. if len(envVarFiles) != 0 {
  104. chain = append(chain, filepath.SplitList(envVarFiles)...)
  105. } else {
  106. chain = append(chain, RecommendedHomeFile)
  107. }
  108. return &ClientConfigLoadingRules{
  109. Precedence: chain,
  110. MigrationRules: currentMigrationRules(),
  111. }
  112. }
  113. // Load starts by running the MigrationRules and then
  114. // takes the loading rules and returns a Config object based on following rules.
  115. // if the ExplicitPath, return the unmerged explicit file
  116. // Otherwise, return a merged config based on the Precedence slice
  117. // A missing ExplicitPath file produces an error. Empty filenames or other missing files are ignored.
  118. // Read errors or files with non-deserializable content produce errors.
  119. // The first file to set a particular map key wins and map key's value is never changed.
  120. // BUT, if you set a struct value that is NOT contained inside of map, the value WILL be changed.
  121. // This results in some odd looking logic to merge in one direction, merge in the other, and then merge the two.
  122. // It also means that if two files specify a "red-user", only values from the first file's red-user are used. Even
  123. // non-conflicting entries from the second file's "red-user" are discarded.
  124. // Relative paths inside of the .kubeconfig files are resolved against the .kubeconfig file's parent folder
  125. // and only absolute file paths are returned.
  126. func (rules *ClientConfigLoadingRules) Load() (*clientcmdapi.Config, error) {
  127. if err := rules.Migrate(); err != nil {
  128. return nil, err
  129. }
  130. errlist := []error{}
  131. kubeConfigFiles := []string{}
  132. // Make sure a file we were explicitly told to use exists
  133. if len(rules.ExplicitPath) > 0 {
  134. if _, err := os.Stat(rules.ExplicitPath); os.IsNotExist(err) {
  135. return nil, err
  136. }
  137. kubeConfigFiles = append(kubeConfigFiles, rules.ExplicitPath)
  138. } else {
  139. kubeConfigFiles = append(kubeConfigFiles, rules.Precedence...)
  140. }
  141. kubeconfigs := []*clientcmdapi.Config{}
  142. // read and cache the config files so that we only look at them once
  143. for _, filename := range kubeConfigFiles {
  144. if len(filename) == 0 {
  145. // no work to do
  146. continue
  147. }
  148. config, err := LoadFromFile(filename)
  149. if os.IsNotExist(err) {
  150. // skip missing files
  151. continue
  152. }
  153. if err != nil {
  154. errlist = append(errlist, fmt.Errorf("Error loading config file \"%s\": %v", filename, err))
  155. continue
  156. }
  157. kubeconfigs = append(kubeconfigs, config)
  158. }
  159. // first merge all of our maps
  160. mapConfig := clientcmdapi.NewConfig()
  161. for _, kubeconfig := range kubeconfigs {
  162. mergo.Merge(mapConfig, kubeconfig)
  163. }
  164. // merge all of the struct values in the reverse order so that priority is given correctly
  165. // errors are not added to the list the second time
  166. nonMapConfig := clientcmdapi.NewConfig()
  167. for i := len(kubeconfigs) - 1; i >= 0; i-- {
  168. kubeconfig := kubeconfigs[i]
  169. mergo.Merge(nonMapConfig, kubeconfig)
  170. }
  171. // since values are overwritten, but maps values are not, we can merge the non-map config on top of the map config and
  172. // get the values we expect.
  173. config := clientcmdapi.NewConfig()
  174. mergo.Merge(config, mapConfig)
  175. mergo.Merge(config, nonMapConfig)
  176. if rules.ResolvePaths() {
  177. if err := ResolveLocalPaths(config); err != nil {
  178. errlist = append(errlist, err)
  179. }
  180. }
  181. return config, utilerrors.NewAggregate(errlist)
  182. }
  183. // Migrate uses the MigrationRules map. If a destination file is not present, then the source file is checked.
  184. // If the source file is present, then it is copied to the destination file BEFORE any further loading happens.
  185. func (rules *ClientConfigLoadingRules) Migrate() error {
  186. if rules.MigrationRules == nil {
  187. return nil
  188. }
  189. for destination, source := range rules.MigrationRules {
  190. if _, err := os.Stat(destination); err == nil {
  191. // if the destination already exists, do nothing
  192. continue
  193. } else if os.IsPermission(err) {
  194. // if we can't access the file, skip it
  195. continue
  196. } else if !os.IsNotExist(err) {
  197. // if we had an error other than non-existence, fail
  198. return err
  199. }
  200. if sourceInfo, err := os.Stat(source); err != nil {
  201. if os.IsNotExist(err) || os.IsPermission(err) {
  202. // if the source file doesn't exist or we can't access it, there's no work to do.
  203. continue
  204. }
  205. // if we had an error other than non-existence, fail
  206. return err
  207. } else if sourceInfo.IsDir() {
  208. return fmt.Errorf("cannot migrate %v to %v because it is a directory", source, destination)
  209. }
  210. in, err := os.Open(source)
  211. if err != nil {
  212. return err
  213. }
  214. defer in.Close()
  215. out, err := os.Create(destination)
  216. if err != nil {
  217. return err
  218. }
  219. defer out.Close()
  220. if _, err = io.Copy(out, in); err != nil {
  221. return err
  222. }
  223. }
  224. return nil
  225. }
  226. // GetLoadingPrecedence implements ConfigAccess
  227. func (rules *ClientConfigLoadingRules) GetLoadingPrecedence() []string {
  228. return rules.Precedence
  229. }
  230. // GetStartingConfig implements ConfigAccess
  231. func (rules *ClientConfigLoadingRules) GetStartingConfig() (*clientcmdapi.Config, error) {
  232. clientConfig := NewNonInteractiveDeferredLoadingClientConfig(rules, &ConfigOverrides{})
  233. rawConfig, err := clientConfig.RawConfig()
  234. if os.IsNotExist(err) {
  235. return clientcmdapi.NewConfig(), nil
  236. }
  237. if err != nil {
  238. return nil, err
  239. }
  240. return &rawConfig, nil
  241. }
  242. // GetDefaultFilename implements ConfigAccess
  243. func (rules *ClientConfigLoadingRules) GetDefaultFilename() string {
  244. // Explicit file if we have one.
  245. if rules.IsExplicitFile() {
  246. return rules.GetExplicitFile()
  247. }
  248. // Otherwise, first existing file from precedence.
  249. for _, filename := range rules.GetLoadingPrecedence() {
  250. if _, err := os.Stat(filename); err == nil {
  251. return filename
  252. }
  253. }
  254. // If none exists, use the first from precedence.
  255. if len(rules.Precedence) > 0 {
  256. return rules.Precedence[0]
  257. }
  258. return ""
  259. }
  260. // IsExplicitFile implements ConfigAccess
  261. func (rules *ClientConfigLoadingRules) IsExplicitFile() bool {
  262. return len(rules.ExplicitPath) > 0
  263. }
  264. // GetExplicitFile implements ConfigAccess
  265. func (rules *ClientConfigLoadingRules) GetExplicitFile() string {
  266. return rules.ExplicitPath
  267. }
  268. // LoadFromFile takes a filename and deserializes the contents into Config object
  269. func LoadFromFile(filename string) (*clientcmdapi.Config, error) {
  270. kubeconfigBytes, err := ioutil.ReadFile(filename)
  271. if err != nil {
  272. return nil, err
  273. }
  274. config, err := Load(kubeconfigBytes)
  275. if err != nil {
  276. return nil, err
  277. }
  278. glog.V(6).Infoln("Config loaded from file", filename)
  279. // set LocationOfOrigin on every Cluster, User, and Context
  280. for key, obj := range config.AuthInfos {
  281. obj.LocationOfOrigin = filename
  282. config.AuthInfos[key] = obj
  283. }
  284. for key, obj := range config.Clusters {
  285. obj.LocationOfOrigin = filename
  286. config.Clusters[key] = obj
  287. }
  288. for key, obj := range config.Contexts {
  289. obj.LocationOfOrigin = filename
  290. config.Contexts[key] = obj
  291. }
  292. if config.AuthInfos == nil {
  293. config.AuthInfos = map[string]*clientcmdapi.AuthInfo{}
  294. }
  295. if config.Clusters == nil {
  296. config.Clusters = map[string]*clientcmdapi.Cluster{}
  297. }
  298. if config.Contexts == nil {
  299. config.Contexts = map[string]*clientcmdapi.Context{}
  300. }
  301. return config, nil
  302. }
  303. // Load takes a byte slice and deserializes the contents into Config object.
  304. // Encapsulates deserialization without assuming the source is a file.
  305. func Load(data []byte) (*clientcmdapi.Config, error) {
  306. config := clientcmdapi.NewConfig()
  307. // if there's no data in a file, return the default object instead of failing (DecodeInto reject empty input)
  308. if len(data) == 0 {
  309. return config, nil
  310. }
  311. decoded, _, err := clientcmdlatest.Codec.Decode(data, &unversioned.GroupVersionKind{Version: clientcmdlatest.Version, Kind: "Config"}, config)
  312. if err != nil {
  313. return nil, err
  314. }
  315. return decoded.(*clientcmdapi.Config), nil
  316. }
  317. // WriteToFile serializes the config to yaml and writes it out to a file. If not present, it creates the file with the mode 0600. If it is present
  318. // it stomps the contents
  319. func WriteToFile(config clientcmdapi.Config, filename string) error {
  320. content, err := Write(config)
  321. if err != nil {
  322. return err
  323. }
  324. dir := filepath.Dir(filename)
  325. if _, err := os.Stat(dir); os.IsNotExist(err) {
  326. if err = os.MkdirAll(dir, 0755); err != nil {
  327. return err
  328. }
  329. }
  330. if err := ioutil.WriteFile(filename, content, 0600); err != nil {
  331. return err
  332. }
  333. return nil
  334. }
  335. func lockFile(filename string) error {
  336. // TODO: find a way to do this with actual file locks. Will
  337. // probably need seperate solution for windows and linux.
  338. // Make sure the dir exists before we try to create a lock file.
  339. dir := filepath.Dir(filename)
  340. if _, err := os.Stat(dir); os.IsNotExist(err) {
  341. if err = os.MkdirAll(dir, 0755); err != nil {
  342. return err
  343. }
  344. }
  345. f, err := os.OpenFile(lockName(filename), os.O_CREATE|os.O_EXCL, 0)
  346. if err != nil {
  347. return err
  348. }
  349. f.Close()
  350. return nil
  351. }
  352. func unlockFile(filename string) error {
  353. return os.Remove(lockName(filename))
  354. }
  355. func lockName(filename string) string {
  356. return filename + ".lock"
  357. }
  358. // Write serializes the config to yaml.
  359. // Encapsulates serialization without assuming the destination is a file.
  360. func Write(config clientcmdapi.Config) ([]byte, error) {
  361. return runtime.Encode(clientcmdlatest.Codec, &config)
  362. }
  363. func (rules ClientConfigLoadingRules) ResolvePaths() bool {
  364. return !rules.DoNotResolvePaths
  365. }
  366. // ResolveLocalPaths resolves all relative paths in the config object with respect to the stanza's LocationOfOrigin
  367. // this cannot be done directly inside of LoadFromFile because doing so there would make it impossible to load a file without
  368. // modification of its contents.
  369. func ResolveLocalPaths(config *clientcmdapi.Config) error {
  370. for _, cluster := range config.Clusters {
  371. if len(cluster.LocationOfOrigin) == 0 {
  372. continue
  373. }
  374. base, err := filepath.Abs(filepath.Dir(cluster.LocationOfOrigin))
  375. if err != nil {
  376. return fmt.Errorf("Could not determine the absolute path of config file %s: %v", cluster.LocationOfOrigin, err)
  377. }
  378. if err := ResolvePaths(GetClusterFileReferences(cluster), base); err != nil {
  379. return err
  380. }
  381. }
  382. for _, authInfo := range config.AuthInfos {
  383. if len(authInfo.LocationOfOrigin) == 0 {
  384. continue
  385. }
  386. base, err := filepath.Abs(filepath.Dir(authInfo.LocationOfOrigin))
  387. if err != nil {
  388. return fmt.Errorf("Could not determine the absolute path of config file %s: %v", authInfo.LocationOfOrigin, err)
  389. }
  390. if err := ResolvePaths(GetAuthInfoFileReferences(authInfo), base); err != nil {
  391. return err
  392. }
  393. }
  394. return nil
  395. }
  396. // RelativizeClusterLocalPaths first absolutizes the paths by calling ResolveLocalPaths. This assumes that any NEW path is already
  397. // absolute, but any existing path will be resolved relative to LocationOfOrigin
  398. func RelativizeClusterLocalPaths(cluster *clientcmdapi.Cluster) error {
  399. if len(cluster.LocationOfOrigin) == 0 {
  400. return fmt.Errorf("no location of origin for %s", cluster.Server)
  401. }
  402. base, err := filepath.Abs(filepath.Dir(cluster.LocationOfOrigin))
  403. if err != nil {
  404. return fmt.Errorf("could not determine the absolute path of config file %s: %v", cluster.LocationOfOrigin, err)
  405. }
  406. if err := ResolvePaths(GetClusterFileReferences(cluster), base); err != nil {
  407. return err
  408. }
  409. if err := RelativizePathWithNoBacksteps(GetClusterFileReferences(cluster), base); err != nil {
  410. return err
  411. }
  412. return nil
  413. }
  414. // RelativizeAuthInfoLocalPaths first absolutizes the paths by calling ResolveLocalPaths. This assumes that any NEW path is already
  415. // absolute, but any existing path will be resolved relative to LocationOfOrigin
  416. func RelativizeAuthInfoLocalPaths(authInfo *clientcmdapi.AuthInfo) error {
  417. if len(authInfo.LocationOfOrigin) == 0 {
  418. return fmt.Errorf("no location of origin for %v", authInfo)
  419. }
  420. base, err := filepath.Abs(filepath.Dir(authInfo.LocationOfOrigin))
  421. if err != nil {
  422. return fmt.Errorf("could not determine the absolute path of config file %s: %v", authInfo.LocationOfOrigin, err)
  423. }
  424. if err := ResolvePaths(GetAuthInfoFileReferences(authInfo), base); err != nil {
  425. return err
  426. }
  427. if err := RelativizePathWithNoBacksteps(GetAuthInfoFileReferences(authInfo), base); err != nil {
  428. return err
  429. }
  430. return nil
  431. }
  432. func RelativizeConfigPaths(config *clientcmdapi.Config, base string) error {
  433. return RelativizePathWithNoBacksteps(GetConfigFileReferences(config), base)
  434. }
  435. func ResolveConfigPaths(config *clientcmdapi.Config, base string) error {
  436. return ResolvePaths(GetConfigFileReferences(config), base)
  437. }
  438. func GetConfigFileReferences(config *clientcmdapi.Config) []*string {
  439. refs := []*string{}
  440. for _, cluster := range config.Clusters {
  441. refs = append(refs, GetClusterFileReferences(cluster)...)
  442. }
  443. for _, authInfo := range config.AuthInfos {
  444. refs = append(refs, GetAuthInfoFileReferences(authInfo)...)
  445. }
  446. return refs
  447. }
  448. func GetClusterFileReferences(cluster *clientcmdapi.Cluster) []*string {
  449. return []*string{&cluster.CertificateAuthority}
  450. }
  451. func GetAuthInfoFileReferences(authInfo *clientcmdapi.AuthInfo) []*string {
  452. return []*string{&authInfo.ClientCertificate, &authInfo.ClientKey, &authInfo.TokenFile}
  453. }
  454. // ResolvePaths updates the given refs to be absolute paths, relative to the given base directory
  455. func ResolvePaths(refs []*string, base string) error {
  456. for _, ref := range refs {
  457. // Don't resolve empty paths
  458. if len(*ref) > 0 {
  459. // Don't resolve absolute paths
  460. if !filepath.IsAbs(*ref) {
  461. *ref = filepath.Join(base, *ref)
  462. }
  463. }
  464. }
  465. return nil
  466. }
  467. // RelativizePathWithNoBacksteps updates the given refs to be relative paths, relative to the given base directory as long as they do not require backsteps.
  468. // Any path requiring a backstep is left as-is as long it is absolute. Any non-absolute path that can't be relativized produces an error
  469. func RelativizePathWithNoBacksteps(refs []*string, base string) error {
  470. for _, ref := range refs {
  471. // Don't relativize empty paths
  472. if len(*ref) > 0 {
  473. rel, err := MakeRelative(*ref, base)
  474. if err != nil {
  475. return err
  476. }
  477. // if we have a backstep, don't mess with the path
  478. if strings.HasPrefix(rel, "../") {
  479. if filepath.IsAbs(*ref) {
  480. continue
  481. }
  482. return fmt.Errorf("%v requires backsteps and is not absolute", *ref)
  483. }
  484. *ref = rel
  485. }
  486. }
  487. return nil
  488. }
  489. func MakeRelative(path, base string) (string, error) {
  490. if len(path) > 0 {
  491. rel, err := filepath.Rel(base, path)
  492. if err != nil {
  493. return path, err
  494. }
  495. return rel, nil
  496. }
  497. return path, nil
  498. }