Skip to content

Commit 9d9e1af

Browse files
authored
Merge pull request kubernetes#130576 from tallclair/allocated-uid
Change allocation manager pod UID to types.UID
2 parents f816be0 + 8fac9c6 commit 9d9e1af

File tree

9 files changed

+36
-37
lines changed

9 files changed

+36
-37
lines changed

pkg/kubelet/allocation/allocation_manager.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const podStatusManagerStateFile = "pod_status_manager_state"
3434
// AllocationManager tracks pod resource allocations.
3535
type Manager interface {
3636
// GetContainerResourceAllocation returns the AllocatedResources value for the container
37-
GetContainerResourceAllocation(podUID string, containerName string) (v1.ResourceRequirements, bool)
37+
GetContainerResourceAllocation(podUID types.UID, containerName string) (v1.ResourceRequirements, bool)
3838

3939
// UpdatePodFromAllocation overwrites the pod spec with the allocation.
4040
// This function does a deep copy only if updates are needed.
@@ -83,7 +83,7 @@ func NewInMemoryManager() Manager {
8383

8484
// GetContainerResourceAllocation returns the last checkpointed AllocatedResources values
8585
// If checkpoint manager has not been initialized, it returns nil, false
86-
func (m *manager) GetContainerResourceAllocation(podUID string, containerName string) (v1.ResourceRequirements, bool) {
86+
func (m *manager) GetContainerResourceAllocation(podUID types.UID, containerName string) (v1.ResourceRequirements, bool) {
8787
return m.state.GetContainerResourceAllocation(podUID, containerName)
8888
}
8989

@@ -96,7 +96,7 @@ func (m *manager) UpdatePodFromAllocation(pod *v1.Pod) (*v1.Pod, bool) {
9696
}
9797

9898
func updatePodFromAllocation(pod *v1.Pod, allocs state.PodResourceAllocation) (*v1.Pod, bool) {
99-
allocated, found := allocs[string(pod.UID)]
99+
allocated, found := allocs[pod.UID]
100100
if !found {
101101
return pod, false
102102
}
@@ -149,11 +149,11 @@ func (m *manager) SetPodAllocation(pod *v1.Pod) error {
149149
}
150150
}
151151

152-
return m.state.SetPodResourceAllocation(string(pod.UID), podAlloc)
152+
return m.state.SetPodResourceAllocation(pod.UID, podAlloc)
153153
}
154154

155155
func (m *manager) DeletePodAllocation(uid types.UID) {
156-
if err := m.state.Delete(string(uid), ""); err != nil {
156+
if err := m.state.Delete(uid, ""); err != nil {
157157
// If the deletion fails, it will be retried by RemoveOrphanedPods, so we can safely ignore the error.
158158
klog.V(3).ErrorS(err, "Failed to delete pod allocation", "podUID", uid)
159159
}

pkg/kubelet/allocation/allocation_manager_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func TestUpdatePodFromAllocation(t *testing.T) {
110110
name: "steady state",
111111
pod: pod,
112112
allocs: state.PodResourceAllocation{
113-
string(pod.UID): map[string]v1.ResourceRequirements{
113+
pod.UID: map[string]v1.ResourceRequirements{
114114
"c1": *pod.Spec.Containers[0].Resources.DeepCopy(),
115115
"c2": *pod.Spec.Containers[1].Resources.DeepCopy(),
116116
"c1-restartable-init": *pod.Spec.InitContainers[0].Resources.DeepCopy(),
@@ -127,7 +127,7 @@ func TestUpdatePodFromAllocation(t *testing.T) {
127127
name: "missing container allocation",
128128
pod: pod,
129129
allocs: state.PodResourceAllocation{
130-
string(pod.UID): map[string]v1.ResourceRequirements{
130+
pod.UID: map[string]v1.ResourceRequirements{
131131
"c2": *pod.Spec.Containers[1].Resources.DeepCopy(),
132132
},
133133
},
@@ -136,7 +136,7 @@ func TestUpdatePodFromAllocation(t *testing.T) {
136136
name: "resized container",
137137
pod: pod,
138138
allocs: state.PodResourceAllocation{
139-
string(pod.UID): map[string]v1.ResourceRequirements{
139+
pod.UID: map[string]v1.ResourceRequirements{
140140
"c1": *resizedPod.Spec.Containers[0].Resources.DeepCopy(),
141141
"c2": *resizedPod.Spec.Containers[1].Resources.DeepCopy(),
142142
"c1-restartable-init": *resizedPod.Spec.InitContainers[0].Resources.DeepCopy(),

pkg/kubelet/allocation/state/checkpoint.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ import (
2121
"fmt"
2222

2323
v1 "k8s.io/api/core/v1"
24+
"k8s.io/apimachinery/pkg/types"
2425
"k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
2526
"k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum"
2627
)
2728

2829
var _ checkpointmanager.Checkpoint = &Checkpoint{}
2930

3031
type PodResourceAllocationInfo struct {
31-
AllocationEntries map[string]map[string]v1.ResourceRequirements `json:"allocationEntries,omitempty"`
32+
AllocationEntries map[types.UID]map[string]v1.ResourceRequirements `json:"allocationEntries,omitempty"`
3233
}
3334

3435
// Checkpoint represents a structure to store pod resource allocation checkpoint data

pkg/kubelet/allocation/state/state.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ import (
2323
)
2424

2525
// PodResourceAllocation type is used in tracking resources allocated to pod's containers
26-
type PodResourceAllocation map[string]map[string]v1.ResourceRequirements
27-
28-
// PodResizeStatus type is used in tracking the last resize decision for pod
29-
type PodResizeStatus map[string]v1.PodResizeStatus
26+
type PodResourceAllocation map[types.UID]map[string]v1.ResourceRequirements
3027

3128
// Clone returns a copy of PodResourceAllocation
3229
func (pr PodResourceAllocation) Clone() PodResourceAllocation {
@@ -42,14 +39,14 @@ func (pr PodResourceAllocation) Clone() PodResourceAllocation {
4239

4340
// Reader interface used to read current pod resource allocation state
4441
type Reader interface {
45-
GetContainerResourceAllocation(podUID string, containerName string) (v1.ResourceRequirements, bool)
42+
GetContainerResourceAllocation(podUID types.UID, containerName string) (v1.ResourceRequirements, bool)
4643
GetPodResourceAllocation() PodResourceAllocation
4744
}
4845

4946
type writer interface {
50-
SetContainerResourceAllocation(podUID string, containerName string, alloc v1.ResourceRequirements) error
51-
SetPodResourceAllocation(podUID string, alloc map[string]v1.ResourceRequirements) error
52-
Delete(podUID string, containerName string) error
47+
SetContainerResourceAllocation(podUID types.UID, containerName string, alloc v1.ResourceRequirements) error
48+
SetPodResourceAllocation(podUID types.UID, alloc map[string]v1.ResourceRequirements) error
49+
Delete(podUID types.UID, containerName string) error
5350
// RemoveOrphanedPods removes the stored state for any pods not included in the set of remaining pods.
5451
RemoveOrphanedPods(remainingPods sets.Set[types.UID])
5552
}

pkg/kubelet/allocation/state/state_checkpoint.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func restoreState(checkpointManager checkpointmanager.CheckpointManager, checkpo
6868
if err = checkpointManager.GetCheckpoint(checkpointName, checkpoint); err != nil {
6969
if err == errors.ErrCheckpointNotFound {
7070
return &PodResourceAllocationInfo{
71-
AllocationEntries: make(map[string]map[string]v1.ResourceRequirements),
71+
AllocationEntries: make(map[types.UID]map[string]v1.ResourceRequirements),
7272
}, nil
7373
}
7474
return nil, err
@@ -101,7 +101,7 @@ func (sc *stateCheckpoint) storeState() error {
101101
}
102102

103103
// GetContainerResourceAllocation returns current resources allocated to a pod's container
104-
func (sc *stateCheckpoint) GetContainerResourceAllocation(podUID string, containerName string) (v1.ResourceRequirements, bool) {
104+
func (sc *stateCheckpoint) GetContainerResourceAllocation(podUID types.UID, containerName string) (v1.ResourceRequirements, bool) {
105105
sc.mux.RLock()
106106
defer sc.mux.RUnlock()
107107
return sc.cache.GetContainerResourceAllocation(podUID, containerName)
@@ -115,15 +115,15 @@ func (sc *stateCheckpoint) GetPodResourceAllocation() PodResourceAllocation {
115115
}
116116

117117
// SetContainerResourceAllocation sets resources allocated to a pod's container
118-
func (sc *stateCheckpoint) SetContainerResourceAllocation(podUID string, containerName string, alloc v1.ResourceRequirements) error {
118+
func (sc *stateCheckpoint) SetContainerResourceAllocation(podUID types.UID, containerName string, alloc v1.ResourceRequirements) error {
119119
sc.mux.Lock()
120120
defer sc.mux.Unlock()
121121
sc.cache.SetContainerResourceAllocation(podUID, containerName, alloc)
122122
return sc.storeState()
123123
}
124124

125125
// SetPodResourceAllocation sets pod resource allocation
126-
func (sc *stateCheckpoint) SetPodResourceAllocation(podUID string, alloc map[string]v1.ResourceRequirements) error {
126+
func (sc *stateCheckpoint) SetPodResourceAllocation(podUID types.UID, alloc map[string]v1.ResourceRequirements) error {
127127
sc.mux.Lock()
128128
defer sc.mux.Unlock()
129129
err := sc.cache.SetPodResourceAllocation(podUID, alloc)
@@ -134,7 +134,7 @@ func (sc *stateCheckpoint) SetPodResourceAllocation(podUID string, alloc map[str
134134
}
135135

136136
// Delete deletes allocations for specified pod
137-
func (sc *stateCheckpoint) Delete(podUID string, containerName string) error {
137+
func (sc *stateCheckpoint) Delete(podUID types.UID, containerName string) error {
138138
sc.mux.Lock()
139139
defer sc.mux.Unlock()
140140
sc.cache.Delete(podUID, containerName)
@@ -154,23 +154,23 @@ func NewNoopStateCheckpoint() State {
154154
return &noopStateCheckpoint{}
155155
}
156156

157-
func (sc *noopStateCheckpoint) GetContainerResourceAllocation(_ string, _ string) (v1.ResourceRequirements, bool) {
157+
func (sc *noopStateCheckpoint) GetContainerResourceAllocation(_ types.UID, _ string) (v1.ResourceRequirements, bool) {
158158
return v1.ResourceRequirements{}, false
159159
}
160160

161161
func (sc *noopStateCheckpoint) GetPodResourceAllocation() PodResourceAllocation {
162162
return nil
163163
}
164164

165-
func (sc *noopStateCheckpoint) SetContainerResourceAllocation(_ string, _ string, _ v1.ResourceRequirements) error {
165+
func (sc *noopStateCheckpoint) SetContainerResourceAllocation(_ types.UID, _ string, _ v1.ResourceRequirements) error {
166166
return nil
167167
}
168168

169-
func (sc *noopStateCheckpoint) SetPodResourceAllocation(_ string, _ map[string]v1.ResourceRequirements) error {
169+
func (sc *noopStateCheckpoint) SetPodResourceAllocation(_ types.UID, _ map[string]v1.ResourceRequirements) error {
170170
return nil
171171
}
172172

173-
func (sc *noopStateCheckpoint) Delete(_ string, _ string) error {
173+
func (sc *noopStateCheckpoint) Delete(_ types.UID, _ string) error {
174174
return nil
175175
}
176176

pkg/kubelet/allocation/state/state_checkpoint_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
v1 "k8s.io/api/core/v1"
2727
"k8s.io/apimachinery/pkg/api/resource"
28+
"k8s.io/apimachinery/pkg/types"
2829
"k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
2930
)
3031

@@ -138,7 +139,7 @@ func Test_stateCheckpoint_formatUpgraded(t *testing.T) {
138139
// pretend that the old checkpoint is unaware for the field ResizeStatusEntries
139140
const checkpointContent = `{"data":"{\"allocationEntries\":{\"pod1\":{\"container1\":{\"requests\":{\"cpu\":\"1Ki\",\"memory\":\"1Ki\"}}}}}","checksum":1555601526}`
140141
expectedPodResourceAllocationInfo := &PodResourceAllocationInfo{
141-
AllocationEntries: map[string]map[string]v1.ResourceRequirements{
142+
AllocationEntries: map[types.UID]map[string]v1.ResourceRequirements{
142143
"pod1": {
143144
"container1": {
144145
Requests: v1.ResourceList{

pkg/kubelet/allocation/state/state_mem.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func NewStateMemory(alloc PodResourceAllocation) State {
4343
}
4444
}
4545

46-
func (s *stateMemory) GetContainerResourceAllocation(podUID string, containerName string) (v1.ResourceRequirements, bool) {
46+
func (s *stateMemory) GetContainerResourceAllocation(podUID types.UID, containerName string) (v1.ResourceRequirements, bool) {
4747
s.RLock()
4848
defer s.RUnlock()
4949

@@ -57,7 +57,7 @@ func (s *stateMemory) GetPodResourceAllocation() PodResourceAllocation {
5757
return s.podAllocation.Clone()
5858
}
5959

60-
func (s *stateMemory) SetContainerResourceAllocation(podUID string, containerName string, alloc v1.ResourceRequirements) error {
60+
func (s *stateMemory) SetContainerResourceAllocation(podUID types.UID, containerName string, alloc v1.ResourceRequirements) error {
6161
s.Lock()
6262
defer s.Unlock()
6363

@@ -70,7 +70,7 @@ func (s *stateMemory) SetContainerResourceAllocation(podUID string, containerNam
7070
return nil
7171
}
7272

73-
func (s *stateMemory) SetPodResourceAllocation(podUID string, alloc map[string]v1.ResourceRequirements) error {
73+
func (s *stateMemory) SetPodResourceAllocation(podUID types.UID, alloc map[string]v1.ResourceRequirements) error {
7474
s.Lock()
7575
defer s.Unlock()
7676

@@ -79,15 +79,15 @@ func (s *stateMemory) SetPodResourceAllocation(podUID string, alloc map[string]v
7979
return nil
8080
}
8181

82-
func (s *stateMemory) deleteContainer(podUID string, containerName string) {
82+
func (s *stateMemory) deleteContainer(podUID types.UID, containerName string) {
8383
delete(s.podAllocation[podUID], containerName)
8484
if len(s.podAllocation[podUID]) == 0 {
8585
delete(s.podAllocation, podUID)
8686
}
8787
klog.V(3).InfoS("Deleted pod resource allocation", "podUID", podUID, "containerName", containerName)
8888
}
8989

90-
func (s *stateMemory) Delete(podUID string, containerName string) error {
90+
func (s *stateMemory) Delete(podUID types.UID, containerName string) error {
9191
s.Lock()
9292
defer s.Unlock()
9393
if len(containerName) == 0 {

pkg/kubelet/kubelet_pods.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,7 +2149,7 @@ func (kl *Kubelet) convertToAPIContainerStatuses(pod *v1.Pod, podStatus *kubecon
21492149

21502150
// Always set the status to the latest allocated resources, even if it differs from the
21512151
// allocation used by the current sync loop.
2152-
alloc, found := kl.allocationManager.GetContainerResourceAllocation(string(pod.UID), cName)
2152+
alloc, found := kl.allocationManager.GetContainerResourceAllocation(pod.UID, cName)
21532153
if !found {
21542154
// This case is expected for non-resizable containers (ephemeral & non-restartable init containers).
21552155
// Don't set status.Resources in this case.
@@ -2369,7 +2369,7 @@ func (kl *Kubelet) convertToAPIContainerStatuses(pod *v1.Pod, podStatus *kubecon
23692369
status.Resources = convertContainerStatusResources(cName, status, cStatus, oldStatuses)
23702370

23712371
if utilfeature.DefaultFeatureGate.Enabled(features.InPlacePodVerticalScalingAllocatedStatus) {
2372-
if alloc, found := kl.allocationManager.GetContainerResourceAllocation(string(pod.UID), cName); found {
2372+
if alloc, found := kl.allocationManager.GetContainerResourceAllocation(pod.UID, cName); found {
23732373
status.AllocatedResources = alloc.Requests
23742374
}
23752375
}

pkg/kubelet/kubelet_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,11 +2575,11 @@ func TestPodResourceAllocationReset(t *testing.T) {
25752575
}
25762576
kubelet.HandlePodAdditions([]*v1.Pod{tc.pod})
25772577

2578-
allocatedResources, found := kubelet.allocationManager.GetContainerResourceAllocation(string(tc.pod.UID), tc.pod.Spec.Containers[0].Name)
2578+
allocatedResources, found := kubelet.allocationManager.GetContainerResourceAllocation(tc.pod.UID, tc.pod.Spec.Containers[0].Name)
25792579
if !found {
25802580
t.Fatalf("resource allocation should exist: (pod: %#v, container: %s)", tc.pod, tc.pod.Spec.Containers[0].Name)
25812581
}
2582-
assert.Equal(t, tc.expectedPodResourceAllocation[string(tc.pod.UID)][tc.pod.Spec.Containers[0].Name], allocatedResources, tc.name)
2582+
assert.Equal(t, tc.expectedPodResourceAllocation[tc.pod.UID][tc.pod.Spec.Containers[0].Name], allocatedResources, tc.name)
25832583
})
25842584
}
25852585
}
@@ -2949,7 +2949,7 @@ func TestHandlePodResourcesResize(t *testing.T) {
29492949
assert.Equal(t, tt.expectedAllocatedReqs, updatedPodCtr.Resources.Requests, "updated pod spec requests")
29502950
assert.Equal(t, tt.expectedAllocatedLims, updatedPodCtr.Resources.Limits, "updated pod spec limits")
29512951

2952-
alloc, found := kubelet.allocationManager.GetContainerResourceAllocation(string(newPod.UID), updatedPodCtr.Name)
2952+
alloc, found := kubelet.allocationManager.GetContainerResourceAllocation(newPod.UID, updatedPodCtr.Name)
29532953
require.True(t, found, "container allocation")
29542954
assert.Equal(t, tt.expectedAllocatedReqs, alloc.Requests, "stored container request allocation")
29552955
assert.Equal(t, tt.expectedAllocatedLims, alloc.Limits, "stored container limit allocation")

0 commit comments

Comments
 (0)