Skip to content

Commit 7b25b18

Browse files
authored
Merge pull request #2410 from haircommander/cleanup/allocation-manager-4.20
OCPBUGS-59641: cleanup: fetch individual PodResourceInfo from allocated resource state
2 parents 44e5bf3 + 8246eb4 commit 7b25b18

File tree

5 files changed

+51
-31
lines changed

5 files changed

+51
-31
lines changed

pkg/kubelet/allocation/allocation_manager.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,20 @@ func (m *manager) GetContainerResourceAllocation(podUID types.UID, containerName
109109
// UpdatePodFromAllocation overwrites the pod spec with the allocation.
110110
// This function does a deep copy only if updates are needed.
111111
func (m *manager) UpdatePodFromAllocation(pod *v1.Pod) (*v1.Pod, bool) {
112-
// TODO(tallclair): This clones the whole cache, but we only need 1 pod.
113-
allocs := m.allocated.GetPodResourceInfoMap()
114-
return updatePodFromAllocation(pod, allocs)
112+
if pod == nil {
113+
return pod, false
114+
}
115+
116+
allocated, ok := m.allocated.GetPodResourceInfo(pod.UID)
117+
if !ok {
118+
return pod, false
119+
}
120+
121+
return updatePodFromAllocation(pod, allocated)
115122
}
116123

117-
func updatePodFromAllocation(pod *v1.Pod, allocs state.PodResourceInfoMap) (*v1.Pod, bool) {
118-
allocated, found := allocs[pod.UID]
119-
if !found {
124+
func updatePodFromAllocation(pod *v1.Pod, allocated state.PodResourceInfo) (*v1.Pod, bool) {
125+
if pod == nil {
120126
return pod, false
121127
}
122128

pkg/kubelet/allocation/allocation_manager_test.go

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -103,50 +103,44 @@ func TestUpdatePodFromAllocation(t *testing.T) {
103103
tests := []struct {
104104
name string
105105
pod *v1.Pod
106-
allocs state.PodResourceInfoMap
106+
allocated state.PodResourceInfo
107107
expectPod *v1.Pod
108108
expectUpdate bool
109109
}{{
110110
name: "steady state",
111111
pod: pod,
112-
allocs: state.PodResourceInfoMap{
113-
pod.UID: state.PodResourceInfo{
114-
ContainerResources: map[string]v1.ResourceRequirements{
115-
"c1": *pod.Spec.Containers[0].Resources.DeepCopy(),
116-
"c2": *pod.Spec.Containers[1].Resources.DeepCopy(),
117-
"c1-restartable-init": *pod.Spec.InitContainers[0].Resources.DeepCopy(),
118-
"c1-init": *pod.Spec.InitContainers[1].Resources.DeepCopy(),
119-
},
112+
allocated: state.PodResourceInfo{
113+
ContainerResources: map[string]v1.ResourceRequirements{
114+
"c1": *pod.Spec.Containers[0].Resources.DeepCopy(),
115+
"c2": *pod.Spec.Containers[1].Resources.DeepCopy(),
116+
"c1-restartable-init": *pod.Spec.InitContainers[0].Resources.DeepCopy(),
117+
"c1-init": *pod.Spec.InitContainers[1].Resources.DeepCopy(),
120118
},
121119
},
122120
expectUpdate: false,
123121
}, {
124122
name: "no allocations",
125123
pod: pod,
126-
allocs: state.PodResourceInfoMap{},
124+
allocated: state.PodResourceInfo{},
127125
expectUpdate: false,
128126
}, {
129127
name: "missing container allocation",
130128
pod: pod,
131-
allocs: state.PodResourceInfoMap{
132-
pod.UID: state.PodResourceInfo{
133-
ContainerResources: map[string]v1.ResourceRequirements{
134-
"c2": *pod.Spec.Containers[1].Resources.DeepCopy(),
135-
},
129+
allocated: state.PodResourceInfo{
130+
ContainerResources: map[string]v1.ResourceRequirements{
131+
"c2": *pod.Spec.Containers[1].Resources.DeepCopy(),
136132
},
137133
},
138134
expectUpdate: false,
139135
}, {
140136
name: "resized container",
141137
pod: pod,
142-
allocs: state.PodResourceInfoMap{
143-
pod.UID: state.PodResourceInfo{
144-
ContainerResources: map[string]v1.ResourceRequirements{
145-
"c1": *resizedPod.Spec.Containers[0].Resources.DeepCopy(),
146-
"c2": *resizedPod.Spec.Containers[1].Resources.DeepCopy(),
147-
"c1-restartable-init": *resizedPod.Spec.InitContainers[0].Resources.DeepCopy(),
148-
"c1-init": *resizedPod.Spec.InitContainers[1].Resources.DeepCopy(),
149-
},
138+
allocated: state.PodResourceInfo{
139+
ContainerResources: map[string]v1.ResourceRequirements{
140+
"c1": *resizedPod.Spec.Containers[0].Resources.DeepCopy(),
141+
"c2": *resizedPod.Spec.Containers[1].Resources.DeepCopy(),
142+
"c1-restartable-init": *resizedPod.Spec.InitContainers[0].Resources.DeepCopy(),
143+
"c1-init": *resizedPod.Spec.InitContainers[1].Resources.DeepCopy(),
150144
},
151145
},
152146
expectUpdate: true,
@@ -156,7 +150,7 @@ func TestUpdatePodFromAllocation(t *testing.T) {
156150
for _, test := range tests {
157151
t.Run(test.name, func(t *testing.T) {
158152
pod := test.pod.DeepCopy()
159-
allocatedPod, updated := updatePodFromAllocation(pod, test.allocs)
153+
allocatedPod, updated := updatePodFromAllocation(pod, test.allocated)
160154

161155
if test.expectUpdate {
162156
assert.True(t, updated, "updated")

pkg/kubelet/allocation/state/state.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func (pr PodResourceInfoMap) Clone() PodResourceInfoMap {
5050
type Reader interface {
5151
GetContainerResources(podUID types.UID, containerName string) (v1.ResourceRequirements, bool)
5252
GetPodResourceInfoMap() PodResourceInfoMap
53+
GetPodResourceInfo(podUID types.UID) (PodResourceInfo, bool)
5354
}
5455

5556
type writer interface {

pkg/kubelet/allocation/state/state_checkpoint.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,20 @@ func (sc *stateCheckpoint) GetContainerResources(podUID types.UID, containerName
112112
return sc.cache.GetContainerResources(podUID, containerName)
113113
}
114114

115-
// GetPodResourceInfoMap returns current pod resource information
115+
// GetPodResourceInfoMap returns current pod resource information map
116116
func (sc *stateCheckpoint) GetPodResourceInfoMap() PodResourceInfoMap {
117117
sc.mux.RLock()
118118
defer sc.mux.RUnlock()
119119
return sc.cache.GetPodResourceInfoMap()
120120
}
121121

122+
// GetPodResourceInfo returns current pod resource information
123+
func (sc *stateCheckpoint) GetPodResourceInfo(podUID types.UID) (PodResourceInfo, bool) {
124+
sc.mux.RLock()
125+
defer sc.mux.RUnlock()
126+
return sc.cache.GetPodResourceInfo(podUID)
127+
}
128+
122129
// SetContainerResoruces sets resources information for a pod's container
123130
func (sc *stateCheckpoint) SetContainerResources(podUID types.UID, containerName string, resources v1.ResourceRequirements) error {
124131
sc.mux.Lock()
@@ -172,6 +179,10 @@ func (sc *noopStateCheckpoint) GetPodResourceInfoMap() PodResourceInfoMap {
172179
return nil
173180
}
174181

182+
func (sc *noopStateCheckpoint) GetPodResourceInfo(_ types.UID) (PodResourceInfo, bool) {
183+
return PodResourceInfo{}, false
184+
}
185+
175186
func (sc *noopStateCheckpoint) SetContainerResources(_ types.UID, _ string, _ v1.ResourceRequirements) error {
176187
return nil
177188
}

pkg/kubelet/allocation/state/state_mem.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ func (s *stateMemory) GetPodResourceInfoMap() PodResourceInfoMap {
6565
return s.podResources.Clone()
6666
}
6767

68+
func (s *stateMemory) GetPodResourceInfo(podUID types.UID) (PodResourceInfo, bool) {
69+
s.RLock()
70+
defer s.RUnlock()
71+
72+
resourceInfo, ok := s.podResources[podUID]
73+
return resourceInfo, ok
74+
}
75+
6876
func (s *stateMemory) SetContainerResources(podUID types.UID, containerName string, resources v1.ResourceRequirements) error {
6977
s.Lock()
7078
defer s.Unlock()

0 commit comments

Comments
 (0)