|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package mountpod |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "io/ioutil" |
| 23 | + "os" |
| 24 | + "path" |
| 25 | + |
| 26 | + "k8s.io/api/core/v1" |
| 27 | + "k8s.io/kubernetes/pkg/kubelet/config" |
| 28 | + kubepod "k8s.io/kubernetes/pkg/kubelet/pod" |
| 29 | + "k8s.io/kubernetes/pkg/util/strings" |
| 30 | +) |
| 31 | + |
| 32 | +// Manager is an interface that tracks pods with mount utilities for individual |
| 33 | +// volume plugins. |
| 34 | +type Manager interface { |
| 35 | + GetMountPod(pluginName string) (pod *v1.Pod, container string, err error) |
| 36 | +} |
| 37 | + |
| 38 | +// basicManager is simple implementation of Manager. Pods with mount utilities |
| 39 | +// are registered by placing a JSON file into |
| 40 | +// /var/lib/kubelet/plugin-containers/<plugin name>.json and this manager just |
| 41 | +// finds them there. |
| 42 | +type basicManager struct { |
| 43 | + registrationDirectory string |
| 44 | + podManager kubepod.Manager |
| 45 | +} |
| 46 | + |
| 47 | +// volumePluginRegistration specified format of the json files placed in |
| 48 | +// /var/lib/kubelet/plugin-containers/ |
| 49 | +type volumePluginRegistration struct { |
| 50 | + PodName string `json:"podName"` |
| 51 | + PodNamespace string `json:"podNamespace"` |
| 52 | + PodUID string `json:"podUID"` |
| 53 | + ContainerName string `json:"containerName"` |
| 54 | +} |
| 55 | + |
| 56 | +// NewManager returns a new mount pod manager. |
| 57 | +func NewManager(rootDirectory string, podManager kubepod.Manager) (Manager, error) { |
| 58 | + regPath := path.Join(rootDirectory, config.DefaultKubeletPluginContainersDirName) |
| 59 | + |
| 60 | + // Create the directory on startup |
| 61 | + os.MkdirAll(regPath, 0700) |
| 62 | + |
| 63 | + return &basicManager{ |
| 64 | + registrationDirectory: regPath, |
| 65 | + podManager: podManager, |
| 66 | + }, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (m *basicManager) getVolumePluginRegistrationPath(pluginName string) string { |
| 70 | + // sanitize plugin name so it does not escape directory |
| 71 | + safePluginName := strings.EscapePluginName(pluginName) + ".json" |
| 72 | + return path.Join(m.registrationDirectory, safePluginName) |
| 73 | +} |
| 74 | + |
| 75 | +func (m *basicManager) GetMountPod(pluginName string) (pod *v1.Pod, containerName string, err error) { |
| 76 | + // Read /var/lib/kubelet/plugin-containers/<plugin name>.json |
| 77 | + regPath := m.getVolumePluginRegistrationPath(pluginName) |
| 78 | + regBytes, err := ioutil.ReadFile(regPath) |
| 79 | + if err != nil { |
| 80 | + if os.IsNotExist(err) { |
| 81 | + // No pod is registered for this plugin |
| 82 | + return nil, "", nil |
| 83 | + } |
| 84 | + return nil, "", fmt.Errorf("cannot read %s: %v", regPath, err) |
| 85 | + } |
| 86 | + |
| 87 | + // Parse json |
| 88 | + var reg volumePluginRegistration |
| 89 | + if err := json.Unmarshal(regBytes, ®); err != nil { |
| 90 | + return nil, "", fmt.Errorf("unable to parse %s: %s", regPath, err) |
| 91 | + } |
| 92 | + if len(reg.ContainerName) == 0 { |
| 93 | + return nil, "", fmt.Errorf("unable to parse %s: \"containerName\" is not set", regPath) |
| 94 | + } |
| 95 | + if len(reg.PodUID) == 0 { |
| 96 | + return nil, "", fmt.Errorf("unable to parse %s: \"podUID\" is not set", regPath) |
| 97 | + } |
| 98 | + if len(reg.PodNamespace) == 0 { |
| 99 | + return nil, "", fmt.Errorf("unable to parse %s: \"podNamespace\" is not set", regPath) |
| 100 | + } |
| 101 | + if len(reg.PodName) == 0 { |
| 102 | + return nil, "", fmt.Errorf("unable to parse %s: \"podName\" is not set", regPath) |
| 103 | + } |
| 104 | + |
| 105 | + pod, ok := m.podManager.GetPodByName(reg.PodNamespace, reg.PodName) |
| 106 | + if !ok { |
| 107 | + return nil, "", fmt.Errorf("unable to process %s: pod %s/%s not found", regPath, reg.PodNamespace, reg.PodName) |
| 108 | + } |
| 109 | + if string(pod.UID) != reg.PodUID { |
| 110 | + return nil, "", fmt.Errorf("unable to process %s: pod %s/%s has unexpected UID", regPath, reg.PodNamespace, reg.PodName) |
| 111 | + } |
| 112 | + // make sure that reg.ContainerName exists in the pod |
| 113 | + for i := range pod.Spec.Containers { |
| 114 | + if pod.Spec.Containers[i].Name == reg.ContainerName { |
| 115 | + return pod, reg.ContainerName, nil |
| 116 | + } |
| 117 | + } |
| 118 | + return nil, "", fmt.Errorf("unable to process %s: pod %s/%s has no container named %q", regPath, reg.PodNamespace, reg.PodName, reg.ContainerName) |
| 119 | + |
| 120 | +} |
0 commit comments