Copyright(C) 2026. Huawei Technologies Co.,Ltd. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"k8s.io/api/core/v1"
)
func CalcMinResources(replicas int32, podSpec v1.PodSpec) *v1.ResourceList {
if replicas <= 0 {
return nil
}
singlePodRes := v1.ResourceList{}
for _, container := range podSpec.Containers {
AddResourceList(singlePodRes, container.Resources.Requests, container.Resources.Limits)
}
if len(singlePodRes) == 0 {
return nil
}
minResources := v1.ResourceList{}
for name, quantity := range singlePodRes {
total := quantity
for i := int32(1); i < replicas; i++ {
total.Add(quantity)
}
minResources[name] = total
}
return &minResources
}
func AddResourceList(list, req, limit v1.ResourceList) {
for name, quantity := range req {
if value, ok := list[name]; ok {
value.Add(quantity)
list[name] = value
} else {
list[name] = quantity
}
}
for name, quantity := range limit {
if _, ok := req[name]; ok {
continue
}
if value, ok := list[name]; ok {
value.Add(quantity)
list[name] = value
} else {
list[name] = quantity
}
}
}