@@ -16,6 +16,7 @@ import (
1616 listers "k8s.io/client-go/listers/core/v1"
1717 ref "k8s.io/client-go/tools/reference"
1818 "k8s.io/klog/v2"
19+ v1pod "k8s.io/kubernetes/pkg/api/v1/pod"
1920
2021 libovsdbclient "github.com/ovn-kubernetes/libovsdb/client"
2122
@@ -117,6 +118,10 @@ func networkStatusAnnotationsChanged(oldPod, newPod *corev1.Pod) bool {
117118 return oldPod .Annotations [nettypes .NetworkStatusAnnot ] != newPod .Annotations [nettypes .NetworkStatusAnnot ]
118119}
119120
121+ func podBecameReady (oldPod , newPod * corev1.Pod ) bool {
122+ return ! v1pod .IsPodReadyConditionTrue (oldPod .Status ) && v1pod .IsPodReadyConditionTrue (newPod .Status )
123+ }
124+
120125// ensurePod tries to set up a pod. It returns nil on success and error on failure; failure
121126// indicates the pod set up should be retried later.
122127func (oc * DefaultNetworkController ) ensurePod (oldPod , pod * corev1.Pod , addPort bool ) error {
@@ -131,6 +136,14 @@ func (oc *DefaultNetworkController) ensurePod(oldPod, pod *corev1.Pod, addPort b
131136 return oc .ensureRemotePodIP (oldPod , pod , addPort )
132137 }
133138
139+ // If an external gateway pod is in terminating or not ready state then remove the
140+ // routes for the external gateway pod
141+ if util .PodTerminating (pod ) || ! v1pod .IsPodReadyConditionTrue (pod .Status ) {
142+ if err := oc .deletePodExternalGW (pod ); err != nil {
143+ return fmt .Errorf ("ensurePod failed %s/%s: %w" , pod .Namespace , pod .Name , err )
144+ }
145+ }
146+
134147 if oc .isPodScheduledinLocalZone (pod ) {
135148 klog .V (5 ).Infof ("Ensuring zone local for Pod %s/%s in node %s" , pod .Namespace , pod .Name , pod .Spec .NodeName )
136149 return oc .ensureLocalZonePod (oldPod , pod , addPort )
@@ -170,7 +183,7 @@ func (oc *DefaultNetworkController) ensureLocalZonePod(oldPod, pod *corev1.Pod,
170183 }
171184 } else {
172185 // either pod is host-networked or its an update for a normal pod (addPort=false case)
173- if oldPod == nil || exGatewayAnnotationsChanged (oldPod , pod ) || networkStatusAnnotationsChanged (oldPod , pod ) {
186+ if oldPod == nil || exGatewayAnnotationsChanged (oldPod , pod ) || networkStatusAnnotationsChanged (oldPod , pod ) || podBecameReady ( oldPod , pod ) {
174187 if err := oc .addPodExternalGW (pod ); err != nil {
175188 return fmt .Errorf ("addPodExternalGW failed for %s/%s: %w" , pod .Namespace , pod .Name , err )
176189 }
@@ -237,7 +250,7 @@ func (oc *DefaultNetworkController) ensureRemoteZonePod(oldPod, pod *corev1.Pod,
237250 }
238251
239252 // either pod is host-networked or its an update for a normal pod (addPort=false case)
240- if oldPod == nil || exGatewayAnnotationsChanged (oldPod , pod ) || networkStatusAnnotationsChanged (oldPod , pod ) {
253+ if oldPod == nil || exGatewayAnnotationsChanged (oldPod , pod ) || networkStatusAnnotationsChanged (oldPod , pod ) || podBecameReady ( oldPod , pod ) {
241254 // check if this remote pod is serving as an external GW. If so add the routes in the namespace
242255 // associated with this remote pod
243256 if err := oc .addPodExternalGW (pod ); err != nil {
0 commit comments