Skip to content

Commit 0e9ca10

Browse files
authored
Merge pull request kubernetes#129504 from felipeagger/cleanup/ckpt-pod-alloc-state-mem
Cleanup Kubelet Checkpointing: Refactor state mem
2 parents 4e54a67 + f7df3ec commit 0e9ca10

File tree

5 files changed

+40
-55
lines changed

5 files changed

+40
-55
lines changed

pkg/kubelet/status/fake_status_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,6 @@ func (m *fakeManager) SetPodResizeStatus(podUID types.UID, resizeStatus v1.PodRe
9393
// NewFakeManager creates empty/fake memory manager
9494
func NewFakeManager() Manager {
9595
return &fakeManager{
96-
state: state.NewStateMemory(),
96+
state: state.NewStateMemory(state.PodResourceAllocation{}),
9797
}
9898
}

pkg/kubelet/status/state/state.go

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

4848
type writer interface {
4949
SetContainerResourceAllocation(podUID string, containerName string, alloc v1.ResourceRequirements) error
50-
SetPodResourceAllocation(PodResourceAllocation) error
5150
SetPodResizeStatus(podUID string, resizeStatus v1.PodResizeStatus)
5251
Delete(podUID string, containerName string) error
5352
ClearState() error

pkg/kubelet/status/state/state_checkpoint.go

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,46 +42,42 @@ func NewStateCheckpoint(stateDir, checkpointName string) (State, error) {
4242
if err != nil {
4343
return nil, fmt.Errorf("failed to initialize checkpoint manager for pod allocation tracking: %v", err)
4444
}
45+
46+
praInfo, err := restoreState(checkpointManager, checkpointName)
47+
if err != nil {
48+
//lint:ignore ST1005 user-facing error message
49+
return nil, fmt.Errorf("could not restore state from checkpoint: %w, please drain this node and delete pod allocation checkpoint file %q before restarting Kubelet",
50+
err, path.Join(stateDir, checkpointName))
51+
}
52+
4553
stateCheckpoint := &stateCheckpoint{
46-
cache: NewStateMemory(),
54+
cache: NewStateMemory(praInfo.AllocationEntries),
4755
checkpointManager: checkpointManager,
4856
checkpointName: checkpointName,
4957
}
50-
51-
if err := stateCheckpoint.restoreState(); err != nil {
52-
//lint:ignore ST1005 user-facing error message
53-
return nil, fmt.Errorf("could not restore state from checkpoint: %v, please drain this node and delete pod allocation checkpoint file %q before restarting Kubelet", err, path.Join(stateDir, checkpointName))
54-
}
5558
return stateCheckpoint, nil
5659
}
5760

5861
// restores state from a checkpoint and creates it if it doesn't exist
59-
func (sc *stateCheckpoint) restoreState() error {
60-
sc.mux.Lock()
61-
defer sc.mux.Unlock()
62+
func restoreState(checkpointManager checkpointmanager.CheckpointManager, checkpointName string) (*PodResourceAllocationInfo, error) {
6263
var err error
64+
checkpoint := &Checkpoint{}
6365

64-
checkpoint, err := NewCheckpoint(nil)
65-
if err != nil {
66-
return fmt.Errorf("failed to create new checkpoint: %w", err)
67-
}
68-
69-
if err = sc.checkpointManager.GetCheckpoint(sc.checkpointName, checkpoint); err != nil {
66+
if err = checkpointManager.GetCheckpoint(checkpointName, checkpoint); err != nil {
7067
if err == errors.ErrCheckpointNotFound {
71-
return sc.storeState()
68+
return &PodResourceAllocationInfo{
69+
AllocationEntries: make(map[string]map[string]v1.ResourceRequirements),
70+
}, nil
7271
}
73-
return err
72+
return nil, err
7473
}
74+
klog.V(2).InfoS("State checkpoint: restored pod resource allocation state from checkpoint")
7575
praInfo, err := checkpoint.GetPodResourceAllocationInfo()
7676
if err != nil {
77-
return fmt.Errorf("failed to get pod resource allocation info: %w", err)
78-
}
79-
err = sc.cache.SetPodResourceAllocation(praInfo.AllocationEntries)
80-
if err != nil {
81-
return fmt.Errorf("failed to set pod resource allocation: %w", err)
77+
return nil, fmt.Errorf("failed to get pod resource allocation info: %w", err)
8278
}
83-
klog.V(2).InfoS("State checkpoint: restored pod resource allocation state from checkpoint")
84-
return nil
79+
80+
return praInfo, nil
8581
}
8682

8783
// saves state to a checkpoint, caller is responsible for locking
@@ -131,14 +127,6 @@ func (sc *stateCheckpoint) SetContainerResourceAllocation(podUID string, contain
131127
return sc.storeState()
132128
}
133129

134-
// SetPodResourceAllocation sets pod resource allocation
135-
func (sc *stateCheckpoint) SetPodResourceAllocation(a PodResourceAllocation) error {
136-
sc.mux.Lock()
137-
defer sc.mux.Unlock()
138-
sc.cache.SetPodResourceAllocation(a)
139-
return sc.storeState()
140-
}
141-
142130
// SetPodResizeStatus sets the last resize decision for a pod
143131
func (sc *stateCheckpoint) SetPodResizeStatus(podUID string, resizeStatus v1.PodResizeStatus) {
144132
sc.mux.Lock()
@@ -185,10 +173,6 @@ func (sc *noopStateCheckpoint) SetContainerResourceAllocation(_ string, _ string
185173
return nil
186174
}
187175

188-
func (sc *noopStateCheckpoint) SetPodResourceAllocation(_ PodResourceAllocation) error {
189-
return nil
190-
}
191-
192176
func (sc *noopStateCheckpoint) SetPodResizeStatus(_ string, _ v1.PodResizeStatus) {}
193177

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

pkg/kubelet/status/state/state_checkpoint_test.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const testCheckpoint = "pod_status_manager_state"
3232

3333
func newTestStateCheckpoint(t *testing.T) *stateCheckpoint {
3434
testingDir := getTestDir(t)
35-
cache := NewStateMemory()
35+
cache := NewStateMemory(PodResourceAllocation{})
3636
checkpointManager, err := checkpointmanager.NewCheckpointManager(testingDir)
3737
require.NoError(t, err, "failed to create checkpoint manager")
3838
checkpointName := "pod_state_checkpoint"
@@ -110,8 +110,12 @@ func Test_stateCheckpoint_storeState(t *testing.T) {
110110
originalSC, err := NewStateCheckpoint(testDir, testCheckpoint)
111111
require.NoError(t, err)
112112

113-
err = originalSC.SetPodResourceAllocation(tt.args.podResourceAllocation)
114-
require.NoError(t, err)
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+
}
118+
}
115119

116120
actual := originalSC.GetPodResourceAllocation()
117121
verifyPodResourceAllocation(t, &tt.args.podResourceAllocation, &actual, "stored pod resource allocation is not equal to original pod resource allocation")
@@ -154,11 +158,15 @@ func Test_stateCheckpoint_formatUpgraded(t *testing.T) {
154158
err = sc.checkpointManager.CreateCheckpoint(sc.checkpointName, checkpoint)
155159
require.NoError(t, err, "failed to create old checkpoint")
156160

157-
err = sc.restoreState()
161+
actualPodResourceAllocationInfo, err := restoreState(sc.checkpointManager, sc.checkpointName)
158162
require.NoError(t, err, "failed to restore state")
159163

160-
actualPodResourceAllocationInfo := &PodResourceAllocationInfo{}
164+
require.Equal(t, expectedPodResourceAllocationInfo, actualPodResourceAllocationInfo, "pod resource allocation info is not equal")
165+
166+
sc.cache = NewStateMemory(actualPodResourceAllocationInfo.AllocationEntries)
167+
168+
actualPodResourceAllocationInfo = &PodResourceAllocationInfo{}
161169
actualPodResourceAllocationInfo.AllocationEntries = sc.cache.GetPodResourceAllocation()
162-
require.NoError(t, err, "failed to get pod resource allocation info")
170+
163171
require.Equal(t, expectedPodResourceAllocationInfo, actualPodResourceAllocationInfo, "pod resource allocation info is not equal")
164172
}

pkg/kubelet/status/state/state_mem.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ type stateMemory struct {
3232
var _ State = &stateMemory{}
3333

3434
// NewStateMemory creates new State to track resources allocated to pods
35-
func NewStateMemory() State {
35+
func NewStateMemory(alloc PodResourceAllocation) State {
36+
if alloc == nil {
37+
alloc = PodResourceAllocation{}
38+
}
3639
klog.V(2).InfoS("Initialized new in-memory state store for pod resource allocation tracking")
3740
return &stateMemory{
38-
podAllocation: PodResourceAllocation{},
41+
podAllocation: alloc,
3942
podResizeStatus: PodResizeStatus{},
4043
}
4144
}
@@ -74,15 +77,6 @@ func (s *stateMemory) SetContainerResourceAllocation(podUID string, containerNam
7477
return nil
7578
}
7679

77-
func (s *stateMemory) SetPodResourceAllocation(a PodResourceAllocation) error {
78-
s.Lock()
79-
defer s.Unlock()
80-
81-
s.podAllocation = a.Clone()
82-
klog.V(3).InfoS("Updated pod resource allocation", "allocation", a)
83-
return nil
84-
}
85-
8680
func (s *stateMemory) SetPodResizeStatus(podUID string, resizeStatus v1.PodResizeStatus) {
8781
s.Lock()
8882
defer s.Unlock()

0 commit comments

Comments
 (0)