Skip to content

Commit af9ac32

Browse files
committed
controller sets observedGeneration on pod conditions
1 parent b82260f commit af9ac32

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

pkg/api/v1/pod/util.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,21 @@ func GetPodObservedGenerationIfEnabled(pod *v1.Pod) int64 {
428428
}
429429
return 0
430430
}
431+
432+
// We will emit condition.observedGeneration if the feature is enabled OR if condition.observedGeneration is already set.
433+
// This protects against an infinite loop of kubelet trying to clear the value after the FG is turned off, and
434+
// the API server preserving existing values when an incoming update tries to clear it.
435+
func GetPodObservedGenerationIfEnabledOnCondition(pod *v1.Pod, conditionType v1.PodConditionType) int64 {
436+
if pod == nil {
437+
return 0
438+
}
439+
if utilfeature.DefaultFeatureGate.Enabled(features.PodObservedGenerationTracking) {
440+
return pod.Generation
441+
}
442+
for _, condition := range pod.Status.Conditions {
443+
if condition.Type == conditionType && condition.ObservedGeneration != 0 {
444+
return pod.Generation
445+
}
446+
}
447+
return 0
448+
}

pkg/controller/disruption/disruption.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,8 +787,9 @@ func (dc *DisruptionController) syncStalePodDisruption(ctx context.Context, key
787787

788788
newPod := pod.DeepCopy()
789789
updated := apipod.UpdatePodCondition(&newPod.Status, &v1.PodCondition{
790-
Type: v1.DisruptionTarget,
791-
Status: v1.ConditionFalse,
790+
Type: v1.DisruptionTarget,
791+
ObservedGeneration: apipod.GetPodObservedGenerationIfEnabledOnCondition(newPod, v1.DisruptionTarget),
792+
Status: v1.ConditionFalse,
792793
})
793794
if !updated {
794795
return nil

pkg/controller/podgc/gc_controller.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,11 @@ func (gcc *PodGCController) gcOrphaned(ctx context.Context, pods []*v1.Pod, node
246246
}
247247
logger.V(2).Info("Found orphaned Pod assigned to the Node, deleting", "pod", klog.KObj(pod), "node", klog.KRef("", pod.Spec.NodeName))
248248
condition := &v1.PodCondition{
249-
Type: v1.DisruptionTarget,
250-
Status: v1.ConditionTrue,
251-
Reason: "DeletionByPodGC",
252-
Message: "PodGC: node no longer exists",
249+
Type: v1.DisruptionTarget,
250+
ObservedGeneration: apipod.GetPodObservedGenerationIfEnabledOnCondition(pod, v1.DisruptionTarget),
251+
Status: v1.ConditionTrue,
252+
Reason: "DeletionByPodGC",
253+
Message: "PodGC: node no longer exists",
253254
}
254255
if err := gcc.markFailedAndDeletePodWithCondition(ctx, pod, condition); err != nil {
255256
utilruntime.HandleError(err)

pkg/controller/tainteviction/taint_eviction.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ func addConditionAndDeletePod(ctx context.Context, c clientset.Interface, name,
133133
}
134134
newStatus := pod.Status.DeepCopy()
135135
updated := apipod.UpdatePodCondition(newStatus, &v1.PodCondition{
136-
Type: v1.DisruptionTarget,
137-
Status: v1.ConditionTrue,
138-
Reason: "DeletionByTaintManager",
139-
Message: "Taint manager: deleting due to NoExecute taint",
136+
Type: v1.DisruptionTarget,
137+
ObservedGeneration: apipod.GetPodObservedGenerationIfEnabledOnCondition(pod, v1.DisruptionTarget),
138+
Status: v1.ConditionTrue,
139+
Reason: "DeletionByTaintManager",
140+
Message: "Taint manager: deleting due to NoExecute taint",
140141
})
141142
if updated {
142143
if _, _, _, err := utilpod.PatchPodStatus(ctx, c, pod.Namespace, pod.Name, pod.UID, pod.Status, *newStatus); err != nil {

0 commit comments

Comments
 (0)