Skip to content

Commit 869b5ab

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#55841 from ConnorDoyle/cpuman-file-state-for-none-policy
Automatic merge from submit-queue (batch tested with PRs 55841, 55948, 55945). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. CPU Manager: file state for all policies **What this PR does / why we need it**: Before this change, the new file-backed state was only enabled for the static CPU manager policy. This patch enables persistent state for all policies. This PR fixes kubernetes#55736 and the potential CPU resource leak described in that issue. **Release note**: ```release-note NONE ``` /kind bug /sig node /assign @balajismaniam
2 parents c60b35b + c95ee34 commit 869b5ab

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

pkg/kubelet/cm/cpumanager/cpu_manager.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,11 @@ func NewManager(
106106
stateFileDirecory string,
107107
) (Manager, error) {
108108
var policy Policy
109-
var stateHandle state.State
110109

111110
switch policyName(cpuPolicyName) {
112111

113112
case PolicyNone:
114113
policy = NewNonePolicy()
115-
stateHandle = state.NewMemoryState()
116114

117115
case PolicyStatic:
118116
topo, err := topology.Discover(machineInfo)
@@ -141,18 +139,20 @@ func NewManager(
141139
reservedCPUsFloat := float64(reservedCPUs.MilliValue()) / 1000
142140
numReservedCPUs := int(math.Ceil(reservedCPUsFloat))
143141
policy = NewStaticPolicy(topo, numReservedCPUs)
144-
stateHandle = state.NewFileState(path.Join(stateFileDirecory, CPUManagerStateFileName), policy.Name())
145142

146143
default:
147144
glog.Errorf("[cpumanager] Unknown policy \"%s\", falling back to default policy \"%s\"", cpuPolicyName, PolicyNone)
148145
policy = NewNonePolicy()
149-
stateHandle = state.NewMemoryState()
150146
}
151147

148+
stateImpl := state.NewFileState(
149+
path.Join(stateFileDirecory, CPUManagerStateFileName),
150+
policy.Name())
151+
152152
manager := &manager{
153153
policy: policy,
154154
reconcilePeriod: reconcilePeriod,
155-
state: stateHandle,
155+
state: stateImpl,
156156
machineInfo: machineInfo,
157157
nodeAllocatableReservation: nodeAllocatableReservation,
158158
}

pkg/kubelet/cm/cpumanager/state/state_file_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,14 @@ func TestFileStateTryRestore(t *testing.T) {
7575
testCases := []struct {
7676
description string
7777
stateFileContent string
78+
policyName string
7879
expErr string
7980
expectedState *stateMemory
8081
}{
8182
{
8283
"Invalid JSON - empty file",
8384
"\n",
85+
"none",
8486
"state file: could not unmarshal, corrupted state file",
8587
&stateMemory{
8688
assignments: ContainerCPUAssignments{},
@@ -90,6 +92,7 @@ func TestFileStateTryRestore(t *testing.T) {
9092
{
9193
"Invalid JSON - invalid content",
9294
"{",
95+
"none",
9396
"state file: could not unmarshal, corrupted state file",
9497
&stateMemory{
9598
assignments: ContainerCPUAssignments{},
@@ -99,6 +102,7 @@ func TestFileStateTryRestore(t *testing.T) {
99102
{
100103
"Try restore defaultCPUSet only",
101104
`{"policyName": "none", "defaultCpuSet": "4-6"}`,
105+
"none",
102106
"",
103107
&stateMemory{
104108
assignments: ContainerCPUAssignments{},
@@ -108,6 +112,7 @@ func TestFileStateTryRestore(t *testing.T) {
108112
{
109113
"Try restore defaultCPUSet only - invalid name",
110114
`{"policyName": "none", "defaultCpuSet" "4-6"}`,
115+
"none",
111116
"",
112117
&stateMemory{
113118
assignments: ContainerCPUAssignments{},
@@ -123,6 +128,7 @@ func TestFileStateTryRestore(t *testing.T) {
123128
"container2": "1-3"
124129
}
125130
}`,
131+
"none",
126132
"",
127133
&stateMemory{
128134
assignments: ContainerCPUAssignments{
@@ -132,9 +138,24 @@ func TestFileStateTryRestore(t *testing.T) {
132138
defaultCPUSet: cpuset.NewCPUSet(),
133139
},
134140
},
141+
{
142+
"Try restore invalid policy name",
143+
`{
144+
"policyName": "A",
145+
"defaultCpuSet": "0-7",
146+
"entries": {}
147+
}`,
148+
"B",
149+
"policy configured \"B\" != policy from state file \"A\"",
150+
&stateMemory{
151+
assignments: ContainerCPUAssignments{},
152+
defaultCPUSet: cpuset.NewCPUSet(),
153+
},
154+
},
135155
{
136156
"Try restore invalid assignments",
137157
`{"entries": }`,
158+
"none",
138159
"state file: could not unmarshal, corrupted state file",
139160
&stateMemory{
140161
assignments: ContainerCPUAssignments{},
@@ -151,6 +172,7 @@ func TestFileStateTryRestore(t *testing.T) {
151172
"container2": "1-3"
152173
}
153174
}`,
175+
"none",
154176
"",
155177
&stateMemory{
156178
assignments: ContainerCPUAssignments{
@@ -166,6 +188,7 @@ func TestFileStateTryRestore(t *testing.T) {
166188
"policyName": "none",
167189
"defaultCpuSet": "2-sd"
168190
}`,
191+
"none",
169192
"state file: could not parse state file",
170193
&stateMemory{
171194
assignments: ContainerCPUAssignments{},
@@ -182,6 +205,7 @@ func TestFileStateTryRestore(t *testing.T) {
182205
"container2": "1-3"
183206
}
184207
}`,
208+
"none",
185209
"state file: could not parse state file",
186210
&stateMemory{
187211
assignments: ContainerCPUAssignments{},
@@ -191,6 +215,7 @@ func TestFileStateTryRestore(t *testing.T) {
191215
{
192216
"TryRestoreState creates empty state file",
193217
"",
218+
"none",
194219
"",
195220
&stateMemory{
196221
assignments: ContainerCPUAssignments{},
@@ -214,7 +239,7 @@ func TestFileStateTryRestore(t *testing.T) {
214239
defer os.Remove(sfilePath.Name())
215240

216241
logData, fileState := stderrCapture(t, func() State {
217-
return NewFileState(sfilePath.Name(), "none")
242+
return NewFileState(sfilePath.Name(), tc.policyName)
218243
})
219244

220245
if tc.expErr != "" {

0 commit comments

Comments
 (0)