@@ -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,7 +208,18 @@ 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 )
211
+ cfg := dataplane .BuildConfiguration (ctx , gr , gw , h .cfg .serviceResolver , h .cfg .plus )
212
+ depCtx , getErr := h .getDeploymentContext (ctx )
213
+ if getErr != nil {
214
+ logger .Error (getErr , "error getting deployment context for usage reporting" )
215
+ }
216
+ cfg .DeploymentContext = depCtx
217
+
218
+ h .setLatestConfiguration (gw , & cfg )
219
+
220
+ deployment .FileLock .Lock ()
221
+ configApplied := h .updateNginxConf (deployment , cfg )
222
+ deployment .FileLock .Unlock ()
219
223
220
224
configErr := deployment .GetLatestConfigError ()
221
225
upstreamErr := deployment .GetLatestUpstreamError ()
@@ -232,53 +236,6 @@ func (h *eventHandlerImpl) sendNginxConfig(
232
236
}
233
237
}
234
238
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 )
248
- depCtx , getErr := h .getDeploymentContext (ctx )
249
- if getErr != nil {
250
- logger .Error (getErr , "error getting deployment context for usage reporting" )
251
- }
252
- cfg .DeploymentContext = depCtx
253
-
254
- h .setLatestConfiguration (& cfg )
255
-
256
- 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
- }
262
- 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
-
272
- h .setLatestConfiguration (& cfg )
273
-
274
- deployment .FileLock .Lock ()
275
- configApplied = h .updateNginxConf (deployment , cfg )
276
- deployment .FileLock .Unlock ()
277
- }
278
-
279
- return configApplied
280
- }
281
-
282
239
func (h * eventHandlerImpl ) waitForStatusUpdates (ctx context.Context ) {
283
240
for {
284
241
item := h .cfg .statusQueue .Dequeue (ctx )
@@ -457,7 +414,7 @@ func (h *eventHandlerImpl) updateNginxConf(
457
414
458
415
// If using NGINX Plus, update upstream servers using the API.
459
416
if h .cfg .plus {
460
- h .cfg .nginxUpdater .UpdateUpstreamServers (deployment , conf )
417
+ applied = h .cfg .nginxUpdater .UpdateUpstreamServers (deployment , conf ) || applied
461
418
}
462
419
463
420
return applied
@@ -570,21 +527,28 @@ func (h *eventHandlerImpl) getDeploymentContext(ctx context.Context) (dataplane.
570
527
}
571
528
572
529
// GetLatestConfiguration gets the latest configuration.
573
- func (h * eventHandlerImpl ) GetLatestConfiguration () * dataplane.Configuration {
530
+ func (h * eventHandlerImpl ) GetLatestConfiguration () [] * dataplane.Configuration {
574
531
h .lock .Lock ()
575
532
defer h .lock .Unlock ()
576
533
577
- return h .latestConfiguration
534
+ configs := make ([]* dataplane.Configuration , 0 , len (h .latestConfigurations ))
535
+ for _ , cfg := range h .latestConfigurations {
536
+ configs = append (configs , cfg )
537
+ }
538
+
539
+ return configs
578
540
}
579
541
580
542
// 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 ) {
543
+ func (h * eventHandlerImpl ) setLatestConfiguration (gateway * graph.Gateway , cfg * dataplane.Configuration ) {
544
+ if gateway == nil || gateway .Source == nil {
545
+ return
546
+ }
547
+
584
548
h .lock .Lock ()
585
549
defer h .lock .Unlock ()
586
550
587
- h .latestConfiguration = cfg
551
+ h .latestConfigurations [ client . ObjectKeyFromObject ( gateway . Source )] = cfg
588
552
}
589
553
590
554
func objectFilterKey (obj client.Object , nsName types.NamespacedName ) filterKey {
0 commit comments