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

Code before:
// ListenAndServeReadOnly runs the kubelet HTTP server in read-only mode.
func (kl *Kubelet) ListenAndServeReadOnly(ctx context.Context, address net.IP, port uint, tp trace.TracerProvider) {
	server.ListenAndServeKubeletReadOnlyServer(ctx, kl, kl.resourceAnalyzer, kl.containerManager.GetHealthCheckers(), kl.flagz, address, port, tp)
}

type kubeletPodsProvider struct {
	kl *Kubelet
}

func (pp *kubeletPodsProvider) GetActivePods() []*v1.Pod {
	return pp.kl.GetActivePods()
}

func (pp *kubeletPodsProvider) GetPods() []*v1.Pod {
	return pp.kl.podManager.GetPods()
}

func (pp *kubeletPodsProvider) GetPodByName(namespace, name string) (*v1.Pod, bool) {
	return pp.kl.podManager.GetPodByName(namespace, name)
}

// ListenAndServePodResources runs the kubelet podresources grpc service
func (kl *Kubelet) ListenAndServePodResources(ctx context.Context) {
	endpoint, err := util.LocalEndpoint(kl.getPodResourcesDir(), podresources.Socket)
	if err != nil {
		klog.FromContext(ctx).V(2).Info("Failed to get local endpoint for PodResources endpoint", "err", err)
		return
	}

	providers := podresources.PodResourcesProviders{
		Pods:             &kubeletPodsProvider{kl: kl},
		Devices:          kl.containerManager,
		Cpus:             kl.containerManager,
		Memory:           kl.containerManager,
		DynamicResources: kl.containerManager,
	}

	server.ListenAndServePodResources(ctx, endpoint, providers)
}

// Delete the eligible dead container instances in a pod. Depending on the configuration, the latest dead containers may be kept around.
func (kl *Kubelet) cleanUpContainersInPod(podID types.UID, exitedContainerID string) {
	if podStatus, err := kl.podCache.Get(podID); err == nil {
		// When an evicted or deleted pod has already synced, all containers can be removed.
		removeAll := kl.podWorkers.ShouldPodContentBeRemoved(podID)
		kl.containerDeletor.deleteContainersInPod(exitedContainerID, podStatus, removeAll)
	}
}

// fastStatusUpdateOnce starts a loop that checks if the current state of kubelet + container runtime
// would be able to turn the node ready, and sync the ready state to the apiserver as soon as possible.

Review comment (bug):
I am fine to merge this as it makes things better. The question from me would be is whether we get ourselves in some sort of race condition here? Will there be code path where the termination will be initiated while we looping over pods?

Code after:
}

// ListenAndServeReadOnly runs the kubelet HTTP server in read-only mode.
func (kl *Kubelet) ListenAndServeReadOnly(ctx context.Context, address net.IP, port uint, tp trace.TracerProvider) {
	server.ListenAndServeKubeletReadOnlyServer(ctx, kl, kl.resourceAnalyzer, kl.containerManager.GetHealthCheckers(), kl.flagz, address, port, tp)
}

type kubeletPodsProvider struct {
	kl *Kubelet
}

func (pp *kubeletPodsProvider) GetActivePods() []*v1.Pod {
	return pp.kl.GetActivePods()
}

func (pp *kubeletPodsProvider) GetPods() []*v1.Pod {
	return pp.kl.podManager.GetPods()
}

func (pp *kubeletPodsProvider) GetPodByName(namespace, name string) (*v1.Pod, bool) {
	pod, found := pp.kl.podManager.GetPodByName(namespace, name)
	if !found {
		return nil, false
	}
	// use the same filtering logic as GetActivePods
	if pp.kl.isPodInactive(pod) {
		return nil, false
	}
	return pod, true
}

// ListenAndServePodResources runs the kubelet podresources grpc service
func (kl *Kubelet) ListenAndServePodResources(ctx context.Context) {
	endpoint, err := util.LocalEndpoint(kl.getPodResourcesDir(), podresources.Socket)
	if err != nil {
		klog.FromContext(ctx).V(2).Info("Failed to get local endpoint for PodResources endpoint", "err", err)
		return
	}

	providers := podresources.PodResourcesProviders{
		Pods:             &kubeletPodsProvider{kl: kl},
		Devices:          kl.containerManager,
		Cpus:             kl.containerManager,
		Memory:           kl.containerManager,
		DynamicResources: kl.containerManager,
	}

	server.ListenAndServePodResources(ctx, endpoint, providers)
}

// Delete the eligible dead container instances in a pod. Depending on the configuration, the latest dead containers may be kept around.