Skip to content

Commit 7825554

Browse files
authored
add pod allocated resources (#9018)
1 parent 26f0f25 commit 7825554

File tree

5 files changed

+203
-110
lines changed

5 files changed

+203
-110
lines changed

modules/api/pkg/resource/node/detail.go

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ func GetNodeDetail(client k8sClient.Interface, metricClient metricapi.MetricClie
172172
func getNodeAllocatedResources(node v1.Node, podList *v1.PodList) (NodeAllocatedResources, error) {
173173
reqs, limits := map[v1.ResourceName]resource.Quantity{}, map[v1.ResourceName]resource.Quantity{}
174174

175-
for _, pod := range podList.Items {
176-
podReqs, podLimits, err := PodRequestsAndLimits(&pod)
175+
for _, p := range podList.Items {
176+
podReqs, podLimits, err := pod.PodRequestsAndLimits(&p)
177177
if err != nil {
178178
return NodeAllocatedResources{}, err
179179
}
@@ -233,63 +233,6 @@ func getNodeAllocatedResources(node v1.Node, podList *v1.PodList) (NodeAllocated
233233
}, nil
234234
}
235235

236-
// PodRequestsAndLimits returns a dictionary of all defined resources summed up for all
237-
// containers of the pod. If pod overhead is non-nil, the pod overhead is added to the
238-
// total container resource requests and to the total container limits which have a
239-
// non-zero quantity.
240-
func PodRequestsAndLimits(pod *v1.Pod) (reqs, limits v1.ResourceList, err error) {
241-
reqs, limits = v1.ResourceList{}, v1.ResourceList{}
242-
for _, container := range pod.Spec.Containers {
243-
addResourceList(reqs, container.Resources.Requests)
244-
addResourceList(limits, container.Resources.Limits)
245-
}
246-
// init containers define the minimum of any resource
247-
for _, container := range pod.Spec.InitContainers {
248-
maxResourceList(reqs, container.Resources.Requests)
249-
maxResourceList(limits, container.Resources.Limits)
250-
}
251-
252-
// Add overhead for running a pod to the sum of requests and to non-zero limits:
253-
if pod.Spec.Overhead != nil {
254-
addResourceList(reqs, pod.Spec.Overhead)
255-
256-
for name, quantity := range pod.Spec.Overhead {
257-
if value, ok := limits[name]; ok && !value.IsZero() {
258-
value.Add(quantity)
259-
limits[name] = value
260-
}
261-
}
262-
}
263-
return
264-
}
265-
266-
// addResourceList adds the resources in newList to list
267-
func addResourceList(list, new v1.ResourceList) {
268-
for name, quantity := range new {
269-
if value, ok := list[name]; !ok {
270-
list[name] = quantity.DeepCopy()
271-
} else {
272-
value.Add(quantity)
273-
list[name] = value
274-
}
275-
}
276-
}
277-
278-
// maxResourceList sets list to the greater of list/newList for every resource
279-
// either list
280-
func maxResourceList(list, new v1.ResourceList) {
281-
for name, quantity := range new {
282-
if value, ok := list[name]; !ok {
283-
list[name] = quantity.DeepCopy()
284-
continue
285-
} else {
286-
if quantity.Cmp(value) > 0 {
287-
list[name] = quantity.DeepCopy()
288-
}
289-
}
290-
}
291-
}
292-
293236
// GetNodePods return pods list in given named node
294237
func GetNodePods(client k8sClient.Interface, metricClient metricapi.MetricClient,
295238
dsQuery *dataselect.DataSelectQuery, name string) (*pod.PodList, error) {

modules/api/pkg/resource/pod/common.go

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"fmt"
1919

2020
v1 "k8s.io/api/core/v1"
21-
2221
metricapi "k8s.io/dashboard/api/pkg/integration/metric/api"
2322
"k8s.io/dashboard/api/pkg/resource/common"
2423
"k8s.io/dashboard/api/pkg/resource/dataselect"
@@ -247,3 +246,94 @@ func getStatus(list *v1.PodList, events []v1.Event) common.ResourceStatus {
247246

248247
return info
249248
}
249+
250+
// addResourceList adds the resources in newList to list
251+
func addResourceList(list, new v1.ResourceList) {
252+
for name, quantity := range new {
253+
if value, ok := list[name]; !ok {
254+
list[name] = quantity.DeepCopy()
255+
} else {
256+
value.Add(quantity)
257+
list[name] = value
258+
}
259+
}
260+
}
261+
262+
// maxResourceList sets list to the greater of list/newList for every resource
263+
// either list
264+
func maxResourceList(list, new v1.ResourceList) {
265+
for name, quantity := range new {
266+
if value, ok := list[name]; !ok {
267+
list[name] = quantity.DeepCopy()
268+
continue
269+
} else {
270+
if quantity.Cmp(value) > 0 {
271+
list[name] = quantity.DeepCopy()
272+
}
273+
}
274+
}
275+
}
276+
277+
// PodRequestsAndLimits returns a dictionary of all defined resources summed up for all
278+
// containers of the pod. If pod overhead is non-nil, the pod overhead is added to the
279+
// total container resource requests and to the total container limits which have a
280+
// non-zero quantity.
281+
func PodRequestsAndLimits(pod *v1.Pod) (reqs, limits v1.ResourceList, err error) {
282+
reqs, limits = v1.ResourceList{}, v1.ResourceList{}
283+
for _, container := range pod.Spec.Containers {
284+
addResourceList(reqs, container.Resources.Requests)
285+
addResourceList(limits, container.Resources.Limits)
286+
}
287+
// init containers define the minimum of any resource
288+
for _, container := range pod.Spec.InitContainers {
289+
maxResourceList(reqs, container.Resources.Requests)
290+
maxResourceList(limits, container.Resources.Limits)
291+
}
292+
293+
// Add overhead for running a pod to the sum of requests and to non-zero limits:
294+
if pod.Spec.Overhead != nil {
295+
addResourceList(reqs, pod.Spec.Overhead)
296+
297+
for name, quantity := range pod.Spec.Overhead {
298+
if value, ok := limits[name]; ok && !value.IsZero() {
299+
value.Add(quantity)
300+
limits[name] = value
301+
}
302+
}
303+
}
304+
return
305+
}
306+
307+
func getPodAllocatedResources(pod *v1.Pod) (PodAllocatedResources, error) {
308+
reqs, limits, err := PodRequestsAndLimits(pod)
309+
if err != nil {
310+
return PodAllocatedResources{}, err
311+
}
312+
313+
for podReqName, podReqValue := range reqs {
314+
if value, ok := reqs[podReqName]; !ok {
315+
reqs[podReqName] = podReqValue.DeepCopy()
316+
} else {
317+
value.Add(podReqValue)
318+
reqs[podReqName] = value
319+
}
320+
}
321+
322+
for podLimitName, podLimitValue := range limits {
323+
if value, ok := limits[podLimitName]; !ok {
324+
limits[podLimitName] = podLimitValue.DeepCopy()
325+
} else {
326+
value.Add(podLimitValue)
327+
limits[podLimitName] = value
328+
}
329+
}
330+
331+
cpuRequests, cpuLimits, memoryRequests, memoryLimits := reqs[v1.ResourceCPU], limits[v1.ResourceCPU], reqs[v1.ResourceMemory], limits[v1.ResourceMemory]
332+
333+
return PodAllocatedResources{
334+
CPURequests: cpuRequests.MilliValue(),
335+
CPULimits: cpuLimits.MilliValue(),
336+
MemoryRequests: memoryRequests.Value(),
337+
MemoryLimits: memoryLimits.Value(),
338+
}, nil
339+
}

modules/api/pkg/resource/pod/list.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
v1 "k8s.io/api/core/v1"
2121
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2222
k8sClient "k8s.io/client-go/kubernetes"
23-
2423
metricapi "k8s.io/dashboard/api/pkg/integration/metric/api"
2524
"k8s.io/dashboard/api/pkg/resource/common"
2625
"k8s.io/dashboard/api/pkg/resource/dataselect"
@@ -75,6 +74,23 @@ type Pod struct {
7574
ContainerImages []string `json:"containerImages"`
7675

7776
ContainerStatuses []ContainerStatus `json:"containerStatuses"`
77+
78+
AllocatedResources PodAllocatedResources `json:"allocatedResources"`
79+
}
80+
81+
// PodAllocatedResources describes pod allocated resources.
82+
type PodAllocatedResources struct {
83+
// CPURequests is number of allocated milicores.
84+
CPURequests int64 `json:"cpuRequests"`
85+
86+
// CPULimits is defined CPU limit.
87+
CPULimits int64 `json:"cpuLimits"`
88+
89+
// MemoryRequests is a fraction of memory, that is allocated.
90+
MemoryRequests int64 `json:"memoryRequests"`
91+
92+
// MemoryLimits is defined memory limit.
93+
MemoryLimits int64 `json:"memoryLimits"`
7894
}
7995

8096
var EmptyPodList = &PodList{
@@ -156,15 +172,21 @@ func ToPodList(pods []v1.Pod, events []v1.Event, nonCriticalErrors []error, dsQu
156172
}
157173

158174
func toPod(pod *v1.Pod, metrics *MetricsByPod, warnings []common.Event) Pod {
175+
allocatedResources, err := getPodAllocatedResources(pod)
176+
if err != nil {
177+
log.Printf("Couldn't get allocated resources of %s pod: %s\n", pod.Name, err)
178+
}
179+
159180
podDetail := Pod{
160-
ObjectMeta: types.NewObjectMeta(pod.ObjectMeta),
161-
TypeMeta: types.NewTypeMeta(types.ResourceKindPod),
162-
Warnings: warnings,
163-
Status: getPodStatus(*pod),
164-
RestartCount: getRestartCount(*pod),
165-
NodeName: pod.Spec.NodeName,
166-
ContainerImages: common.GetContainerImages(&pod.Spec),
167-
ContainerStatuses: ToContainerStatuses(append(pod.Status.InitContainerStatuses, pod.Status.ContainerStatuses...)),
181+
ObjectMeta: types.NewObjectMeta(pod.ObjectMeta),
182+
TypeMeta: types.NewTypeMeta(types.ResourceKindPod),
183+
Warnings: warnings,
184+
Status: getPodStatus(*pod),
185+
RestartCount: getRestartCount(*pod),
186+
NodeName: pod.Spec.NodeName,
187+
ContainerImages: common.GetContainerImages(&pod.Spec),
188+
ContainerStatuses: ToContainerStatuses(append(pod.Status.InitContainerStatuses, pod.Status.ContainerStatuses...)),
189+
AllocatedResources: allocatedResources,
168190
}
169191

170192
if m, exists := metrics.MetricsMap[pod.UID]; exists {

0 commit comments

Comments
 (0)