Skip to content

Commit 7fce6f2

Browse files
committed
More comments around PLEG WatchConditions
1 parent 35bd1e6 commit 7fce6f2

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

pkg/kubelet/kuberuntime/kuberuntime_manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,9 @@ func (m *kubeGenericRuntimeManager) updatePodContainerResources(pod *v1.Pod, res
799799
return err
800800
}
801801
resizeKey := fmt.Sprintf("%s:resize:%s", container.Name, resourceName)
802+
803+
// Watch (poll) the container for the expected resources update. Stop watching once the resources
804+
// match the desired values.
802805
resizeCondition := pleg.RunningContainerWatchCondition(container.Name, func(status *kubecontainer.Status) bool {
803806
if status.Resources == nil {
804807
return false
@@ -814,6 +817,7 @@ func (m *kubeGenericRuntimeManager) updatePodContainerResources(pod *v1.Pod, res
814817
}
815818
})
816819
m.runtimeHelper.SetPodWatchCondition(pod.UID, resizeKey, resizeCondition)
820+
817821
// If UpdateContainerResources is error-free, it means desired values for 'resourceName' was accepted by runtime.
818822
// So we update currentContainerResources for 'resourceName', which is our view of most recently configured resources.
819823
// Note: We can't rely on GetPodStatus as runtime may lag in actuating the resource values it just accepted.

pkg/kubelet/pleg/generic.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,15 @@ func (g *GenericPLEG) Relist() {
314314
var completedConditions []versionedWatchCondition
315315
for _, condition := range watchConditions {
316316
if condition.condition(status) {
317+
// condition was met: add it to the list of completed conditions.
317318
completedConditions = append(completedConditions, condition)
318319
}
319320
}
320321
if len(completedConditions) > 0 {
321322
g.completeWatchConditions(pid, completedConditions)
323+
// If at least 1 condition completed, emit a ConditionMet event to trigger a pod sync.
324+
// We only emit 1 event even if multiple conditions are met, since SyncPod reevaluates
325+
// all containers in the pod with the latest status.
322326
events = append(events, &PodLifecycleEvent{ID: pid, Type: ConditionMet})
323327
}
324328

@@ -484,24 +488,25 @@ func (g *GenericPLEG) updateCache(ctx context.Context, pod *kubecontainer.Pod, p
484488
return status, g.cache.Set(pod.ID, status, err, timestamp), err
485489
}
486490

491+
// SetPodWatchCondition flags the pod for reinspection on every Relist iteration until the watch
492+
// condition is met. The condition is keyed so it can be updated before the condition
493+
// is met.
487494
func (g *GenericPLEG) SetPodWatchCondition(podUID types.UID, conditionKey string, condition WatchCondition) {
488495
g.watchConditionsLock.Lock()
489496
defer g.watchConditionsLock.Unlock()
490497

491498
conditions, ok := g.watchConditions[podUID]
492499
if !ok {
493-
if condition == nil {
494-
return // Condition isn't set, nothing to do.
495-
}
496500
conditions = make(map[string]versionedWatchCondition)
497501
}
498502

499503
versioned, found := conditions[conditionKey]
500504
if found {
505+
// Watch condition was already set. Increment its version & update the condition function.
501506
versioned.version++
502507
versioned.condition = condition
503508
conditions[conditionKey] = versioned
504-
} else if condition != nil {
509+
} else {
505510
conditions[conditionKey] = versionedWatchCondition{
506511
key: conditionKey,
507512
condition: condition,
@@ -516,19 +521,22 @@ func (g *GenericPLEG) getPodWatchConditions(podUID types.UID) []versionedWatchCo
516521
g.watchConditionsLock.Lock()
517522
defer g.watchConditionsLock.Unlock()
518523

519-
conditions, ok := g.watchConditions[podUID]
524+
podConditions, ok := g.watchConditions[podUID]
520525
if !ok {
521526
return nil
522527
}
523528

524-
filtered := make([]versionedWatchCondition, 0, len(conditions))
525-
for _, condition := range conditions {
526-
filtered = append(filtered, condition)
529+
// Flatten the map into a list of conditions. This also serves to create a copy, so the lock can
530+
// be released.
531+
conditions := make([]versionedWatchCondition, 0, len(podConditions))
532+
for _, condition := range podConditions {
533+
conditions = append(conditions, condition)
527534
}
528-
return filtered
535+
return conditions
529536
}
530537

531-
// completeWatchConditions clears the completed watch conditions.
538+
// completeWatchConditions removes the completed watch conditions, unless they have been updated
539+
// since the condition was checked.
532540
func (g *GenericPLEG) completeWatchConditions(podUID types.UID, completedConditions []versionedWatchCondition) {
533541
g.watchConditionsLock.Lock()
534542
defer g.watchConditionsLock.Unlock()
@@ -549,6 +557,8 @@ func (g *GenericPLEG) completeWatchConditions(podUID types.UID, completedConditi
549557
g.watchConditions[podUID] = conditions
550558
}
551559

560+
// cleanupOrphanedWatchConditions purges the watchConditions map of any pods that were removed from
561+
// the pod records. Events are not emitted for removed pods.
552562
func (g *GenericPLEG) cleanupOrphanedWatchConditions() {
553563
g.watchConditionsLock.Lock()
554564
defer g.watchConditionsLock.Unlock()

0 commit comments

Comments
 (0)