@@ -201,10 +201,13 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
201201 signal .Notify (sigs , syscall .SIGUSR1 , syscall .SIGUSR2 )
202202 go handleSignals (ctx , cancel , sigs , client , haproxyOptions , users )
203203
204+ ra := configureReloadAgent (ctx , haproxyOptions , client )
205+
204206 if ! haproxyOptions .DisableInotify {
205- if err := startWatcher (ctx , client , haproxyOptions , users ); err != nil {
207+ if err := startWatcher (ctx , client , haproxyOptions , users , ra ); err != nil {
206208 haproxyOptions .DisableInotify = true
207209 client = configureNativeClient (clientCtx , haproxyOptions , mWorker )
210+ ra = configureReloadAgent (ctx , haproxyOptions , client )
208211 }
209212 }
210213
@@ -213,26 +216,6 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
213216 go cfg .MapSync .SyncAll (client )
214217 }
215218
216- // Initialize reload agent
217- raParams := haproxy.ReloadAgentParams {
218- Delay : haproxyOptions .ReloadDelay ,
219- ReloadCmd : haproxyOptions .ReloadCmd ,
220- UseMasterSocket : canUseMasterSocketReload (& haproxyOptions , client ),
221- RestartCmd : haproxyOptions .RestartCmd ,
222- StatusCmd : haproxyOptions .StatusCmd ,
223- ConfigFile : haproxyOptions .ConfigFile ,
224- BackupDir : haproxyOptions .BackupsDir ,
225- Retention : haproxyOptions .ReloadRetention ,
226- Client : client ,
227- Ctx : ctx ,
228- }
229-
230- ra , e := haproxy .NewReloadAgent (raParams )
231- if e != nil {
232- // nolint:gocritic
233- log .Fatalf ("Cannot initialize reload agent: %v" , e )
234- }
235-
236219 // setup discovery handlers
237220 api .DiscoveryGetAPIEndpointsHandler = discovery .GetAPIEndpointsHandlerFunc (func (params discovery.GetAPIEndpointsParams , principal interface {}) middleware.Responder {
238221 ends , err := misc .DiscoverChildPaths ("" , SwaggerJSON )
@@ -881,6 +864,29 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
881864 return setupGlobalMiddleware (api .Serve (setupMiddlewares ), adpts ... )
882865}
883866
867+ func configureReloadAgent (ctx context.Context , haproxyOptions dataplaneapi_config.HAProxyConfiguration , client client_native.HAProxyClient ) * haproxy.ReloadAgent {
868+ // Initialize reload agent
869+ raParams := haproxy.ReloadAgentParams {
870+ Delay : haproxyOptions .ReloadDelay ,
871+ ReloadCmd : haproxyOptions .ReloadCmd ,
872+ UseMasterSocket : canUseMasterSocketReload (& haproxyOptions , client ),
873+ RestartCmd : haproxyOptions .RestartCmd ,
874+ StatusCmd : haproxyOptions .StatusCmd ,
875+ ConfigFile : haproxyOptions .ConfigFile ,
876+ BackupDir : haproxyOptions .BackupsDir ,
877+ Retention : haproxyOptions .ReloadRetention ,
878+ Client : client ,
879+ Ctx : ctx ,
880+ }
881+
882+ ra , e := haproxy .NewReloadAgent (raParams )
883+ if e != nil {
884+ // nolint:gocritic
885+ log .Fatalf ("Cannot initialize reload agent: %v" , e )
886+ }
887+ return ra
888+ }
889+
884890// The TLS configuration before HTTPS server starts.
885891func configureTLS (tlsConfig * tls.Config ) {
886892 // Make all necessary changes to the TLS configuration here.
@@ -1047,12 +1053,40 @@ func reloadConfigurationFile(client client_native.HAProxyClient, haproxyOptions
10471053 client .ReplaceConfiguration (confClient )
10481054}
10491055
1050- func startWatcher (ctx context.Context , client client_native.HAProxyClient , haproxyOptions dataplaneapi_config.HAProxyConfiguration , users * dataplaneapi_config.Users ) error {
1056+ func startWatcher (ctx context.Context , client client_native.HAProxyClient , haproxyOptions dataplaneapi_config.HAProxyConfiguration , users * dataplaneapi_config.Users , reloadAgent * haproxy. ReloadAgent ) error {
10511057 cb := func () {
1052- reloadConfigurationFile (client , haproxyOptions , users )
10531058 configuration , err := client .Configuration ()
10541059 if err != nil {
1055- log .Warningf ("Failed to increment configuration version: %v" , err )
1060+ log .Warningf ("Failed to get configuration: %s" , err )
1061+ return
1062+ }
1063+
1064+ // save old runtime configuration to know if the runtime client must be configured after the new configuration is
1065+ // reloaded by HAProxy. Logic is done by cn.ReconfigureRuntime() function.
1066+ _ , globalConf , err := configuration .GetGlobalConfiguration ("" )
1067+ if err != nil {
1068+ log .Warningf ("Failed to get global configuration section: %s" , err )
1069+ return
1070+ }
1071+ runtimeAPIsOld := globalConf .RuntimeAPIs
1072+
1073+ // reload configuration from config file.
1074+ reloadConfigurationFile (client , haproxyOptions , users )
1075+
1076+ // reload runtime client if necessary.
1077+ callbackNeeded , reconfigureFunc , err := cn .ReconfigureRuntime (client , runtimeAPIsOld )
1078+ if err != nil {
1079+ log .Warningf ("Failed to check if native client need to be reloaded: %s" , err )
1080+ return
1081+ }
1082+ if callbackNeeded {
1083+ reloadAgent .ReloadWithCallback (reconfigureFunc )
1084+ }
1085+
1086+ // get the last configuration which has been updated by reloadConfigurationFile and increment version in config file.
1087+ configuration , err = client .Configuration ()
1088+ if err != nil {
1089+ log .Warningf ("Failed to get configuration: %s" , err )
10561090 return
10571091 }
10581092 if err := configuration .IncrementVersion (); err != nil {
0 commit comments