Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5c219a7

Browse files
authored
Merge pull request#130387 from shiya0705/Pod_Resize_complete_event
[FG:InPlacePodVerticalScaling] Add Pod resize complete event
2 parents7fa6cdd +2256f57 commit5c219a7

File tree

4 files changed

+145
-21
lines changed

4 files changed

+145
-21
lines changed

‎pkg/kubelet/allocation/allocation_manager.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ import (
2424
"sync"
2525
"time"
2626

27+
"encoding/json"
2728
v1"k8s.io/api/core/v1"
2829
apiequality"k8s.io/apimachinery/pkg/api/equality"
2930
metav1"k8s.io/apimachinery/pkg/apis/meta/v1"
3031
"k8s.io/apimachinery/pkg/types"
3132
"k8s.io/apimachinery/pkg/util/sets"
3233
utilfeature"k8s.io/apiserver/pkg/util/feature"
34+
"k8s.io/client-go/tools/record"
3335
resourcehelper"k8s.io/component-helpers/resource"
3436
"k8s.io/klog/v2"
3537
podutil"k8s.io/kubernetes/pkg/api/v1/pod"
@@ -40,6 +42,7 @@ import (
4042
"k8s.io/kubernetes/pkg/kubelet/cm"
4143
"k8s.io/kubernetes/pkg/kubelet/config"
4244
kubecontainer"k8s.io/kubernetes/pkg/kubelet/container"
45+
"k8s.io/kubernetes/pkg/kubelet/events"
4346
"k8s.io/kubernetes/pkg/kubelet/lifecycle"
4447
"k8s.io/kubernetes/pkg/kubelet/status"
4548
kubetypes"k8s.io/kubernetes/pkg/kubelet/types"
@@ -129,6 +132,8 @@ type manager struct {
129132

130133
allocationMutex sync.Mutex
131134
podsWithPendingResizes []types.UID
135+
136+
recorder record.EventRecorder
132137
}
133138

134139
funcNewManager(checkpointDirectorystring,
@@ -138,6 +143,7 @@ func NewManager(checkpointDirectory string,
138143
getActivePodsfunc() []*v1.Pod,
139144
getPodByUIDfunc(types.UID) (*v1.Pod,bool),
140145
sourcesReady config.SourcesReady,
146+
recorder record.EventRecorder,
141147
)Manager {
142148
return&manager{
143149
allocated:newStateImpl(checkpointDirectory,allocatedPodsStateFile),
@@ -152,9 +158,21 @@ func NewManager(checkpointDirectory string,
152158
triggerPodSync:triggerPodSync,
153159
getActivePods:getActivePods,
154160
getPodByUID:getPodByUID,
161+
recorder:recorder,
155162
}
156163
}
157164

165+
typecontainerAllocationstruct {
166+
Namestring`json:"name"`
167+
Resources v1.ResourceRequirements`json:"resources,omitempty"`
168+
}
169+
170+
typepodResourceSummarystruct {
171+
//TODO: resources v1.ResourceRequirements, add pod-level resources here once resizing pod-level resources is supported
172+
InitContainers []containerAllocation`json:"initContainers,omitempty"`
173+
Containers []containerAllocation`json:"containers,omitempty"`
174+
}
175+
158176
funcnewStateImpl(checkpointDirectory,checkpointNamestring) state.State {
159177
if!utilfeature.DefaultFeatureGate.Enabled(features.InPlacePodVerticalScaling) {
160178
returnstate.NewNoopStateCheckpoint()
@@ -214,6 +232,33 @@ func (m *manager) Run(ctx context.Context) {
214232
}()
215233
}
216234

235+
// Gernerate pod resize completed event message
236+
func (m*manager)podResizeCompletionMsg(allocatedPod*v1.Pod)string {
237+
podResizeSource:=&podResourceSummary{}
238+
podutil.VisitContainers(&allocatedPod.Spec,podutil.InitContainers|podutil.Containers,
239+
func(allocatedContainer*v1.Container,containerType podutil.ContainerType)bool {
240+
allocation:=containerAllocation{
241+
Name:allocatedContainer.Name,
242+
Resources:allocatedContainer.Resources,
243+
}
244+
switchcontainerType {
245+
casepodutil.InitContainers:
246+
podResizeSource.InitContainers=append(podResizeSource.InitContainers,allocation)
247+
casepodutil.Containers:
248+
podResizeSource.Containers=append(podResizeSource.Containers,allocation)
249+
}
250+
returntrue
251+
})
252+
253+
podResizeMsgDetailsJSON,err:=json.Marshal(podResizeSource)
254+
iferr!=nil {
255+
klog.ErrorS(err,"Failed to serialize resource summary","pod",format.Pod(allocatedPod))
256+
return"Pod resize completed"
257+
}
258+
podResizeCompletedMsg:=fmt.Sprintf("Pod resize completed: %s",string(podResizeMsgDetailsJSON))
259+
returnpodResizeCompletedMsg
260+
}
261+
217262
func (m*manager)RetryPendingResizes() []*v1.Pod {
218263
m.allocationMutex.Lock()
219264
deferm.allocationMutex.Unlock()
@@ -709,6 +754,11 @@ func (m *manager) CheckPodResizeInProgress(allocatedPod *v1.Pod, podStatus *kube
709754
m.statusManager.SetPodResizeInProgressCondition(allocatedPod.UID,"","",allocatedPod.Generation)
710755
}elseifm.statusManager.ClearPodResizeInProgressCondition(allocatedPod.UID) {
711756
// (Allocated == Actual) => clear the resize in-progress status.
757+
// Generate Pod resize completed event
758+
podResizeCompletedEventMsg:=m.podResizeCompletionMsg(allocatedPod)
759+
ifm.recorder!=nil {
760+
m.recorder.Eventf(allocatedPod,v1.EventTypeNormal,events.ResizeCompleted,podResizeCompletedEventMsg)
761+
}
712762
// TODO(natasha41575): We only need to make this call if any of the resources were decreased.
713763
m.RetryPendingResizes()
714764
}

‎pkg/kubelet/allocation/allocation_manager_test.go

Lines changed: 93 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"k8s.io/apimachinery/pkg/util/sets"
3232
utilfeature"k8s.io/apiserver/pkg/util/feature"
3333
"k8s.io/client-go/kubernetes/fake"
34+
"k8s.io/client-go/tools/record"
3435
featuregatetesting"k8s.io/component-base/featuregate/testing"
3536
"k8s.io/kubernetes/pkg/features"
3637
"k8s.io/kubernetes/pkg/kubelet/allocation/state"
@@ -185,7 +186,16 @@ func TestUpdatePodFromAllocation(t *testing.T) {
185186
}
186187
}
187188

188-
funcTestIsPodResizeInProgress(t*testing.T) {
189+
funcgetEventsFromFakeRecorder(t*testing.T,amManager)string {
190+
select {
191+
casee:=<-am.(*manager).recorder.(*record.FakeRecorder).Events:
192+
returne
193+
default:
194+
return""
195+
}
196+
}
197+
198+
funcTestCheckPodResizeInProgress(t*testing.T) {
189199
typetestResourcesstruct {
190200
cpuReq,cpuLim,memReq,memLimint64
191201
}
@@ -198,72 +208,83 @@ func TestIsPodResizeInProgress(t *testing.T) {
198208
}
199209

200210
tests:= []struct {
201-
namestring
202-
containers []testContainer
203-
expectHasResizebool
211+
namestring
212+
containers []testContainer
213+
oldPodResizeInProgressConditionbool
214+
expectHasResizebool
215+
expectPodResizeCompletedMsgstring
204216
}{{
205217
name:"simple running container",
206218
containers: []testContainer{{
207219
allocated:testResources{100,100,100,100},
208220
actuated:&testResources{100,100,100,100},
209221
isRunning:true,
210222
}},
211-
expectHasResize:false,
223+
oldPodResizeInProgressCondition:true,
224+
expectHasResize:false,
225+
expectPodResizeCompletedMsg:`Normal ResizeCompleted Pod resize completed: {"containers":[{"name":"c0","resources":{"limits":{"cpu":"100m","memory":"100"},"requests":{"cpu":"100m","memory":"100"}}}]}`,
212226
}, {
213227
name:"simple unstarted container",
214228
containers: []testContainer{{
215229
allocated:testResources{100,100,100,100},
216230
unstarted:true,
217231
}},
218-
expectHasResize:false,
232+
oldPodResizeInProgressCondition:false,
233+
expectHasResize:false,
219234
}, {
220235
name:"simple resized container/cpu req",
221236
containers: []testContainer{{
222237
allocated:testResources{100,200,100,200},
223238
actuated:&testResources{150,200,100,200},
224239
isRunning:true,
225240
}},
226-
expectHasResize:true,
241+
oldPodResizeInProgressCondition:false,
242+
expectHasResize:true,
227243
}, {
228244
name:"simple resized container/cpu limit",
229245
containers: []testContainer{{
230246
allocated:testResources{100,200,100,200},
231247
actuated:&testResources{100,300,100,200},
232248
isRunning:true,
233249
}},
234-
expectHasResize:true,
250+
oldPodResizeInProgressCondition:false,
251+
expectHasResize:true,
235252
}, {
236253
name:"simple resized container/mem req",
237254
containers: []testContainer{{
238255
allocated:testResources{100,200,100,200},
239256
actuated:&testResources{100,200,150,200},
240257
isRunning:true,
241258
}},
242-
expectHasResize:true,
259+
oldPodResizeInProgressCondition:false,
260+
expectHasResize:true,
243261
}, {
244262
name:"simple resized container/cpu+mem req",
245263
containers: []testContainer{{
246264
allocated:testResources{100,200,100,200},
247265
actuated:&testResources{150,200,150,200},
248266
isRunning:true,
249267
}},
250-
expectHasResize:true,
268+
oldPodResizeInProgressCondition:false,
269+
expectHasResize:true,
251270
}, {
252271
name:"simple resized container/mem limit",
253272
containers: []testContainer{{
254273
allocated:testResources{100,200,100,200},
255274
actuated:&testResources{100,200,100,300},
256275
isRunning:true,
257276
}},
258-
expectHasResize:true,
277+
oldPodResizeInProgressCondition:false,
278+
expectHasResize:true,
259279
}, {
260280
name:"terminated resized container",
261281
containers: []testContainer{{
262282
allocated:testResources{100,200,100,200},
263283
actuated:&testResources{200,200,100,200},
264284
isRunning:false,
265285
}},
266-
expectHasResize:false,
286+
oldPodResizeInProgressCondition:false,
287+
expectHasResize:false,
267288
}, {
268289
name:"non-sidecar init container",
269290
containers: []testContainer{{
@@ -275,7 +296,8 @@ func TestIsPodResizeInProgress(t *testing.T) {
275296
actuated:&testResources{100,200,100,200},
276297
isRunning:true,
277298
}},
278-
expectHasResize:false,
299+
oldPodResizeInProgressCondition:false,
300+
expectHasResize:false,
279301
}, {
280302
name:"non-resized sidecar",
281303
containers: []testContainer{{
@@ -288,7 +310,8 @@ func TestIsPodResizeInProgress(t *testing.T) {
288310
actuated:&testResources{100,200,100,200},
289311
isRunning:true,
290312
}},
291-
expectHasResize:false,
313+
oldPodResizeInProgressCondition:false,
314+
expectHasResize:false,
292315
}, {
293316
name:"resized sidecar",
294317
containers: []testContainer{{
@@ -301,7 +324,8 @@ func TestIsPodResizeInProgress(t *testing.T) {
301324
actuated:&testResources{100,200,100,200},
302325
isRunning:true,
303326
}},
304-
expectHasResize:true,
327+
oldPodResizeInProgressCondition:false,
328+
expectHasResize:true,
305329
}, {
306330
name:"several containers and a resize",
307331
containers: []testContainer{{
@@ -320,31 +344,55 @@ func TestIsPodResizeInProgress(t *testing.T) {
320344
actuated:&testResources{200,200,100,200},// Resized
321345
isRunning:true,
322346
}},
323-
expectHasResize:true,
347+
oldPodResizeInProgressCondition:false,
348+
expectHasResize:true,
349+
}, {
350+
name:"several containers",
351+
containers: []testContainer{{
352+
allocated:testResources{cpuReq:100,cpuLim:200},
353+
actuated:&testResources{cpuReq:100,cpuLim:200},
354+
sidecar:true,
355+
isRunning:true,
356+
}, {
357+
allocated:testResources{memReq:100,memLim:200},
358+
actuated:&testResources{memReq:100,memLim:200},
359+
isRunning:true,
360+
}, {
361+
allocated:testResources{cpuReq:200,memReq:100},
362+
actuated:&testResources{cpuReq:200,memReq:100},
363+
isRunning:true,
364+
}},
365+
oldPodResizeInProgressCondition:true,
366+
expectHasResize:false,
367+
expectPodResizeCompletedMsg:`Normal ResizeCompleted Pod resize completed: {"initContainers":[{"name":"c0","resources":{"limits":{"cpu":"200m"},"requests":{"cpu":"100m"}}}],"containers":[{"name":"c1","resources":{"limits":{"memory":"200"},"requests":{"memory":"100"}}},{"name":"c2","resources":{"requests":{"cpu":"200m","memory":"100"}}}]}`,
324368
}, {
325369
name:"best-effort pod",
326370
containers: []testContainer{{
327371
allocated:testResources{},
328372
actuated:&testResources{},
329373
isRunning:true,
330374
}},
331-
expectHasResize:false,
375+
oldPodResizeInProgressCondition:true,
376+
expectHasResize:false,
377+
expectPodResizeCompletedMsg:`Normal ResizeCompleted Pod resize completed: {"containers":[{"name":"c0","resources":{}}]}`,
332378
}, {
333379
name:"burstable pod/not resizing",
334380
containers: []testContainer{{
335381
allocated:testResources{cpuReq:100},
336382
actuated:&testResources{cpuReq:100},
337383
isRunning:true,
338384
}},
339-
expectHasResize:false,
385+
oldPodResizeInProgressCondition:false,
386+
expectHasResize:false,
340387
}, {
341388
name:"burstable pod/resized",
342389
containers: []testContainer{{
343390
allocated:testResources{cpuReq:100},
344391
actuated:&testResources{cpuReq:500},
345392
isRunning:true,
346393
}},
347-
expectHasResize:true,
394+
oldPodResizeInProgressCondition:false,
395+
expectHasResize:true,
348396
}}
349397

350398
mkRequirements:=func(rtestResources) v1.ResourceRequirements {
@@ -431,8 +479,32 @@ func TestIsPodResizeInProgress(t *testing.T) {
431479
}
432480
require.NoError(t,am.SetAllocatedResources(pod))
433481

434-
hasResizedResources:=am.(*manager).isPodResizeInProgress(pod,podStatus)
435-
require.Equal(t,test.expectHasResize,hasResizedResources,"hasResizedResources")
482+
am.(*manager).recorder=record.NewFakeRecorder(200)
483+
484+
// Set old Pod condition as Inprogress, so that ClearPodResizeInProgressCondition is true and emit resize completed event
485+
iftest.oldPodResizeInProgressCondition {
486+
am.(*manager).statusManager.SetPodResizeInProgressCondition(pod.UID,"","",int64(1))
487+
}
488+
489+
am.CheckPodResizeInProgress(pod,podStatus)
490+
491+
// Verify pod resize completed event is emitted
492+
podResizeCompletionEvent:=getEventsFromFakeRecorder(t,am)
493+
assert.Equal(t,test.expectPodResizeCompletedMsg,podResizeCompletionEvent)
494+
495+
iftest.expectHasResize {
496+
// Verify the status manager has the InProgress condition set on it
497+
gotResizeConditions:=am.(*manager).statusManager.GetPodResizeConditions(pod.UID)
498+
for_,c:=rangegotResizeConditions {
499+
require.Equal(t,v1.PodResizeInProgress,c.Type,"ResizeConditions Type should be PodResizeInProgress")
500+
require.Empty(t,c.Reason,"ResizeConditions Error")
501+
require.Empty(t,c.Message,"ResizeConditions Message")
502+
}
503+
}else {
504+
// Verify pod resize Inprogress condition is cleared
505+
gotResizeConditions:=am.(*manager).statusManager.GetPodResizeConditions(pod.UID)
506+
require.Empty(t,gotResizeConditions,"ResizeConditions Error")
507+
}
436508
})
437509
}
438510
}

‎pkg/kubelet/events/event.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const (
3636
NetworkNotReady="NetworkNotReady"
3737
ResizeDeferred="ResizeDeferred"
3838
ResizeInfeasible="ResizeInfeasible"
39+
ResizeCompleted="ResizeCompleted"
3940
)
4041

4142
// Image event reason list

‎pkg/kubelet/kubelet.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,7 @@ func NewMainKubelet(kubeCfg *kubeletconfiginternal.KubeletConfiguration,
686686
klet.GetActivePods,
687687
klet.podManager.GetPodByUID,
688688
klet.sourcesReady,
689+
kubeDeps.Recorder,
689690
)
690691

691692
klet.resourceAnalyzer=serverstats.NewResourceAnalyzer(klet,kubeCfg.VolumeStatsAggPeriod.Duration,kubeDeps.Recorder)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp