Skip to content

Commit 07a5d14

Browse files
committed
fix: huge storage account list usage
fix golint fix mount point
1 parent c47d66f commit 07a5d14

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

pkg/blob/blob.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,3 +604,40 @@ func (d *Driver) getSubnetResourceID() string {
604604

605605
return fmt.Sprintf(subnetTemplate, subsID, rg, d.cloud.VnetName, d.cloud.SubnetName)
606606
}
607+
608+
// appendDefaultMountOptions return mount options combined with mountOptions and defaultMountOptions
609+
func appendDefaultMountOptions(mountOptions []string, tmpPath, containerName string) []string {
610+
var defaultMountOptions = map[string]string{
611+
"--pre-mount-validate": "true",
612+
"--use-https": "true",
613+
"--tmp-path": tmpPath,
614+
"--container-name": containerName,
615+
// prevent billing charges on mounting
616+
"--cancel-list-on-mount-seconds": "60",
617+
}
618+
619+
// stores the mount options already included in mountOptions
620+
included := make(map[string]bool)
621+
622+
for _, mountOption := range mountOptions {
623+
for k := range defaultMountOptions {
624+
if strings.HasPrefix(mountOption, k) {
625+
included[k] = true
626+
}
627+
}
628+
}
629+
630+
allMountOptions := mountOptions
631+
632+
for k, v := range defaultMountOptions {
633+
if _, isIncluded := included[k]; !isIncluded {
634+
if v != "" {
635+
allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%s", k, v))
636+
} else {
637+
allMountOptions = append(allMountOptions, k)
638+
}
639+
}
640+
}
641+
642+
return allMountOptions
643+
}

pkg/blob/blob_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io/ioutil"
2424
"os"
2525
"reflect"
26+
"sort"
2627
"testing"
2728

2829
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-02-01/storage"
@@ -773,3 +774,47 @@ func TestSetAzureCredentials(t *testing.T) {
773774
}
774775
}
775776
}
777+
778+
func TestAppendDefaultMountOptions(t *testing.T) {
779+
tests := []struct {
780+
options []string
781+
tmpPath string
782+
containerName string
783+
expected []string
784+
}{
785+
{
786+
options: []string{"targetPath"},
787+
tmpPath: "/tmp",
788+
containerName: "containerName",
789+
expected: []string{"--cancel-list-on-mount-seconds=60",
790+
"--container-name=containerName",
791+
"--pre-mount-validate=true",
792+
"--tmp-path=/tmp",
793+
"--use-https=true",
794+
"targetPath",
795+
},
796+
},
797+
{
798+
options: []string{"targetPath", "--cancel-list-on-mount-seconds=0", "--pre-mount-validate=false"},
799+
tmpPath: "/tmp",
800+
containerName: "containerName",
801+
expected: []string{"--cancel-list-on-mount-seconds=0",
802+
"--container-name=containerName",
803+
"--pre-mount-validate=false",
804+
"--tmp-path=/tmp",
805+
"--use-https=true",
806+
"targetPath",
807+
},
808+
},
809+
}
810+
811+
for _, test := range tests {
812+
result := appendDefaultMountOptions(test.options, test.tmpPath, test.containerName)
813+
sort.Strings(result)
814+
sort.Strings(test.expected)
815+
816+
if !reflect.DeepEqual(result, test.expected) {
817+
t.Errorf("input: %q, appendDefaultMountOptions result: %q, expected: %q", test.options, result, test.expected)
818+
}
819+
}
820+
}

pkg/blob/nodeserver.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,15 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
267267
}
268268

269269
// Get mountOptions that the volume will be formatted and mounted with
270-
mountOptions := util.JoinMountOptions(mountFlags, []string{"--use-https=true"})
270+
mountOptions := mountFlags
271271
if ephemeralVol {
272272
mountOptions = util.JoinMountOptions(mountOptions, strings.Split(ephemeralVolMountOptions, ","))
273273
}
274-
275274
// set different tmp-path with time info
276275
tmpPath := fmt.Sprintf("%s/%s#%d", "/mnt", volumeID, time.Now().Unix())
277-
args := fmt.Sprintf("%s --pre-mount-validate=true --tmp-path=%s --container-name=%s", targetPath, tmpPath, containerName)
276+
mountOptions = appendDefaultMountOptions(mountOptions, tmpPath, containerName)
277+
278+
args := targetPath
278279
for _, opt := range mountOptions {
279280
args = args + " " + opt
280281
}

0 commit comments

Comments
 (0)