Skip to content

Commit 493c5d0

Browse files
authored
Sync groups members for pods that turns into terminated status (#7217) (#7247)
Once a Pod is updated to terminated state (succeeded or failed), the selectorItems in GroupEntityIndex need to be notified so that they are excluded from any Network Policy computations in appliedTo or address groups. Signed-off-by: Dyanngg <[email protected]>
1 parent c2f24bc commit 493c5d0

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

pkg/controller/grouping/controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,14 @@ func (c *GroupEntityController) Run(stopCh <-chan struct{}) {
207207

208208
func (c *GroupEntityController) addPod(obj interface{}) {
209209
pod := obj.(*v1.Pod)
210-
klog.V(2).Infof("Processing Pod %s/%s ADD event, labels: %v", pod.Namespace, pod.Name, pod.Labels)
210+
klog.V(2).InfoS("Processing Pod ADD event", "pod", klog.KObj(pod), "labels", pod.Labels)
211211
c.groupEntityIndex.AddPod(pod)
212212
c.podAddEvents.Increment()
213213
}
214214

215215
func (c *GroupEntityController) updatePod(_, curObj interface{}) {
216216
curPod := curObj.(*v1.Pod)
217-
klog.V(2).Infof("Processing Pod %s/%s UPDATE event, labels: %v", curPod.Namespace, curPod.Name, curPod.Labels)
217+
klog.V(2).InfoS("Processing Pod UPDATE event", "pod", klog.KObj(curPod), "labels", curPod.Labels, "phase", curPod.Status.Phase)
218218
c.groupEntityIndex.AddPod(curPod)
219219
}
220220

@@ -237,14 +237,14 @@ func (c *GroupEntityController) deletePod(old interface{}) {
237237

238238
func (c *GroupEntityController) addNamespace(obj interface{}) {
239239
namespace := obj.(*v1.Namespace)
240-
klog.V(2).Infof("Processing Namespace %s ADD event, labels: %v", namespace.Name, namespace.Labels)
240+
klog.V(2).InfoS("Processing Namespace ADD event", "namespace", namespace.Name, "labels", namespace.Labels)
241241
c.groupEntityIndex.AddNamespace(namespace)
242242
c.namespaceAddEvents.Increment()
243243
}
244244

245245
func (c *GroupEntityController) updateNamespace(_, curObj interface{}) {
246246
curNamespace := curObj.(*v1.Namespace)
247-
klog.V(2).Infof("Processing Namespace %s UPDATE event, labels: %v", curNamespace.Name, curNamespace.Labels)
247+
klog.V(2).InfoS("Processing Namespace UPDATE event", "namespace", curNamespace.Name, "labels", curNamespace.Labels)
248248
c.groupEntityIndex.AddNamespace(curNamespace)
249249
}
250250

pkg/controller/grouping/controller_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func TestGroupEntityControllerRun(t *testing.T) {
8989
informerFactory := informers.NewSharedInformerFactory(client, informerDefaultResync)
9090
crdInformerFactory := crdinformers.NewSharedInformerFactory(crdClient, informerDefaultResync)
9191
stopCh := make(chan struct{})
92+
defer close(stopCh)
9293

9394
c := NewGroupEntityController(index, informerFactory.Core().V1().Pods(), informerFactory.Core().V1().Namespaces(), crdInformerFactory.Crd().V1alpha2().ExternalEntities())
9495
assert.False(t, index.HasSynced(), "GroupEntityIndex has been synced before starting InformerFactories")

pkg/controller/grouping/group_entity_index.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
"antrea.io/antrea/pkg/apis/crd/v1alpha2"
3131
"antrea.io/antrea/pkg/controller/types"
32+
"antrea.io/antrea/pkg/util/k8s"
3233
utilsets "antrea.io/antrea/pkg/util/sets"
3334
)
3435

@@ -744,6 +745,9 @@ func entityAttrsUpdated(oldEntity, newEntity metav1.Object) bool {
744745
switch oldValue := oldEntity.(type) {
745746
case *v1.Pod:
746747
// For Pod, we only care about PodIP and NodeName update.
748+
// Also, when a Pod is updated to terminated state, the selectorItems need to be
749+
// notified so that they are excluded from any Network Policy computations in
750+
// appliedTo or address groups.
747751
// Some other attributes we care about are immutable, e.g. the named ContainerPort.
748752
newValue := newEntity.(*v1.Pod)
749753
if oldValue.Status.PodIP != newValue.Status.PodIP {
@@ -752,6 +756,9 @@ func entityAttrsUpdated(oldEntity, newEntity metav1.Object) bool {
752756
if oldValue.Spec.NodeName != newValue.Spec.NodeName {
753757
return true
754758
}
759+
if k8s.IsPodTerminated(oldValue) != k8s.IsPodTerminated(newValue) {
760+
return true
761+
}
755762
return false
756763
case *v1alpha2.ExternalEntity:
757764
newValue := newEntity.(*v1alpha2.ExternalEntity)

pkg/controller/grouping/group_entity_index_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,39 @@ func TestGroupEntityIndexEventHandlers(t *testing.T) {
472472
},
473473
expectedGroupsCalled: map[GroupType][]string{},
474474
},
475+
{
476+
name: "update an existing pod's phase to running",
477+
existingPods: []*v1.Pod{podFoo1, podBar1, podFoo1InOtherNamespace},
478+
existingGroups: []*group{groupPodFooType1, groupPodFooAllNamespaceType1, groupEEFooType1, groupPodAllNamespaceType1},
479+
inputEvent: func(i *GroupEntityIndex) {
480+
i.AddPod(copyAndMutatePod(podFoo1, func(pod *v1.Pod) {
481+
pod.Status.Phase = v1.PodRunning
482+
}))
483+
},
484+
expectedGroupsCalled: map[GroupType][]string{},
485+
},
486+
{
487+
name: "update an existing pod's phase to succeeded",
488+
existingPods: []*v1.Pod{podFoo1, podBar1, podFoo1InOtherNamespace},
489+
existingGroups: []*group{groupPodFooType1, groupPodFooAllNamespaceType1, groupEEFooType1, groupPodAllNamespaceType1},
490+
inputEvent: func(i *GroupEntityIndex) {
491+
i.AddPod(copyAndMutatePod(podFoo1, func(pod *v1.Pod) {
492+
pod.Status.Phase = v1.PodSucceeded
493+
}))
494+
},
495+
expectedGroupsCalled: map[GroupType][]string{groupType1: {groupPodFooType1.groupName, groupPodFooAllNamespaceType1.groupName, groupPodAllNamespaceType1.groupName}},
496+
},
497+
{
498+
name: "update an existing pod's phase to failed",
499+
existingPods: []*v1.Pod{podFoo1, podBar1, podFoo1InOtherNamespace},
500+
existingGroups: []*group{groupPodFooType1, groupPodFooAllNamespaceType1, groupPodBarType1, groupPodAllNamespaceType1},
501+
inputEvent: func(i *GroupEntityIndex) {
502+
i.AddPod(copyAndMutatePod(podBar1, func(pod *v1.Pod) {
503+
pod.Status.Phase = v1.PodFailed
504+
}))
505+
},
506+
expectedGroupsCalled: map[GroupType][]string{groupType1: {groupPodBarType1.groupName, groupPodAllNamespaceType1.groupName}},
507+
},
475508
{
476509
name: "delete an existing pod",
477510
existingPods: []*v1.Pod{podFoo1, podBar1, podFoo1InOtherNamespace},

0 commit comments

Comments
 (0)