Skip to content

Commit 04f18b9

Browse files
authored
fix extraction of EBS volume id when there's no region prefix (#2351)
* add prefix /vol- on when EBS doesn't have * add new unit test for to get the volumeID * add a prefix to search in the string of volumeID --------- Co-authored-by: Jociele Padilha <[email protected]>
1 parent af084a5 commit 04f18b9

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

pkg/cluster/volumes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (c *Cluster) syncVolumes() error {
3535

3636
err = c.populateVolumeMetaData()
3737
if err != nil {
38-
c.logger.Errorf("populating EBS meta data failed, skipping potential adjustements: %v", err)
38+
c.logger.Errorf("populating EBS meta data failed, skipping potential adjustments: %v", err)
3939
} else {
4040
err = c.syncUnderlyingEBSVolume()
4141
if err != nil {

pkg/util/volumes/ebs.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ func (r *EBSVolumeResizer) VolumeBelongsToProvider(pv *v1.PersistentVolume) bool
3939
return pv.Spec.AWSElasticBlockStore != nil && pv.Annotations[constants.VolumeStorateProvisionerAnnotation] == constants.EBSProvisioner
4040
}
4141

42-
// ExtractVolumeID extracts volumeID
42+
// ExtractVolumeID extracts volumeID from "aws://eu-central-1a/vol-075ddfc4a127d0bd4"
43+
// or return only the vol-075ddfc4a127d0bd4 when it doesn't have "aws://"
4344
func (r *EBSVolumeResizer) ExtractVolumeID(volumeID string) (string, error) {
45+
if (strings.HasPrefix(volumeID, "vol-")) && !(strings.HasPrefix(volumeID, "aws://")) {
46+
return volumeID, nil
47+
}
4448
idx := strings.LastIndex(volumeID, constants.EBSVolumeIDStart) + 1
4549
if idx == 0 {
4650
return "", fmt.Errorf("malformed EBS volume id %q", volumeID)

pkg/util/volumes/volumes_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package volumes
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestExtractVolumeID(t *testing.T) {
9+
var tests = []struct {
10+
input string
11+
expectedResult string
12+
expectedErr error
13+
}{
14+
{
15+
input: "aws://eu-central-1c/vol-01234a5b6c78df9gh",
16+
expectedResult: "vol-01234a5b6c78df9gh",
17+
expectedErr: nil,
18+
},
19+
{
20+
input: "vol-0g9fd87c6b5a43210",
21+
expectedResult: "vol-0g9fd87c6b5a43210",
22+
expectedErr: nil,
23+
},
24+
{
25+
input: "aws://eu-central-1c/01234a5b6c78df9g0",
26+
expectedResult: "",
27+
expectedErr: fmt.Errorf("malformed EBS volume id %q", "aws://eu-central-1c/01234a5b6c78df9g0"),
28+
},
29+
{
30+
input: "hg9fd87c6b5a43210",
31+
expectedResult: "",
32+
expectedErr: fmt.Errorf("malformed EBS volume id %q", "hg9fd87c6b5a43210"),
33+
},
34+
}
35+
36+
resizer := EBSVolumeResizer{}
37+
38+
for _, tt := range tests {
39+
volumeId, err := resizer.ExtractVolumeID(tt.input)
40+
if volumeId != tt.expectedResult {
41+
t.Errorf("%s expected: %s, got %s", t.Name(), tt.expectedResult, volumeId)
42+
}
43+
if err != tt.expectedErr {
44+
if tt.expectedErr != nil && err.Error() != tt.expectedErr.Error() {
45+
t.Errorf("%s unexpected error: got %v", t.Name(), err)
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)