Skip to content

Commit

Permalink
refine(util): refine getting reserved
Browse files Browse the repository at this point in the history
  • Loading branch information
csfldf committed May 24, 2024
1 parent 6685716 commit 5e1814b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
8 changes: 2 additions & 6 deletions pkg/agent/qrm-plugins/cpu/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,11 @@ func GetCoresReservedForSystem(conf *config.Configuration, metaServer *metaserve
reservedQuantity, found, err := utilkubeconfig.GetReservedQuantity(klConfig, string(v1.ResourceCPU))
if err != nil {
return machine.NewCPUSet(), fmt.Errorf("GetKubeletReservedQuantity failed with error: %v", err)
} else if found {
} else {
reservedQuantityFloat := float64(reservedQuantity.MilliValue()) / 1000
reservedQuantityInt = int(math.Ceil(reservedQuantityFloat))

general.Infof("get reservedQuantityInt: %d from kubelet config", reservedQuantityInt)
} else if !found {
reservedQuantityInt = conf.ReservedCPUCores
general.Infof("reserved cpu config isn't found in kubelet config, fallback to get reservedQuantityInt: %d from ReservedCPUCores configuration",
reservedQuantityInt)
general.Infof("get reservedQuantityInt: %d from kubelet config, found: %v", reservedQuantityInt, found)
}
} else {
reservedQuantityInt = conf.ReservedCPUCores
Expand Down
8 changes: 2 additions & 6 deletions pkg/agent/qrm-plugins/memory/dynamicpolicy/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,10 @@ func getReservedMemory(conf *config.Configuration, metaServer *metaserver.MetaSe
reservedQuantity, found, err := utilkubeconfig.GetReservedQuantity(klConfig, string(v1.ResourceMemory))
if err != nil {
return nil, fmt.Errorf("GetKubeletReservedQuantity failed with error: %v", err)
} else if found {
} else {
unitGB := resource.MustParse("1Gi")
reservedMemoryGB = float64(reservedQuantity.Value()) / float64(unitGB.Value())
general.Infof("get reservedMemoryGB: %.2f from kubelet config", reservedMemoryGB)
} else if !found {
reservedMemoryGB = float64(conf.ReservedMemoryGB)
general.Infof("reserved memory config isn't found in kubelet config, "+
"fallback to get reservedMemoryGB: %.2f from ReservedMemoryGB configuration", reservedMemoryGB)
general.Infof("get reservedMemoryGB: %.2f from kubelet config, found: %v", reservedMemoryGB, found)
}
} else {
reservedMemoryGB = float64(conf.ReservedMemoryGB)
Expand Down
8 changes: 4 additions & 4 deletions pkg/util/kubelet/config/kubeletconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ func GetReservedQuantity(kubeletConfig *kubeletconfigv1beta1.KubeletConfiguratio
found := false
reservedQuantity := resource.NewQuantity(0, resource.DecimalSI)

if kubeletConfig.KubeReserved != nil {
kubeReserved, err := resource.ParseQuantity(kubeletConfig.KubeReserved[resourceName])
if kubeReservedStr, ok := kubeletConfig.KubeReserved[resourceName]; ok {
kubeReserved, err := resource.ParseQuantity(kubeReservedStr)
if err != nil {
return resource.MustParse("0"), false,
fmt.Errorf("failed because parse cpu quantity for kube-reserved failed with error: %v", err)
}
reservedQuantity.Add(kubeReserved)
found = true
}
if kubeletConfig.SystemReserved != nil {
systemReserved, err := resource.ParseQuantity(kubeletConfig.SystemReserved[resourceName])
if systemReservedStr, ok := kubeletConfig.SystemReserved[resourceName]; ok {
systemReserved, err := resource.ParseQuantity(systemReservedStr)
if err != nil {
return resource.MustParse("0"), false,
fmt.Errorf("parse cpu quantity for system-reserved failed with error: %v", err)
Expand Down

0 comments on commit 5e1814b

Please sign in to comment.