@@ -201,10 +201,13 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
201
201
signal .Notify (sigs , syscall .SIGUSR1 , syscall .SIGUSR2 )
202
202
go handleSignals (ctx , cancel , sigs , client , haproxyOptions , users )
203
203
204
+ ra := configureReloadAgent (ctx , haproxyOptions , client )
205
+
204
206
if ! haproxyOptions .DisableInotify {
205
- if err := startWatcher (ctx , client , haproxyOptions , users ); err != nil {
207
+ if err := startWatcher (ctx , client , haproxyOptions , users , ra ); err != nil {
206
208
haproxyOptions .DisableInotify = true
207
209
client = configureNativeClient (clientCtx , haproxyOptions , mWorker )
210
+ ra = configureReloadAgent (ctx , haproxyOptions , client )
208
211
}
209
212
}
210
213
@@ -213,26 +216,6 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
213
216
go cfg .MapSync .SyncAll (client )
214
217
}
215
218
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
-
236
219
// setup discovery handlers
237
220
api .DiscoveryGetAPIEndpointsHandler = discovery .GetAPIEndpointsHandlerFunc (func (params discovery.GetAPIEndpointsParams , principal interface {}) middleware.Responder {
238
221
ends , err := misc .DiscoverChildPaths ("" , SwaggerJSON )
@@ -881,6 +864,29 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
881
864
return setupGlobalMiddleware (api .Serve (setupMiddlewares ), adpts ... )
882
865
}
883
866
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
+
884
890
// The TLS configuration before HTTPS server starts.
885
891
func configureTLS (tlsConfig * tls.Config ) {
886
892
// Make all necessary changes to the TLS configuration here.
@@ -1047,12 +1053,40 @@ func reloadConfigurationFile(client client_native.HAProxyClient, haproxyOptions
1047
1053
client .ReplaceConfiguration (confClient )
1048
1054
}
1049
1055
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 {
1051
1057
cb := func () {
1052
- reloadConfigurationFile (client , haproxyOptions , users )
1053
1058
configuration , err := client .Configuration ()
1054
1059
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 )
1056
1090
return
1057
1091
}
1058
1092
if err := configuration .IncrementVersion (); err != nil {
0 commit comments