fsstore.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 store
  14. import (
  15. "fmt"
  16. "path/filepath"
  17. "time"
  18. kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
  19. "k8s.io/kubernetes/pkg/kubelet/kubeletconfig/checkpoint"
  20. "k8s.io/kubernetes/pkg/kubelet/kubeletconfig/configfiles"
  21. utilfiles "k8s.io/kubernetes/pkg/kubelet/kubeletconfig/util/files"
  22. utillog "k8s.io/kubernetes/pkg/kubelet/kubeletconfig/util/log"
  23. utilfs "k8s.io/kubernetes/pkg/util/filesystem"
  24. )
  25. const (
  26. metaDir = "meta"
  27. assignedFile = "assigned"
  28. lastKnownGoodFile = "last-known-good"
  29. checkpointsDir = "checkpoints"
  30. )
  31. // fsStore is for tracking checkpoints in the local filesystem, implements Store
  32. type fsStore struct {
  33. // fs is the filesystem to use for storage operations; can be mocked for testing
  34. fs utilfs.Filesystem
  35. // dir is the absolute path to the storage directory for fsStore
  36. dir string
  37. }
  38. var _ Store = (*fsStore)(nil)
  39. // NewFsStore returns a Store that saves its data in dir
  40. func NewFsStore(fs utilfs.Filesystem, dir string) Store {
  41. return &fsStore{
  42. fs: fs,
  43. dir: dir,
  44. }
  45. }
  46. func (s *fsStore) Initialize() error {
  47. utillog.Infof("initializing config checkpoints directory %q", s.dir)
  48. // ensure top-level dir for store
  49. if err := utilfiles.EnsureDir(s.fs, s.dir); err != nil {
  50. return err
  51. }
  52. // ensure metadata directory and reference files (tracks assigned and lkg configs)
  53. if err := utilfiles.EnsureDir(s.fs, filepath.Join(s.dir, metaDir)); err != nil {
  54. return err
  55. }
  56. if err := utilfiles.EnsureFile(s.fs, s.metaPath(assignedFile)); err != nil {
  57. return err
  58. }
  59. if err := utilfiles.EnsureFile(s.fs, s.metaPath(lastKnownGoodFile)); err != nil {
  60. return err
  61. }
  62. // ensure checkpoints directory (saves unpacked payloads in subdirectories named after payload UID)
  63. return utilfiles.EnsureDir(s.fs, filepath.Join(s.dir, checkpointsDir))
  64. }
  65. func (s *fsStore) Exists(source checkpoint.RemoteConfigSource) (bool, error) {
  66. const errfmt = "failed to determine whether checkpoint exists for source %s, UID: %s, ResourceVersion: %s exists, error: %v"
  67. if len(source.UID()) == 0 {
  68. return false, fmt.Errorf(errfmt, source.APIPath(), source.UID(), source.ResourceVersion(), "empty UID is ambiguous")
  69. }
  70. if len(source.ResourceVersion()) == 0 {
  71. return false, fmt.Errorf(errfmt, source.APIPath(), source.UID(), source.ResourceVersion(), "empty ResourceVersion is ambiguous")
  72. }
  73. // we check whether the directory was created for the resource
  74. ok, err := utilfiles.DirExists(s.fs, s.checkpointPath(source.UID(), source.ResourceVersion()))
  75. if err != nil {
  76. return false, fmt.Errorf(errfmt, source.APIPath(), source.UID(), source.ResourceVersion(), err)
  77. }
  78. return ok, nil
  79. }
  80. func (s *fsStore) Save(payload checkpoint.Payload) error {
  81. // Note: Payload interface guarantees UID() and ResourceVersion() to be non-empty
  82. path := s.checkpointPath(payload.UID(), payload.ResourceVersion())
  83. // ensure the parent dir (checkpoints/uid) exists, since ReplaceDir requires the parent of the replace
  84. // to exist, and we checkpoint as checkpoints/uid/resourceVersion/files-from-configmap
  85. if err := utilfiles.EnsureDir(s.fs, filepath.Dir(path)); err != nil {
  86. return err
  87. }
  88. // save the checkpoint's files in the appropriate checkpoint dir
  89. return utilfiles.ReplaceDir(s.fs, path, payload.Files())
  90. }
  91. func (s *fsStore) Load(source checkpoint.RemoteConfigSource) (*kubeletconfig.KubeletConfiguration, error) {
  92. sourceFmt := fmt.Sprintf("%s, UID: %s, ResourceVersion: %s", source.APIPath(), source.UID(), source.ResourceVersion())
  93. // check if a checkpoint exists for the source
  94. if ok, err := s.Exists(source); err != nil {
  95. return nil, err
  96. } else if !ok {
  97. return nil, fmt.Errorf("no checkpoint for source %s", sourceFmt)
  98. }
  99. // load the kubelet config file
  100. utillog.Infof("loading Kubelet configuration checkpoint for source %s", sourceFmt)
  101. loader, err := configfiles.NewFsLoader(s.fs, filepath.Join(s.checkpointPath(source.UID(), source.ResourceVersion()), source.KubeletFilename()))
  102. if err != nil {
  103. return nil, err
  104. }
  105. kc, err := loader.Load()
  106. if err != nil {
  107. return nil, err
  108. }
  109. return kc, nil
  110. }
  111. func (s *fsStore) AssignedModified() (time.Time, error) {
  112. path := s.metaPath(assignedFile)
  113. info, err := s.fs.Stat(path)
  114. if err != nil {
  115. return time.Time{}, fmt.Errorf("failed to stat %q while checking modification time, error: %v", path, err)
  116. }
  117. return info.ModTime(), nil
  118. }
  119. func (s *fsStore) Assigned() (checkpoint.RemoteConfigSource, error) {
  120. return readRemoteConfigSource(s.fs, s.metaPath(assignedFile))
  121. }
  122. func (s *fsStore) LastKnownGood() (checkpoint.RemoteConfigSource, error) {
  123. return readRemoteConfigSource(s.fs, s.metaPath(lastKnownGoodFile))
  124. }
  125. func (s *fsStore) SetAssigned(source checkpoint.RemoteConfigSource) error {
  126. return writeRemoteConfigSource(s.fs, s.metaPath(assignedFile), source)
  127. }
  128. func (s *fsStore) SetLastKnownGood(source checkpoint.RemoteConfigSource) error {
  129. return writeRemoteConfigSource(s.fs, s.metaPath(lastKnownGoodFile), source)
  130. }
  131. func (s *fsStore) Reset() (bool, error) {
  132. return reset(s)
  133. }
  134. func (s *fsStore) checkpointPath(uid, resourceVersion string) string {
  135. return filepath.Join(s.dir, checkpointsDir, uid, resourceVersion)
  136. }
  137. func (s *fsStore) metaPath(name string) string {
  138. return filepath.Join(s.dir, metaDir, name)
  139. }
  140. func readRemoteConfigSource(fs utilfs.Filesystem, path string) (checkpoint.RemoteConfigSource, error) {
  141. data, err := fs.ReadFile(path)
  142. if err != nil {
  143. return nil, err
  144. } else if len(data) == 0 {
  145. return nil, nil
  146. }
  147. return checkpoint.DecodeRemoteConfigSource(data)
  148. }
  149. func writeRemoteConfigSource(fs utilfs.Filesystem, path string, source checkpoint.RemoteConfigSource) error {
  150. // if nil, reset the file
  151. if source == nil {
  152. return utilfiles.ReplaceFile(fs, path, []byte{})
  153. }
  154. // check that UID and ResourceVersion are non-empty,
  155. // error to save reference if the checkpoint can't be fully resolved
  156. if source.UID() == "" {
  157. return fmt.Errorf("failed to write RemoteConfigSource, empty UID is ambiguous")
  158. }
  159. if source.ResourceVersion() == "" {
  160. return fmt.Errorf("failed to write RemoteConfigSource, empty ResourceVersion is ambiguous")
  161. }
  162. // encode the source and save it to the file
  163. data, err := source.Encode()
  164. if err != nil {
  165. return err
  166. }
  167. return utilfiles.ReplaceFile(fs, path, data)
  168. }