Skip to content

Commit c58eaae

Browse files
committed
kubelet: Simplify rejected pod update logic
Signed-off-by: Pablo Acevedo Montserrat <pacevedo@redhat.com>
1 parent 2a126dc commit c58eaae

File tree

4 files changed

+33
-47
lines changed

4 files changed

+33
-47
lines changed

pkg/kubelet/kubelet.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,27 +2422,6 @@ func (kl *Kubelet) rejectPod(pod *v1.Pod, reason, message string) {
24222422
Message: PodRejectionMessagePrefix + message})
24232423
}
24242424

2425-
// isLocallyRejected checks if a pod has already been rejected. These pods
2426-
// have never had containers created, have a Failed phase and a specific
2427-
// status that is forced by rejectPod() call. The only distinctive feature
2428-
// they have from other pods is the presence of a specific status: empty
2429-
// and having a message with the prefix PodRejectionMessagePrefix.
2430-
// This function looks for the message in both statusManager (current
2431-
// session rejection) and, if not found, in status from API server (rejection
2432-
// that was already synced to the API in case of a kubelet restart).
2433-
func (kl *Kubelet) isLocallyRejected(pod *v1.Pod) bool {
2434-
localStatus, found := kl.statusManager.GetPodStatus(pod.UID)
2435-
if found && strings.HasPrefix(localStatus.Message, PodRejectionMessagePrefix) {
2436-
return true
2437-
}
2438-
2439-
if strings.HasPrefix(pod.Status.Message, PodRejectionMessagePrefix) {
2440-
return true
2441-
}
2442-
2443-
return false
2444-
}
2445-
24462425
func recordAdmissionRejection(reason string) {
24472426
// It is possible that the "reason" label can have high cardinality.
24482427
// To avoid this metric from exploding, we create an allowlist of known
@@ -2797,15 +2776,14 @@ func (kl *Kubelet) HandlePodUpdates(pods []*v1.Pod) {
27972776
}
27982777

27992778
// Skip rejected pods during UPDATE operations.
2800-
//
2801-
// Pods always arrive as ADD before UPDATE (guaranteed by the config layer).
2802-
// If a pod failed admission in HandlePodAdditions, it was never sent to
2803-
// pod_workers and won't be known to the worker. UPDATE operations for such
2804-
// pods should be ignored because:
2779+
// Pods always arrive as ADD before UPDATE (enforced by the config layer).
2780+
// If a pod failed admission in HandlePodAdditions, it was rejected and
2781+
// never sent to pod_workers. UPDATE operations for such pods should be
2782+
// ignored because:
28052783
// 1. No containers exist - nothing to sync or clean up
28062784
// 2. The pod is already in terminal Failed state
28072785
// 3. REMOVE operation will eventually clean up podManager
2808-
if !kl.podWorkers.IsPodKnownToWorker(pod.UID) {
2786+
if kl.isLocallyRejected(pod) {
28092787
continue
28102788
}
28112789
kl.podWorkers.UpdatePod(UpdatePodOptions{

pkg/kubelet/kubelet_pods.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,34 @@ func (kl *Kubelet) isAdmittedPodTerminal(pod *v1.Pod) bool {
11661166
return false
11671167
}
11681168

1169+
// isLocallyRejected checks if a pod was rejected by this kubelet instance.
1170+
// Rejected pods are pods that failed admission (e.g., insufficient resources,
1171+
// node affinity mismatch). These pods:
1172+
// - Never had containers created
1173+
// - Were marked as Failed with PodRejectionMessagePrefix in the status message
1174+
// - Should never enter the pod_workers pipeline
1175+
//
1176+
// This function checks both the local statusManager cache (for current session
1177+
// rejections) and the pod.Status from API server (for rejections that survived
1178+
// kubelet restart).
1179+
func (kl *Kubelet) isLocallyRejected(pod *v1.Pod) bool {
1180+
// Check local statusManager cache first (current session rejections).
1181+
// This catches pods rejected since the kubelet started.
1182+
localStatus, found := kl.statusManager.GetPodStatus(pod.UID)
1183+
if found && strings.HasPrefix(localStatus.Message, PodRejectionMessagePrefix) {
1184+
return true
1185+
}
1186+
1187+
// Check API server status (rejections from before kubelet restart).
1188+
// When kubelet restarts, the local statusManager is empty, but the rejection
1189+
// status was synced to the API server and is available in pod.Status.
1190+
if strings.HasPrefix(pod.Status.Message, PodRejectionMessagePrefix) {
1191+
return true
1192+
}
1193+
1194+
return false
1195+
}
1196+
11691197
// removeOrphanedPodStatuses removes obsolete entries in podStatus where
11701198
// the pod is no longer considered bound to this node.
11711199
func (kl *Kubelet) removeOrphanedPodStatuses(pods []*v1.Pod, mirrorPods []*v1.Pod) {

pkg/kubelet/pod_workers.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,6 @@ type PodWorkers interface {
183183
// Intended for use by the kubelet config loops, but not subsystems, which should
184184
// use ShouldPod*().
185185
IsPodKnownTerminated(uid types.UID) bool
186-
// IsPodKnownToWorker returns true if the pod has ever been sent to the pod
187-
// worker via UpdatePod (i.e., has an entry in podSyncStatuses). A pod that
188-
// did not enter the pod worker pipeline is a pod that was rejected and
189-
// therefore should not be processed. No containers exist for these pods
190-
// and they are already in a terminal Failed state.
191-
// Intended for use by the kubelet config loops, but not subsystems.
192-
IsPodKnownToWorker(uid types.UID) bool
193186
// CouldHaveRunningContainers returns true before the pod workers have synced,
194187
// once the pod workers see the pod (syncPod could be called), and returns false
195188
// after the pod has been terminated (running containers guaranteed stopped).
@@ -656,13 +649,6 @@ func (p *podWorkers) IsPodKnownTerminated(uid types.UID) bool {
656649
return false
657650
}
658651

659-
func (p *podWorkers) IsPodKnownToWorker(uid types.UID) bool {
660-
p.podLock.Lock()
661-
defer p.podLock.Unlock()
662-
_, ok := p.podSyncStatuses[uid]
663-
return ok
664-
}
665-
666652
func (p *podWorkers) CouldHaveRunningContainers(uid types.UID) bool {
667653
p.podLock.Lock()
668654
defer p.podLock.Unlock()

pkg/kubelet/pod_workers_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,6 @@ func (f *fakePodWorkers) IsPodKnownTerminated(uid types.UID) bool {
102102
defer f.statusLock.Unlock()
103103
return f.terminated[uid]
104104
}
105-
func (f *fakePodWorkers) IsPodKnownToWorker(uid types.UID) bool {
106-
f.statusLock.Lock()
107-
defer f.statusLock.Unlock()
108-
// In tests, a pod is known if it's in any of the tracking maps
109-
return f.running[uid] || f.terminating[uid] || f.terminated[uid]
110-
}
111105
func (f *fakePodWorkers) CouldHaveRunningContainers(uid types.UID) bool {
112106
f.statusLock.Lock()
113107
defer f.statusLock.Unlock()

0 commit comments

Comments
 (0)