From e0edfebe82df31791fc81de3682456d3c36bbbba Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Tue, 26 Jul 2016 10:59:20 -0400 Subject: [PATCH] Verify volume.GetPath() never returns "" Add a new helper method volume.GetPath(Mounter) instead of calling the GetPath() of the Mounter directly. Check if GetPath() is returning a "" and convert that into an error. At this point, we only have information about the type of the Mounter, so let's log that if there is a problem Fixes #23163 --- pkg/kubelet/kubelet.go | 11 +++++++++-- pkg/volume/util.go | 10 ++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 9ea267f736c..206ed89e9aa 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -1012,7 +1012,11 @@ func (kl *Kubelet) relabelVolumes(pod *api.Pod, volumes kubecontainer.VolumeMap) for _, vol := range volumes { if vol.Mounter.GetAttributes().Managed && vol.Mounter.GetAttributes().SupportsSELinux { // Relabel the volume and its content to match the 'Level' of the pod - err := filepath.Walk(vol.Mounter.GetPath(), func(path string, info os.FileInfo, err error) error { + path, err := volume.GetPath(vol.Mounter) + if err != nil { + return err + } + err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error { if err != nil { return err } @@ -1053,7 +1057,10 @@ func makeMounts(pod *api.Pod, podDir string, container *api.Container, hostName, vol.SELinuxLabeled = true relabelVolume = true } - hostPath := vol.Mounter.GetPath() + hostPath, err := volume.GetPath(vol.Mounter) + if err != nil { + return nil, err + } if mount.SubPath != "" { hostPath = filepath.Join(hostPath, mount.SubPath) } diff --git a/pkg/volume/util.go b/pkg/volume/util.go index b36632c9f52..6a44f9677d5 100644 --- a/pkg/volume/util.go +++ b/pkg/volume/util.go @@ -18,6 +18,7 @@ package volume import ( "fmt" + "reflect" "time" "k8s.io/kubernetes/pkg/api" @@ -193,6 +194,15 @@ func GenerateVolumeName(clusterName, pvName string, maxLength int) string { return prefix + "-" + pvName } +// Check if the path from the mounter is empty. +func GetPath(mounter Mounter) (string, error) { + path := mounter.GetPath() + if path == "" { + return "", fmt.Errorf("Path is empty %s", reflect.TypeOf(mounter).String()) + } + return path, nil +} + // ChooseZone implements our heuristics for choosing a zone for volume creation based on the volume name // Volumes are generally round-robin-ed across all active zones, using the hash of the PVC Name. // However, if the PVCName ends with `-`, we will hash the prefix, and then add the integer to the hash.