kubelet_volumes.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Copyright 2016 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 kubelet
  14. import (
  15. "fmt"
  16. v1 "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/types"
  18. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  19. "k8s.io/apimachinery/pkg/util/sets"
  20. "k8s.io/klog"
  21. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  22. "k8s.io/kubernetes/pkg/util/removeall"
  23. "k8s.io/kubernetes/pkg/volume"
  24. volumetypes "k8s.io/kubernetes/pkg/volume/util/types"
  25. )
  26. // ListVolumesForPod returns a map of the mounted volumes for the given pod.
  27. // The key in the map is the OuterVolumeSpecName (i.e. pod.Spec.Volumes[x].Name)
  28. func (kl *Kubelet) ListVolumesForPod(podUID types.UID) (map[string]volume.Volume, bool) {
  29. volumesToReturn := make(map[string]volume.Volume)
  30. podVolumes := kl.volumeManager.GetMountedVolumesForPod(
  31. volumetypes.UniquePodName(podUID))
  32. for outerVolumeSpecName, volume := range podVolumes {
  33. // TODO: volume.Mounter could be nil if volume object is recovered
  34. // from reconciler's sync state process. PR 33616 will fix this problem
  35. // to create Mounter object when recovering volume state.
  36. if volume.Mounter == nil {
  37. continue
  38. }
  39. volumesToReturn[outerVolumeSpecName] = volume.Mounter
  40. }
  41. return volumesToReturn, len(volumesToReturn) > 0
  42. }
  43. // podVolumesExist checks with the volume manager and returns true any of the
  44. // pods for the specified volume are mounted.
  45. func (kl *Kubelet) podVolumesExist(podUID types.UID) bool {
  46. if mountedVolumes :=
  47. kl.volumeManager.GetMountedVolumesForPod(
  48. volumetypes.UniquePodName(podUID)); len(mountedVolumes) > 0 {
  49. return true
  50. }
  51. // TODO: This checks pod volume paths and whether they are mounted. If checking returns error, podVolumesExist will return true
  52. // which means we consider volumes might exist and requires further checking.
  53. // There are some volume plugins such as flexvolume might not have mounts. See issue #61229
  54. volumePaths, err := kl.getMountedVolumePathListFromDisk(podUID)
  55. if err != nil {
  56. klog.Errorf("pod %q found, but error %v occurred during checking mounted volumes from disk", podUID, err)
  57. return true
  58. }
  59. if len(volumePaths) > 0 {
  60. klog.V(4).Infof("pod %q found, but volumes are still mounted on disk %v", podUID, volumePaths)
  61. return true
  62. }
  63. return false
  64. }
  65. // newVolumeMounterFromPlugins attempts to find a plugin by volume spec, pod
  66. // and volume options and then creates a Mounter.
  67. // Returns a valid mounter or an error.
  68. func (kl *Kubelet) newVolumeMounterFromPlugins(spec *volume.Spec, pod *v1.Pod, opts volume.VolumeOptions) (volume.Mounter, error) {
  69. plugin, err := kl.volumePluginMgr.FindPluginBySpec(spec)
  70. if err != nil {
  71. return nil, fmt.Errorf("can't use volume plugins for %s: %v", spec.Name(), err)
  72. }
  73. physicalMounter, err := plugin.NewMounter(spec, pod, opts)
  74. if err != nil {
  75. return nil, fmt.Errorf("failed to instantiate mounter for volume: %s using plugin: %s with a root cause: %v", spec.Name(), plugin.GetPluginName(), err)
  76. }
  77. klog.V(10).Infof("Using volume plugin %q to mount %s", plugin.GetPluginName(), spec.Name())
  78. return physicalMounter, nil
  79. }
  80. // cleanupOrphanedPodDirs removes the volumes of pods that should not be
  81. // running and that have no containers running. Note that we roll up logs here since it runs in the main loop.
  82. func (kl *Kubelet) cleanupOrphanedPodDirs(pods []*v1.Pod, runningPods []*kubecontainer.Pod) error {
  83. allPods := sets.NewString()
  84. for _, pod := range pods {
  85. allPods.Insert(string(pod.UID))
  86. }
  87. for _, pod := range runningPods {
  88. allPods.Insert(string(pod.ID))
  89. }
  90. found, err := kl.listPodsFromDisk()
  91. if err != nil {
  92. return err
  93. }
  94. orphanRemovalErrors := []error{}
  95. orphanVolumeErrors := []error{}
  96. for _, uid := range found {
  97. if allPods.Has(string(uid)) {
  98. continue
  99. }
  100. // If volumes have not been unmounted/detached, do not delete directory.
  101. // Doing so may result in corruption of data.
  102. // TODO: getMountedVolumePathListFromDisk() call may be redundant with
  103. // kl.getPodVolumePathListFromDisk(). Can this be cleaned up?
  104. if podVolumesExist := kl.podVolumesExist(uid); podVolumesExist {
  105. klog.V(3).Infof("Orphaned pod %q found, but volumes are not cleaned up", uid)
  106. continue
  107. }
  108. // If there are still volume directories, do not delete directory
  109. volumePaths, err := kl.getPodVolumePathListFromDisk(uid)
  110. if err != nil {
  111. orphanVolumeErrors = append(orphanVolumeErrors, fmt.Errorf("Orphaned pod %q found, but error %v occurred during reading volume dir from disk", uid, err))
  112. continue
  113. }
  114. if len(volumePaths) > 0 {
  115. orphanVolumeErrors = append(orphanVolumeErrors, fmt.Errorf("Orphaned pod %q found, but volume paths are still present on disk", uid))
  116. continue
  117. }
  118. // If there are any volume-subpaths, do not cleanup directories
  119. volumeSubpathExists, err := kl.podVolumeSubpathsDirExists(uid)
  120. if err != nil {
  121. orphanVolumeErrors = append(orphanVolumeErrors, fmt.Errorf("Orphaned pod %q found, but error %v occurred during reading of volume-subpaths dir from disk", uid, err))
  122. continue
  123. }
  124. if volumeSubpathExists {
  125. orphanVolumeErrors = append(orphanVolumeErrors, fmt.Errorf("Orphaned pod %q found, but volume subpaths are still present on disk", uid))
  126. continue
  127. }
  128. klog.V(3).Infof("Orphaned pod %q found, removing", uid)
  129. if err := removeall.RemoveAllOneFilesystem(kl.mounter, kl.getPodDir(uid)); err != nil {
  130. klog.Errorf("Failed to remove orphaned pod %q dir; err: %v", uid, err)
  131. orphanRemovalErrors = append(orphanRemovalErrors, err)
  132. }
  133. }
  134. logSpew := func(errs []error) {
  135. if len(errs) > 0 {
  136. klog.Errorf("%v : There were a total of %v errors similar to this. Turn up verbosity to see them.", errs[0], len(errs))
  137. for _, err := range errs {
  138. klog.V(5).Infof("Orphan pod: %v", err)
  139. }
  140. }
  141. }
  142. logSpew(orphanVolumeErrors)
  143. logSpew(orphanRemovalErrors)
  144. return utilerrors.NewAggregate(orphanRemovalErrors)
  145. }