mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-11-28 20:33:54 +00:00
Merge pull request #76456 from codenrhoden/mv-exec-mounter
Move ExecMount to pkg/volume/util/exec
This commit is contained in:
@@ -84,6 +84,7 @@ filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [
|
||||
":package-srcs",
|
||||
"//pkg/volume/util/exec:all-srcs",
|
||||
"//pkg/volume/util/fs:all-srcs",
|
||||
"//pkg/volume/util/nestedpendingoperations:all-srcs",
|
||||
"//pkg/volume/util/nsenter:all-srcs",
|
||||
|
||||
74
pkg/volume/util/exec/BUILD
Normal file
74
pkg/volume/util/exec/BUILD
Normal file
@@ -0,0 +1,74 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"exec_mount.go",
|
||||
"exec_mount_unsupported.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/volume/util/exec",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = select({
|
||||
"@io_bazel_rules_go//go/platform:android": [
|
||||
"//pkg/util/mount:go_default_library",
|
||||
],
|
||||
"@io_bazel_rules_go//go/platform:darwin": [
|
||||
"//pkg/util/mount:go_default_library",
|
||||
],
|
||||
"@io_bazel_rules_go//go/platform:dragonfly": [
|
||||
"//pkg/util/mount:go_default_library",
|
||||
],
|
||||
"@io_bazel_rules_go//go/platform:freebsd": [
|
||||
"//pkg/util/mount:go_default_library",
|
||||
],
|
||||
"@io_bazel_rules_go//go/platform:linux": [
|
||||
"//pkg/util/mount:go_default_library",
|
||||
"//vendor/k8s.io/klog:go_default_library",
|
||||
],
|
||||
"@io_bazel_rules_go//go/platform:nacl": [
|
||||
"//pkg/util/mount:go_default_library",
|
||||
],
|
||||
"@io_bazel_rules_go//go/platform:netbsd": [
|
||||
"//pkg/util/mount:go_default_library",
|
||||
],
|
||||
"@io_bazel_rules_go//go/platform:openbsd": [
|
||||
"//pkg/util/mount:go_default_library",
|
||||
],
|
||||
"@io_bazel_rules_go//go/platform:plan9": [
|
||||
"//pkg/util/mount:go_default_library",
|
||||
],
|
||||
"@io_bazel_rules_go//go/platform:solaris": [
|
||||
"//pkg/util/mount:go_default_library",
|
||||
],
|
||||
"@io_bazel_rules_go//go/platform:windows": [
|
||||
"//pkg/util/mount:go_default_library",
|
||||
],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["exec_mount_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
deps = select({
|
||||
"@io_bazel_rules_go//go/platform:linux": [
|
||||
"//pkg/util/mount:go_default_library",
|
||||
],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
161
pkg/volume/util/exec/exec_mount.go
Normal file
161
pkg/volume/util/exec/exec_mount.go
Normal file
@@ -0,0 +1,161 @@
|
||||
// +build linux
|
||||
|
||||
/*
|
||||
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 exec
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"k8s.io/klog"
|
||||
|
||||
"k8s.io/kubernetes/pkg/util/mount"
|
||||
)
|
||||
|
||||
// ExecMounter is a mounter that uses provided Exec interface to mount and
|
||||
// unmount a filesystem. For all other calls it uses a wrapped mounter.
|
||||
type execMounter struct {
|
||||
wrappedMounter mount.Interface
|
||||
exec mount.Exec
|
||||
}
|
||||
|
||||
// NewExecMounter returns a mounter that uses provided Exec interface to mount and
|
||||
// unmount a filesystem. For all other calls it uses a wrapped mounter.
|
||||
func NewExecMounter(exec mount.Exec, wrapped mount.Interface) mount.Interface {
|
||||
return &execMounter{
|
||||
wrappedMounter: wrapped,
|
||||
exec: exec,
|
||||
}
|
||||
}
|
||||
|
||||
// execMounter implements mount.Interface
|
||||
var _ mount.Interface = &execMounter{}
|
||||
|
||||
// Mount runs mount(8) using given exec interface.
|
||||
func (m *execMounter) Mount(source string, target string, fstype string, options []string) error {
|
||||
bind, bindOpts, bindRemountOpts := mount.IsBind(options)
|
||||
|
||||
if bind {
|
||||
err := m.doExecMount(source, target, fstype, bindOpts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return m.doExecMount(source, target, fstype, bindRemountOpts)
|
||||
}
|
||||
|
||||
return m.doExecMount(source, target, fstype, options)
|
||||
}
|
||||
|
||||
// doExecMount calls exec(mount <what> <where>) using given exec interface.
|
||||
func (m *execMounter) doExecMount(source, target, fstype string, options []string) error {
|
||||
klog.V(5).Infof("Exec Mounting %s %s %s %v", source, target, fstype, options)
|
||||
mountArgs := mount.MakeMountArgs(source, target, fstype, options)
|
||||
output, err := m.exec.Run("mount", mountArgs...)
|
||||
klog.V(5).Infof("Exec mounted %v: %v: %s", mountArgs, err, string(output))
|
||||
if err != nil {
|
||||
return fmt.Errorf("mount failed: %v\nMounting command: %s\nMounting arguments: %s %s %s %v\nOutput: %s",
|
||||
err, "mount", source, target, fstype, options, string(output))
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Unmount runs umount(8) using given exec interface.
|
||||
func (m *execMounter) Unmount(target string) error {
|
||||
outputBytes, err := m.exec.Run("umount", target)
|
||||
if err == nil {
|
||||
klog.V(5).Infof("Exec unmounted %s: %s", target, string(outputBytes))
|
||||
} else {
|
||||
klog.V(5).Infof("Failed to exec unmount %s: err: %q, umount output: %s", target, err, string(outputBytes))
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// List returns a list of all mounted filesystems.
|
||||
func (m *execMounter) List() ([]mount.MountPoint, error) {
|
||||
return m.wrappedMounter.List()
|
||||
}
|
||||
|
||||
// IsLikelyNotMountPoint determines whether a path is a mountpoint.
|
||||
func (m *execMounter) IsLikelyNotMountPoint(file string) (bool, error) {
|
||||
return m.wrappedMounter.IsLikelyNotMountPoint(file)
|
||||
}
|
||||
|
||||
// DeviceOpened checks if block device in use by calling Open with O_EXCL flag.
|
||||
// Returns true if open returns errno EBUSY, and false if errno is nil.
|
||||
// Returns an error if errno is any error other than EBUSY.
|
||||
// Returns with error if pathname is not a device.
|
||||
func (m *execMounter) DeviceOpened(pathname string) (bool, error) {
|
||||
return m.wrappedMounter.DeviceOpened(pathname)
|
||||
}
|
||||
|
||||
// PathIsDevice uses FileInfo returned from os.Stat to check if path refers
|
||||
// to a device.
|
||||
func (m *execMounter) PathIsDevice(pathname string) (bool, error) {
|
||||
return m.wrappedMounter.PathIsDevice(pathname)
|
||||
}
|
||||
|
||||
//GetDeviceNameFromMount given a mount point, find the volume id from checking /proc/mounts
|
||||
func (m *execMounter) GetDeviceNameFromMount(mountPath, pluginDir string) (string, error) {
|
||||
return m.wrappedMounter.GetDeviceNameFromMount(mountPath, pluginDir)
|
||||
}
|
||||
|
||||
func (m *execMounter) IsMountPointMatch(mp mount.MountPoint, dir string) bool {
|
||||
return m.wrappedMounter.IsMountPointMatch(mp, dir)
|
||||
}
|
||||
|
||||
func (m *execMounter) MakeRShared(path string) error {
|
||||
return m.wrappedMounter.MakeRShared(path)
|
||||
}
|
||||
|
||||
func (m *execMounter) GetFileType(pathname string) (mount.FileType, error) {
|
||||
return m.wrappedMounter.GetFileType(pathname)
|
||||
}
|
||||
|
||||
func (m *execMounter) MakeFile(pathname string) error {
|
||||
return m.wrappedMounter.MakeFile(pathname)
|
||||
}
|
||||
|
||||
func (m *execMounter) MakeDir(pathname string) error {
|
||||
return m.wrappedMounter.MakeDir(pathname)
|
||||
}
|
||||
|
||||
func (m *execMounter) ExistsPath(pathname string) (bool, error) {
|
||||
return m.wrappedMounter.ExistsPath(pathname)
|
||||
}
|
||||
|
||||
func (m *execMounter) EvalHostSymlinks(pathname string) (string, error) {
|
||||
return m.wrappedMounter.EvalHostSymlinks(pathname)
|
||||
}
|
||||
|
||||
func (m *execMounter) GetMountRefs(pathname string) ([]string, error) {
|
||||
return m.wrappedMounter.GetMountRefs(pathname)
|
||||
}
|
||||
|
||||
func (m *execMounter) GetFSGroup(pathname string) (int64, error) {
|
||||
return m.wrappedMounter.GetFSGroup(pathname)
|
||||
}
|
||||
|
||||
func (m *execMounter) GetSELinuxSupport(pathname string) (bool, error) {
|
||||
return m.wrappedMounter.GetSELinuxSupport(pathname)
|
||||
}
|
||||
|
||||
func (m *execMounter) GetMode(pathname string) (os.FileMode, error) {
|
||||
return m.wrappedMounter.GetMode(pathname)
|
||||
}
|
||||
119
pkg/volume/util/exec/exec_mount_test.go
Normal file
119
pkg/volume/util/exec/exec_mount_test.go
Normal file
@@ -0,0 +1,119 @@
|
||||
// +build linux
|
||||
|
||||
/*
|
||||
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 exec
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/util/mount"
|
||||
)
|
||||
|
||||
var (
|
||||
sourcePath = "/mnt/srv"
|
||||
destinationPath = "/mnt/dst"
|
||||
fsType = "xfs"
|
||||
mountOptions = []string{"vers=1", "foo=bar"}
|
||||
)
|
||||
|
||||
func TestMount(t *testing.T) {
|
||||
exec := mount.NewFakeExec(func(cmd string, args ...string) ([]byte, error) {
|
||||
if cmd != "mount" {
|
||||
t.Errorf("expected mount command, got %q", cmd)
|
||||
}
|
||||
// mount -t fstype -o options source target
|
||||
expectedArgs := []string{"-t", fsType, "-o", strings.Join(mountOptions, ","), sourcePath, destinationPath}
|
||||
if !reflect.DeepEqual(expectedArgs, args) {
|
||||
t.Errorf("expected arguments %q, got %q", strings.Join(expectedArgs, " "), strings.Join(args, " "))
|
||||
}
|
||||
return nil, nil
|
||||
})
|
||||
|
||||
wrappedMounter := &fakeMounter{FakeMounter: &mount.FakeMounter{}, t: t}
|
||||
mounter := NewExecMounter(exec, wrappedMounter)
|
||||
|
||||
mounter.Mount(sourcePath, destinationPath, fsType, mountOptions)
|
||||
}
|
||||
|
||||
func TestBindMount(t *testing.T) {
|
||||
cmdCount := 0
|
||||
exec := mount.NewFakeExec(func(cmd string, args ...string) ([]byte, error) {
|
||||
cmdCount++
|
||||
if cmd != "mount" {
|
||||
t.Errorf("expected mount command, got %q", cmd)
|
||||
}
|
||||
var expectedArgs []string
|
||||
switch cmdCount {
|
||||
case 1:
|
||||
// mount -t fstype -o "bind" source target
|
||||
expectedArgs = []string{"-t", fsType, "-o", "bind", sourcePath, destinationPath}
|
||||
case 2:
|
||||
// mount -t fstype -o "remount,opts" source target
|
||||
expectedArgs = []string{"-t", fsType, "-o", "bind,remount," + strings.Join(mountOptions, ","), sourcePath, destinationPath}
|
||||
}
|
||||
if !reflect.DeepEqual(expectedArgs, args) {
|
||||
t.Errorf("expected arguments %q, got %q", strings.Join(expectedArgs, " "), strings.Join(args, " "))
|
||||
}
|
||||
return nil, nil
|
||||
})
|
||||
|
||||
wrappedMounter := &fakeMounter{FakeMounter: &mount.FakeMounter{}, t: t}
|
||||
mounter := NewExecMounter(exec, wrappedMounter)
|
||||
bindOptions := append(mountOptions, "bind")
|
||||
mounter.Mount(sourcePath, destinationPath, fsType, bindOptions)
|
||||
}
|
||||
|
||||
func TestUnmount(t *testing.T) {
|
||||
exec := mount.NewFakeExec(func(cmd string, args ...string) ([]byte, error) {
|
||||
if cmd != "umount" {
|
||||
t.Errorf("expected unmount command, got %q", cmd)
|
||||
}
|
||||
// unmount $target
|
||||
expectedArgs := []string{destinationPath}
|
||||
if !reflect.DeepEqual(expectedArgs, args) {
|
||||
t.Errorf("expected arguments %q, got %q", strings.Join(expectedArgs, " "), strings.Join(args, " "))
|
||||
}
|
||||
return nil, nil
|
||||
})
|
||||
|
||||
wrappedMounter := &fakeMounter{&mount.FakeMounter{}, t}
|
||||
mounter := NewExecMounter(exec, wrappedMounter)
|
||||
|
||||
mounter.Unmount(destinationPath)
|
||||
}
|
||||
|
||||
/* Fake wrapped mounter */
|
||||
type fakeMounter struct {
|
||||
*mount.FakeMounter
|
||||
t *testing.T
|
||||
}
|
||||
|
||||
func (fm *fakeMounter) Mount(source string, target string, fstype string, options []string) error {
|
||||
// Mount() of wrapped mounter should never be called. We call exec instead.
|
||||
fm.t.Errorf("Unexpected wrapped mount call")
|
||||
return fmt.Errorf("Unexpected wrapped mount call")
|
||||
}
|
||||
|
||||
func (fm *fakeMounter) Unmount(target string) error {
|
||||
// umount() of wrapped mounter should never be called. We call exec instead.
|
||||
fm.t.Errorf("Unexpected wrapped mount call")
|
||||
return fmt.Errorf("Unexpected wrapped mount call")
|
||||
}
|
||||
106
pkg/volume/util/exec/exec_mount_unsupported.go
Normal file
106
pkg/volume/util/exec/exec_mount_unsupported.go
Normal file
@@ -0,0 +1,106 @@
|
||||
// +build !linux
|
||||
|
||||
/*
|
||||
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 exec
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"k8s.io/kubernetes/pkg/util/mount"
|
||||
)
|
||||
|
||||
type execMounter struct{}
|
||||
|
||||
// NewExecMounter returns a mounter that uses provided Exec interface to mount and
|
||||
// unmount a filesystem. For all other calls it uses a wrapped mounter.
|
||||
func NewExecMounter(exec mount.Exec, wrapped mount.Interface) mount.Interface {
|
||||
return &execMounter{}
|
||||
}
|
||||
|
||||
func (mounter *execMounter) Mount(source string, target string, fstype string, options []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mounter *execMounter) Unmount(target string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mounter *execMounter) List() ([]mount.MountPoint, error) {
|
||||
return []mount.MountPoint{}, nil
|
||||
}
|
||||
|
||||
func (mounter *execMounter) IsMountPointMatch(mp mount.MountPoint, dir string) bool {
|
||||
return (mp.Path == dir)
|
||||
}
|
||||
|
||||
func (mounter *execMounter) IsLikelyNotMountPoint(file string) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (mounter *execMounter) GetDeviceNameFromMount(mountPath, pluginDir string) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (mounter *execMounter) DeviceOpened(pathname string) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (mounter *execMounter) PathIsDevice(pathname string) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (mounter *execMounter) MakeRShared(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mounter *execMounter) GetFileType(pathname string) (mount.FileType, error) {
|
||||
return mount.FileType("fake"), errors.New("not implemented")
|
||||
}
|
||||
|
||||
func (mounter *execMounter) MakeDir(pathname string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mounter *execMounter) MakeFile(pathname string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mounter *execMounter) ExistsPath(pathname string) (bool, error) {
|
||||
return true, errors.New("not implemented")
|
||||
}
|
||||
|
||||
func (mounter *execMounter) EvalHostSymlinks(pathname string) (string, error) {
|
||||
return "", errors.New("not implemented")
|
||||
}
|
||||
|
||||
func (mounter *execMounter) GetMountRefs(pathname string) ([]string, error) {
|
||||
return nil, errors.New("not implemented")
|
||||
}
|
||||
|
||||
func (mounter *execMounter) GetFSGroup(pathname string) (int64, error) {
|
||||
return -1, errors.New("not implemented")
|
||||
}
|
||||
|
||||
func (mounter *execMounter) GetSELinuxSupport(pathname string) (bool, error) {
|
||||
return false, errors.New("not implemented")
|
||||
}
|
||||
|
||||
func (mounter *execMounter) GetMode(pathname string) (os.FileMode, error) {
|
||||
return 0, errors.New("not implemented")
|
||||
}
|
||||
Reference in New Issue
Block a user