Skip to content

Commit

Permalink
Use volumeHandle as PV name when translating EBS inline volume
Browse files Browse the repository at this point in the history
  • Loading branch information
wongma7 committed Jan 13, 2021
1 parent a525ddc commit 8ae140d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 8 deletions.
2 changes: 1 addition & 1 deletion staging/src/k8s.io/csi-translation-lib/plugins/aws_ebs.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (t *awsElasticBlockStoreCSITranslator) TranslateInTreeInlineVolumeToCSI(vol
ObjectMeta: metav1.ObjectMeta{
// Must be unique per disk as it is used as the unique part of the
// staging path
Name: fmt.Sprintf("%s-%s", AWSEBSDriverName, ebsSource.VolumeID),
Name: fmt.Sprintf("%s-%s", AWSEBSDriverName, volumeHandle),
},
Spec: v1.PersistentVolumeSpec{
PersistentVolumeSource: v1.PersistentVolumeSource{
Expand Down
100 changes: 93 additions & 7 deletions staging/src/k8s.io/csi-translation-lib/plugins/aws_ebs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ limitations under the License.
package plugins

import (
v1 "k8s.io/api/core/v1"
"reflect"
"testing"

storage "k8s.io/api/storage/v1"
)

const (
normalVolumeID = "vol-02399794d890f9375"
awsVolumeID = "aws:///vol-02399794d890f9375"
awsZoneVolumeID = "aws://us-west-2a/vol-02399794d890f9375"
invalidVolumeID = "aws://us-west-2a/02399794d890f9375"
)

func TestKubernetesVolumeIDToEBSVolumeID(t *testing.T) {
testCases := []struct {
name string
Expand All @@ -32,22 +40,22 @@ func TestKubernetesVolumeIDToEBSVolumeID(t *testing.T) {
}{
{
name: "Normal ID format",
kubernetesID: "vol-02399794d890f9375",
ebsVolumeID: "vol-02399794d890f9375",
kubernetesID: normalVolumeID,
ebsVolumeID: normalVolumeID,
},
{
name: "aws:///{volumeId} format",
kubernetesID: "aws:///vol-02399794d890f9375",
ebsVolumeID: "vol-02399794d890f9375",
kubernetesID: awsVolumeID,
ebsVolumeID: normalVolumeID,
},
{
name: "aws://{zone}/{volumeId} format",
kubernetesID: "aws://us-west-2a/vol-02399794d890f9375",
ebsVolumeID: "vol-02399794d890f9375",
kubernetesID: awsZoneVolumeID,
ebsVolumeID: normalVolumeID,
},
{
name: "fails on invalid volume ID",
kubernetesID: "aws://us-west-2a/02399794d890f9375",
kubernetesID: invalidVolumeID,
expErr: true,
},
}
Expand Down Expand Up @@ -112,3 +120,81 @@ func TestTranslateEBSInTreeStorageClassToCSI(t *testing.T) {

}
}

func TestTranslateInTreeInlineVolumeToCSI(t *testing.T) {
translator := NewAWSElasticBlockStoreCSITranslator()

cases := []struct {
name string
volumeSource v1.VolumeSource
expPVName string
expErr bool
}{
{
name: "Normal ID format",
volumeSource: v1.VolumeSource{
AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{
VolumeID: normalVolumeID,
},
},
expPVName: "ebs.csi.aws.com-" + normalVolumeID,
},
{
name: "aws:///{volumeId} format",
volumeSource: v1.VolumeSource{
AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{
VolumeID: awsVolumeID,
},
},
expPVName: "ebs.csi.aws.com-" + normalVolumeID,
},
{
name: "aws://{zone}/{volumeId} format",
volumeSource: v1.VolumeSource{
AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{
VolumeID: awsZoneVolumeID,
},
},
expPVName: "ebs.csi.aws.com-" + normalVolumeID,
},
{
name: "fails on invalid volume ID",
volumeSource: v1.VolumeSource{
AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{
VolumeID: invalidVolumeID,
},
},
expErr: true,
},
{
name: "fails on empty volume source",
volumeSource: v1.VolumeSource{},
expErr: true,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Logf("Testing %v", tc.name)
got, err := translator.TranslateInTreeInlineVolumeToCSI(&v1.Volume{Name: "volume", VolumeSource: tc.volumeSource})
if err != nil && !tc.expErr {
t.Fatalf("Did not expect error but got: %v", err)
}

if err == nil && tc.expErr {
t.Fatalf("Expected error, but did not get one.")
}

if err == nil {
if !reflect.DeepEqual(got.Name, tc.expPVName) {
t.Errorf("Got PV name: %v, expected :%v", got.Name, tc.expPVName)
}

if !reflect.DeepEqual(got.Spec.CSI.VolumeHandle, normalVolumeID) {
t.Errorf("Got PV volumeHandle: %v, expected :%v", got.Spec.CSI.VolumeHandle, normalVolumeID)
}
}

})
}
}

0 comments on commit 8ae140d

Please sign in to comment.