kubelet_volumes.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. "os"
  17. "path/filepath"
  18. "github.com/golang/glog"
  19. "k8s.io/kubernetes/pkg/api"
  20. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  21. "k8s.io/kubernetes/pkg/securitycontext"
  22. "k8s.io/kubernetes/pkg/types"
  23. utilerrors "k8s.io/kubernetes/pkg/util/errors"
  24. "k8s.io/kubernetes/pkg/util/selinux"
  25. "k8s.io/kubernetes/pkg/util/sets"
  26. "k8s.io/kubernetes/pkg/volume"
  27. volumetypes "k8s.io/kubernetes/pkg/volume/util/types"
  28. )
  29. // ListVolumesForPod returns a map of the mounted volumes for the given pod.
  30. // The key in the map is the OuterVolumeSpecName (i.e. pod.Spec.Volumes[x].Name)
  31. func (kl *Kubelet) ListVolumesForPod(podUID types.UID) (map[string]volume.Volume, bool) {
  32. volumesToReturn := make(map[string]volume.Volume)
  33. podVolumes := kl.volumeManager.GetMountedVolumesForPod(
  34. volumetypes.UniquePodName(podUID))
  35. for outerVolumeSpecName, volume := range podVolumes {
  36. volumesToReturn[outerVolumeSpecName] = volume.Mounter
  37. }
  38. return volumesToReturn, len(volumesToReturn) > 0
  39. }
  40. // podVolumesExist checks with the volume manager and returns true any of the
  41. // pods for the specified volume are mounted.
  42. func (kl *Kubelet) podVolumesExist(podUID types.UID) bool {
  43. if mountedVolumes :=
  44. kl.volumeManager.GetMountedVolumesForPod(
  45. volumetypes.UniquePodName(podUID)); len(mountedVolumes) > 0 {
  46. return true
  47. }
  48. return false
  49. }
  50. // newVolumeMounterFromPlugins attempts to find a plugin by volume spec, pod
  51. // and volume options and then creates a Mounter.
  52. // Returns a valid Unmounter or an error.
  53. func (kl *Kubelet) newVolumeMounterFromPlugins(spec *volume.Spec, pod *api.Pod, opts volume.VolumeOptions) (volume.Mounter, error) {
  54. plugin, err := kl.volumePluginMgr.FindPluginBySpec(spec)
  55. if err != nil {
  56. return nil, fmt.Errorf("can't use volume plugins for %s: %v", spec.Name(), err)
  57. }
  58. physicalMounter, err := plugin.NewMounter(spec, pod, opts)
  59. if err != nil {
  60. return nil, fmt.Errorf("failed to instantiate mounter for volume: %s using plugin: %s with a root cause: %v", spec.Name(), plugin.GetPluginName(), err)
  61. }
  62. glog.V(10).Infof("Using volume plugin %q to mount %s", plugin.GetPluginName(), spec.Name())
  63. return physicalMounter, nil
  64. }
  65. // relabelVolumes relabels SELinux volumes to match the pod's
  66. // SELinuxOptions specification. This is only needed if the pod uses
  67. // hostPID or hostIPC. Otherwise relabeling is delegated to docker.
  68. func (kl *Kubelet) relabelVolumes(pod *api.Pod, volumes kubecontainer.VolumeMap) error {
  69. if pod.Spec.SecurityContext.SELinuxOptions == nil {
  70. return nil
  71. }
  72. rootDirContext, err := kl.getRootDirContext()
  73. if err != nil {
  74. return err
  75. }
  76. selinuxRunner := selinux.NewSelinuxContextRunner()
  77. // Apply the pod's Level to the rootDirContext
  78. rootDirSELinuxOptions, err := securitycontext.ParseSELinuxOptions(rootDirContext)
  79. if err != nil {
  80. return err
  81. }
  82. rootDirSELinuxOptions.Level = pod.Spec.SecurityContext.SELinuxOptions.Level
  83. volumeContext := fmt.Sprintf("%s:%s:%s:%s", rootDirSELinuxOptions.User, rootDirSELinuxOptions.Role, rootDirSELinuxOptions.Type, rootDirSELinuxOptions.Level)
  84. for _, vol := range volumes {
  85. if vol.Mounter.GetAttributes().Managed && vol.Mounter.GetAttributes().SupportsSELinux {
  86. // Relabel the volume and its content to match the 'Level' of the pod
  87. path, err := volume.GetPath(vol.Mounter)
  88. if err != nil {
  89. return err
  90. }
  91. err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
  92. if err != nil {
  93. return err
  94. }
  95. return selinuxRunner.SetContext(path, volumeContext)
  96. })
  97. if err != nil {
  98. return err
  99. }
  100. vol.SELinuxLabeled = true
  101. }
  102. }
  103. return nil
  104. }
  105. // cleanupOrphanedPodDirs removes the volumes of pods that should not be
  106. // running and that have no containers running.
  107. func (kl *Kubelet) cleanupOrphanedPodDirs(
  108. pods []*api.Pod, runningPods []*kubecontainer.Pod) error {
  109. allPods := sets.NewString()
  110. for _, pod := range pods {
  111. allPods.Insert(string(pod.UID))
  112. }
  113. for _, pod := range runningPods {
  114. allPods.Insert(string(pod.ID))
  115. }
  116. found, err := kl.listPodsFromDisk()
  117. if err != nil {
  118. return err
  119. }
  120. errlist := []error{}
  121. for _, uid := range found {
  122. if allPods.Has(string(uid)) {
  123. continue
  124. }
  125. // If volumes have not been unmounted/detached, do not delete directory.
  126. // Doing so may result in corruption of data.
  127. if podVolumesExist := kl.podVolumesExist(uid); podVolumesExist {
  128. glog.V(3).Infof("Orphaned pod %q found, but volumes are not cleaned up; err: %v", uid, err)
  129. continue
  130. }
  131. // Check whether volume is still mounted on disk. If so, do not delete directory
  132. if volumeNames, err := kl.getPodVolumeNameListFromDisk(uid); err != nil || len(volumeNames) != 0 {
  133. glog.V(3).Infof("Orphaned pod %q found, but volumes are still mounted; err: %v, volumes: %v ", uid, err, volumeNames)
  134. continue
  135. }
  136. glog.V(3).Infof("Orphaned pod %q found, removing", uid)
  137. if err := os.RemoveAll(kl.getPodDir(uid)); err != nil {
  138. glog.Errorf("Failed to remove orphaned pod %q dir; err: %v", uid, err)
  139. errlist = append(errlist, err)
  140. }
  141. }
  142. return utilerrors.NewAggregate(errlist)
  143. }