configfiles.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright 2017 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 configfiles
  14. import (
  15. "fmt"
  16. "path/filepath"
  17. "k8s.io/apimachinery/pkg/runtime/serializer"
  18. kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
  19. kubeletscheme "k8s.io/kubernetes/pkg/kubelet/apis/config/scheme"
  20. utilcodec "k8s.io/kubernetes/pkg/kubelet/kubeletconfig/util/codec"
  21. utilfs "k8s.io/kubernetes/pkg/util/filesystem"
  22. )
  23. // Loader loads configuration from a storage layer
  24. type Loader interface {
  25. // Load loads and returns the KubeletConfiguration from the storage layer, or an error if a configuration could not be loaded
  26. Load() (*kubeletconfig.KubeletConfiguration, error)
  27. }
  28. // fsLoader loads configuration from `configDir`
  29. type fsLoader struct {
  30. // fs is the filesystem where the config files exist; can be mocked for testing
  31. fs utilfs.Filesystem
  32. // kubeletCodecs is the scheme used to decode config files
  33. kubeletCodecs *serializer.CodecFactory
  34. // kubeletFile is an absolute path to the file containing a serialized KubeletConfiguration
  35. kubeletFile string
  36. }
  37. // NewFsLoader returns a Loader that loads a KubeletConfiguration from the `kubeletFile`
  38. func NewFsLoader(fs utilfs.Filesystem, kubeletFile string) (Loader, error) {
  39. _, kubeletCodecs, err := kubeletscheme.NewSchemeAndCodecs()
  40. if err != nil {
  41. return nil, err
  42. }
  43. return &fsLoader{
  44. fs: fs,
  45. kubeletCodecs: kubeletCodecs,
  46. kubeletFile: kubeletFile,
  47. }, nil
  48. }
  49. func (loader *fsLoader) Load() (*kubeletconfig.KubeletConfiguration, error) {
  50. data, err := loader.fs.ReadFile(loader.kubeletFile)
  51. if err != nil {
  52. return nil, fmt.Errorf("failed to read kubelet config file %q, error: %v", loader.kubeletFile, err)
  53. }
  54. // no configuration is an error, some parameters are required
  55. if len(data) == 0 {
  56. return nil, fmt.Errorf("kubelet config file %q was empty", loader.kubeletFile)
  57. }
  58. kc, err := utilcodec.DecodeKubeletConfiguration(loader.kubeletCodecs, data)
  59. if err != nil {
  60. return nil, err
  61. }
  62. // make all paths absolute
  63. resolveRelativePaths(kubeletconfig.KubeletConfigurationPathRefs(kc), filepath.Dir(loader.kubeletFile))
  64. return kc, nil
  65. }
  66. // resolveRelativePaths makes relative paths absolute by resolving them against `root`
  67. func resolveRelativePaths(paths []*string, root string) {
  68. for _, path := range paths {
  69. // leave empty paths alone, "no path" is a valid input
  70. // do not attempt to resolve paths that are already absolute
  71. if len(*path) > 0 && !filepath.IsAbs(*path) {
  72. *path = filepath.Join(root, *path)
  73. }
  74. }
  75. }