mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	Merge pull request #129477 from felipeagger/feat/improve-alloc-res-ckpt
[FG:InPlacePodVerticalScaling] Improve allocated resources checkpointing
This commit is contained in:
		@@ -47,6 +47,7 @@ type Reader interface {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
type writer interface {
 | 
					type writer interface {
 | 
				
			||||||
	SetContainerResourceAllocation(podUID string, containerName string, alloc v1.ResourceRequirements) error
 | 
						SetContainerResourceAllocation(podUID string, containerName string, alloc v1.ResourceRequirements) error
 | 
				
			||||||
 | 
						SetPodResourceAllocation(podUID string, alloc map[string]v1.ResourceRequirements) error
 | 
				
			||||||
	SetPodResizeStatus(podUID string, resizeStatus v1.PodResizeStatus)
 | 
						SetPodResizeStatus(podUID string, resizeStatus v1.PodResizeStatus)
 | 
				
			||||||
	Delete(podUID string, containerName string) error
 | 
						Delete(podUID string, containerName string) error
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -127,6 +127,17 @@ func (sc *stateCheckpoint) SetContainerResourceAllocation(podUID string, contain
 | 
				
			|||||||
	return sc.storeState()
 | 
						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
 | 
					// SetPodResizeStatus sets the last resize decision for a pod
 | 
				
			||||||
func (sc *stateCheckpoint) SetPodResizeStatus(podUID string, resizeStatus v1.PodResizeStatus) {
 | 
					func (sc *stateCheckpoint) SetPodResizeStatus(podUID string, resizeStatus v1.PodResizeStatus) {
 | 
				
			||||||
	sc.mux.Lock()
 | 
						sc.mux.Lock()
 | 
				
			||||||
@@ -165,6 +176,10 @@ func (sc *noopStateCheckpoint) SetContainerResourceAllocation(_ string, _ string
 | 
				
			|||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (sc *noopStateCheckpoint) SetPodResourceAllocation(_ string, _ map[string]v1.ResourceRequirements) error {
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (sc *noopStateCheckpoint) SetPodResizeStatus(_ string, _ v1.PodResizeStatus) {}
 | 
					func (sc *noopStateCheckpoint) SetPodResizeStatus(_ string, _ v1.PodResizeStatus) {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (sc *noopStateCheckpoint) Delete(_ string, _ string) error {
 | 
					func (sc *noopStateCheckpoint) Delete(_ string, _ string) error {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -110,11 +110,9 @@ func Test_stateCheckpoint_storeState(t *testing.T) {
 | 
				
			|||||||
			originalSC, err := NewStateCheckpoint(testDir, testCheckpoint)
 | 
								originalSC, err := NewStateCheckpoint(testDir, testCheckpoint)
 | 
				
			||||||
			require.NoError(t, err)
 | 
								require.NoError(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			for podUID, containerAlloc := range tt.args.podResourceAllocation {
 | 
								for podUID, alloc := range tt.args.podResourceAllocation {
 | 
				
			||||||
				for containerName, alloc := range containerAlloc {
 | 
									err = originalSC.SetPodResourceAllocation(podUID, alloc)
 | 
				
			||||||
					err = originalSC.SetContainerResourceAllocation(podUID, containerName, alloc)
 | 
									require.NoError(t, err)
 | 
				
			||||||
					require.NoError(t, err)
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			actual := originalSC.GetPodResourceAllocation()
 | 
								actual := originalSC.GetPodResourceAllocation()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -77,6 +77,15 @@ func (s *stateMemory) SetContainerResourceAllocation(podUID string, containerNam
 | 
				
			|||||||
	return nil
 | 
						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) {
 | 
					func (s *stateMemory) SetPodResizeStatus(podUID string, resizeStatus v1.PodResizeStatus) {
 | 
				
			||||||
	s.Lock()
 | 
						s.Lock()
 | 
				
			||||||
	defer s.Unlock()
 | 
						defer s.Unlock()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -310,25 +310,23 @@ func (m *manager) GetPodResizeStatus(podUID types.UID) v1.PodResizeStatus {
 | 
				
			|||||||
func (m *manager) SetPodAllocation(pod *v1.Pod) error {
 | 
					func (m *manager) SetPodAllocation(pod *v1.Pod) error {
 | 
				
			||||||
	m.podStatusesLock.RLock()
 | 
						m.podStatusesLock.RLock()
 | 
				
			||||||
	defer m.podStatusesLock.RUnlock()
 | 
						defer m.podStatusesLock.RUnlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						podAlloc := make(map[string]v1.ResourceRequirements)
 | 
				
			||||||
	for _, container := range pod.Spec.Containers {
 | 
						for _, container := range pod.Spec.Containers {
 | 
				
			||||||
		alloc := *container.Resources.DeepCopy()
 | 
							alloc := *container.Resources.DeepCopy()
 | 
				
			||||||
		if err := m.state.SetContainerResourceAllocation(string(pod.UID), container.Name, alloc); err != nil {
 | 
							podAlloc[container.Name] = alloc
 | 
				
			||||||
			return err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) {
 | 
						if utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) {
 | 
				
			||||||
		for _, container := range pod.Spec.InitContainers {
 | 
							for _, container := range pod.Spec.InitContainers {
 | 
				
			||||||
			if podutil.IsRestartableInitContainer(&container) {
 | 
								if podutil.IsRestartableInitContainer(&container) {
 | 
				
			||||||
				alloc := *container.Resources.DeepCopy()
 | 
									alloc := *container.Resources.DeepCopy()
 | 
				
			||||||
				if err := m.state.SetContainerResourceAllocation(string(pod.UID), container.Name, alloc); err != nil {
 | 
									podAlloc[container.Name] = alloc
 | 
				
			||||||
					return err
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return m.state.SetPodResourceAllocation(string(pod.UID), podAlloc)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// SetPodResizeStatus checkpoints the last resizing decision for the pod.
 | 
					// SetPodResizeStatus checkpoints the last resizing decision for the pod.
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user