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 <jociele.padilha@zalando.de>
This commit is contained in:
Jociele Padilha 2023-06-12 15:18:19 +02:00 committed by GitHub
parent af084a5a65
commit 04f18b9716
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 2 deletions

View File

@ -35,7 +35,7 @@ func (c *Cluster) syncVolumes() error {
err = c.populateVolumeMetaData()
if err != nil {
c.logger.Errorf("populating EBS meta data failed, skipping potential adjustements: %v", err)
c.logger.Errorf("populating EBS meta data failed, skipping potential adjustments: %v", err)
} else {
err = c.syncUnderlyingEBSVolume()
if err != nil {

View File

@ -39,8 +39,12 @@ func (r *EBSVolumeResizer) VolumeBelongsToProvider(pv *v1.PersistentVolume) bool
return pv.Spec.AWSElasticBlockStore != nil && pv.Annotations[constants.VolumeStorateProvisionerAnnotation] == constants.EBSProvisioner
}
// ExtractVolumeID extracts volumeID
// ExtractVolumeID extracts volumeID from "aws://eu-central-1a/vol-075ddfc4a127d0bd4"
// or return only the vol-075ddfc4a127d0bd4 when it doesn't have "aws://"
func (r *EBSVolumeResizer) ExtractVolumeID(volumeID string) (string, error) {
if (strings.HasPrefix(volumeID, "vol-")) && !(strings.HasPrefix(volumeID, "aws://")) {
return volumeID, nil
}
idx := strings.LastIndex(volumeID, constants.EBSVolumeIDStart) + 1
if idx == 0 {
return "", fmt.Errorf("malformed EBS volume id %q", volumeID)

View File

@ -0,0 +1,49 @@
package volumes
import (
"fmt"
"testing"
)
func TestExtractVolumeID(t *testing.T) {
var tests = []struct {
input string
expectedResult string
expectedErr error
}{
{
input: "aws://eu-central-1c/vol-01234a5b6c78df9gh",
expectedResult: "vol-01234a5b6c78df9gh",
expectedErr: nil,
},
{
input: "vol-0g9fd87c6b5a43210",
expectedResult: "vol-0g9fd87c6b5a43210",
expectedErr: nil,
},
{
input: "aws://eu-central-1c/01234a5b6c78df9g0",
expectedResult: "",
expectedErr: fmt.Errorf("malformed EBS volume id %q", "aws://eu-central-1c/01234a5b6c78df9g0"),
},
{
input: "hg9fd87c6b5a43210",
expectedResult: "",
expectedErr: fmt.Errorf("malformed EBS volume id %q", "hg9fd87c6b5a43210"),
},
}
resizer := EBSVolumeResizer{}
for _, tt := range tests {
volumeId, err := resizer.ExtractVolumeID(tt.input)
if volumeId != tt.expectedResult {
t.Errorf("%s expected: %s, got %s", t.Name(), tt.expectedResult, volumeId)
}
if err != tt.expectedErr {
if tt.expectedErr != nil && err.Error() != tt.expectedErr.Error() {
t.Errorf("%s unexpected error: got %v", t.Name(), err)
}
}
}
}