Skip to content

Commit 5704b09

Browse files
committed
chore: fix GenerateVolumeName
fix gofmt fix go
1 parent 9f1bbdb commit 5704b09

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ replace (
196196
k8s.io/component-helpers => k8s.io/component-helpers v0.31.12
197197
k8s.io/controller-manager => k8s.io/controller-manager v0.31.12
198198
k8s.io/cri-api => k8s.io/cri-api v0.31.12
199+
k8s.io/cri-client => k8s.io/cri-client v0.31.12
199200
k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.31.12
200201
k8s.io/dynamic-resource-allocation => k8s.io/dynamic-resource-allocation v0.31.12
201202
k8s.io/endpointslice => k8s.io/endpointslice v0.31.12

pkg/azurefile/azurefile.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import (
4646
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4747
"k8s.io/apimachinery/pkg/util/wait"
4848
"k8s.io/klog/v2"
49-
"k8s.io/kubernetes/pkg/volume/util"
5049
mount "k8s.io/mount-utils"
5150

5251
csicommon "sigs.k8s.io/azurefile-csi-driver/pkg/csi-common"
@@ -657,7 +656,7 @@ func getValidFileShareName(volumeName string) string {
657656
fileShareName = fileShareName[0:fileShareNameMaxLength]
658657
}
659658
if !checkShareNameBeginAndEnd(fileShareName) || len(fileShareName) < fileShareNameMinLength {
660-
fileShareName = util.GenerateVolumeName("pvc-file", uuid.NewString(), fileShareNameMaxLength)
659+
fileShareName = fileutil.GenerateVolumeName("pvc-file", uuid.NewString(), fileShareNameMaxLength)
661660
klog.Warningf("the requested volume name (%q) is invalid, so it is regenerated as (%q)", volumeName, fileShareName)
662661
}
663662
fileShareName = strings.Replace(fileShareName, "--", "-", -1)

pkg/util/util.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,19 @@ func WaitUntilTimeout(timeout time.Duration, execFunc ExecFunc, timeoutFunc Time
234234
return timeoutFunc()
235235
}
236236
}
237+
238+
// GenerateVolumeName returns a PV name with clusterName prefix. The function
239+
// should be used to generate a name of GCE PD or Cinder volume. It basically
240+
// adds "<clusterName>-dynamic-" before the PV name, making sure the resulting
241+
// string fits given length and cuts "dynamic" if not.
242+
func GenerateVolumeName(clusterName, pvName string, maxLength int) string {
243+
prefix := clusterName + "-dynamic"
244+
pvLen := len(pvName)
245+
246+
// cut the "<clusterName>-dynamic" to fit full pvName into maxLength
247+
// +1 for the '-' dash
248+
if pvLen+1+len(prefix) > maxLength {
249+
prefix = prefix[:maxLength-pvLen-1]
250+
}
251+
return prefix + "-" + pvName
252+
}

pkg/util/util_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package util
1919
import (
2020
"fmt"
2121
"reflect"
22+
"strings"
2223
"testing"
2324
"time"
2425

@@ -404,3 +405,24 @@ func TestWaitUntilTimeout(t *testing.T) {
404405
}
405406
}
406407
}
408+
409+
func TestGenerateVolumeName(t *testing.T) {
410+
// Normal operation, no truncate
411+
v1 := GenerateVolumeName("kubernetes", "pv-cinder-abcde", 255)
412+
if v1 != "kubernetes-dynamic-pv-cinder-abcde" {
413+
t.Errorf("Expected kubernetes-dynamic-pv-cinder-abcde, got %s", v1)
414+
}
415+
// Truncate trailing "6789-dynamic"
416+
prefix := strings.Repeat("0123456789", 9) // 90 characters prefix + 8 chars. of "-dynamic"
417+
v2 := GenerateVolumeName(prefix, "pv-cinder-abcde", 100)
418+
expect := prefix[:84] + "-pv-cinder-abcde"
419+
if v2 != expect {
420+
t.Errorf("Expected %s, got %s", expect, v2)
421+
}
422+
// Truncate really long cluster name
423+
prefix = strings.Repeat("0123456789", 1000) // 10000 characters prefix
424+
v3 := GenerateVolumeName(prefix, "pv-cinder-abcde", 100)
425+
if v3 != expect {
426+
t.Errorf("Expected %s, got %s", expect, v3)
427+
}
428+
}

vendor/modules.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,6 +1867,7 @@ sigs.k8s.io/yaml/goyaml.v2
18671867
# k8s.io/component-helpers => k8s.io/component-helpers v0.31.12
18681868
# k8s.io/controller-manager => k8s.io/controller-manager v0.31.12
18691869
# k8s.io/cri-api => k8s.io/cri-api v0.31.12
1870+
# k8s.io/cri-client => k8s.io/cri-client v0.31.12
18701871
# k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.31.12
18711872
# k8s.io/dynamic-resource-allocation => k8s.io/dynamic-resource-allocation v0.31.12
18721873
# k8s.io/endpointslice => k8s.io/endpointslice v0.31.12

0 commit comments

Comments
 (0)