diff --git a/pkg/neg/syncers/transaction.go b/pkg/neg/syncers/transaction.go index 01a2f38dfc..ebe4d34502 100644 --- a/pkg/neg/syncers/transaction.go +++ b/pkg/neg/syncers/transaction.go @@ -329,11 +329,20 @@ func (s *transactionSyncer) syncInternalImpl() error { // it returns false if the pod: // 1. doesn't exist // 2. is in terminal state +// 3. corresponds to a non-existent node func (s *transactionSyncer) isValidPod(pod *apiv1.Pod) bool { // Terminal Pod means a pod is in PodFailed or PodSucceeded phase if pod.Status.Phase == corev1.PodFailed || pod.Status.Phase == corev1.PodSucceeded { return false } + obj, exists, err := s.nodeLister.GetByKey(pod.Spec.NodeName) + if err != nil || !exists { + return false + } + _, isNode := obj.(*apiv1.Node) + if !isNode { + return false + } return true } diff --git a/pkg/neg/syncers/transaction_test.go b/pkg/neg/syncers/transaction_test.go index 76d13badff..1873c83c3a 100644 --- a/pkg/neg/syncers/transaction_test.go +++ b/pkg/neg/syncers/transaction_test.go @@ -2352,6 +2352,13 @@ func TestIsValidEPBatch(t *testing.T) { func TestIsValidPod(t *testing.T) { t.Parallel() _, ts := newTestTransactionSyncer(negtypes.NewAdapter(gce.NewFakeGCECloud(gce.DefaultTestClusterValues())), negtypes.VmIpPortEndpointType, false, true) + testNode1 := "node1" + testNodeNonExistent := "node-non-existent" + ts.nodeLister.Add(&corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: testNode1, + }, + }) testCases := []struct { desc string @@ -2368,6 +2375,9 @@ func TestIsValidPod(t *testing.T) { Status: corev1.PodStatus{ Phase: corev1.PodRunning, }, + Spec: corev1.PodSpec{ + NodeName: testNode1, + }, }, expect: true, }, @@ -2381,6 +2391,9 @@ func TestIsValidPod(t *testing.T) { Status: corev1.PodStatus{ Phase: corev1.PodFailed, }, + Spec: corev1.PodSpec{ + NodeName: testNode1, + }, }, expect: false, }, @@ -2394,6 +2407,25 @@ func TestIsValidPod(t *testing.T) { Status: corev1.PodStatus{ Phase: corev1.PodSucceeded, }, + Spec: corev1.PodSpec{ + NodeName: testNode1, + }, + }, + expect: false, + }, + { + desc: "pod from non-existent pod", + pod: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: testNamespace, + Name: "pod4", + }, + Status: corev1.PodStatus{ + Phase: corev1.PodSucceeded, + }, + Spec: corev1.PodSpec{ + NodeName: testNodeNonExistent, + }, }, expect: false, },