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

Code before:
		memoryProvider:           providers.Memory,
		dynamicResourcesProvider: providers.DynamicResources,
		useActivePods:            useActivePods,
	}
}

// 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 {
		pods = p.podsProvider.GetActivePods()
	} else {
		pods = p.podsProvider.GetPods()
	}

	// Filter out terminal pods.
	// Terminal pods (Failed or Succeeded) should not appear in the podresources list
	// because they no longer consume resources, even if they haven't been fully cleaned up yet.
	// See https://github.com/kubernetes/kubernetes/pull/132028
	var filteredPods []*v1.Pod
	for _, pod := range pods {
		if pod.Status.Phase != v1.PodFailed && pod.Status.Phase != v1.PodSucceeded {
			filteredPods = append(filteredPods, pod)
		}
	}
	pods = filteredPods

	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))

Review comment (suggestion):
It looks like we can avoid having filteredPod list if we revert a condition. We can then merge this loop with the one below.

Code after:
		memoryProvider:           providers.Memory,
		dynamicResourcesProvider: providers.DynamicResources,
		useActivePods:            useActivePods,
	}
}

// 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))
		}