Skip to content

Commit 14c0bc1

Browse files
committed
kubelet: improve allocated resources checkpointing
changed calls to set allocation from container level to pod level on status manager.
1 parent 491a23f commit 14c0bc1

File tree

5 files changed

+33
-12
lines changed

5 files changed

+33
-12
lines changed

pkg/kubelet/status/state/state.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type Reader interface {
4747

4848
type writer interface {
4949
SetContainerResourceAllocation(podUID string, containerName string, alloc v1.ResourceRequirements) error
50+
SetPodResourceAllocation(podUID string, alloc map[string]v1.ResourceRequirements) error
5051
SetPodResizeStatus(podUID string, resizeStatus v1.PodResizeStatus)
5152
Delete(podUID string, containerName string) error
5253
}

pkg/kubelet/status/state/state_checkpoint.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ func (sc *stateCheckpoint) SetContainerResourceAllocation(podUID string, contain
127127
return sc.storeState()
128128
}
129129

130+
// SetPodResourceAllocation sets pod resource allocation
131+
func (sc *stateCheckpoint) SetPodResourceAllocation(podUID string, alloc map[string]v1.ResourceRequirements) error {
132+
sc.mux.Lock()
133+
defer sc.mux.Unlock()
134+
err := sc.cache.SetPodResourceAllocation(podUID, alloc)
135+
if err != nil {
136+
return err
137+
}
138+
return sc.storeState()
139+
}
140+
130141
// SetPodResizeStatus sets the last resize decision for a pod
131142
func (sc *stateCheckpoint) SetPodResizeStatus(podUID string, resizeStatus v1.PodResizeStatus) {
132143
sc.mux.Lock()
@@ -165,6 +176,10 @@ func (sc *noopStateCheckpoint) SetContainerResourceAllocation(_ string, _ string
165176
return nil
166177
}
167178

179+
func (sc *noopStateCheckpoint) SetPodResourceAllocation(_ string, _ map[string]v1.ResourceRequirements) error {
180+
return nil
181+
}
182+
168183
func (sc *noopStateCheckpoint) SetPodResizeStatus(_ string, _ v1.PodResizeStatus) {}
169184

170185
func (sc *noopStateCheckpoint) Delete(_ string, _ string) error {

pkg/kubelet/status/state/state_checkpoint_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,9 @@ func Test_stateCheckpoint_storeState(t *testing.T) {
110110
originalSC, err := NewStateCheckpoint(testDir, testCheckpoint)
111111
require.NoError(t, err)
112112

113-
for podUID, containerAlloc := range tt.args.podResourceAllocation {
114-
for containerName, alloc := range containerAlloc {
115-
err = originalSC.SetContainerResourceAllocation(podUID, containerName, alloc)
116-
require.NoError(t, err)
117-
}
113+
for podUID, alloc := range tt.args.podResourceAllocation {
114+
err = originalSC.SetPodResourceAllocation(podUID, alloc)
115+
require.NoError(t, err)
118116
}
119117

120118
actual := originalSC.GetPodResourceAllocation()

pkg/kubelet/status/state/state_mem.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ func (s *stateMemory) SetContainerResourceAllocation(podUID string, containerNam
7777
return nil
7878
}
7979

80+
func (s *stateMemory) SetPodResourceAllocation(podUID string, alloc map[string]v1.ResourceRequirements) error {
81+
s.Lock()
82+
defer s.Unlock()
83+
84+
s.podAllocation[podUID] = alloc
85+
klog.V(3).InfoS("Updated pod resource allocation", "podUID", podUID, "allocation", alloc)
86+
return nil
87+
}
88+
8089
func (s *stateMemory) SetPodResizeStatus(podUID string, resizeStatus v1.PodResizeStatus) {
8190
s.Lock()
8291
defer s.Unlock()

pkg/kubelet/status/status_manager.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -310,25 +310,23 @@ func (m *manager) GetPodResizeStatus(podUID types.UID) v1.PodResizeStatus {
310310
func (m *manager) SetPodAllocation(pod *v1.Pod) error {
311311
m.podStatusesLock.RLock()
312312
defer m.podStatusesLock.RUnlock()
313+
314+
podAlloc := make(map[string]v1.ResourceRequirements)
313315
for _, container := range pod.Spec.Containers {
314316
alloc := *container.Resources.DeepCopy()
315-
if err := m.state.SetContainerResourceAllocation(string(pod.UID), container.Name, alloc); err != nil {
316-
return err
317-
}
317+
podAlloc[container.Name] = alloc
318318
}
319319

320320
if utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) {
321321
for _, container := range pod.Spec.InitContainers {
322322
if podutil.IsRestartableInitContainer(&container) {
323323
alloc := *container.Resources.DeepCopy()
324-
if err := m.state.SetContainerResourceAllocation(string(pod.UID), container.Name, alloc); err != nil {
325-
return err
326-
}
324+
podAlloc[container.Name] = alloc
327325
}
328326
}
329327
}
330328

331-
return nil
329+
return m.state.SetPodResourceAllocation(string(pod.UID), podAlloc)
332330
}
333331

334332
// SetPodResizeStatus checkpoints the last resizing decision for the pod.

0 commit comments

Comments
 (0)