Enhance ExistsPath check

It should return error when the check fails (e.g. no permissions, symlink link
loop etc.)
This commit is contained in:
Jan Safranek 2018-05-22 12:56:25 +02:00
parent 7450d1b427
commit 74ba0878a1
16 changed files with 49 additions and 46 deletions

View File

@ -92,8 +92,8 @@ func (mi *fakeMountInterface) MakeFile(pathname string) error {
return nil return nil
} }
func (mi *fakeMountInterface) ExistsPath(pathname string) bool { func (mi *fakeMountInterface) ExistsPath(pathname string) (bool, error) {
return true return true, errors.New("not implemented")
} }
func (mi *fakeMountInterface) PrepareSafeSubpath(subPath mount.Subpath) (newHostPath string, cleanupAction func(), err error) { func (mi *fakeMountInterface) PrepareSafeSubpath(subPath mount.Subpath) (newHostPath string, cleanupAction func(), err error) {

View File

@ -60,7 +60,6 @@ import (
"k8s.io/kubernetes/pkg/kubelet/status" "k8s.io/kubernetes/pkg/kubelet/status"
kubetypes "k8s.io/kubernetes/pkg/kubelet/types" kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
"k8s.io/kubernetes/pkg/kubelet/util/format" "k8s.io/kubernetes/pkg/kubelet/util/format"
utilfile "k8s.io/kubernetes/pkg/util/file"
mountutil "k8s.io/kubernetes/pkg/util/mount" mountutil "k8s.io/kubernetes/pkg/util/mount"
volumeutil "k8s.io/kubernetes/pkg/volume/util" volumeutil "k8s.io/kubernetes/pkg/volume/util"
"k8s.io/kubernetes/pkg/volume/util/volumepathhandler" "k8s.io/kubernetes/pkg/volume/util/volumepathhandler"
@ -181,7 +180,7 @@ func makeMounts(pod *v1.Pod, podDir string, container *v1.Container, hostName, h
} }
hostPath = filepath.Join(volumePath, mount.SubPath) hostPath = filepath.Join(volumePath, mount.SubPath)
if subPathExists, err := utilfile.FileOrSymlinkExists(hostPath); err != nil { if subPathExists, err := mounter.ExistsPath(hostPath); err != nil {
glog.Errorf("Could not determine if subPath %s exists; will not attempt to change its permissions", hostPath) glog.Errorf("Could not determine if subPath %s exists; will not attempt to change its permissions", hostPath)
} else if !subPathExists { } else if !subPathExists {
// Create the sub path now because if it's auto-created later when referenced, it may have an // Create the sub path now because if it's auto-created later when referenced, it may have an

View File

@ -72,11 +72,15 @@ go_library(
"//vendor/k8s.io/utils/exec:go_default_library", "//vendor/k8s.io/utils/exec:go_default_library",
] + select({ ] + select({
"@io_bazel_rules_go//go/platform:linux": [ "@io_bazel_rules_go//go/platform:linux": [
"//pkg/util/file:go_default_library",
"//pkg/util/io:go_default_library", "//pkg/util/io:go_default_library",
"//pkg/util/nsenter:go_default_library", "//pkg/util/nsenter:go_default_library",
"//vendor/golang.org/x/sys/unix:go_default_library", "//vendor/golang.org/x/sys/unix:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library", "//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
], ],
"@io_bazel_rules_go//go/platform:windows": [
"//pkg/util/file:go_default_library",
],
"//conditions:default": [], "//conditions:default": [],
}), }),
) )

View File

@ -136,7 +136,7 @@ func (m *execMounter) MakeDir(pathname string) error {
return m.wrappedMounter.MakeDir(pathname) return m.wrappedMounter.MakeDir(pathname)
} }
func (m *execMounter) ExistsPath(pathname string) bool { func (m *execMounter) ExistsPath(pathname string) (bool, error) {
return m.wrappedMounter.ExistsPath(pathname) return m.wrappedMounter.ExistsPath(pathname)
} }

View File

