Repository: kubernetes/kubernetes
File: pkg/kubelet/apis/podresources/server_v1.go (Go)

Code before:
}

// List returns information about the resources assigned to pods on the node
func (p *v1PodResourcesServer) List(ctx context.Context, req *podresourcesv1.ListPodResourcesRequest) (*podresourcesv1.ListPodResourcesResponse, error) {
	metrics.PodResourcesEndpointRequestsTotalCount.WithLabelValues("v1").Inc()
	metrics.PodResourcesEndpointRequestsListCount.WithLabelValues("v1").Inc()

	var pods []*v1.Pod
	if p.useActivePods {
		// GetActivePods already filters out terminal pods, so no need for additional filtering.
		pods = p.podsProvider.GetActivePods()
	} else {
		// GetPods may include terminal pods, so we filter them out ourselves.
		allPods := p.podsProvider.GetPods()
		pods = make([]*v1.Pod, 0, len(allPods))
		for _, pod := range allPods {
			// Skip terminal pods (Failed or Succeeded).
			// Terminal pods should not appear in podresources as they no longer consume resources.
			if pod.Status.Phase == v1.PodFailed || pod.Status.Phase == v1.PodSucceeded {
				continue
			}
			pods = append(pods, pod)
		}
	}

	podResources := make([]*podresourcesv1.PodResources, 0, len(pods))
	p.devicesProvider.UpdateAllocatedDevices()

	for _, pod := range pods {
		pRes := podresourcesv1.PodResources{
			Name:       pod.Name,
			Namespace:  pod.Namespace,
			Containers: make([]*podresourcesv1.ContainerResources, 0, len(pod.Spec.Containers)),
		}

		pRes.Containers = make([]*podresourcesv1.ContainerResources, 0, len(pod.Spec.InitContainers)+len(pod.Spec.Containers))
		for _, container := range pod.Spec.InitContainers {
			if !podutil.IsRestartableInitContainer(&container) {
				continue
			}

			pRes.Containers = append(pRes.Containers, p.getContainerResources(pod, &container))
		}

		for _, container := range pod.Spec.Containers {
			pRes.Containers = append(pRes.Containers, p.getContainerResources(pod, &container))
		}
		podResources = append(podResources, &pRes)
	}

	response := &podresourcesv1.ListPodResourcesResponse{

Review comment (suggestion):
This (and below) change seems not needed anymore as `podResources` size is always equal to `pods` size.

Code after:
}

// List returns information about the resources assigned to pods on the node
func (p *v1PodResourcesServer) List(ctx context.Context, req *podresourcesv1.ListPodResourcesRequest) (*podresourcesv1.ListPodResourcesResponse, error) {
	metrics.PodResourcesEndpointRequestsTotalCount.WithLabelValues("v1").Inc()
	metrics.PodResourcesEndpointRequestsListCount.WithLabelValues("v1").Inc()

	var pods []*v1.Pod
	if p.useActivePods {
		// GetActivePods already filters out terminal pods, so no need for additional filtering.
		pods = p.podsProvider.GetActivePods()
	} else {
		// GetPods may include terminal pods, so we filter them out ourselves.
		allPods := p.podsProvider.GetPods()
		pods = make([]*v1.Pod, 0, len(allPods))
		for _, pod := range allPods {
			// Skip terminal pods (Failed or Succeeded).
			// Terminal pods should not appear in podresources as they no longer consume resources.
			if podutil.IsPodTerminal(pod) {
				continue
			}
			pods = append(pods, pod)
		}
	}

	podResources := make([]*podresourcesv1.PodResources, len(pods))
	p.devicesProvider.UpdateAllocatedDevices()

	for i, pod := range pods {
		pRes := podresourcesv1.PodResources{
			Name:       pod.Name,
			Namespace:  pod.Namespace,
			Containers: make([]*podresourcesv1.ContainerResources, 0, len(pod.Spec.Containers)),
		}

		pRes.Containers = make([]*podresourcesv1.ContainerResources, 0, len(pod.Spec.InitContainers)+len(pod.Spec.Containers))
		for _, container := range pod.Spec.InitContainers {
			if !podutil.IsRestartableInitContainer(&container) {
				continue
			}

			pRes.Containers = append(pRes.Containers, p.getContainerResources(pod, &container))
		}

		for _, container := range pod.Spec.Containers {
			pRes.Containers = append(pRes.Containers, p.getContainerResources(pod, &container))
		}
		podResources[i] = &pRes
	}

	response := &podresourcesv1.ListPodResourcesResponse{