Skip to content

Commit

Permalink
parse default resource for buffer-size
Browse files Browse the repository at this point in the history
  • Loading branch information
zwwhdls committed Sep 18, 2023
1 parent 1ae4be3 commit c446e26
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
8 changes: 4 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ const (
DeleteDelayAtKey = "juicefs-delete-at"

// default value
defaultMountPodCpuLimit = "2000m"
defaultMountPodMemLimit = "5Gi"
defaultMountPodCpuRequest = "1000m"
defaultMountPodMemRequest = "1Gi"
DefaultMountPodCpuLimit = "2000m"
DefaultMountPodMemLimit = "5Gi"
DefaultMountPodCpuRequest = "1000m"
DefaultMountPodMemRequest = "1Gi"
)

var PodLocks [1024]sync.Mutex
Expand Down
20 changes: 10 additions & 10 deletions pkg/config/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func ParseSetting(secrets, volCtx map[string]string, options []string, usePod bo
memoryLimit := volCtx[MountPodMemLimitKey]
cpuRequest := volCtx[MountPodCpuRequestKey]
memoryRequest := volCtx[MountPodMemRequestKey]
jfsSetting.Resources, err = parsePodResources(cpuLimit, memoryLimit, cpuRequest, memoryRequest)
jfsSetting.Resources, err = ParsePodResources(cpuLimit, memoryLimit, cpuRequest, memoryRequest)
if err != nil {
klog.Errorf("Parse resource error: %v", err)
return nil, err
Expand Down Expand Up @@ -432,14 +432,14 @@ func parseYamlOrJson(source string, dst interface{}) error {
return nil
}

func parsePodResources(cpuLimit, memoryLimit, cpuRequest, memoryRequest string) (corev1.ResourceRequirements, error) {
func ParsePodResources(cpuLimit, memoryLimit, cpuRequest, memoryRequest string) (corev1.ResourceRequirements, error) {
podLimit := map[corev1.ResourceName]resource.Quantity{}
podRequest := map[corev1.ResourceName]resource.Quantity{}
// set default value
podLimit[corev1.ResourceCPU] = resource.MustParse(defaultMountPodCpuLimit)
podLimit[corev1.ResourceMemory] = resource.MustParse(defaultMountPodMemLimit)
podRequest[corev1.ResourceCPU] = resource.MustParse(defaultMountPodCpuRequest)
podRequest[corev1.ResourceMemory] = resource.MustParse(defaultMountPodMemRequest)
podLimit[corev1.ResourceCPU] = resource.MustParse(DefaultMountPodCpuLimit)
podLimit[corev1.ResourceMemory] = resource.MustParse(DefaultMountPodMemLimit)
podRequest[corev1.ResourceCPU] = resource.MustParse(DefaultMountPodCpuRequest)
podRequest[corev1.ResourceMemory] = resource.MustParse(DefaultMountPodMemRequest)
var err error
if cpuLimit != "" {
if podLimit[corev1.ResourceCPU], err = resource.ParseQuantity(cpuLimit); err != nil {
Expand Down Expand Up @@ -470,12 +470,12 @@ func parsePodResources(cpuLimit, memoryLimit, cpuRequest, memoryRequest string)
func getDefaultResource() corev1.ResourceRequirements {
return corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse(defaultMountPodCpuLimit),
corev1.ResourceMemory: resource.MustParse(defaultMountPodMemLimit),
corev1.ResourceCPU: resource.MustParse(DefaultMountPodCpuLimit),
corev1.ResourceMemory: resource.MustParse(DefaultMountPodMemLimit),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse(defaultMountPodCpuRequest),
corev1.ResourceMemory: resource.MustParse(defaultMountPodMemRequest),
corev1.ResourceCPU: resource.MustParse(DefaultMountPodCpuRequest),
corev1.ResourceMemory: resource.MustParse(DefaultMountPodMemRequest),
},
}
}
31 changes: 18 additions & 13 deletions pkg/juicefs/juicefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,21 +506,26 @@ func (j *juicefs) validOptions(volumeId string, options []string, volCtx map[str
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()
if len(ops) == 2 && ops[0] == "buffer-size" {
rs := volCtx[config.MountPodMemLimitKey]
if rs == "" {
rs = config.DefaultMountPodMemLimit
}
memLimit, err := resource.ParseQuantity(rs)
if err != nil {
return []string{}, fmt.Errorf("invalid memory limit: %s", 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
// 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())
}
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)
Expand Down

0 comments on commit c446e26

Please sign in to comment.