forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubernetes#81794 from codenrhoden/split-host-utils2
Split HostUtil functionality into its own files
- Loading branch information
Showing
19 changed files
with
1,576 additions
and
1,303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
Copyright 2015 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 mount | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"sync" | ||
) | ||
|
||
// FakeHostUtil is a fake mount.HostUtils implementation for testing | ||
type FakeHostUtil struct { | ||
MountPoints []MountPoint | ||
Filesystem map[string]FileType | ||
|
||
mutex sync.Mutex | ||
} | ||
|
||
var _ HostUtils = &FakeHostUtil{} | ||
|
||
// DeviceOpened checks if block device referenced by pathname is in use by | ||
// checking if is listed as a device in the in-memory mountpoint table. | ||
func (hu *FakeHostUtil) DeviceOpened(pathname string) (bool, error) { | ||
hu.mutex.Lock() | ||
defer hu.mutex.Unlock() | ||
|
||
for _, mp := range hu.MountPoints { | ||
if mp.Device == pathname { | ||
return true, nil | ||
} | ||
} | ||
return false, nil | ||
} | ||
|
||
// PathIsDevice always returns true | ||
func (hu *FakeHostUtil) PathIsDevice(pathname string) (bool, error) { | ||
return true, nil | ||
} | ||
|
||
// GetDeviceNameFromMount given a mount point, find the volume id | ||
func (hu *FakeHostUtil) GetDeviceNameFromMount(mounter Interface, mountPath, pluginMountDir string) (string, error) { | ||
return getDeviceNameFromMount(mounter, mountPath, pluginMountDir) | ||
} | ||
|
||
// MakeRShared checks if path is shared and bind-mounts it as rshared if needed. | ||
// No-op for testing | ||
func (hu *FakeHostUtil) MakeRShared(path string) error { | ||
return nil | ||
} | ||
|
||
// GetFileType checks for file/directory/socket/block/character devices. | ||
// Defaults to Directory if otherwise unspecified. | ||
func (hu *FakeHostUtil) GetFileType(pathname string) (FileType, error) { | ||
if t, ok := hu.Filesystem[pathname]; ok { | ||
return t, nil | ||
} | ||
return FileType("Directory"), nil | ||
} | ||
|
||
// MakeDir creates a new directory. | ||
// No-op for testing | ||
func (hu *FakeHostUtil) MakeDir(pathname string) error { | ||
return nil | ||
} | ||
|
||
// MakeFile creates a new file. | ||
// No-op for testing | ||
func (hu *FakeHostUtil) MakeFile(pathname string) error { | ||
return nil | ||
} | ||
|
||
// PathExists checks if pathname exists. | ||
func (hu *FakeHostUtil) PathExists(pathname string) (bool, error) { | ||
if _, ok := hu.Filesystem[pathname]; ok { | ||
return true, nil | ||
} | ||
return false, nil | ||
} | ||
|
||
// EvalHostSymlinks returns the path name after evaluating symlinks. | ||
// No-op for testing | ||
func (hu *FakeHostUtil) EvalHostSymlinks(pathname string) (string, error) { | ||
return pathname, nil | ||
} | ||
|
||
// GetOwner returns the integer ID for the user and group of the given path | ||
// Not implemented for testing | ||
func (hu *FakeHostUtil) GetOwner(pathname string) (int64, int64, error) { | ||
return -1, -1, errors.New("GetOwner not implemented") | ||
} | ||
|
||
// GetSELinuxSupport tests if pathname is on a mount that supports SELinux. | ||
// Not implemented for testing | ||
func (hu *FakeHostUtil) GetSELinuxSupport(pathname string) (bool, error) { | ||
return false, errors.New("GetSELinuxSupport not implemented") | ||
} | ||
|
||
// GetMode returns permissions of pathname. | ||
// Not implemented for testing | ||
func (hu *FakeHostUtil) GetMode(pathname string) (os.FileMode, error) { | ||
return 0, errors.New("not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
Copyright 2014 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. | ||
*/ | ||
|
||
// TODO(thockin): This whole pkg is pretty linux-centric. As soon as we have | ||
// an alternate platform, we will need to abstract further. | ||
|
||
package mount | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// FileType enumerates the known set of possible file types. | ||
type FileType string | ||
|
||
const ( | ||
// FileTypeBlockDev defines a constant for the block device FileType. | ||
FileTypeBlockDev FileType = "BlockDevice" | ||
// FileTypeCharDev defines a constant for the character device FileType. | ||
FileTypeCharDev FileType = "CharDevice" | ||
// FileTypeDirectory defines a constant for the directory FileType. | ||
FileTypeDirectory FileType = "Directory" | ||
// FileTypeFile defines a constant for the file FileType. | ||
FileTypeFile FileType = "File" | ||
// FileTypeSocket defines a constant for the socket FileType. | ||
FileTypeSocket FileType = "Socket" | ||
// FileTypeUnknown defines a constant for an unknown FileType. | ||
FileTypeUnknown FileType = "" | ||
) | ||
|
||
// HostUtils defines the set of methods for interacting with paths on a host. | ||
type HostUtils interface { | ||
// DeviceOpened determines if the device (e.g. /dev/sdc) is in use elsewhere | ||
// on the system, i.e. still mounted. | ||
DeviceOpened(pathname string) (bool, error) | ||
// PathIsDevice determines if a path is a device. | ||
PathIsDevice(pathname string) (bool, error) | ||
// GetDeviceNameFromMount finds the device name by checking the mount path | ||
// to get the global mount path within its plugin directory. | ||
GetDeviceNameFromMount(mounter Interface, mountPath, pluginMountDir string) (string, error) | ||
// MakeRShared checks that given path is on a mount with 'rshared' mount | ||
// propagation. If not, it bind-mounts the path as rshared. | ||
MakeRShared(path string) error | ||
// GetFileType checks for file/directory/socket/block/character devices. | ||
GetFileType(pathname string) (FileType, error) | ||
// MakeFile creates an empty file. | ||
MakeFile(pathname string) error | ||
// MakeDir creates a new directory. | ||
MakeDir(pathname string) error | ||
// PathExists tests if the given path already exists | ||
// Error is returned on any other error than "file not found". | ||
PathExists(pathname string) (bool, error) | ||
// EvalHostSymlinks returns the path name after evaluating symlinks. | ||
EvalHostSymlinks(pathname string) (string, error) | ||
// GetOwner returns the integer ID for the user and group of the given path | ||
GetOwner(pathname string) (int64, int64, error) | ||
// GetSELinuxSupport returns true if given path is on a mount that supports | ||
// SELinux. | ||
GetSELinuxSupport(pathname string) (bool, error) | ||
// GetMode returns permissions of the path. | ||
GetMode(pathname string) (os.FileMode, error) | ||
} | ||
|
||
// Compile-time check to ensure all HostUtil implementations satisfy | ||
// the HostUtils Interface. | ||
var _ HostUtils = &hostUtil{} | ||
|
||
// getFileType checks for file/directory/socket and block/character devices. | ||
func getFileType(pathname string) (FileType, error) { | ||
var pathType FileType | ||
info, err := os.Stat(pathname) | ||
if os.IsNotExist(err) { | ||
return pathType, fmt.Errorf("path %q does not exist", pathname) | ||
} | ||
// err in call to os.Stat | ||
if err != nil { | ||
return pathType, err | ||
} | ||
|
||
// checks whether the mode is the target mode. | ||
isSpecificMode := func(mode, targetMode os.FileMode) bool { | ||
return mode&targetMode == targetMode | ||
} | ||
|
||
mode := info.Mode() | ||
if mode.IsDir() { | ||
return FileTypeDirectory, nil | ||
} else if mode.IsRegular() { | ||
return FileTypeFile, nil | ||
} else if isSpecificMode(mode, os.ModeSocket) { | ||
return FileTypeSocket, nil | ||
} else if isSpecificMode(mode, os.ModeDevice) { | ||
if isSpecificMode(mode, os.ModeCharDevice) { | ||
return FileTypeCharDev, nil | ||
} | ||
return FileTypeBlockDev, nil | ||
} | ||
|
||
return pathType, fmt.Errorf("only recognise file, directory, socket, block device and character device") | ||
} |
Oops, something went wrong.