@@ -109,23 +109,21 @@ type objectFilter struct {
109109// (3) Updating control plane configuration.
110110// (4) Tracks the NGINX Plus usage reporting Secret (if applicable).
111111type 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
114114
115115 // objectFilters contains all created objectFilters, with the key being a filterKey
116116 objectFilters map [filterKey ]objectFilter
117117
118118 cfg eventHandlerConfig
119119 lock sync.Mutex
120-
121- // version is the current version number of the nginx config.
122- version int
123120}
124121
125122// newEventHandlerImpl creates a new eventHandlerImpl.
126123func newEventHandlerImpl (cfg eventHandlerConfig ) * eventHandlerImpl {
127124 handler := & eventHandlerImpl {
128- cfg : cfg ,
125+ cfg : cfg ,
126+ latestConfigurations : make (map [types.NamespacedName ]* dataplane.Configuration ),
129127 }
130128
131129 handler .objectFilters = map [filterKey ]objectFilter {
@@ -158,28 +156,23 @@ func (h *eventHandlerImpl) HandleEventBatch(ctx context.Context, logger logr.Log
158156 h .parseAndCaptureEvent (ctx , logger , event )
159157 }
160158
161- changeType , gr := h .cfg .processor .Process ()
159+ gr := h .cfg .processor .Process ()
162160
163161 // Once we've processed resources on startup and built our first graph, mark the Pod as ready.
164162 if ! h .cfg .graphBuiltHealthChecker .ready {
165163 h .cfg .graphBuiltHealthChecker .setAsReady ()
166164 }
167165
168- h .sendNginxConfig (ctx , logger , gr , changeType )
166+ h .sendNginxConfig (ctx , logger , gr )
169167}
170168
171169// enable is called when the pod becomes leader to ensure the provisioner has
172170// the latest configuration.
173171func (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 ())
175173}
176174
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 ) {
183176 if gr == nil {
184177 return
185178 }
@@ -215,68 +208,30 @@ func (h *eventHandlerImpl) sendNginxConfig(
215208 panic ("expected deployment, got nil" )
216209 }
217210
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 )
248212 depCtx , getErr := h .getDeploymentContext (ctx )
249213 if getErr != nil {
250214 logger .Error (getErr , "error getting deployment context for usage reporting" )
251215 }
252216 cfg .DeploymentContext = depCtx
253217
254- h .setLatestConfiguration (& cfg )
218+ h .setLatestConfiguration (gw , & cfg )
255219
256220 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 )
262222 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
271223
272- h .setLatestConfiguration (& cfg )
224+ configErr := deployment .GetLatestConfigError ()
225+ upstreamErr := deployment .GetLatestUpstreamError ()
226+ err := errors .Join (configErr , upstreamErr )
273227
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 )
277234 }
278-
279- return configApplied
280235}
281236
282237func (h * eventHandlerImpl ) waitForStatusUpdates (ctx context.Context ) {
@@ -451,16 +406,14 @@ func (h *eventHandlerImpl) parseAndCaptureEvent(ctx context.Context, logger logr
451406func (h * eventHandlerImpl ) updateNginxConf (
452407 deployment * agent.Deployment ,
453408 conf dataplane.Configuration ,
454- ) bool {
409+ ) {
455410 files := h .cfg .generator .Generate (conf )
456- applied := h .cfg .nginxUpdater .UpdateConfig (deployment , files )
411+ h .cfg .nginxUpdater .UpdateConfig (deployment , files )
457412
458413 // If using NGINX Plus, update upstream servers using the API.
459414 if h .cfg .plus {
460415 h .cfg .nginxUpdater .UpdateUpstreamServers (deployment , conf )
461416 }
462-
463- return applied
464417}
465418
466419// updateControlPlaneAndSetStatus updates the control plane configuration and then sets the status
@@ -570,21 +523,28 @@ func (h *eventHandlerImpl) getDeploymentContext(ctx context.Context) (dataplane.
570523}
571524
572525// GetLatestConfiguration gets the latest configuration.
573- func (h * eventHandlerImpl ) GetLatestConfiguration () * dataplane.Configuration {
526+ func (h * eventHandlerImpl ) GetLatestConfiguration () [] * dataplane.Configuration {
574527 h .lock .Lock ()
575528 defer h .lock .Unlock ()
576529
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
578536}
579537
580538// 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+
584544 h .lock .Lock ()
585545 defer h .lock .Unlock ()
586546
587- h .latestConfiguration = cfg
547+ h .latestConfigurations [ client . ObjectKeyFromObject ( gateway . Source )] = cfg
588548}
589549
590550func objectFilterKey (obj client.Object , nsName types.NamespacedName ) filterKey {
0 commit comments