Skip to content

Commit 001d9c6

Browse files
author
Cheng Pan
committed
Add CSI migration logic for EBS Volume ID format
1 parent 596a48d commit 001d9c6

File tree

5 files changed

+133
-41
lines changed

5 files changed

+133
-41
lines changed

pkg/cloudprovider/providers/aws/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ go_library(
4545
"//staging/src/k8s.io/cloud-provider/volume:go_default_library",
4646
"//staging/src/k8s.io/cloud-provider/volume/errors:go_default_library",
4747
"//staging/src/k8s.io/cloud-provider/volume/helpers:go_default_library",
48+
"//staging/src/k8s.io/csi-translation-lib/plugins:go_default_library",
4849
"//vendor/github.com/aws/aws-sdk-go/aws:go_default_library",
4950
"//vendor/github.com/aws/aws-sdk-go/aws/awserr:go_default_library",
5051
"//vendor/github.com/aws/aws-sdk-go/aws/credentials:go_default_library",

pkg/cloudprovider/providers/aws/volumes.go

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,15 @@ package aws
1818

1919
import (
2020
"fmt"
21-
"net/url"
22-
"regexp"
23-
"strings"
2421

2522
"github.com/aws/aws-sdk-go/aws"
2623
"github.com/aws/aws-sdk-go/service/ec2"
24+
csimigration "k8s.io/csi-translation-lib/plugins"
2725
"k8s.io/klog"
2826

2927
"k8s.io/apimachinery/pkg/types"
3028
)
3129

32-
// awsVolumeRegMatch represents Regex Match for AWS volume.
33-
var awsVolumeRegMatch = regexp.MustCompile("^vol-[^/]*$")
34-
3530
// EBSVolumeID represents the ID of the volume in the AWS API, e.g.
3631
// vol-12345678 The "traditional" format is "vol-12345678" A new longer format
3732
// is also being introduced: "vol-12345678abcdef01" We should not assume
@@ -62,41 +57,10 @@ type diskInfo struct {
6257

6358
// MapToAWSVolumeID extracts the EBSVolumeID from the KubernetesVolumeID
6459
func (name KubernetesVolumeID) MapToAWSVolumeID() (EBSVolumeID, error) {
65-
// name looks like aws://availability-zone/awsVolumeId
66-
67-
// The original idea of the URL-style name was to put the AZ into the
68-
// host, so we could find the AZ immediately from the name without
69-
// querying the API. But it turns out we don't actually need it for
70-
// multi-AZ clusters, as we put the AZ into the labels on the PV instead.
71-
// However, if in future we want to support multi-AZ cluster
72-
// volume-awareness without using PersistentVolumes, we likely will
73-
// want the AZ in the host.
74-
75-
s := string(name)
76-
77-
if !strings.HasPrefix(s, "aws://") {
78-
// Assume a bare aws volume id (vol-1234...)
79-
// Build a URL with an empty host (AZ)
80-
s = "aws://" + "" + "/" + s
81-
}
82-
url, err := url.Parse(s)
60+
awsID, err := csimigration.KubernetesVolumeIDToEBSVolumeID(string(name))
8361
if err != nil {
84-
// TODO: Maybe we should pass a URL into the Volume functions
85-
return "", fmt.Errorf("Invalid disk name (%s): %v", name, err)
62+
return "", err
8663
}
87-
if url.Scheme != "aws" {
88-
return "", fmt.Errorf("Invalid scheme for AWS volume (%s)", name)
89-
}
90-
91-
awsID := url.Path
92-
awsID = strings.Trim(awsID, "/")
93-
94-
// We sanity check the resulting volume; the two known formats are
95-
// vol-12345678 and vol-12345678abcdef01
96-
if !awsVolumeRegMatch.MatchString(awsID) {
97-
return "", fmt.Errorf("Invalid format for AWS volume (%s)", name)
98-
}
99-
10064
return EBSVolumeID(awsID), nil
10165
}
10266

staging/src/k8s.io/csi-translation-lib/plugins/BUILD

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@io_bazel_rules_go//go:def.bzl", "go_library")
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
22

33
go_library(
44
name = "go_default_library",
@@ -31,3 +31,9 @@ filegroup(
3131
tags = ["automanaged"],
3232
visibility = ["//visibility:public"],
3333
)
34+
35+
go_test(
36+
name = "go_default_test",
37+
srcs = ["aws_ebs_test.go"],
38+
embed = [":go_default_library"],
39+
)

staging/src/k8s.io/csi-translation-lib/plugins/aws_ebs.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ package plugins
1818

1919
import (
2020
"fmt"
21+
"net/url"
22+
"regexp"
2123
"strconv"
24+
"strings"
2225

2326
"k8s.io/api/core/v1"
2427
)
@@ -54,9 +57,14 @@ func (t *awsElasticBlockStoreCSITranslator) TranslateInTreePVToCSI(pv *v1.Persis
5457

5558
ebsSource := pv.Spec.AWSElasticBlockStore
5659

60+
volumeHandle, err := KubernetesVolumeIDToEBSVolumeID(ebsSource.VolumeID)
61+
if err != nil {
62+
return nil, fmt.Errorf("failed to translate Kubernetes ID to EBS Volume ID %v", err)
63+
}
64+
5765
csiSource := &v1.CSIPersistentVolumeSource{
5866
Driver: AWSEBSDriverName,
59-
VolumeHandle: ebsSource.VolumeID,
67+
VolumeHandle: volumeHandle,
6068
ReadOnly: ebsSource.ReadOnly,
6169
FSType: ebsSource.FSType,
6270
VolumeAttributes: map[string]string{
@@ -113,3 +121,50 @@ func (t *awsElasticBlockStoreCSITranslator) GetInTreePluginName() string {
113121
func (t *awsElasticBlockStoreCSITranslator) GetCSIPluginName() string {
114122
return AWSEBSDriverName
115123
}
124+
125+
// awsVolumeRegMatch represents Regex Match for AWS volume.
126+
var awsVolumeRegMatch = regexp.MustCompile("^vol-[^/]*$")
127+
128+
// KubernetesVolumeIDToEBSVolumeID translates Kubernetes volume ID to EBS volume ID
129+
// KubernetsVolumeID forms:
130+
// * aws://<zone>/<awsVolumeId>
131+
// * aws:///<awsVolumeId>
132+
// * <awsVolumeId>
133+
// EBS Volume ID form:
134+
// * vol-<alphanumberic>
135+
// This translation shouldn't be needed and should be fixed in long run
136+
// See https://github.com/kubernetes/kubernetes/issues/73730
137+
func KubernetesVolumeIDToEBSVolumeID(kubernetesID string) (string, error) {
138+
// name looks like aws://availability-zone/awsVolumeId
139+
140+
// The original idea of the URL-style name was to put the AZ into the
141+
// host, so we could find the AZ immediately from the name without
142+
// querying the API. But it turns out we don't actually need it for
143+
// multi-AZ clusters, as we put the AZ into the labels on the PV instead.
144+
// However, if in future we want to support multi-AZ cluster
145+
// volume-awareness without using PersistentVolumes, we likely will
146+
// want the AZ in the host.
147+
if !strings.HasPrefix(kubernetesID, "aws://") {
148+
// Assume a bare aws volume id (vol-1234...)
149+
return kubernetesID, nil
150+
}
151+
url, err := url.Parse(kubernetesID)
152+
if err != nil {
153+
// TODO: Maybe we should pass a URL into the Volume functions
154+
return "", fmt.Errorf("Invalid disk name (%s): %v", kubernetesID, err)
155+
}
156+
if url.Scheme != "aws" {
157+
return "", fmt.Errorf("Invalid scheme for AWS volume (%s)", kubernetesID)
158+
}
159+
160+
awsID := url.Path
161+
awsID = strings.Trim(awsID, "/")
162+
163+
// We sanity check the resulting volume; the two known formats are
164+
// vol-12345678 and vol-12345678abcdef01
165+
if !awsVolumeRegMatch.MatchString(awsID) {
166+
return "", fmt.Errorf("Invalid format for AWS volume (%s)", kubernetesID)
167+
}
168+
169+
return awsID, nil
170+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package plugins
18+
19+
import (
20+
"testing"
21+
)
22+
23+
func TestKubernetesVolumeIDToEBSVolumeID(t *testing.T) {
24+
testCases := []struct {
25+
name string
26+
kubernetesID string
27+
ebsVolumeID string
28+
expErr bool
29+
}{
30+
{
31+
name: "Normal ID format",
32+
kubernetesID: "vol-02399794d890f9375",
33+
ebsVolumeID: "vol-02399794d890f9375",
34+
},
35+
{
36+
name: "aws:///{volumeId} format",
37+
kubernetesID: "aws:///vol-02399794d890f9375",
38+
ebsVolumeID: "vol-02399794d890f9375",
39+
},
40+
{
41+
name: "aws://{zone}/{volumeId} format",
42+
kubernetesID: "aws://us-west-2a/vol-02399794d890f9375",
43+
ebsVolumeID: "vol-02399794d890f9375",
44+
},
45+
{
46+
name: "fails on invalid volume ID",
47+
kubernetesID: "aws://us-west-2a/02399794d890f9375",
48+
expErr: true,
49+
},
50+
}
51+
52+
for _, tc := range testCases {
53+
t.Run(tc.name, func(t *testing.T) {
54+
actual, err := KubernetesVolumeIDToEBSVolumeID(tc.kubernetesID)
55+
if err != nil {
56+
if !tc.expErr {
57+
t.Errorf("KubernetesVolumeIDToEBSVolumeID failed %v", err)
58+
}
59+
} else {
60+
if actual != tc.ebsVolumeID {
61+
t.Errorf("Wrong EBS Volume ID. actual: %s expected: %s", actual, tc.ebsVolumeID)
62+
}
63+
}
64+
})
65+
}
66+
}

0 commit comments

Comments
 (0)