@@ -109,23 +109,21 @@ type objectFilter struct {
109
109
// (3) Updating control plane configuration.
110
110
// (4) Tracks the NGINX Plus usage reporting Secret (if applicable).
111
111
type eventHandlerImpl struct {
112
- // latestConfiguration is the latest Configuration generation.
113
- latestConfiguration * dataplane.Configuration
112
+ // latestConfigurations are the latest Configuration generation for each Gateway tree .
113
+ latestConfigurations map [types. NamespacedName ] * dataplane.Configuration
114
114
115
115
// objectFilters contains all created objectFilters, with the key being a filterKey
116
116
objectFilters map [filterKey ]objectFilter
117
117
118
118
cfg eventHandlerConfig
119
119
lock sync.Mutex
120
-
121
- // version is the current version number of the nginx config.
122
- version int
123
120
}
124
121
125
122
// newEventHandlerImpl creates a new eventHandlerImpl.
126
123
func newEventHandlerImpl (cfg eventHandlerConfig ) * eventHandlerImpl {
127
124
handler := & eventHandlerImpl {
128
- cfg : cfg ,
125
+ cfg : cfg ,
126
+ latestConfigurations : make (map [types.NamespacedName ]* dataplane.Configuration ),
129
127
}
130
128
131
129
handler .objectFilters = map [filterKey ]objectFilter {
@@ -158,28 +156,23 @@ func (h *eventHandlerImpl) HandleEventBatch(ctx context.Context, logger logr.Log
158
156
h .parseAndCaptureEvent (ctx , logger , event )
159
157
}
160
158
161
- changeType , gr := h .cfg .processor .Process ()
159
+ gr := h .cfg .processor .Process ()
162
160
163
161
// Once we've processed resources on startup and built our first graph, mark the Pod as ready.
164
162
if ! h .cfg .graphBuiltHealthChecker .ready {
165
163
h .cfg .graphBuiltHealthChecker .setAsReady ()
166
164
}
167
165
168
- h .sendNginxConfig (ctx , logger , gr , changeType )
166
+ h .sendNginxConfig (ctx , logger , gr )
169
167
}
170
168
171
169
// enable is called when the pod becomes leader to ensure the provisioner has
172
170
// the latest configuration.
173
171
func (h * eventHandlerImpl ) enable (ctx context.Context ) {
174
- h .sendNginxConfig (ctx , h .cfg .logger , h .cfg .processor .GetLatestGraph (), state . ClusterStateChange )
172
+ h .sendNginxConfig (ctx , h .cfg .logger , h .cfg .processor .GetLatestGraph ())
175
173
}
176
174
177
- func (h * eventHandlerImpl ) sendNginxConfig (
178
- ctx context.Context ,
179
- logger logr.Logger ,
180
- gr * graph.Graph ,
181
- changeType state.ChangeType ,
182
- ) {
175
+ func (h * eventHandlerImpl ) sendNginxConfig (ctx context.Context , logger logr.Logger , gr * graph.Graph ) {
183
176
if gr == nil {
184
177
return
185
178
}
@@ -215,68 +208,30 @@ func (h *eventHandlerImpl) sendNginxConfig(
215
208
panic ("expected deployment, got nil" )
216
209
}
217
210
218
- configApplied := h .processStateAndBuildConfig (ctx , logger , gr , gw , changeType , deployment )
219
-
220
- configErr := deployment .GetLatestConfigError ()
221
- upstreamErr := deployment .GetLatestUpstreamError ()
222
- err := errors .Join (configErr , upstreamErr )
223
-
224
- if configApplied || err != nil {
225
- obj := & status.QueueObject {
226
- UpdateType : status .UpdateAll ,
227
- Error : err ,
228
- Deployment : gw .DeploymentName ,
229
- }
230
- h .cfg .statusQueue .Enqueue (obj )
231
- }
232
- }
233
- }
234
-
235
- func (h * eventHandlerImpl ) processStateAndBuildConfig (
236
- ctx context.Context ,
237
- logger logr.Logger ,
238
- gr * graph.Graph ,
239
- currentGateway * graph.Gateway ,
240
- changeType state.ChangeType ,
241
- deployment * agent.Deployment ,
242
- ) bool {
243
- var configApplied bool
244
- switch changeType {
245
- case state .EndpointsOnlyChange :
246
- h .version ++
247
- cfg := dataplane .BuildConfiguration (ctx , gr , currentGateway , h .cfg .serviceResolver , h .version , h .cfg .plus )
211
+ cfg := dataplane .BuildConfiguration (ctx , gr , gw , h .cfg .serviceResolver , h .cfg .plus )
248
212
depCtx , getErr := h .getDeploymentContext (ctx )
249
213
if getErr != nil {
250
214
logger .Error (getErr , "error getting deployment context for usage reporting" )
251
215
}
252
216
cfg .DeploymentContext = depCtx
253
217
254
- h .setLatestConfiguration (& cfg )
218
+ h .setLatestConfiguration (gw , & cfg )
255
219
256
220
deployment .FileLock .Lock ()
257
- if h .cfg .plus {
258
- configApplied = h .cfg .nginxUpdater .UpdateUpstreamServers (deployment , cfg )
259
- } else {
260
- configApplied = h .updateNginxConf (deployment , cfg )
261
- }
221
+ h .updateNginxConf (deployment , cfg )
262
222
deployment .FileLock .Unlock ()
263
- case state .ClusterStateChange :
264
- h .version ++
265
- cfg := dataplane .BuildConfiguration (ctx , gr , currentGateway , h .cfg .serviceResolver , h .version , h .cfg .plus )
266
- depCtx , getErr := h .getDeploymentContext (ctx )
267
- if getErr != nil {
268
- logger .Error (getErr , "error getting deployment context for usage reporting" )
269
- }
270
- cfg .DeploymentContext = depCtx
271
223
272
- h .setLatestConfiguration (& cfg )
224
+ configErr := deployment .GetLatestConfigError ()
225
+ upstreamErr := deployment .GetLatestUpstreamError ()
226
+ err := errors .Join (configErr , upstreamErr )
273
227
274
- deployment .FileLock .Lock ()
275
- configApplied = h .updateNginxConf (deployment , cfg )
276
- deployment .FileLock .Unlock ()
228
+ obj := & status.QueueObject {
229
+ UpdateType : status .UpdateAll ,
230
+ Error : err ,
231
+ Deployment : gw .DeploymentName ,
232
+ }
233
+ h .cfg .statusQueue .Enqueue (obj )
277
234
}
278
-
279
- return configApplied
280
235
}
281
236
282
237
func (h * eventHandlerImpl ) waitForStatusUpdates (ctx context.Context ) {
@@ -451,16 +406,14 @@ func (h *eventHandlerImpl) parseAndCaptureEvent(ctx context.Context, logger logr
451
406
func (h * eventHandlerImpl ) updateNginxConf (
452
407
deployment * agent.Deployment ,
453
408
conf dataplane.Configuration ,
454
- ) bool {
409
+ ) {
455
410
files := h .cfg .generator .Generate (conf )
456
- applied := h .cfg .nginxUpdater .UpdateConfig (deployment , files )
411
+ h .cfg .nginxUpdater .UpdateConfig (deployment , files )
457
412
458
413
// If using NGINX Plus, update upstream servers using the API.
459
414
if h .cfg .plus {
460
415
h .cfg .nginxUpdater .UpdateUpstreamServers (deployment , conf )
461
416
}
462
-
463
- return applied
464
417
}
465
418
466
419
// updateControlPlaneAndSetStatus updates the control plane configuration and then sets the status
@@ -570,21 +523,28 @@ func (h *eventHandlerImpl) getDeploymentContext(ctx context.Context) (dataplane.
570
523
}
571
524
572
525
// GetLatestConfiguration gets the latest configuration.
573
- func (h * eventHandlerImpl ) GetLatestConfiguration () * dataplane.Configuration {
526
+ func (h * eventHandlerImpl ) GetLatestConfiguration () [] * dataplane.Configuration {
574
527
h .lock .Lock ()
575
528
defer h .lock .Unlock ()
576
529
577
- return h .latestConfiguration
530
+ configs := make ([]* dataplane.Configuration , 0 , len (h .latestConfigurations ))
531
+ for _ , cfg := range h .latestConfigurations {
532
+ configs = append (configs , cfg )
533
+ }
534
+
535
+ return configs
578
536
}
579
537
580
538
// setLatestConfiguration sets the latest configuration.
581
- // TODO(sberman): once we support multiple Gateways, this will likely have to be a map
582
- // of all configurations.
583
- func (h * eventHandlerImpl ) setLatestConfiguration (cfg * dataplane.Configuration ) {
539
+ func (h * eventHandlerImpl ) setLatestConfiguration (gateway * graph.Gateway , cfg * dataplane.Configuration ) {
540
+ if gateway == nil || gateway .Source == nil {
541
+ return
542
+ }
543
+
584
544
h .lock .Lock ()
585
545
defer h .lock .Unlock ()
586
546
587
- h .latestConfiguration = cfg
547
+ h .latestConfigurations [ client . ObjectKeyFromObject ( gateway . Source )] = cfg
588
548
}
589
549
590
550
func objectFilterKey (obj client.Object , nsName types.NamespacedName ) filterKey {
0 commit comments