@@ -40,6 +40,11 @@ import (
40
40
"k8s.io/ingress-nginx/pkg/apis/ingress"
41
41
)
42
42
43
+ const (
44
+ // ElectionIDLabelKey is the label key used to identify controller pods by election ID
45
+ ElectionIDLabelKey = "nginx.ingress.kubernetes.io/electionID"
46
+ )
47
+
43
48
// UpdateInterval defines the time interval, in seconds, in
44
49
// which the status should check if an update is required.
45
50
var UpdateInterval = 60
@@ -68,6 +73,10 @@ type Config struct {
68
73
69
74
UseNodeInternalIP bool
70
75
76
+ UseElectionIDSelectorOnShutdown bool
77
+
78
+ ElectionID string
79
+
71
80
IngressLister ingressLister
72
81
}
73
82
@@ -175,6 +184,36 @@ func nameOrIPToLoadBalancerIngress(nameOrIP string) v1.IngressLoadBalancerIngres
175
184
176
185
// runningAddresses returns a list of IP addresses and/or FQDN where the
177
186
// ingress controller is currently running
187
+ // listControllerPods returns a list of running pods with controller labels
188
+ func (s * statusSync ) listControllerPods (useElectionID bool ) (* apiv1.PodList , error ) {
189
+ podLabel := make (map [string ]string )
190
+
191
+ if useElectionID {
192
+ // When using electionID, only look for pods with the electionID label
193
+ // This is more specific and will correctly identify pods belonging to the same controller group
194
+ // Note: This requires the electionID label to be set on the pods (done by helm chart)
195
+ podLabel [ElectionIDLabelKey ] = s .Config .ElectionID
196
+ } else {
197
+ // As a standard, app.kubernetes.io are "reserved well-known" labels.
198
+ // In our case, we add those labels as identifiers of the Ingress
199
+ // deployment in this namespace, so we can select it as a set of Ingress instances.
200
+ // As those labels are also generated as part of a HELM deployment, we can be "safe" they
201
+ // cover 95% of the cases
202
+ for k , v := range k8s .IngressPodDetails .Labels {
203
+ // Skip labels that are frequently modified by deployment controllers
204
+ if k != "pod-template-hash" && k != "controller-revision-hash" && k != "pod-template-generation" {
205
+ podLabel [k ] = v
206
+ }
207
+ }
208
+ }
209
+
210
+ return s .Client .CoreV1 ().Pods (k8s .IngressPodDetails .Namespace ).List (
211
+ context .TODO (),
212
+ metav1.ListOptions {
213
+ LabelSelector : labels .SelectorFromSet (podLabel ).String (),
214
+ })
215
+ }
216
+
178
217
func (s * statusSync ) runningAddresses () ([]v1.IngressLoadBalancerIngress , error ) {
179
218
if s .PublishStatusAddress != "" {
180
219
re := regexp .MustCompile (`,\s*` )
@@ -191,9 +230,7 @@ func (s *statusSync) runningAddresses() ([]v1.IngressLoadBalancerIngress, error)
191
230
}
192
231
193
232
// get information about all the pods running the ingress controller
194
- pods , err := s .Client .CoreV1 ().Pods (k8s .IngressPodDetails .Namespace ).List (context .TODO (), metav1.ListOptions {
195
- LabelSelector : labels .SelectorFromSet (k8s .IngressPodDetails .Labels ).String (),
196
- })
233
+ pods , err := s .listControllerPods (false )
197
234
if err != nil {
198
235
return nil , err
199
236
}
@@ -230,21 +267,7 @@ func (s *statusSync) runningAddresses() ([]v1.IngressLoadBalancerIngress, error)
230
267
}
231
268
232
269
func (s * statusSync ) isRunningMultiplePods () bool {
233
- // As a standard, app.kubernetes.io are "reserved well-known" labels.
234
- // In our case, we add those labels as identifiers of the Ingress
235
- // deployment in this namespace, so we can select it as a set of Ingress instances.
236
- // As those labels are also generated as part of a HELM deployment, we can be "safe" they
237
- // cover 95% of the cases
238
- podLabel := make (map [string ]string )
239
- for k , v := range k8s .IngressPodDetails .Labels {
240
- if k != "pod-template-hash" && k != "controller-revision-hash" && k != "pod-template-generation" {
241
- podLabel [k ] = v
242
- }
243
- }
244
-
245
- pods , err := s .Client .CoreV1 ().Pods (k8s .IngressPodDetails .Namespace ).List (context .TODO (), metav1.ListOptions {
246
- LabelSelector : labels .SelectorFromSet (podLabel ).String (),
247
- })
270
+ pods , err := s .listControllerPods (s .UseElectionIDSelectorOnShutdown ) // Use election ID-compatible labels if configured
248
271
if err != nil {
249
272
return false
250
273
}
0 commit comments