1616package handlers
1717
1818import (
19- "context"
2019 "fmt"
2120 "strconv"
2221 "strings"
23- "time"
2422
2523 "github.com/go-openapi/runtime/middleware"
2624 client_native "github.com/haproxytech/client-native/v5"
2725 "github.com/haproxytech/client-native/v5/models"
28- "github.com/haproxytech/client-native/v5/runtime"
29-
3026 cn "github.com/haproxytech/dataplaneapi/client-native"
31- dataplaneapi_config "github.com/haproxytech/dataplaneapi/configuration"
3227 "github.com/haproxytech/dataplaneapi/haproxy"
33- "github.com/haproxytech/dataplaneapi/log"
3428 "github.com/haproxytech/dataplaneapi/misc"
3529 "github.com/haproxytech/dataplaneapi/operations/configuration"
3630)
@@ -137,7 +131,7 @@ func (h *PostRawConfigurationHandlerImpl) Handle(params configuration.PostHAProx
137131 if forceReload {
138132 var callbackNeeded bool
139133 var reconfigureFunc func ()
140- callbackNeeded , reconfigureFunc , err = h . reconfigureRuntime ( runtimeAPIsOld )
134+ callbackNeeded , reconfigureFunc , err = cn . ReconfigureRuntime ( h . Client , runtimeAPIsOld )
141135 if err != nil {
142136 e := misc .HandleError (err )
143137 return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (e )
@@ -153,7 +147,7 @@ func (h *PostRawConfigurationHandlerImpl) Handle(params configuration.PostHAProx
153147 }
154148 return configuration .NewPostHAProxyConfigurationCreated ().WithPayload (params .Data )
155149 }
156- callbackNeeded , reconfigureFunc , err := h . reconfigureRuntime ( runtimeAPIsOld )
150+ callbackNeeded , reconfigureFunc , err := cn . ReconfigureRuntime ( h . Client , runtimeAPIsOld )
157151 if err != nil {
158152 e := misc .HandleError (err )
159153 return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (e )
@@ -169,67 +163,6 @@ func (h *PostRawConfigurationHandlerImpl) Handle(params configuration.PostHAProx
169163 return configuration .NewPostHAProxyConfigurationAccepted ().WithReloadID (rID ).WithPayload (params .Data )
170164}
171165
172- func (h * PostRawConfigurationHandlerImpl ) reconfigureRuntime (runtimeAPIsOld []* models.RuntimeAPI ) (callbackNeeded bool , callback func (), err error ) {
173- cfg , err := h .Client .Configuration ()
174- if err != nil {
175- return false , nil , err
176- }
177- _ , globalConf , err := cfg .GetGlobalConfiguration ("" )
178- if err != nil {
179- return false , nil , err
180- }
181- runtimeAPIsNew := globalConf .RuntimeAPIs
182- reconfigureRuntime := false
183- if len (runtimeAPIsOld ) != len (runtimeAPIsNew ) {
184- reconfigureRuntime = true
185- } else {
186- for _ , runtimeOld := range runtimeAPIsOld {
187- if runtimeOld .Address == nil {
188- continue
189- }
190- found := false
191- for _ , runtimeNew := range runtimeAPIsNew {
192- if runtimeNew .Address == nil {
193- continue
194- }
195- if * runtimeNew .Address == * runtimeOld .Address {
196- found = true
197- break
198- }
199- }
200- if ! found {
201- reconfigureRuntime = true
202- break
203- }
204- }
205- }
206-
207- if reconfigureRuntime {
208- dpapiCfg := dataplaneapi_config .Get ()
209- haproxyOptions := dpapiCfg .HAProxy
210- return true , func () {
211- var rnt runtime.Runtime
212- i := 1
213- for i < 10 {
214- rnt = cn .ConfigureRuntimeClient (context .Background (), cfg , haproxyOptions )
215- if rnt != nil {
216- break
217- }
218- time .Sleep (time .Duration (i ) * time .Second )
219- i += i // exponential backoof
220- }
221- h .Client .ReplaceRuntime (rnt )
222- if rnt == nil {
223- log .Debugf ("reload callback completed, no runtime API" )
224- } else {
225- log .Debugf ("reload callback completed, runtime API reconfigured" )
226- }
227- }, nil
228- }
229-
230- return false , nil , nil
231- }
232-
233166func executeRuntimeActions (actionsStr string , client client_native.HAProxyClient ) error {
234167 runtime , err := client .Runtime ()
235168 if err != nil {
0 commit comments