@@ -194,10 +194,13 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
194194 signal .Notify (sigs , syscall .SIGUSR1 , syscall .SIGUSR2 )
195195 go handleSignals (ctx , cancel , sigs , client , haproxyOptions , users )
196196
197+ ra := configureReloadAgent (ctx , haproxyOptions , client )
198+
197199 if ! haproxyOptions .DisableInotify {
198- if err := startWatcher (ctx , client , haproxyOptions , users ); err != nil {
200+ if err := startWatcher (ctx , client , haproxyOptions , users , ra ); err != nil {
199201 haproxyOptions .DisableInotify = true
200202 client = configureNativeClient (clientCtx , haproxyOptions , mWorker )
203+ ra = configureReloadAgent (ctx , haproxyOptions , client )
201204 }
202205 }
203206
@@ -206,24 +209,6 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
206209 go cfg .MapSync .SyncAll (client )
207210 }
208211
209- // Initialize reload agent
210- raParams := haproxy.ReloadAgentParams {
211- Delay : haproxyOptions .ReloadDelay ,
212- ReloadCmd : haproxyOptions .ReloadCmd ,
213- RestartCmd : haproxyOptions .RestartCmd ,
214- StatusCmd : haproxyOptions .StatusCmd ,
215- ConfigFile : haproxyOptions .ConfigFile ,
216- BackupDir : haproxyOptions .BackupsDir ,
217- Retention : haproxyOptions .ReloadRetention ,
218- Ctx : ctx ,
219- }
220-
221- ra , e := haproxy .NewReloadAgent (raParams )
222- if e != nil {
223- // nolint:gocritic
224- log .Fatalf ("Cannot initialize reload agent: %v" , e )
225- }
226-
227212 // setup discovery handlers
228213 api .DiscoveryGetAPIEndpointsHandler = discovery .GetAPIEndpointsHandlerFunc (func (params discovery.GetAPIEndpointsParams , principal interface {}) middleware.Responder {
229214 ends , err := misc .DiscoverChildPaths ("" , SwaggerJSON )
@@ -816,6 +801,26 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
816801 return setupGlobalMiddleware (api .Serve (setupMiddlewares ), adpts ... )
817802}
818803
804+ func configureReloadAgent (ctx context.Context , haproxyOptions dataplaneapi_config.HAProxyConfiguration , client client_native.HAProxyClient ) * haproxy.ReloadAgent {
805+ raParams := haproxy.ReloadAgentParams {
806+ Delay : haproxyOptions .ReloadDelay ,
807+ ReloadCmd : haproxyOptions .ReloadCmd ,
808+ RestartCmd : haproxyOptions .RestartCmd ,
809+ StatusCmd : haproxyOptions .StatusCmd ,
810+ ConfigFile : haproxyOptions .ConfigFile ,
811+ BackupDir : haproxyOptions .BackupsDir ,
812+ Retention : haproxyOptions .ReloadRetention ,
813+ Ctx : ctx ,
814+ }
815+
816+ ra , e := haproxy .NewReloadAgent (raParams )
817+ if e != nil {
818+ // nolint:gocritic
819+ log .Fatalf ("Cannot initialize reload agent: %v" , e )
820+ }
821+ return ra
822+ }
823+
819824// The TLS configuration before HTTPS server starts.
820825func configureTLS (tlsConfig * tls.Config ) {
821826 // Make all necessary changes to the TLS configuration here.
@@ -962,12 +967,40 @@ func reloadConfigurationFile(client client_native.HAProxyClient, haproxyOptions
962967 client .ReplaceConfiguration (confClient )
963968}
964969
965- func startWatcher (ctx context.Context , client client_native.HAProxyClient , haproxyOptions dataplaneapi_config.HAProxyConfiguration , users * dataplaneapi_config.Users ) error {
970+ func startWatcher (ctx context.Context , client client_native.HAProxyClient , haproxyOptions dataplaneapi_config.HAProxyConfiguration , users * dataplaneapi_config.Users , reloadAgent * haproxy. ReloadAgent ) error {
966971 cb := func () {
967- reloadConfigurationFile (client , haproxyOptions , users )
968972 configuration , err := client .Configuration ()
969973 if err != nil {
970- log .Warningf ("Failed to increment configuration version: %v" , err )
974+ log .Warningf ("Failed to get configuration: %s" , err )
975+ return
976+ }
977+
978+ // save old runtime configuration to know if the runtime client must be configured after the new configuration is
979+ // reloaded by HAProxy. Logic is done by cn.ReconfigureRuntime() function.
980+ _ , globalConf , err := configuration .GetGlobalConfiguration ("" )
981+ if err != nil {
982+ log .Warningf ("Failed to get global configuration section: %s" , err )
983+ return
984+ }
985+ runtimeAPIsOld := globalConf .RuntimeAPIs
986+
987+ // reload configuration from config file.
988+ reloadConfigurationFile (client , haproxyOptions , users )
989+
990+ // reload runtime client if necessary.
991+ callbackNeeded , reconfigureFunc , err := cn .ReconfigureRuntime (client , runtimeAPIsOld )
992+ if err != nil {
993+ log .Warningf ("Failed to check if native client need to be reloaded: %s" , err )
994+ return
995+ }
996+ if callbackNeeded {
997+ reloadAgent .ReloadWithCallback (reconfigureFunc )
998+ }
999+
1000+ // get the last configuration which has been updated by reloadConfigurationFile and increment version in config file.
1001+ configuration , err = client .Configuration ()
1002+ if err != nil {
1003+ log .Warningf ("Failed to get configuration: %s" , err )
9711004 return
9721005 }
9731006 if err := configuration .IncrementVersion (); err != nil {
0 commit comments