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