mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-06 02:34:03 +00:00
api, kubectl: move getSinglePodTotalRequestsAndLimits to api.PodRequestsAndLimits
This commit is contained in:
parent
2d4757a8df
commit
1a43dcf720
@ -97,3 +97,26 @@ func IsNodeReady(node *Node) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// PodRequestsAndLimits returns a dictionary of all defined resources summed up for all
|
||||
// containers of the pod.
|
||||
func PodRequestsAndLimits(pod *Pod) (reqs map[ResourceName]resource.Quantity, limits map[ResourceName]resource.Quantity, err error) {
|
||||
reqs, limits = map[ResourceName]resource.Quantity{}, map[ResourceName]resource.Quantity{}
|
||||
for _, container := range pod.Spec.Containers {
|
||||
for name, quantity := range container.Resources.Requests {
|
||||
if value, ok := reqs[name]; !ok {
|
||||
reqs[name] = *quantity.Copy()
|
||||
} else if err = value.Add(quantity); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
for name, quantity := range container.Resources.Limits {
|
||||
if value, ok := limits[name]; !ok {
|
||||
limits[name] = *quantity.Copy()
|
||||
} else if err = value.Add(quantity); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
@ -1415,7 +1415,7 @@ func describeNodeResource(pods []*api.Pod, node *api.Node, out io.Writer) error
|
||||
fmt.Fprint(out, " Namespace\tName\t\tCPU Requests\tCPU Limits\tMemory Requests\tMemory Limits\n")
|
||||
fmt.Fprint(out, " ─────────\t────\t\t────────────\t──────────\t───────────────\t─────────────\n")
|
||||
for _, pod := range nonTerminatedPods {
|
||||
req, limit, err := getSinglePodTotalRequestsAndLimits(pod)
|
||||
req, limit, err := api.PodRequestsAndLimits(pod)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -1463,7 +1463,7 @@ func filterTerminatedPods(pods []*api.Pod) []*api.Pod {
|
||||
func getPodsTotalRequestsAndLimits(pods []*api.Pod) (reqs map[api.ResourceName]resource.Quantity, limits map[api.ResourceName]resource.Quantity, err error) {
|
||||
reqs, limits = map[api.ResourceName]resource.Quantity{}, map[api.ResourceName]resource.Quantity{}
|
||||
for _, pod := range pods {
|
||||
podReqs, podLimits, err := getSinglePodTotalRequestsAndLimits(pod)
|
||||
podReqs, podLimits, err := api.PodRequestsAndLimits(pod)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@ -1485,27 +1485,6 @@ func getPodsTotalRequestsAndLimits(pods []*api.Pod) (reqs map[api.ResourceName]r
|
||||
return
|
||||
}
|
||||
|
||||
func getSinglePodTotalRequestsAndLimits(pod *api.Pod) (reqs map[api.ResourceName]resource.Quantity, limits map[api.ResourceName]resource.Quantity, err error) {
|
||||
reqs, limits = map[api.ResourceName]resource.Quantity{}, map[api.ResourceName]resource.Quantity{}
|
||||
for _, container := range pod.Spec.Containers {
|
||||
for name, quantity := range container.Resources.Requests {
|
||||
if value, ok := reqs[name]; !ok {
|
||||
reqs[name] = *quantity.Copy()
|
||||
} else if err = value.Add(quantity); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
for name, quantity := range container.Resources.Limits {
|
||||
if value, ok := limits[name]; !ok {
|
||||
limits[name] = *quantity.Copy()
|
||||
} else if err = value.Add(quantity); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func DescribeEvents(el *api.EventList, w io.Writer) {
|
||||
if len(el.Items) == 0 {
|
||||
fmt.Fprint(w, "No events.")
|
||||
|
Loading…
Reference in New Issue
Block a user