mount_pod.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 mountpod
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "path"
  20. "k8s.io/api/core/v1"
  21. "k8s.io/kubernetes/pkg/kubelet/config"
  22. kubepod "k8s.io/kubernetes/pkg/kubelet/pod"
  23. utilstrings "k8s.io/utils/strings"
  24. )
  25. // Manager is an interface that tracks pods with mount utilities for individual
  26. // volume plugins.
  27. type Manager interface {
  28. GetMountPod(pluginName string) (pod *v1.Pod, container string, err error)
  29. }
  30. // basicManager is simple implementation of Manager. Pods with mount utilities
  31. // are registered by placing a JSON file into
  32. // /var/lib/kubelet/plugin-containers/<plugin name>.json and this manager just
  33. // finds them there.
  34. type basicManager struct {
  35. registrationDirectory string
  36. podManager kubepod.Manager
  37. }
  38. // volumePluginRegistration specified format of the json files placed in
  39. // /var/lib/kubelet/plugin-containers/
  40. type volumePluginRegistration struct {
  41. PodName string `json:"podName"`
  42. PodNamespace string `json:"podNamespace"`
  43. PodUID string `json:"podUID"`
  44. ContainerName string `json:"containerName"`
  45. }
  46. // NewManager returns a new mount pod manager.
  47. func NewManager(rootDirectory string, podManager kubepod.Manager) (Manager, error) {
  48. regPath := path.Join(rootDirectory, config.DefaultKubeletPluginContainersDirName)
  49. // Create the directory on startup
  50. os.MkdirAll(regPath, 0700)
  51. return &basicManager{
  52. registrationDirectory: regPath,
  53. podManager: podManager,
  54. }, nil
  55. }
  56. func (m *basicManager) getVolumePluginRegistrationPath(pluginName string) string {
  57. // sanitize plugin name so it does not escape directory
  58. safePluginName := utilstrings.EscapeQualifiedName(pluginName) + ".json"
  59. return path.Join(m.registrationDirectory, safePluginName)
  60. }
  61. func (m *basicManager) GetMountPod(pluginName string) (pod *v1.Pod, containerName string, err error) {
  62. // Read /var/lib/kubelet/plugin-containers/<plugin name>.json
  63. regPath := m.getVolumePluginRegistrationPath(pluginName)
  64. regBytes, err := ioutil.ReadFile(regPath)
  65. if err != nil {
  66. if os.IsNotExist(err) {
  67. // No pod is registered for this plugin
  68. return nil, "", nil
  69. }
  70. return nil, "", fmt.Errorf("cannot read %s: %v", regPath, err)
  71. }
  72. // Parse json
  73. var reg volumePluginRegistration
  74. if err := json.Unmarshal(regBytes, &reg); err != nil {
  75. return nil, "", fmt.Errorf("unable to parse %s: %s", regPath, err)
  76. }
  77. if len(reg.ContainerName) == 0 {
  78. return nil, "", fmt.Errorf("unable to parse %s: \"containerName\" is not set", regPath)
  79. }
  80. if len(reg.PodUID) == 0 {
  81. return nil, "", fmt.Errorf("unable to parse %s: \"podUID\" is not set", regPath)
  82. }
  83. if len(reg.PodNamespace) == 0 {
  84. return nil, "", fmt.Errorf("unable to parse %s: \"podNamespace\" is not set", regPath)
  85. }
  86. if len(reg.PodName) == 0 {
  87. return nil, "", fmt.Errorf("unable to parse %s: \"podName\" is not set", regPath)
  88. }
  89. pod, ok := m.podManager.GetPodByName(reg.PodNamespace, reg.PodName)
  90. if !ok {
  91. return nil, "", fmt.Errorf("unable to process %s: pod %s/%s not found", regPath, reg.PodNamespace, reg.PodName)
  92. }
  93. if string(pod.UID) != reg.PodUID {
  94. return nil, "", fmt.Errorf("unable to process %s: pod %s/%s has unexpected UID", regPath, reg.PodNamespace, reg.PodName)
  95. }
  96. // make sure that reg.ContainerName exists in the pod
  97. for i := range pod.Spec.Containers {
  98. if pod.Spec.Containers[i].Name == reg.ContainerName {
  99. return pod, reg.ContainerName, nil
  100. }
  101. }
  102. return nil, "", fmt.Errorf("unable to process %s: pod %s/%s has no container named %q", regPath, reg.PodNamespace, reg.PodName, reg.ContainerName)
  103. }