Skip to content

Commit

Permalink
Merge pull request #1958 from sawsa307/filter-terminal-pod
Browse files Browse the repository at this point in the history
Filter terminal pods in degraded mode
  • Loading branch information
k8s-ci-robot authored Mar 14, 2023
2 parents 12624df + 530ea5b commit 0479f43
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/neg/syncers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ func validateAndAddEndpoints(ep negtypes.AddressData, zoneGetter negtypes.ZoneGe
}
pod, ok := obj.(*apiv1.Pod)
if !ok {
klog.V(2).Infof("Endpoint %q does not correspond to a pod object. Skipping", address)
continue
}
if !validatePod(pod) {
klog.V(2).Infof("Endpoint %q does not correspond to a valid pod resource. Skipping", address)
continue
}
Expand Down Expand Up @@ -356,6 +360,19 @@ func validateAndAddEndpoints(ep negtypes.AddressData, zoneGetter negtypes.ZoneGe
return dupCount
}

// validatePod checks if this pod is a valid pod resource
// it returns false if the pod:
// 1. is in terminal state
func validatePod(pod *apiv1.Pod) bool {
// Terminal Pod means a pod is in PodFailed or PodSucceeded phase
phase := pod.Status.Phase
if phase == apiv1.PodFailed || phase == apiv1.PodSucceeded {
klog.V(2).Info("Pod %s/%s is a terminal pod with status %v, skipping", pod.ObjectMeta.Namespace, pod.ObjectMeta.Name, phase)
return false
}
return true
}

// retrieveExistingZoneNetworkEndpointMap lists existing network endpoints in the neg and return the zone and endpoints map
func retrieveExistingZoneNetworkEndpointMap(negName string, zoneGetter negtypes.ZoneGetter, cloud negtypes.NetworkEndpointGroupCloud, version meta.Version, mode negtypes.EndpointsCalculatorMode) (map[string]negtypes.NetworkEndpointSet, error) {
// Include zones that have non-candidate nodes currently. It is possible that NEGs were created in those zones previously and the endpoints now became non-candidates.
Expand Down
75 changes: 75 additions & 0 deletions pkg/neg/syncers/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,63 @@ func TestValidateAndAddEndpoints(t *testing.T) {
}
}

func TestValidatePod(t *testing.T) {
t.Parallel()

testCases := []struct {
desc string
pod *v1.Pod
expect bool
}{
{
desc: "a valid pod with phase running",
pod: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace,
Name: "pod1",
},
Status: v1.PodStatus{
Phase: v1.PodRunning,
},
},
expect: true,
},
{
desc: "a terminal pod with phase failed",
pod: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace,
Name: "pod2",
},
Status: v1.PodStatus{
Phase: v1.PodFailed,
},
},
expect: false,
},
{
desc: "a terminal pod with phase succeeded",
pod: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace,
Name: "pod3",
},
Status: v1.PodStatus{
Phase: v1.PodSucceeded,
},
},
expect: false,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
if got := validatePod(tc.pod); got != tc.expect {
t.Errorf("validatePod() = %t, expected %t", got, tc.expect)
}
})
}
}

func addPodsToLister(podLister cache.Indexer) {
// add all pods in default endpoint into podLister
for i := 1; i <= 6; i++ {
Expand All @@ -1421,6 +1478,9 @@ func addPodsToLister(podLister cache.Indexer) {
Namespace: testNamespace,
Name: fmt.Sprintf("pod%v", i),
},
Status: corev1.PodStatus{
Phase: v1.PodRunning,
},
Spec: corev1.PodSpec{
NodeName: testInstance1,
},
Expand All @@ -1432,6 +1492,9 @@ func addPodsToLister(podLister cache.Indexer) {
Namespace: testNamespace,
Name: fmt.Sprintf("pod%v", i),
},
Status: corev1.PodStatus{
Phase: v1.PodRunning,
},
Spec: corev1.PodSpec{
NodeName: testInstance4,
},
Expand All @@ -1443,6 +1506,9 @@ func addPodsToLister(podLister cache.Indexer) {
Namespace: testNamespace,
Name: "pod3",
},
Status: corev1.PodStatus{
Phase: v1.PodRunning,
},
Spec: corev1.PodSpec{
NodeName: testInstance2,
},
Expand All @@ -1452,6 +1518,9 @@ func addPodsToLister(podLister cache.Indexer) {
Namespace: testNamespace,
Name: "pod4",
},
Status: corev1.PodStatus{
Phase: v1.PodRunning,
},
Spec: corev1.PodSpec{
NodeName: testInstance3,
},
Expand All @@ -1461,6 +1530,9 @@ func addPodsToLister(podLister cache.Indexer) {
Namespace: testNamespace,
Name: "pod7",
},
Status: corev1.PodStatus{
Phase: v1.PodRunning,
},
Spec: corev1.PodSpec{
NodeName: testInstance2,
},
Expand All @@ -1470,6 +1542,9 @@ func addPodsToLister(podLister cache.Indexer) {
Namespace: testNamespace,
Name: "pod10",
},
Status: corev1.PodStatus{
Phase: v1.PodRunning,
},
Spec: corev1.PodSpec{
NodeName: testInstance3,
},
Expand Down

0 comments on commit 0479f43

Please sign in to comment.