Skip to content

Commit ba29e2b

Browse files
authored
Merge pull request #44 from ZeroMagic/same_capacity
fix: set container name as volume name
2 parents 306f90a + acf40a8 commit ba29e2b

File tree

3 files changed

+117
-6
lines changed

3 files changed

+117
-6
lines changed

pkg/blobfuse/blobfuse.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import (
2222

2323
"github.com/container-storage-interface/spec/lib/go/csi"
2424
csicommon "github.com/csi-driver/blobfuse-csi-driver/pkg/csi-common"
25+
"github.com/pborman/uuid"
2526

2627
"k8s.io/klog"
2728
"k8s.io/kubernetes/pkg/util/mount"
29+
k8sutil "k8s.io/kubernetes/pkg/volume/util"
2830
"k8s.io/legacy-cloud-providers/azure"
2931
)
3032

@@ -39,6 +41,10 @@ const (
3941
defaultFileMode = "0777"
4042
defaultDirMode = "0777"
4143
defaultVers = "3.0"
44+
45+
// See https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata#container-names
46+
containerNameMinLength = 3
47+
containerNameMaxLength = 63
4248
)
4349

4450
// Driver implements all interfaces of CSI drivers
@@ -181,3 +187,38 @@ func getStorageAccount(secrets map[string]string) (string, string, error) {
181187

182188
return accountName, accountKey, nil
183189
}
190+
191+
// A container name must be a valid DNS name, conforming to the following naming rules:
192+
// 1. Container names must start with a letter or number, and can contain only letters, numbers, and the dash (-) character.
193+
// 2. Every dash (-) character must be immediately preceded and followed by a letter or number; consecutive dashes are not permitted in container names.
194+
// 3. All letters in a container name must be lowercase.
195+
// 4. Container names must be from 3 through 63 characters long.
196+
//
197+
// See https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata#container-names
198+
func getValidContainerName(volumeName string) string {
199+
containerName := strings.ToLower(volumeName)
200+
if len(containerName) > containerNameMaxLength {
201+
containerName = containerName[0:containerNameMaxLength]
202+
}
203+
if !checkContainerNameBeginAndEnd(containerName) || len(containerName) < containerNameMinLength {
204+
// now we set as 63 for maximum container name length
205+
// todo: get cluster name
206+
containerName = k8sutil.GenerateVolumeName("pvc-fuse", uuid.NewUUID().String(), 63)
207+
klog.Warningf("the requested volume name (%q) is invalid, so it is regenerated as (%q)", volumeName, containerName)
208+
}
209+
containerName = strings.Replace(containerName, "--", "-", -1)
210+
211+
return containerName
212+
}
213+
214+
func checkContainerNameBeginAndEnd(containerName string) bool {
215+
length := len(containerName)
216+
if (('a' <= containerName[0] && containerName[0] <= 'z') ||
217+
('0' <= containerName[0] && containerName[0] <= '9')) &&
218+
(('a' <= containerName[length-1] && containerName[length-1] <= 'z') ||
219+
('0' <= containerName[length-1] && containerName[length-1] <= '9')) {
220+
return true
221+
}
222+
223+
return false
224+
}

pkg/blobfuse/blobfuse_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,77 @@ func TestGetStorageAccount(t *testing.T) {
214214
}
215215
}
216216
}
217+
218+
func TestGetValidContainerName(t *testing.T) {
219+
tests := []struct {
220+
volumeName string
221+
expected string
222+
}{
223+
{
224+
volumeName: "aqz",
225+
expected: "aqz",
226+
},
227+
{
228+
volumeName: "029",
229+
expected: "029",
230+
},
231+
{
232+
volumeName: "a--z",
233+
expected: "a-z",
234+
},
235+
{
236+
volumeName: "A2Z",
237+
expected: "a2z",
238+
},
239+
{
240+
volumeName: "1234567891234567891234567891234567891234567891234567891234567891",
241+
expected: "123456789123456789123456789123456789123456789123456789123456789",
242+
},
243+
}
244+
245+
for _, test := range tests {
246+
result := getValidContainerName(test.volumeName)
247+
if !reflect.DeepEqual(result, test.expected) {
248+
t.Errorf("input: %q, getValidContainerName result: %q, expected: %q", test.volumeName, result, test.expected)
249+
}
250+
}
251+
}
252+
253+
func TestCheckContainerNameBeginAndEnd(t *testing.T) {
254+
tests := []struct {
255+
containerName string
256+
expected bool
257+
}{
258+
{
259+
containerName: "aqz",
260+
expected: true,
261+
},
262+
{
263+
containerName: "029",
264+
expected: true,
265+
},
266+
{
267+
containerName: "a-9",
268+
expected: true,
269+
},
270+
{
271+
containerName: "0-z",
272+
expected: true,
273+
},
274+
{
275+
containerName: "-1-",
276+
expected: false,
277+
},
278+
{
279+
containerName: ":1p",
280+
expected: false,
281+
},
282+
}
283+
284+
for _, test := range tests {
285+
result := checkContainerNameBeginAndEnd(test.containerName)
286+
if !reflect.DeepEqual(result, test.expected) {
287+
t.Errorf("input: %q, checkContainerNameBeginAndEnd result: %v, expected: %v", test.containerName, result, test.expected)
288+
}
289+
}
290+
}

pkg/blobfuse/controllerserver.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ import (
2121
"fmt"
2222
"strings"
2323

24-
k8sutil "k8s.io/kubernetes/pkg/volume/util"
25-
2624
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2018-07-01/storage"
2725
azstorage "github.com/Azure/azure-sdk-for-go/storage"
2826
"github.com/container-storage-interface/spec/lib/go/csi"
2927
"github.com/csi-driver/blobfuse-csi-driver/pkg/util"
30-
"github.com/pborman/uuid"
28+
3129
"k8s.io/klog"
3230

3331
"google.golang.org/grpc/codes"
@@ -90,9 +88,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
9088
accountName = account
9189

9290
if containerName == "" {
93-
// now we set as 63 for maximum container name length
94-
// todo: get cluster name
95-
containerName = k8sutil.GenerateVolumeName("pvc-fuse", uuid.NewUUID().String(), 63)
91+
containerName = getValidContainerName(name)
9692
}
9793

9894
klog.V(2).Infof("begin to create container(%s) on account(%s) type(%s) rg(%s) location(%s) size(%d)", containerName, accountName, storageAccountType, resourceGroup, location, requestGiB)

0 commit comments

Comments
 (0)