Skip to content

Commit

Permalink
avoid buffer-size larger than memory limit
Browse files Browse the repository at this point in the history
Signed-off-by: zwwhdls <[email protected]>
  • Loading branch information
zwwhdls committed Sep 7, 2023
1 parent 864d9db commit 1ae4be3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
8 changes: 4 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ const (
InjectSidecarDisable = "disable" + injectSidecar

// config in pv
mountPodCpuLimitKey = "juicefs/mount-cpu-limit"
mountPodMemLimitKey = "juicefs/mount-memory-limit"
mountPodCpuRequestKey = "juicefs/mount-cpu-request"
mountPodMemRequestKey = "juicefs/mount-memory-request"
MountPodCpuLimitKey = "juicefs/mount-cpu-limit"
MountPodMemLimitKey = "juicefs/mount-memory-limit"
MountPodCpuRequestKey = "juicefs/mount-cpu-request"
MountPodMemRequestKey = "juicefs/mount-memory-request"
mountPodLabelKey = "juicefs/mount-labels"
mountPodAnnotationKey = "juicefs/mount-annotations"
mountPodServiceAccount = "juicefs/mount-service-account"
Expand Down
8 changes: 4 additions & 4 deletions pkg/config/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ func ParseSetting(secrets, volCtx map[string]string, options []string, usePod bo
jfsSetting.SubPath = volCtx["subPath"]
}

cpuLimit := volCtx[mountPodCpuLimitKey]
memoryLimit := volCtx[mountPodMemLimitKey]
cpuRequest := volCtx[mountPodCpuRequestKey]
memoryRequest := volCtx[mountPodMemRequestKey]
cpuLimit := volCtx[MountPodCpuLimitKey]
memoryLimit := volCtx[MountPodMemLimitKey]
cpuRequest := volCtx[MountPodCpuRequestKey]
memoryRequest := volCtx[MountPodMemRequestKey]
jfsSetting.Resources, err = parsePodResources(cpuLimit, memoryLimit, cpuRequest, memoryRequest)
if err != nil {
klog.Errorf("Parse resource error: %v", err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/config/setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func TestParseSecret(t *testing.T) {
name: "test-cpu-limit",
args: args{
secrets: map[string]string{"name": "test", "storage": "s3"},
volCtx: map[string]string{mountPodCpuLimitKey: "1"},
volCtx: map[string]string{MountPodCpuLimitKey: "1"},
usePod: true,
},
want: &JfsSetting{
Expand Down Expand Up @@ -199,7 +199,7 @@ func TestParseSecret(t *testing.T) {
name: "test-mem-limit",
args: args{
secrets: map[string]string{"name": "test", "storage": "s3"},
volCtx: map[string]string{mountPodMemLimitKey: "1G"},
volCtx: map[string]string{MountPodMemLimitKey: "1G"},
usePod: true,
},
want: &JfsSetting{
Expand Down Expand Up @@ -232,7 +232,7 @@ func TestParseSecret(t *testing.T) {
name: "test-mem-request",
args: args{
secrets: map[string]string{"name": "test", "storage": "s3"},
volCtx: map[string]string{mountPodMemRequestKey: "1G"},
volCtx: map[string]string{MountPodMemRequestKey: "1G"},
usePod: true,
},
want: &JfsSetting{
Expand Down Expand Up @@ -265,7 +265,7 @@ func TestParseSecret(t *testing.T) {
name: "test-cpu-request",
args: args{
secrets: map[string]string{"name": "test"},
volCtx: map[string]string{mountPodCpuRequestKey: "1"},
volCtx: map[string]string{MountPodCpuRequestKey: "1"},
},
want: &JfsSetting{
Name: "test",
Expand Down
22 changes: 20 additions & 2 deletions pkg/juicefs/juicefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"google.golang.org/grpc/status"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/klog"
Expand Down Expand Up @@ -336,7 +337,7 @@ func (j *juicefs) JfsMount(ctx context.Context, volumeID string, target string,

// Settings get all jfs settings and generate format/auth command
func (j *juicefs) Settings(ctx context.Context, volumeID string, secrets, volCtx map[string]string, options []string) (*config.JfsSetting, error) {
mountOptions, err := j.validOptions(volumeID, options)
mountOptions, err := j.validOptions(volumeID, options, volCtx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -491,7 +492,7 @@ func (j *juicefs) validTarget(target string) error {
return nil
}

func (j *juicefs) validOptions(volumeId string, options []string) ([]string, error) {
func (j *juicefs) validOptions(volumeId string, options []string, volCtx map[string]string) ([]string, error) {
mountOptions := []string{}
for _, option := range options {
mountOption := strings.TrimSpace(option)
Expand All @@ -505,6 +506,23 @@ func (j *juicefs) validOptions(volumeId string, options []string) ([]string, err
if mountOption == "writeback" {
klog.Warningf("writeback is not suitable in CSI, please do not use it. volumeId: %s", volumeId)
}
if len(ops) == 2 && ops[0] == "buffer-size" && volCtx != nil {
if volCtx[config.MountPodMemLimitKey] != "" {
memLimit, err := resource.ParseQuantity(volCtx[config.MountPodMemLimitKey])
memLimitByte := memLimit.Value()

// buffer-size is in MiB, turn to byte
bufSize, err := strconv.Atoi(ops[1])
if err != nil {
return []string{}, fmt.Errorf("invalid mount option: %s", mountOption)
}
bufferSize := int64(bufSize) << 20

if bufferSize > memLimitByte {
return []string{}, fmt.Errorf("buffer-size %s MiB is greater than pod memory limit %s", ops[1], memLimit.String())
}
}
}
mountOptions = append(mountOptions, mountOption)
}
return mountOptions, nil
Expand Down
15 changes: 14 additions & 1 deletion pkg/juicefs/juicefs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,7 @@ func Test_juicefs_validOptions(t *testing.T) {
type args struct {
volumeId string
options []string
volCtx map[string]string
}
tests := []struct {
name string
Expand Down Expand Up @@ -1252,11 +1253,23 @@ func Test_juicefs_validOptions(t *testing.T) {
want: []string{},
wantErr: true,
},
{
name: "test-buffersize",
args: args{
volumeId: "test",
options: []string{"buffer-size=1024"},
volCtx: map[string]string{
config.MountPodMemLimitKey: "1Mi",
},
},
want: []string{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
j := &juicefs{}
got, err := j.validOptions(tt.args.volumeId, tt.args.options)
got, err := j.validOptions(tt.args.volumeId, tt.args.options, tt.args.volCtx)
if (err != nil) != tt.wantErr {
t.Errorf("validOptions() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down

0 comments on commit 1ae4be3

Please sign in to comment.