Skip to content

Commit

Permalink
Exclude terminal Pods from group members
Browse files Browse the repository at this point in the history
When calculating AddressGroups, terminal Pods should be excluded because
their IPs can be recycled and reused by other active Pods.

When calculating AppliedToGroups and EgressGroups, terminal Pods could
be excluded as their network resources including network interfaces have
been deleted.

Signed-off-by: Quan Tian <quan.tian@broadcom.com>
  • Loading branch information
tnqn committed Jul 8, 2024
1 parent 0d6f201 commit 5d8b991
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
5 changes: 3 additions & 2 deletions pkg/controller/egress/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"k8s.io/client-go/util/retry"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
podutil "k8s.io/kubernetes/pkg/api/v1/pod"

"antrea.io/antrea/pkg/apis/controlplane"
egressv1beta1 "antrea.io/antrea/pkg/apis/crd/v1beta1"
Expand Down Expand Up @@ -371,9 +372,9 @@ func (c *EgressController) syncEgress(key string) error {
egressGroup := egressGroupObj.(*antreatypes.EgressGroup)
pods, _ := c.groupingInterface.GetEntities(egressGroupType, key)
for _, pod := range pods {
// Ignore Pod if it's not scheduled or not running. And Egress does not support HostNetwork Pods, so also ignore
// Ignore Pod if it's not scheduled or is already terminated. And Egress does not support HostNetwork Pods, so also ignore
// Pod if it's HostNetwork Pod.
if pod.Spec.NodeName == "" || pod.Spec.HostNetwork {
if pod.Spec.NodeName == "" || pod.Spec.HostNetwork || podutil.IsPodTerminal(pod) {
continue
}
podNum++
Expand Down
7 changes: 6 additions & 1 deletion pkg/controller/egress/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ func newController(objects, crdObjects []runtime.Object) *egressController {
}

func TestAddEgress(t *testing.T) {
podSucceeded := newPod("default", "succeeded-pod", map[string]string{"app": "foo"}, node1, "1.1.5.1", false)
podSucceeded.Status.Phase = v1.PodSucceeded
podFailed := newPod("default", "failed-pod", map[string]string{"app": "foo"}, node1, "1.1.5.2", false)
podFailed.Status.Phase = v1.PodFailed

tests := []struct {
name string
inputEgress *v1beta1.Egress
Expand Down Expand Up @@ -347,7 +352,7 @@ func TestAddEgress(t *testing.T) {
defer close(stopCh)
var fakeObjects []runtime.Object
fakeObjects = append(fakeObjects, nsDefault, nsOther)
fakeObjects = append(fakeObjects, podFoo1, podFoo2, podBar1, podFoo1InOtherNamespace, podUnscheduled, podNonIP, podWithHostNetwork)
fakeObjects = append(fakeObjects, podFoo1, podFoo2, podBar1, podFoo1InOtherNamespace, podUnscheduled, podNonIP, podWithHostNetwork, podSucceeded, podFailed)
var fakeCRDObjects []runtime.Object
fakeCRDObjects = append(fakeCRDObjects, eipFoo1)
controller := newController(fakeObjects, fakeCRDObjects)
Expand Down
11 changes: 6 additions & 5 deletions pkg/controller/networkpolicy/networkpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
podutil "k8s.io/kubernetes/pkg/api/v1/pod"

Check failure on line 46 in pkg/controller/networkpolicy/networkpolicy_controller.go

View workflow job for this annotation

GitHub Actions / Analyze on Linux (go)

missing go.sum entry for module providing package k8s.io/kubernetes/pkg/api/v1/pod (imported by antrea.io/antrea/pkg/controller/egress); to add:

Check failure on line 46 in pkg/controller/networkpolicy/networkpolicy_controller.go

View workflow job for this annotation

GitHub Actions / Analyze on Windows (go)

missing go.sum entry for module providing package k8s.io/kubernetes/pkg/api/v1/pod (imported by antrea.io/antrea/pkg/controller/egress); to add:

Check failure on line 46 in pkg/controller/networkpolicy/networkpolicy_controller.go

View workflow job for this annotation

GitHub Actions / golicense

missing go.sum entry for module providing package k8s.io/kubernetes/pkg/api/v1/pod (imported by antrea.io/antrea/pkg/controller/egress); to add:

Check failure on line 46 in pkg/controller/networkpolicy/networkpolicy_controller.go

View workflow job for this annotation

GitHub Actions / Build Antrea and antctl binaries

missing go.sum entry for module providing package k8s.io/kubernetes/pkg/api/v1/pod (imported by antrea.io/antrea/pkg/controller/egress); to add:

Check failure on line 46 in pkg/controller/networkpolicy/networkpolicy_controller.go

View workflow job for this annotation

GitHub Actions / Unit test (ubuntu-latest)

missing go.sum entry for module providing package k8s.io/kubernetes/pkg/api/v1/pod (imported by antrea.io/antrea/pkg/controller/egress); to add:

Check failure on line 46 in pkg/controller/networkpolicy/networkpolicy_controller.go

View workflow job for this annotation

GitHub Actions / Unit test (windows-2022)

missing go.sum entry for module providing package k8s.io/kubernetes/pkg/api/v1/pod (imported by antrea.io/antrea/pkg/controller/egress); to add:

Check failure on line 46 in pkg/controller/networkpolicy/networkpolicy_controller.go

View workflow job for this annotation

GitHub Actions / Build Antrea Windows binaries

missing go.sum entry for module providing package k8s.io/kubernetes/pkg/api/v1/pod (imported by antrea.io/antrea/pkg/controller/egress); to add:

Check failure on line 46 in pkg/controller/networkpolicy/networkpolicy_controller.go

View workflow job for this annotation

GitHub Actions / Go benchmark test

missing go.sum entry for module providing package k8s.io/kubernetes/pkg/api/v1/pod (imported by antrea.io/antrea/pkg/controller/egress); to add:
policyinformers "sigs.k8s.io/network-policy-api/pkg/client/informers/externalversions/apis/v1alpha1"
policylisters "sigs.k8s.io/network-policy-api/pkg/client/listers/apis/v1alpha1"

Expand Down Expand Up @@ -1184,9 +1185,9 @@ func (n *NetworkPolicyController) getMemberSetForGroupType(groupType grouping.Gr
groupMemberSet := controlplane.GroupMemberSet{}
pods, externalEntities := n.groupingInterface.GetEntities(groupType, name)
for _, pod := range pods {
// HostNetwork Pods should be excluded from group members
// https://github.com/antrea-io/antrea/issues/3078
if pod.Spec.HostNetwork == true || len(pod.Status.PodIPs) == 0 {
// HostNetwork Pods should be excluded from group members: https://github.com/antrea-io/antrea/issues/3078.
// Terminated Pods should be excluded as their IPs can be recycled and used by other Pods.
if pod.Spec.HostNetwork || podutil.IsPodTerminal(pod) || len(pod.Status.PodIPs) == 0 {
continue
}
groupMemberSet.Insert(podToGroupMember(pod, true))
Expand Down Expand Up @@ -1329,8 +1330,8 @@ func (n *NetworkPolicyController) syncAppliedToGroup(key string) error {
} else {
scheduledPodNum, scheduledExtEntityNum := 0, 0
for _, pod := range pods {
if pod.Spec.NodeName == "" || pod.Spec.HostNetwork == true {
// No need to process Pod when it's not scheduled.
if pod.Spec.NodeName == "" || pod.Spec.HostNetwork || podutil.IsPodTerminal(pod) {
// No need to process Pod when it's not scheduled or is already terminated.
// HostNetwork Pods will not be applied to by policies.
continue
}
Expand Down
66 changes: 66 additions & 0 deletions pkg/controller/networkpolicy/networkpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,72 @@ func TestAddPod(t *testing.T) {
outAddressGroupMatch: false,
groupMatch: false,
},
{
name: "match-all-selectors-succeeded",
addedPod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "podA",
Namespace: "nsA",
Labels: map[string]string{
"role": "app",
"group": "appliedTo",
"inGroup": "inAddress",
"outGroup": "outAddress",
"clustergroup": "yes",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: "container-1",
}},
NodeName: "nodeA",
},
Status: corev1.PodStatus{
Phase: corev1.PodSucceeded,
PodIP: "1.2.3.4",
PodIPs: []corev1.PodIP{
{IP: "1.2.3.4"},
},
},
},
appGroupMatch: false,
inAddressGroupMatch: false,
outAddressGroupMatch: false,
groupMatch: false,
},
{
name: "match-all-selectors-failed",
addedPod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "podA",
Namespace: "nsA",
Labels: map[string]string{
"role": "app",
"group": "appliedTo",
"inGroup": "inAddress",
"outGroup": "outAddress",
"clustergroup": "yes",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: "container-1",
}},
NodeName: "nodeA",
},
Status: corev1.PodStatus{
Phase: corev1.PodFailed,
PodIP: "1.2.3.4",
PodIPs: []corev1.PodIP{
{IP: "1.2.3.4"},
},
},
},
appGroupMatch: false,
inAddressGroupMatch: false,
outAddressGroupMatch: false,
groupMatch: false,
},
{
name: "match-spec-podselector-no-podip",
addedPod: &corev1.Pod{
Expand Down

0 comments on commit 5d8b991

Please sign in to comment.