Merge pull request #129477 from felipeagger/feat/improve-alloc-res-ckpt

[FG:InPlacePodVerticalScaling] Improve allocated resources checkpointing
This commit is contained in:
Kubernetes Prow Robot 2025-02-11 12:06:10 -08:00 committed by GitHub
commit c81431de59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 12 deletions

View File

@ -47,6 +47,7 @@ type Reader interface {
type writer interface {
SetContainerResourceAllocation(podUID string, containerName string, alloc v1.ResourceRequirements) error
SetPodResourceAllocation(podUID string, alloc map[string]v1.ResourceRequirements) error
SetPodResizeStatus(podUID string, resizeStatus v1.PodResizeStatus)
Delete(podUID string, containerName string) error
}

View File

@ -127,6 +127,17 @@ func (sc *stateCheckpoint) SetContainerResourceAllocation(podUID string, contain
return sc.storeState()
}
// SetPodResourceAllocation sets pod resource allocation
func (sc *stateCheckpoint) SetPodResourceAllocation(podUID string, alloc map[string]v1.ResourceRequirements) error {
sc.mux.Lock()
defer sc.mux.Unlock()
err := sc.cache.SetPodResourceAllocation(podUID, alloc)
if err != nil {
return err
}
return sc.storeState()
}
// SetPodResizeStatus sets the last resize decision for a pod
func (sc *stateCheckpoint) SetPodResizeStatus(podUID string, resizeStatus v1.PodResizeStatus) {
sc.mux.Lock()
@ -165,6 +176,10 @@ func (sc *noopStateCheckpoint) SetContainerResourceAllocation(_ string, _ string
return nil
}
func (sc *noopStateCheckpoint) SetPodResourceAllocation(_ string, _ map[string]v1.ResourceRequirements) error {
return nil
}
func (sc *noopStateCheckpoint) SetPodResizeStatus(_ string, _ v1.PodResizeStatus) {}
func (sc *noopStateCheckpoint) Delete(_ string, _ string) error {

View File

@ -110,11 +110,9 @@ func Test_stateCheckpoint_storeState(t *testing.T) {
originalSC, err := NewStateCheckpoint(testDir, testCheckpoint)
require.NoError(t, err)
for podUID, containerAlloc := range tt.args.podResourceAllocation {
for containerName, alloc := range containerAlloc {
err = originalSC.SetContainerResourceAllocation(podUID, containerName, alloc)
require.NoError(t, err)
}
for podUID, alloc := range tt.args.podResourceAllocation {
err = originalSC.SetPodResourceAllocation(podUID, alloc)
require.NoError(t, err)
}
actual := originalSC.GetPodResourceAllocation()

View File

@ -77,6 +77,15 @@ func (s *stateMemory) SetContainerResourceAllocation(podUID string, containerNam
return nil
}
func (s *stateMemory) SetPodResourceAllocation(podUID string, alloc map[string]v1.ResourceRequirements) error {
s.Lock()
defer s.Unlock()
s.podAllocation[podUID] = alloc
klog.V(3).InfoS("Updated pod resource allocation", "podUID", podUID, "allocation", alloc)
return nil
}
func (s *stateMemory) SetPodResizeStatus(podUID string, resizeStatus v1.PodResizeStatus) {
s.Lock()
defer s.Unlock()

View File

@ -310,25 +310,23 @@ func (m *manager) GetPodResizeStatus(podUID types.UID) v1.PodResizeStatus {
func (m *manager) SetPodAllocation(pod *v1.Pod) error {
m.podStatusesLock.RLock()
defer m.podStatusesLock.RUnlock()
podAlloc := make(map[string]v1.ResourceRequirements)
for _, container := range pod.Spec.Containers {
alloc := *container.Resources.DeepCopy()
if err := m.state.SetContainerResourceAllocation(string(pod.UID), container.Name, alloc); err != nil {
return err
}
podAlloc[container.Name] = alloc
}
if utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) {
for _, container := range pod.Spec.InitContainers {
if podutil.IsRestartableInitContainer(&container) {
alloc := *container.Resources.DeepCopy()
if err := m.state.SetContainerResourceAllocation(string(pod.UID), container.Name, alloc); err != nil {
return err
}
podAlloc[container.Name] = alloc
}
}
}
return nil
return m.state.SetPodResourceAllocation(string(pod.UID), podAlloc)
}
// SetPodResizeStatus checkpoints the last resizing decision for the pod.