@ -147,8 +147,8 @@ func (fm *fakeMounter) MakeFile(pathname string) error {
func (fm *fakeMounter) MakeDir(pathname string) error { func (fm *fakeMounter) MakeDir(pathname string) error {
return nil return nil
} }
func (fm *fakeMounter) ExistsPath(pathname string) bool { func (fm *fakeMounter) ExistsPath(pathname string) (bool, error) {
return false return false, errors.New("not implemented")
} }
func (fm *fakeMounter) GetFileType(pathname string) (FileType, error) { func (fm *fakeMounter) GetFileType(pathname string) (FileType, error) {
return FileTypeFile, nil return FileTypeFile, nil

View File

@ -83,8 +83,8 @@ func (mounter *execMounter) MakeFile(pathname string) error {
return nil return nil
} }
func (mounter *execMounter) ExistsPath(pathname string) bool { func (mounter *execMounter) ExistsPath(pathname string) (bool, error) {
return true return true, errors.New("not implemented")
} }
func (mounter *execMounter) PrepareSafeSubpath(subPath Subpath) (newHostPath string, cleanupAction func(), err error) { func (mounter *execMounter) PrepareSafeSubpath(subPath Subpath) (newHostPath string, cleanupAction func(), err error) {

View File

@ -201,8 +201,8 @@ func (f *FakeMounter) MakeFile(pathname string) error {
return nil return nil
} }
func (f *FakeMounter) ExistsPath(pathname string) bool { func (f *FakeMounter) ExistsPath(pathname string) (bool, error) {
return false return false, errors.New("not implemented")
} }
func (f *FakeMounter) PrepareSafeSubpath(subPath Subpath) (newHostPath string, cleanupAction func(), err error) { func (f *FakeMounter) PrepareSafeSubpath(subPath Subpath) (newHostPath string, cleanupAction func(), err error) {

View File

@ -91,9 +91,9 @@ type Interface interface {
// else. E.g. if the directory already exists, it may exists outside of the // else. E.g. if the directory already exists, it may exists outside of the
// base due to symlinks. // base due to symlinks.
SafeMakeDir(pathname string, base string, perm os.FileMode) error SafeMakeDir(pathname string, base string, perm os.FileMode) error
// ExistsPath checks whether the path exists. // Will operate in the host mount namespace if kubelet is running in a container.
// Will operate in the host mount namespace if kubelet is running in a container // Error is returned on any other error than "file not found".
ExistsPath(pathname string) bool ExistsPath(pathname string) (bool, error)
// CleanSubPaths removes any bind-mounts created by PrepareSafeSubpath in given // CleanSubPaths removes any bind-mounts created by PrepareSafeSubpath in given
// pod volume directory. // pod volume directory.
CleanSubPaths(podDir string, volumeName string) error CleanSubPaths(podDir string, volumeName string) error

View File

@ -33,6 +33,7 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/sets"
utilfile "k8s.io/kubernetes/pkg/util/file"
utilio "k8s.io/kubernetes/pkg/util/io" utilio "k8s.io/kubernetes/pkg/util/io"
utilexec "k8s.io/utils/exec" utilexec "k8s.io/utils/exec"
) )
@ -447,12 +448,8 @@ func (mounter *Mounter) MakeFile(pathname string) error {
return nil return nil
} }
func (mounter *Mounter) ExistsPath(pathname string) bool { func (mounter *Mounter) ExistsPath(pathname string) (bool, error) {
_, err := os.Stat(pathname) return utilfile.FileExists(pathname)
if err != nil {
return false
}
return true
} }
// formatAndMount uses unix utils to format and mount the given disk // formatAndMount uses unix utils to format and mount the given disk

View File

@ -21,8 +21,6 @@ package mount
import ( import (
"errors" "errors"
"os" "os"
"github.com/golang/glog"
) )
type Mounter struct { type Mounter struct {
@ -110,9 +108,8 @@ func (mounter *Mounter) MakeFile(pathname string) error {
return unsupportedErr return unsupportedErr
} }
func (mounter *Mounter) ExistsPath(pathname string) bool { func (mounter *Mounter) ExistsPath(pathname string) (bool, error) {
glog.Errorf("%s", unsupportedErr) return true, errors.New("not implemented")
return true
} }
func (mounter *Mounter) PrepareSafeSubpath(subPath Subpath) (newHostPath string, cleanupAction func(), err error) { func (mounter *Mounter) PrepareSafeSubpath(subPath Subpath) (newHostPath string, cleanupAction func(), err error) {

View File

@ -29,6 +29,8 @@ import (
"syscall" "syscall"
"github.com/golang/glog" "github.com/golang/glog"
utilfile "k8s.io/kubernetes/pkg/util/file"
) )
// Mounter provides the default implementation of mount.Interface // Mounter provides the default implementation of mount.Interface
@ -147,9 +149,13 @@ func (mounter *Mounter) IsLikelyNotMountPoint(file string) (bool, error) {
if stat.Mode()&os.ModeSymlink != 0 { if stat.Mode()&os.ModeSymlink != 0 {
target, err := os.Readlink(file) target, err := os.Readlink(file)
if err != nil { if err != nil {
return true, fmt.Errorf("Readlink error: %v", err) return true, fmt.Errorf("readlink error: %v", err)
} }
return !mounter.ExistsPath(target), nil exists, err := mounter.ExistsPath(target)
if err != nil {
return true, err
}
return !exists, nil
} }
return true, nil return true, nil
@ -232,12 +238,8 @@ func (mounter *Mounter) MakeFile(pathname string) error {
} }
// ExistsPath checks whether the path exists // ExistsPath checks whether the path exists
func (mounter *Mounter) ExistsPath(pathname string) bool { func (mounter *Mounter) ExistsPath(pathname string) (bool, error) {
_, err := os.Stat(pathname) return utilfile.FileExists(pathname)
if err != nil {
return false
}
return true
} }
// check whether hostPath is within volume path // check whether hostPath is within volume path

View File

@ -27,6 +27,7 @@ import (
"strings" "strings"
"github.com/golang/glog" "github.com/golang/glog"
utilfile "k8s.io/kubernetes/pkg/util/file"
utilio "k8s.io/kubernetes/pkg/util/io" utilio "k8s.io/kubernetes/pkg/util/io"
"k8s.io/kubernetes/pkg/util/nsenter" "k8s.io/kubernetes/pkg/util/nsenter"
) )
@ -281,13 +282,15 @@ func (mounter *NsenterMounter) MakeFile(pathname string) error {
return nil return nil
} }
func (mounter *NsenterMounter) ExistsPath(pathname string) bool { func (mounter *NsenterMounter) ExistsPath(pathname string) (bool, error) {
args := []string{pathname} // Resolve the symlinks but allow the target not to exist. EvalSymlinks
_, err := mounter.ne.Exec("ls", args).CombinedOutput() // would return an generic error when the target does not exist.
if err == nil { hostPath, err := mounter.ne.EvalSymlinks(pathname, false /* mustExist */)
return true if err != nil {
return false, err
} }
return false kubeletpath := mounter.ne.KubeletPath(hostPath)
return utilfile.FileExists(kubeletpath)
} }
func (mounter *NsenterMounter) CleanSubPaths(podDir string, volumeName string) error { func (mounter *NsenterMounter) CleanSubPaths(podDir string, volumeName string) error {

View File

@ -83,8 +83,8 @@ func (*NsenterMounter) MakeFile(pathname string) error {
return nil return nil
} }
func (*NsenterMounter) ExistsPath(pathname string) bool { func (*NsenterMounter) ExistsPath(pathname string) (bool, error) {
return true return true, errors.New("not implemented")
} }
func (*NsenterMounter) SafeMakeDir(pathname string, base string, perm os.FileMode) error { func (*NsenterMounter) SafeMakeDir(pathname string, base string, perm os.FileMode) error {

View File

@ -75,8 +75,8 @@ func (mounter *fakeMounter) MakeFile(pathname string) error {
return nil return nil
} }
func (mounter *fakeMounter) ExistsPath(pathname string) bool { func (mounter *fakeMounter) ExistsPath(pathname string) (bool, error) {
return true return true, errors.New("not implemented")
} }
func (mounter *fakeMounter) PrepareSafeSubpath(subPath mount.Subpath) (newHostPath string, cleanupAction func(), err error) { func (mounter *fakeMounter) PrepareSafeSubpath(subPath mount.Subpath) (newHostPath string, cleanupAction func(), err error) {

View File

@ -350,7 +350,8 @@ type fileTypeChecker struct {
} }
func (ftc *fileTypeChecker) Exists() bool { func (ftc *fileTypeChecker) Exists() bool {
return ftc.mounter.ExistsPath(ftc.path) exists, err := ftc.mounter.ExistsPath(ftc.path)
return exists && err == nil
} }
func (ftc *fileTypeChecker) IsFile() bool { func (ftc *fileTypeChecker) IsFile() bool {

View File

@ -369,8 +369,8 @@ func (fftc *fakeFileTypeChecker) MakeDir(pathname string) error {
return nil return nil
} }
func (fftc *fakeFileTypeChecker) ExistsPath(pathname string) bool { func (fftc *fakeFileTypeChecker) ExistsPath(pathname string) (bool, error) {
return true return true, nil
} }
func (fftc *fakeFileTypeChecker) GetFileType(_ string) (utilmount.FileType, error) { func (fftc *fakeFileTypeChecker) GetFileType(_ string) (utilmount.FileType, error) {