From d1f1bf200cb512eefbee0daf7a1dc53c0d90b91d Mon Sep 17 00:00:00 2001 From: Tim Allclair Date: Thu, 24 Oct 2024 15:51:19 -0700 Subject: [PATCH] Add more comments --- pkg/kubelet/kubelet.go | 9 +++++++-- pkg/kubelet/kuberuntime/kuberuntime_manager.go | 8 ++++---- pkg/kubelet/qos/policy.go | 2 ++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 65264fa41e4..dbcb82caca3 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -1788,7 +1788,10 @@ func (kl *Kubelet) SyncPod(ctx context.Context, updateType kubetypes.SyncPodType } } - if utilfeature.DefaultFeatureGate.Enabled(features.InPlacePodVerticalScaling) && !kubetypes.IsStaticPod(pod) { + // handlePodResourcesResize updates the pod to use the allocated resources. This should come + // before the main business logic of SyncPod, so that a consistent view of the pod is used + // across the sync loop. + if kuberuntime.IsInPlacePodVerticalScalingAllowed(pod) { // Handle pod resize here instead of doing it in HandlePodUpdates because // this conveniently retries any Deferred resize requests // TODO(vinaykul,InPlacePodVerticalScaling): Investigate doing this in HandlePodUpdates + periodic SyncLoop scan @@ -1976,7 +1979,9 @@ func (kl *Kubelet) SyncPod(ctx context.Context, updateType kubetypes.SyncPodType } if utilfeature.DefaultFeatureGate.Enabled(features.InPlacePodVerticalScaling) && isPodResizeInProgress(pod, podStatus) { - // While resize is in progress, periodically call PLEG to update pod cache + // While resize is in progress, periodically request the latest status from the runtime via + // the PLEG. This is necessary since ordinarily pod status is only fetched when a container + // undergoes a state transition. runningPod := kubecontainer.ConvertPodStatusToRunningPod(kl.getRuntime().Type(), podStatus) if err, _ := kl.pleg.UpdateCache(&runningPod, pod.UID); err != nil { klog.ErrorS(err, "Failed to update pod cache", "pod", klog.KObj(pod)) diff --git a/pkg/kubelet/kuberuntime/kuberuntime_manager.go b/pkg/kubelet/kuberuntime/kuberuntime_manager.go index 2f0ddfaa55f..4b87d452049 100644 --- a/pkg/kubelet/kuberuntime/kuberuntime_manager.go +++ b/pkg/kubelet/kuberuntime/kuberuntime_manager.go @@ -539,7 +539,7 @@ func containerSucceeded(c *v1.Container, podStatus *kubecontainer.PodStatus) boo return cStatus.State == kubecontainer.ContainerStateExited && cStatus.ExitCode == 0 } -func isInPlacePodVerticalScalingAllowed(pod *v1.Pod) bool { +func IsInPlacePodVerticalScalingAllowed(pod *v1.Pod) bool { if !utilfeature.DefaultFeatureGate.Enabled(features.InPlacePodVerticalScaling) { return false } @@ -927,7 +927,7 @@ func (m *kubeGenericRuntimeManager) computePodActions(ctx context.Context, pod * } } - if isInPlacePodVerticalScalingAllowed(pod) { + if IsInPlacePodVerticalScalingAllowed(pod) { changes.ContainersToUpdate = make(map[v1.ResourceName][]containerToUpdateInfo) } @@ -985,7 +985,7 @@ func (m *kubeGenericRuntimeManager) computePodActions(ctx context.Context, pod * // If the container failed the startup probe, we should kill it. message = fmt.Sprintf("Container %s failed startup probe", container.Name) reason = reasonStartupProbe - } else if isInPlacePodVerticalScalingAllowed(pod) && !m.computePodResizeAction(pod, idx, containerStatus, &changes) { + } else if IsInPlacePodVerticalScalingAllowed(pod) && !m.computePodResizeAction(pod, idx, containerStatus, &changes) { // computePodResizeAction updates 'changes' if resize policy requires restarting this container continue } else { @@ -1302,7 +1302,7 @@ func (m *kubeGenericRuntimeManager) SyncPod(ctx context.Context, pod *v1.Pod, po } // Step 7: For containers in podContainerChanges.ContainersToUpdate[CPU,Memory] list, invoke UpdateContainerResources - if isInPlacePodVerticalScalingAllowed(pod) { + if IsInPlacePodVerticalScalingAllowed(pod) { if len(podContainerChanges.ContainersToUpdate) > 0 || podContainerChanges.UpdatePodResources { m.doPodResizeAction(pod, podStatus, podContainerChanges, result) } diff --git a/pkg/kubelet/qos/policy.go b/pkg/kubelet/qos/policy.go index 93d0934c280..492bb9d2b8a 100644 --- a/pkg/kubelet/qos/policy.go +++ b/pkg/kubelet/qos/policy.go @@ -37,6 +37,8 @@ const ( // multiplied by 10 (barring exceptional cases) + a configurable quantity which is between -1000 // and 1000. Containers with higher OOM scores are killed if the system runs out of memory. // See https://lwn.net/Articles/391222/ for more information. +// OOMScoreAdjust should be calculated based on the allocated resources, so the pod argument should +// contain the allocated resources in the spec. func GetContainerOOMScoreAdjust(pod *v1.Pod, container *v1.Container, memoryCapacity int64) int { if types.IsNodeCriticalPod(pod) { // Only node critical pod should be the last to get killed.