mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-17 15:13:08 +00:00
Adds support for attaching GCEPersitentDisks
Adds GCEPersistentDisk volume struct Adds gce-utils to attach disk to kubelet's VM. Updates config to give compute-rw to every minion. Adds GCEPersistentDisk to API Adds ability to mount attached disks Generalizes PD and adds tests. PD now uses an pluggable API interface. Unit Tests more cleanly separates TearDown and SetUp Modify boilerplate hook to omit build tags Adds Mounter interface; mount is now built by OS TearDown() for PD now detaches disk on final refcount Un-generalized PD; GCE calls moved to cloudprovider Address comments.
This commit is contained in:
committed by
Brendan Burns
parent
48a9ed3147
commit
4ec25f3b81
136
pkg/volume/gce_util.go
Normal file
136
pkg/volume/gce_util.go
Normal file
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
|
||||
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 volume
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider/gce"
|
||||
)
|
||||
|
||||
const partitionRegex = "[a-z][a-z]*(?P<partition>[0-9][0-9]*)?"
|
||||
|
||||
var regexMatcher = regexp.MustCompile(partitionRegex)
|
||||
|
||||
type GCEDiskUtil struct{}
|
||||
|
||||
// Attaches a disk specified by a volume.GCEPersistentDisk to the current kubelet.
|
||||
// Mounts the disk to it's global path.
|
||||
func (util *GCEDiskUtil) AttachDisk(GCEPD *GCEPersistentDisk) error {
|
||||
gce, err := cloudprovider.GetCloudProvider("gce", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
flags := uintptr(0)
|
||||
if GCEPD.ReadOnly {
|
||||
flags = MOUNT_MS_RDONLY
|
||||
}
|
||||
if err := gce.(*gce_cloud.GCECloud).AttachDisk(GCEPD.PDName, GCEPD.ReadOnly); err != nil {
|
||||
return err
|
||||
}
|
||||
devicePath := path.Join("/dev/disk/by-id/", "google-"+GCEPD.PDName)
|
||||
if GCEPD.Partition != "" {
|
||||
devicePath = devicePath + "-part" + GCEPD.Partition
|
||||
}
|
||||
//TODO(jonesdl) There should probably be better method than busy-waiting here.
|
||||
numTries := 0
|
||||
for {
|
||||
_, err := os.Stat(devicePath)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
numTries++
|
||||
if numTries == 10 {
|
||||
return errors.New("Could not attach disk: Timeout after 10s")
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
globalPDPath := makeGlobalPDName(GCEPD.RootDir, GCEPD.PDName, GCEPD.ReadOnly)
|
||||
// Only mount the PD globally once.
|
||||
_, err = os.Stat(globalPDPath)
|
||||
if os.IsNotExist(err) {
|
||||
err = os.MkdirAll(globalPDPath, 0750)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = GCEPD.mounter.Mount(devicePath, globalPDPath, GCEPD.FSType, flags, "")
|
||||
if err != nil {
|
||||
os.RemoveAll(globalPDPath)
|
||||
return err
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getDeviceName(devicePath, canonicalDevicePath string) (string, error) {
|
||||
isMatch := regexMatcher.MatchString(path.Base(canonicalDevicePath))
|
||||
if !isMatch {
|
||||
return "", fmt.Errorf("unexpected device: %s", canonicalDevicePath)
|
||||
}
|
||||
if isMatch {
|
||||
result := make(map[string]string)
|
||||
substrings := regexMatcher.FindStringSubmatch(path.Base(canonicalDevicePath))
|
||||
for i, label := range regexMatcher.SubexpNames() {
|
||||
result[label] = substrings[i]
|
||||
}
|
||||
partition := result["partition"]
|
||||
devicePath = strings.TrimSuffix(devicePath, "-part"+partition)
|
||||
}
|
||||
return strings.TrimPrefix(path.Base(devicePath), "google-"), nil
|
||||
}
|
||||
|
||||
// Unmounts the device and detaches the disk from the kubelet's host machine.
|
||||
// Expects a GCE device path symlink. Ex: /dev/disk/by-id/google-mydisk-part1
|
||||
func (util *GCEDiskUtil) DetachDisk(GCEPD *GCEPersistentDisk, devicePath string) error {
|
||||
// Follow the symlink to the actual device path.
|
||||
canonicalDevicePath, err := filepath.EvalSymlinks(devicePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
deviceName, err := getDeviceName(devicePath, canonicalDevicePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
globalPDPath := makeGlobalPDName(GCEPD.RootDir, deviceName, GCEPD.ReadOnly)
|
||||
if err := GCEPD.mounter.Unmount(globalPDPath, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.RemoveAll(globalPDPath); err != nil {
|
||||
return err
|
||||
}
|
||||
gce, err := cloudprovider.GetCloudProvider("gce", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := gce.(*gce_cloud.GCECloud).DetachDisk(deviceName); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
55
pkg/volume/gce_util_test.go
Normal file
55
pkg/volume/gce_util_test.go
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
|
||||
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 volume
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetDeviceName(t *testing.T) {
|
||||
tests := []struct {
|
||||
deviceName string
|
||||
canonicalName string
|
||||
expectedName string
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
deviceName: "/dev/google-sd0-part0",
|
||||
canonicalName: "/dev/google/sd0P1",
|
||||
expectedName: "sd0",
|
||||
},
|
||||
{
|
||||
canonicalName: "0123456",
|
||||
expectError: true,
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
name, err := getDeviceName(test.deviceName, test.canonicalName)
|
||||
if test.expectError {
|
||||
if err == nil {
|
||||
t.Error("unexpected non-error")
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if name != test.expectedName {
|
||||
t.Errorf("expected: %s, got %s", test.expectedName, name)
|
||||
}
|
||||
}
|
||||
}
|
86
pkg/volume/mounter_linux.go
Normal file
86
pkg/volume/mounter_linux.go
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
|
||||
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 volume
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
const MOUNT_MS_BIND = syscall.MS_BIND
|
||||
const MOUNT_MS_RDONLY = syscall.MS_RDONLY
|
||||
|
||||
type DiskMounter struct{}
|
||||
|
||||
// Wraps syscall.Mount()
|
||||
func (mounter *DiskMounter) Mount(source string, target string, fstype string, flags uintptr, data string) error {
|
||||
return syscall.Mount(source, target, fstype, flags, data)
|
||||
}
|
||||
|
||||
// Wraps syscall.Unmount()
|
||||
func (mounter *DiskMounter) Unmount(target string, flags int) error {
|
||||
return syscall.Unmount(target, flags)
|
||||
}
|
||||
|
||||
// Examines /proc/mounts to find the source device of the PD resource and the
|
||||
// number of references to that device. Returns both the full device path under
|
||||
// the /dev tree and the number of references.
|
||||
func (mounter *DiskMounter) RefCount(mount Interface) (string, int, error) {
|
||||
// TODO(jonesdl) This can be split up into two procedures, finding the device path
|
||||
// and finding the number of references. The parsing could also be separated and another
|
||||
// utility could determine if a volume's path is an active mount point.
|
||||
file, err := os.Open("/proc/mounts")
|
||||
if err != nil {
|
||||
return "", -1, err
|
||||
}
|
||||
defer file.Close()
|
||||
scanner := bufio.NewReader(file)
|
||||
refCount := 0
|
||||
var deviceName string
|
||||
// Find the actual device path.
|
||||
for {
|
||||
line, err := scanner.ReadString('\n')
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
success, err := regexp.MatchString(mount.GetPath(), line)
|
||||
if err != nil {
|
||||
return "", -1, err
|
||||
}
|
||||
if success {
|
||||
deviceName = strings.Split(line, " ")[0]
|
||||
}
|
||||
}
|
||||
file.Close()
|
||||
file, err = os.Open("/proc/mounts")
|
||||
scanner.Reset(bufio.NewReader(file))
|
||||
// Find the number of references to the device.
|
||||
for {
|
||||
line, err := scanner.ReadString('\n')
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if strings.Split(line, " ")[0] == deviceName {
|
||||
refCount++
|
||||
}
|
||||
}
|
||||
return deviceName, refCount, nil
|
||||
}
|
36
pkg/volume/mounter_unsupported.go
Normal file
36
pkg/volume/mounter_unsupported.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// +build !linux
|
||||
|
||||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
|
||||
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 volume
|
||||
|
||||
const MOUNT_MS_BIND = 0
|
||||
const MOUNT_MS_RDONLY = 0
|
||||
|
||||
type DiskMounter struct{}
|
||||
|
||||
func (mounter *DiskMounter) Mount(source string, target string, fstype string, flags uintptr, data string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mounter *DiskMounter) Unmount(target string, flags int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mounter *DiskMounter) RefCount(PD Interface) (string, int, error) {
|
||||
return "", 0, nil
|
||||
}
|
@@ -21,6 +21,7 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/golang/glog"
|
||||
@@ -49,6 +50,22 @@ type Cleaner interface {
|
||||
TearDown() error
|
||||
}
|
||||
|
||||
type gcePersistentDiskUtil interface {
|
||||
// Attaches the disk to the kubelet's host machine.
|
||||
AttachDisk(PD *GCEPersistentDisk) error
|
||||
// Detaches the disk from the kubelet's host machine.
|
||||
DetachDisk(PD *GCEPersistentDisk, devicePath string) error
|
||||
}
|
||||
|
||||
// Mounters wrap os/system specific calls to perform mounts.
|
||||
type mounter interface {
|
||||
Mount(source string, target string, fstype string, flags uintptr, data string) error
|
||||
Unmount(target string, flags int) error
|
||||
// RefCount returns the device path for the source disk of a volume, and
|
||||
// the number of references to that target disk.
|
||||
RefCount(vol Interface) (string, int, error)
|
||||
}
|
||||
|
||||
// HostDir volumes represent a bare host directory mount.
|
||||
// The directory in Path will be directly exposed to the container.
|
||||
type HostDir struct {
|
||||
@@ -118,11 +135,128 @@ func createHostDir(volume *api.Volume) *HostDir {
|
||||
return &HostDir{volume.Source.HostDir.Path}
|
||||
}
|
||||
|
||||
// GCEPersistentDisk volumes are disk resources provided by Google Compute Engine
|
||||
// that are attached to the kubelet's host machine and exposed to the pod.
|
||||
type GCEPersistentDisk struct {
|
||||
Name string
|
||||
PodID string
|
||||
RootDir string
|
||||
// Unique identifier of the PD, used to find the disk resource in the provider.
|
||||
PDName string
|
||||
// Filesystem type, optional.
|
||||
FSType string
|
||||
// Specifies the partition to mount
|
||||
Partition string
|
||||
// Specifies whether the disk will be attached as ReadOnly.
|
||||
ReadOnly bool
|
||||
// Utility interface that provides API calls to the provider to attach/detach disks.
|
||||
util gcePersistentDiskUtil
|
||||
// Mounter interface that provides system calls to mount the disks.
|
||||
mounter mounter
|
||||
}
|
||||
|
||||
func (PD *GCEPersistentDisk) GetPath() string {
|
||||
return path.Join(PD.RootDir, PD.PodID, "volumes", "gce-pd", PD.Name)
|
||||
}
|
||||
|
||||
// Attaches the disk and bind mounts to the volume path.
|
||||
func (PD *GCEPersistentDisk) SetUp() error {
|
||||
// TODO: handle failed mounts here.
|
||||
if _, err := os.Stat(PD.GetPath()); !os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
err := PD.util.AttachDisk(PD)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
flags := uintptr(0)
|
||||
if PD.ReadOnly {
|
||||
flags = MOUNT_MS_RDONLY
|
||||
}
|
||||
//Perform a bind mount to the full path to allow duplicate mounts of the same PD.
|
||||
if _, err = os.Stat(PD.GetPath()); os.IsNotExist(err) {
|
||||
err = os.MkdirAll(PD.GetPath(), 0750)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
globalPDPath := makeGlobalPDName(PD.RootDir, PD.PDName, PD.ReadOnly)
|
||||
err = PD.mounter.Mount(globalPDPath, PD.GetPath(), "", MOUNT_MS_BIND|flags, "")
|
||||
if err != nil {
|
||||
os.RemoveAll(PD.GetPath())
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Unmounts the bind mount, and detaches the disk only if the PD
|
||||
// resource was the last reference to that disk on the kubelet.
|
||||
func (PD *GCEPersistentDisk) TearDown() error {
|
||||
devicePath, refCount, err := PD.mounter.RefCount(PD)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := PD.mounter.Unmount(PD.GetPath(), 0); err != nil {
|
||||
return err
|
||||
}
|
||||
refCount--
|
||||
if err := os.RemoveAll(PD.GetPath()); err != nil {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// If refCount is 1, then all bind mounts have been removed, and the
|
||||
// remaining reference is the global mount. It is safe to detach.
|
||||
if refCount == 1 {
|
||||
if err := PD.util.DetachDisk(PD, devicePath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//TODO(jonesdl) prevent name collisions by using designated pod space as well.
|
||||
// Ex. (ROOT_DIR)/pods/...
|
||||
func makeGlobalPDName(rootDir, devName string, readOnly bool) string {
|
||||
var mode string
|
||||
if readOnly {
|
||||
mode = "ro"
|
||||
} else {
|
||||
mode = "rw"
|
||||
}
|
||||
return path.Join(rootDir, "global", "pd", mode, devName)
|
||||
}
|
||||
|
||||
// createEmptyDir interprets API volume as an EmptyDir.
|
||||
func createEmptyDir(volume *api.Volume, podID string, rootDir string) *EmptyDir {
|
||||
return &EmptyDir{volume.Name, podID, rootDir}
|
||||
}
|
||||
|
||||
// Interprets API volume as a PersistentDisk
|
||||
func createGCEPersistentDisk(volume *api.Volume, podID string, rootDir string) (*GCEPersistentDisk, error) {
|
||||
PDName := volume.Source.GCEPersistentDisk.PDName
|
||||
FSType := volume.Source.GCEPersistentDisk.FSType
|
||||
partition := strconv.Itoa(volume.Source.GCEPersistentDisk.Partition)
|
||||
if partition == "0" {
|
||||
partition = ""
|
||||
}
|
||||
readOnly := volume.Source.GCEPersistentDisk.ReadOnly
|
||||
// TODO: move these up into the Kubelet.
|
||||
util := &GCEDiskUtil{}
|
||||
mounter := &DiskMounter{}
|
||||
return &GCEPersistentDisk{
|
||||
Name: volume.Name,
|
||||
PodID: podID,
|
||||
RootDir: rootDir,
|
||||
PDName: PDName,
|
||||
FSType: FSType,
|
||||
Partition: partition,
|
||||
ReadOnly: readOnly,
|
||||
util: util,
|
||||
mounter: mounter}, nil
|
||||
}
|
||||
|
||||
// CreateVolumeBuilder returns a Builder capable of mounting a volume described by an
|
||||
// *api.Volume, or an error.
|
||||
func CreateVolumeBuilder(volume *api.Volume, podID string, rootDir string) (Builder, error) {
|
||||
@@ -133,12 +267,18 @@ func CreateVolumeBuilder(volume *api.Volume, podID string, rootDir string) (Buil
|
||||
return nil, nil
|
||||
}
|
||||
var vol Builder
|
||||
var err error
|
||||
// TODO(jonesdl) We should probably not check every pointer and directly
|
||||
// resolve these types instead.
|
||||
if source.HostDir != nil {
|
||||
vol = createHostDir(volume)
|
||||
} else if source.EmptyDir != nil {
|
||||
vol = createEmptyDir(volume, podID, rootDir)
|
||||
} else if source.GCEPersistentDisk != nil {
|
||||
vol, err = createGCEPersistentDisk(volume, podID, rootDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
return nil, ErrUnsupportedVolumeType
|
||||
}
|
||||
@@ -150,6 +290,13 @@ func CreateVolumeCleaner(kind string, name string, podID string, rootDir string)
|
||||
switch kind {
|
||||
case "empty":
|
||||
return &EmptyDir{name, podID, rootDir}, nil
|
||||
case "gce-pd":
|
||||
return &GCEPersistentDisk{
|
||||
Name: name,
|
||||
PodID: podID,
|
||||
RootDir: rootDir,
|
||||
util: &GCEDiskUtil{},
|
||||
mounter: &DiskMounter{}}, nil
|
||||
default:
|
||||
return nil, ErrUnsupportedVolumeType
|
||||
}
|
||||
@@ -159,10 +306,9 @@ func CreateVolumeCleaner(kind string, name string, podID string, rootDir string)
|
||||
// presently active and mounted. Returns a map of Cleaner types.
|
||||
func GetCurrentVolumes(rootDirectory string) map[string]Cleaner {
|
||||
currentVolumes := make(map[string]Cleaner)
|
||||
mountPath := rootDirectory
|
||||
podIDDirs, err := ioutil.ReadDir(mountPath)
|
||||
podIDDirs, err := ioutil.ReadDir(rootDirectory)
|
||||
if err != nil {
|
||||
glog.Errorf("Could not read directory: %s, (%s)", mountPath, err)
|
||||
glog.Errorf("Could not read directory: %s, (%s)", rootDirectory, err)
|
||||
}
|
||||
// Volume information is extracted from the directory structure:
|
||||
// (ROOT_DIR)/(POD_ID)/volumes/(VOLUME_KIND)/(VOLUME_NAME)
|
||||
@@ -171,7 +317,10 @@ func GetCurrentVolumes(rootDirectory string) map[string]Cleaner {
|
||||
continue
|
||||
}
|
||||
podID := podIDDir.Name()
|
||||
podIDPath := path.Join(mountPath, podID, "volumes")
|
||||
podIDPath := path.Join(rootDirectory, podID, "volumes")
|
||||
if _, err := os.Stat(podIDPath); os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
volumeKindDirs, err := ioutil.ReadDir(podIDPath)
|
||||
if err != nil {
|
||||
glog.Errorf("Could not read directory: %s, (%s)", podIDPath, err)
|
||||
@@ -189,7 +338,7 @@ func GetCurrentVolumes(rootDirectory string) map[string]Cleaner {
|
||||
// TODO(thockin) This should instead return a reference to an extant volume object
|
||||
cleaner, err := CreateVolumeCleaner(volumeKind, volumeName, podID, rootDirectory)
|
||||
if err != nil {
|
||||
glog.Errorf("Could not create volume cleaner: %s, (%s)", volumeNameDirs, err)
|
||||
glog.Errorf("Could not create volume cleaner: %s, (%s)", volumeNameDir.Name(), err)
|
||||
continue
|
||||
}
|
||||
currentVolumes[identifier] = cleaner
|
||||
|
@@ -20,22 +20,52 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
)
|
||||
|
||||
func TestCreateVolumeBuilders(t *testing.T) {
|
||||
tempDir, err := ioutil.TempDir("", "CreateVolumes")
|
||||
type MockDiskUtil struct{}
|
||||
|
||||
// TODO(jonesdl) To fully test this, we could create a loopback device
|
||||
// and mount that instead.
|
||||
func (util *MockDiskUtil) AttachDisk(PD *GCEPersistentDisk) error {
|
||||
err := os.MkdirAll(path.Join(PD.RootDir, "global", "pd", PD.PDName), 0750)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return err
|
||||
}
|
||||
defer os.RemoveAll(tempDir)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (util *MockDiskUtil) DetachDisk(PD *GCEPersistentDisk, devicePath string) error {
|
||||
err := os.RemoveAll(path.Join(PD.RootDir, "global", "pd", PD.PDName))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type MockMounter struct{}
|
||||
|
||||
func (mounter *MockMounter) Mount(source string, target string, fstype string, flags uintptr, data string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mounter *MockMounter) Unmount(target string, flags int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mounter *MockMounter) RefCount(vol Interface) (string, int, error) {
|
||||
return "", 0, nil
|
||||
}
|
||||
|
||||
func TestCreateVolumeBuilders(t *testing.T) {
|
||||
tempDir := "CreateVolumes"
|
||||
createVolumesTests := []struct {
|
||||
volume api.Volume
|
||||
path string
|
||||
podID string
|
||||
kind string
|
||||
}{
|
||||
{
|
||||
api.Volume{
|
||||
@@ -45,7 +75,6 @@ func TestCreateVolumeBuilders(t *testing.T) {
|
||||
},
|
||||
},
|
||||
"/dir/path",
|
||||
"my-id",
|
||||
"",
|
||||
},
|
||||
{
|
||||
@@ -57,9 +86,18 @@ func TestCreateVolumeBuilders(t *testing.T) {
|
||||
},
|
||||
path.Join(tempDir, "/my-id/volumes/empty/empty-dir"),
|
||||
"my-id",
|
||||
"empty",
|
||||
},
|
||||
{api.Volume{}, "", "", ""},
|
||||
{
|
||||
api.Volume{
|
||||
Name: "gce-pd",
|
||||
Source: &api.VolumeSource{
|
||||
GCEPersistentDisk: &api.GCEPersistentDisk{"my-disk", "ext4", 0, false},
|
||||
},
|
||||
},
|
||||
path.Join(tempDir, "/my-id/volumes/gce-pd/gce-pd"),
|
||||
"my-id",
|
||||
},
|
||||
{api.Volume{}, "", ""},
|
||||
{
|
||||
api.Volume{
|
||||
Name: "empty-dir",
|
||||
@@ -67,7 +105,6 @@ func TestCreateVolumeBuilders(t *testing.T) {
|
||||
},
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
},
|
||||
}
|
||||
for _, createVolumesTest := range createVolumesTests {
|
||||
@@ -79,7 +116,7 @@ func TestCreateVolumeBuilders(t *testing.T) {
|
||||
}
|
||||
continue
|
||||
}
|
||||
if tt.volume.Source.HostDir == nil && tt.volume.Source.EmptyDir == nil {
|
||||
if tt.volume.Source.HostDir == nil && tt.volume.Source.EmptyDir == nil && tt.volume.Source.GCEPersistentDisk == nil {
|
||||
if err != ErrUnsupportedVolumeType {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
@@ -88,22 +125,68 @@ func TestCreateVolumeBuilders(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
err = vb.SetUp()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
path := vb.GetPath()
|
||||
if path != tt.path {
|
||||
t.Errorf("Unexpected bind path. Expected %v, got %v", tt.path, path)
|
||||
}
|
||||
vc, err := CreateVolumeCleaner(tt.kind, tt.volume.Name, tt.podID, tempDir)
|
||||
if tt.kind == "" {
|
||||
if err != ErrUnsupportedVolumeType {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateVolumeCleaners(t *testing.T) {
|
||||
tempDir := "CreateVolumeCleaners"
|
||||
createVolumeCleanerTests := []struct {
|
||||
kind string
|
||||
name string
|
||||
podID string
|
||||
}{
|
||||
{"empty", "empty-vol", "my-id"},
|
||||
{"", "", ""},
|
||||
{"gce-pd", "gce-pd-vol", "my-id"},
|
||||
}
|
||||
for _, tt := range createVolumeCleanerTests {
|
||||
vol, err := CreateVolumeCleaner(tt.kind, tt.name, tt.podID, tempDir)
|
||||
if tt.kind == "" && err != nil && vol == nil {
|
||||
continue
|
||||
}
|
||||
err = vc.TearDown()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error occured: %s", err)
|
||||
}
|
||||
actualKind := reflect.TypeOf(vol).Elem().Name()
|
||||
if tt.kind == "empty" && actualKind != "EmptyDir" {
|
||||
t.Errorf("CreateVolumeCleaner returned invalid type. Expected EmptyDirectory, got %v, %v", tt.kind, actualKind)
|
||||
}
|
||||
if tt.kind == "gce-pd" && actualKind != "GCEPersistentDisk" {
|
||||
t.Errorf("CreateVolumeCleaner returned invalid type. Expected PersistentDisk, got %v, %v", tt.kind, actualKind)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetUpAndTearDown(t *testing.T) {
|
||||
tempDir, err := ioutil.TempDir("", "CreateVolumes")
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tempDir)
|
||||
fakeID := "my-id"
|
||||
type VolumeTester interface {
|
||||
Builder
|
||||
Cleaner
|
||||
}
|
||||
volumes := []VolumeTester{
|
||||
&EmptyDir{"empty", fakeID, tempDir},
|
||||
&GCEPersistentDisk{"pd", fakeID, tempDir, "pd-disk", "ext4", "", false, &MockDiskUtil{}, &MockMounter{}},
|
||||
}
|
||||
|
||||
for _, vol := range volumes {
|
||||
err = vol.SetUp()
|
||||
path := vol.GetPath()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
t.Errorf("SetUp() failed, volume path not created: %v", path)
|
||||
}
|
||||
err = vol.TearDown()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user