1 Star 0 Fork 0

zhuchance/kubernetes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
mount_pod.go 4.01 KB
一键复制 编辑 原始数据 按行查看 历史
Jan Safranek 提交于 2017-11-10 13:14 . Add mount containers to kubelet
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mountpod
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"k8s.io/api/core/v1"
"k8s.io/kubernetes/pkg/kubelet/config"
kubepod "k8s.io/kubernetes/pkg/kubelet/pod"
"k8s.io/kubernetes/pkg/util/strings"
)
// Manager is an interface that tracks pods with mount utilities for individual
// volume plugins.
type Manager interface {
GetMountPod(pluginName string) (pod *v1.Pod, container string, err error)
}
// basicManager is simple implementation of Manager. Pods with mount utilities
// are registered by placing a JSON file into
// /var/lib/kubelet/plugin-containers/<plugin name>.json and this manager just
// finds them there.
type basicManager struct {
registrationDirectory string
podManager kubepod.Manager
}
// volumePluginRegistration specified format of the json files placed in
// /var/lib/kubelet/plugin-containers/
type volumePluginRegistration struct {
PodName string `json:"podName"`
PodNamespace string `json:"podNamespace"`
PodUID string `json:"podUID"`
ContainerName string `json:"containerName"`
}
// NewManager returns a new mount pod manager.
func NewManager(rootDirectory string, podManager kubepod.Manager) (Manager, error) {
regPath := path.Join(rootDirectory, config.DefaultKubeletPluginContainersDirName)
// Create the directory on startup
os.MkdirAll(regPath, 0700)
return &basicManager{
registrationDirectory: regPath,
podManager: podManager,
}, nil
}
func (m *basicManager) getVolumePluginRegistrationPath(pluginName string) string {
// sanitize plugin name so it does not escape directory
safePluginName := strings.EscapePluginName(pluginName) + ".json"
return path.Join(m.registrationDirectory, safePluginName)
}
func (m *basicManager) GetMountPod(pluginName string) (pod *v1.Pod, containerName string, err error) {
// Read /var/lib/kubelet/plugin-containers/<plugin name>.json
regPath := m.getVolumePluginRegistrationPath(pluginName)
regBytes, err := ioutil.ReadFile(regPath)
if err != nil {
if os.IsNotExist(err) {
// No pod is registered for this plugin
return nil, "", nil
}
return nil, "", fmt.Errorf("cannot read %s: %v", regPath, err)
}
// Parse json
var reg volumePluginRegistration
if err := json.Unmarshal(regBytes, &reg); err != nil {
return nil, "", fmt.Errorf("unable to parse %s: %s", regPath, err)
}
if len(reg.ContainerName) == 0 {
return nil, "", fmt.Errorf("unable to parse %s: \"containerName\" is not set", regPath)
}
if len(reg.PodUID) == 0 {
return nil, "", fmt.Errorf("unable to parse %s: \"podUID\" is not set", regPath)
}
if len(reg.PodNamespace) == 0 {
return nil, "", fmt.Errorf("unable to parse %s: \"podNamespace\" is not set", regPath)
}
if len(reg.PodName) == 0 {
return nil, "", fmt.Errorf("unable to parse %s: \"podName\" is not set", regPath)
}
pod, ok := m.podManager.GetPodByName(reg.PodNamespace, reg.PodName)
if !ok {
return nil, "", fmt.Errorf("unable to process %s: pod %s/%s not found", regPath, reg.PodNamespace, reg.PodName)
}
if string(pod.UID) != reg.PodUID {
return nil, "", fmt.Errorf("unable to process %s: pod %s/%s has unexpected UID", regPath, reg.PodNamespace, reg.PodName)
}
// make sure that reg.ContainerName exists in the pod
for i := range pod.Spec.Containers {
if pod.Spec.Containers[i].Name == reg.ContainerName {
return pod, reg.ContainerName, nil
}
}
return nil, "", fmt.Errorf("unable to process %s: pod %s/%s has no container named %q", regPath, reg.PodNamespace, reg.PodName, reg.ContainerName)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/meoom/kubernetes.git
git@gitee.com:meoom/kubernetes.git
meoom
kubernetes
kubernetes
v1.12.8-beta.0

搜索帮助