diff --git a/pkg/volume/glusterfs/glusterfs.go b/pkg/volume/glusterfs/glusterfs.go index 8931d5d7d1..d33adcbbe4 100644 --- a/pkg/volume/glusterfs/glusterfs.go +++ b/pkg/volume/glusterfs/glusterfs.go @@ -65,7 +65,8 @@ func (plugin *glusterfsPlugin) GetAccessModes() []api.PersistentVolumeAccessMode } func (plugin *glusterfsPlugin) NewBuilder(spec *volume.Spec, pod *api.Pod, _ volume.VolumeOptions, mounter mount.Interface) (volume.Builder, error) { - ep_name := spec.VolumeSource.Glusterfs.EndpointsName + source := plugin.getGlusterVolumeSource(spec) + ep_name := source.EndpointsName ns := pod.Namespace ep, err := plugin.host.GetKubeClient().Endpoints(ns).Get(ep_name) if err != nil { @@ -76,12 +77,21 @@ func (plugin *glusterfsPlugin) NewBuilder(spec *volume.Spec, pod *api.Pod, _ vol return plugin.newBuilderInternal(spec, ep, pod, mounter, exec.New()) } +func (plugin *glusterfsPlugin) getGlusterVolumeSource(spec *volume.Spec) *api.GlusterfsVolumeSource { + if spec.VolumeSource.Glusterfs != nil { + return spec.VolumeSource.Glusterfs + } else { + return spec.PersistentVolumeSource.Glusterfs + } +} + func (plugin *glusterfsPlugin) newBuilderInternal(spec *volume.Spec, ep *api.Endpoints, pod *api.Pod, mounter mount.Interface, exe exec.Interface) (volume.Builder, error) { + source := plugin.getGlusterVolumeSource(spec) return &glusterfs{ volName: spec.Name, hosts: ep, - path: spec.VolumeSource.Glusterfs.Path, - readonly: spec.VolumeSource.Glusterfs.ReadOnly, + path: source.Path, + readonly: source.ReadOnly, mounter: mounter, exe: exe, pod: pod, diff --git a/pkg/volume/glusterfs/glusterfs_test.go b/pkg/volume/glusterfs/glusterfs_test.go index abfbbffdfd..57baf67189 100644 --- a/pkg/volume/glusterfs/glusterfs_test.go +++ b/pkg/volume/glusterfs/glusterfs_test.go @@ -70,17 +70,13 @@ func contains(modes []api.PersistentVolumeAccessMode, mode api.PersistentVolumeA return false } -func TestPlugin(t *testing.T) { +func doTestPlugin(t *testing.T, spec *volume.Spec) { plugMgr := volume.VolumePluginMgr{} plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) plug, err := plugMgr.FindPluginByName("kubernetes.io/glusterfs") if err != nil { t.Errorf("Can't find the plugin by name") } - spec := &api.Volume{ - Name: "vol1", - VolumeSource: api.VolumeSource{Glusterfs: &api.GlusterfsVolumeSource{"ep", "vol", false}}, - } ep := &api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "foo"}, Subsets: []api.EndpointSubset{{ Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}}}}} var fcmd exec.FakeCmd @@ -98,7 +94,7 @@ func TestPlugin(t *testing.T) { }, } pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: types.UID("poduid")}} - builder, err := plug.(*glusterfsPlugin).newBuilderInternal(volume.NewSpecFromVolume(spec), ep, pod, &mount.FakeMounter{}, &fake) + builder, err := plug.(*glusterfsPlugin).newBuilderInternal(spec, ep, pod, &mount.FakeMounter{}, &fake) volumePath := builder.GetPath() if err != nil { t.Errorf("Failed to make a new Builder: %v", err) @@ -136,3 +132,26 @@ func TestPlugin(t *testing.T) { t.Errorf("SetUp() failed: %v", err) } } + +func TestPluginVolume(t *testing.T) { + vol := &api.Volume{ + Name: "vol1", + VolumeSource: api.VolumeSource{Glusterfs: &api.GlusterfsVolumeSource{"ep", "vol", false}}, + } + doTestPlugin(t, volume.NewSpecFromVolume(vol)) +} + +func TestPluginPersistentVolume(t *testing.T) { + vol := &api.PersistentVolume{ + ObjectMeta: api.ObjectMeta{ + Name: "vol1", + }, + Spec: api.PersistentVolumeSpec{ + PersistentVolumeSource: api.PersistentVolumeSource{ + Glusterfs: &api.GlusterfsVolumeSource{"ep", "vol", false}, + }, + }, + } + + doTestPlugin(t, volume.NewSpecFromPersistentVolume(vol)) +} diff --git a/pkg/volume/iscsi/iscsi.go b/pkg/volume/iscsi/iscsi.go index bfc8e5986c..00c5c3318f 100644 --- a/pkg/volume/iscsi/iscsi.go +++ b/pkg/volume/iscsi/iscsi.go @@ -80,7 +80,13 @@ func (plugin *ISCSIPlugin) NewBuilder(spec *volume.Spec, pod *api.Pod, _ volume. } func (plugin *ISCSIPlugin) newBuilderInternal(spec *volume.Spec, podUID types.UID, manager diskManager, mounter mount.Interface) (volume.Builder, error) { - iscsi := spec.VolumeSource.ISCSI + var iscsi *api.ISCSIVolumeSource + if spec.VolumeSource.ISCSI != nil { + iscsi = spec.VolumeSource.ISCSI + } else { + iscsi = spec.PersistentVolumeSource.ISCSI + } + lun := strconv.Itoa(iscsi.Lun) return &iscsiDisk{ diff --git a/pkg/volume/iscsi/iscsi_test.go b/pkg/volume/iscsi/iscsi_test.go index 90db44cf43..4f7a863681 100644 --- a/pkg/volume/iscsi/iscsi_test.go +++ b/pkg/volume/iscsi/iscsi_test.go @@ -96,7 +96,7 @@ func (fake *fakeDiskManager) DetachDisk(disk iscsiDisk, mntPath string) error { return nil } -func TestPlugin(t *testing.T) { +func doTestPlugin(t *testing.T, spec *volume.Spec) { plugMgr := volume.VolumePluginMgr{} plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) @@ -104,20 +104,9 @@ func TestPlugin(t *testing.T) { if err != nil { t.Errorf("Can't find the plugin by name") } - spec := &api.Volume{ - Name: "vol1", - VolumeSource: api.VolumeSource{ - ISCSI: &api.ISCSIVolumeSource{ - TargetPortal: "127.0.0.1:3260", - IQN: "iqn.2014-12.server:storage.target01", - FSType: "ext4", - Lun: 0, - }, - }, - } fakeManager := &fakeDiskManager{} fakeMounter := &mount.FakeMounter{} - builder, err := plug.(*ISCSIPlugin).newBuilderInternal(volume.NewSpecFromVolume(spec), types.UID("poduid"), fakeManager, fakeMounter) + builder, err := plug.(*ISCSIPlugin).newBuilderInternal(spec, types.UID("poduid"), fakeManager, fakeMounter) if err != nil { t.Errorf("Failed to make a new Builder: %v", err) } @@ -172,3 +161,37 @@ func TestPlugin(t *testing.T) { t.Errorf("Detach was not called") } } + +func TestPluginVolume(t *testing.T) { + vol := &api.Volume{ + Name: "vol1", + VolumeSource: api.VolumeSource{ + ISCSI: &api.ISCSIVolumeSource{ + TargetPortal: "127.0.0.1:3260", + IQN: "iqn.2014-12.server:storage.target01", + FSType: "ext4", + Lun: 0, + }, + }, + } + doTestPlugin(t, volume.NewSpecFromVolume(vol)) +} + +func TestPluginPersistentVolume(t *testing.T) { + vol := &api.PersistentVolume{ + ObjectMeta: api.ObjectMeta{ + Name: "vol1", + }, + Spec: api.PersistentVolumeSpec{ + PersistentVolumeSource: api.PersistentVolumeSource{ + ISCSI: &api.ISCSIVolumeSource{ + TargetPortal: "127.0.0.1:3260", + IQN: "iqn.2014-12.server:storage.target01", + FSType: "ext4", + Lun: 0, + }, + }, + }, + } + doTestPlugin(t, volume.NewSpecFromPersistentVolume(vol)) +} diff --git a/pkg/volume/nfs/nfs.go b/pkg/volume/nfs/nfs.go index 073518fd57..a3183112aa 100644 --- a/pkg/volume/nfs/nfs.go +++ b/pkg/volume/nfs/nfs.go @@ -68,15 +68,23 @@ func (plugin *nfsPlugin) NewBuilder(spec *volume.Spec, pod *api.Pod, _ volume.Vo } func (plugin *nfsPlugin) newBuilderInternal(spec *volume.Spec, pod *api.Pod, mounter mount.Interface) (volume.Builder, error) { + var source *api.NFSVolumeSource + + if spec.VolumeSource.NFS != nil { + source = spec.VolumeSource.NFS + } else { + source = spec.PersistentVolumeSource.NFS + } return &nfs{ volName: spec.Name, - server: spec.VolumeSource.NFS.Server, - exportPath: spec.VolumeSource.NFS.Path, - readOnly: spec.VolumeSource.NFS.ReadOnly, + server: source.Server, + exportPath: source.Path, + readOnly: source.ReadOnly, mounter: mounter, pod: pod, plugin: plugin, }, nil + } func (plugin *nfsPlugin) NewCleaner(volName string, podUID types.UID, mounter mount.Interface) (volume.Cleaner, error) { diff --git a/pkg/volume/nfs/nfs_test.go b/pkg/volume/nfs/nfs_test.go index 137a985782..948397eb3c 100644 --- a/pkg/volume/nfs/nfs_test.go +++ b/pkg/volume/nfs/nfs_test.go @@ -69,20 +69,16 @@ func contains(modes []api.PersistentVolumeAccessMode, mode api.PersistentVolumeA return false } -func TestPlugin(t *testing.T) { +func doTestPlugin(t *testing.T, spec *volume.Spec) { plugMgr := volume.VolumePluginMgr{} plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) plug, err := plugMgr.FindPluginByName("kubernetes.io/nfs") if err != nil { t.Errorf("Can't find the plugin by name") } - spec := &api.Volume{ - Name: "vol1", - VolumeSource: api.VolumeSource{NFS: &api.NFSVolumeSource{"localhost", "/tmp", false}}, - } fake := &mount.FakeMounter{} pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: types.UID("poduid")}} - builder, err := plug.(*nfsPlugin).newBuilderInternal(volume.NewSpecFromVolume(spec), pod, fake) + builder, err := plug.(*nfsPlugin).newBuilderInternal(spec, pod, fake) volumePath := builder.GetPath() if err != nil { t.Errorf("Failed to make a new Builder: %v", err) @@ -141,3 +137,26 @@ func TestPlugin(t *testing.T) { fake.ResetLog() } + +func TestPluginVolume(t *testing.T) { + vol := &api.Volume{ + Name: "vol1", + VolumeSource: api.VolumeSource{NFS: &api.NFSVolumeSource{"localhost", "/tmp", false}}, + } + doTestPlugin(t, volume.NewSpecFromVolume(vol)) +} + +func TestPluginPersistentVolume(t *testing.T) { + vol := &api.PersistentVolume{ + ObjectMeta: api.ObjectMeta{ + Name: "vol1", + }, + Spec: api.PersistentVolumeSpec{ + PersistentVolumeSource: api.PersistentVolumeSource{ + NFS: &api.NFSVolumeSource{"localhost", "/tmp", false}, + }, + }, + } + + doTestPlugin(t, volume.NewSpecFromPersistentVolume(vol)) +} diff --git a/pkg/volume/rbd/rbd.go b/pkg/volume/rbd/rbd.go index fd7480366c..1839211890 100644 --- a/pkg/volume/rbd/rbd.go +++ b/pkg/volume/rbd/rbd.go @@ -74,15 +74,17 @@ func (plugin *RBDPlugin) GetAccessModes() []api.PersistentVolumeAccessMode { func (plugin *RBDPlugin) NewBuilder(spec *volume.Spec, pod *api.Pod, _ volume.VolumeOptions, mounter mount.Interface) (volume.Builder, error) { secret := "" - if spec.VolumeSource.RBD.SecretRef != nil { + source := plugin.getRBDVolumeSource(spec) + + if source.SecretRef != nil { kubeClient := plugin.host.GetKubeClient() if kubeClient == nil { return nil, fmt.Errorf("Cannot get kube client") } - secretName, err := kubeClient.Secrets(pod.Namespace).Get(spec.VolumeSource.RBD.SecretRef.Name) + secretName, err := kubeClient.Secrets(pod.Namespace).Get(source.SecretRef.Name) if err != nil { - glog.Errorf("Couldn't get secret %v/%v", pod.Namespace, spec.VolumeSource.RBD.SecretRef) + glog.Errorf("Couldn't get secret %v/%v", pod.Namespace, source.SecretRef) return nil, err } for name, data := range secretName.Data { @@ -95,16 +97,25 @@ func (plugin *RBDPlugin) NewBuilder(spec *volume.Spec, pod *api.Pod, _ volume.Vo return plugin.newBuilderInternal(spec, pod.UID, &RBDUtil{}, mounter, secret) } +func (plugin *RBDPlugin) getRBDVolumeSource(spec *volume.Spec) *api.RBDVolumeSource { + if spec.VolumeSource.RBD != nil { + return spec.VolumeSource.RBD + } else { + return spec.PersistentVolumeSource.RBD + } +} + func (plugin *RBDPlugin) newBuilderInternal(spec *volume.Spec, podUID types.UID, manager diskManager, mounter mount.Interface, secret string) (volume.Builder, error) { - pool := spec.VolumeSource.RBD.RBDPool + source := plugin.getRBDVolumeSource(spec) + pool := source.RBDPool if pool == "" { pool = "rbd" } - id := spec.VolumeSource.RBD.RadosUser + id := source.RadosUser if id == "" { id = "admin" } - keyring := spec.VolumeSource.RBD.Keyring + keyring := source.Keyring if keyring == "" { keyring = "/etc/ceph/keyring" } @@ -112,14 +123,14 @@ func (plugin *RBDPlugin) newBuilderInternal(spec *volume.Spec, podUID types.UID, return &rbd{ podUID: podUID, volName: spec.Name, - mon: spec.VolumeSource.RBD.CephMonitors, - image: spec.VolumeSource.RBD.RBDImage, + mon: source.CephMonitors, + image: source.RBDImage, pool: pool, id: id, keyring: keyring, secret: secret, - fsType: spec.VolumeSource.RBD.FSType, - readOnly: spec.VolumeSource.RBD.ReadOnly, + fsType: source.FSType, + readOnly: source.ReadOnly, manager: manager, mounter: mounter, plugin: plugin, diff --git a/pkg/volume/rbd/rbd_test.go b/pkg/volume/rbd/rbd_test.go index 0696e42d92..bb2dd08325 100644 --- a/pkg/volume/rbd/rbd_test.go +++ b/pkg/volume/rbd/rbd_test.go @@ -65,7 +65,7 @@ func (fake *fakeDiskManager) DetachDisk(disk rbd, mntPath string) error { return nil } -func TestPlugin(t *testing.T) { +func doTestPlugin(t *testing, spec *volume.SpecT) { plugMgr := volume.VolumePluginMgr{} plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) @@ -73,17 +73,7 @@ func TestPlugin(t *testing.T) { if err != nil { t.Errorf("Can't find the plugin by name") } - spec := &api.Volume{ - Name: "vol1", - VolumeSource: api.VolumeSource{ - RBD: &api.RBDVolumeSource{ - CephMonitors: []string{"a", "b"}, - RBDImage: "bar", - FSType: "ext4", - }, - }, - } - builder, err := plug.(*RBDPlugin).newBuilderInternal(volume.NewSpecFromVolume(spec), types.UID("poduid"), &fakeDiskManager{}, &mount.FakeMounter{}, "secrets") + builder, err := plug.(*RBDPlugin).newBuilderInternal(spec, types.UID("poduid"), &fakeDiskManager{}, &mount.FakeMounter{}, "secrets") if err != nil { t.Errorf("Failed to make a new Builder: %v", err) } @@ -131,3 +121,35 @@ func TestPlugin(t *testing.T) { t.Errorf("SetUp() failed: %v", err) } } + +func TestPluginVolume(t *testing.T) { + vol := &api.Volume{ + Name: "vol1", + VolumeSource: api.VolumeSource{ + RBD: &api.RBDVolumeSource{ + CephMonitors: []string{"a", "b"}, + RBDImage: "bar", + FSType: "ext4", + }, + }, + } + doTestPlugin(t, volume.NewSpecFromVolume(vol)) +} +func TestPluginPersistentVolume(t *testing.T) { + vol := &api.PersistentVolume{ + ObjectMeta: api.ObjectMeta{ + Name: "vol1", + }, + Spec: api.PersistentVolumeSpec{ + PersistentVolumeSource: api.PersistentVolumeSource{ + RBD: &api.RBDVolumeSource{ + CephMonitors: []string{"a", "b"}, + RBDImage: "bar", + FSType: "ext4", + }, + }, + }, + } + + doTestPlugin(t, volume.NewSpecFromPersistentVolume(vol)) +}