16
16
package handlers
17
17
18
18
import (
19
- "context"
20
19
"fmt"
21
20
"strconv"
22
21
"strings"
23
- "time"
24
22
25
23
"github.com/go-openapi/runtime/middleware"
26
24
client_native "github.com/haproxytech/client-native/v5"
27
25
"github.com/haproxytech/client-native/v5/models"
28
- "github.com/haproxytech/client-native/v5/runtime"
29
-
30
26
cn "github.com/haproxytech/dataplaneapi/client-native"
31
- dataplaneapi_config "github.com/haproxytech/dataplaneapi/configuration"
32
27
"github.com/haproxytech/dataplaneapi/haproxy"
33
- "github.com/haproxytech/dataplaneapi/log"
34
28
"github.com/haproxytech/dataplaneapi/misc"
35
29
"github.com/haproxytech/dataplaneapi/operations/configuration"
36
30
)
@@ -137,7 +131,7 @@ func (h *PostRawConfigurationHandlerImpl) Handle(params configuration.PostHAProx
137
131
if forceReload {
138
132
var callbackNeeded bool
139
133
var reconfigureFunc func ()
140
- callbackNeeded , reconfigureFunc , err = h . reconfigureRuntime ( runtimeAPIsOld )
134
+ callbackNeeded , reconfigureFunc , err = cn . ReconfigureRuntime ( h . Client , runtimeAPIsOld )
141
135
if err != nil {
142
136
e := misc .HandleError (err )
143
137
return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (e )
@@ -153,7 +147,7 @@ func (h *PostRawConfigurationHandlerImpl) Handle(params configuration.PostHAProx
153
147
}
154
148
return configuration .NewPostHAProxyConfigurationCreated ().WithPayload (params .Data )
155
149
}
156
- callbackNeeded , reconfigureFunc , err := h . reconfigureRuntime ( runtimeAPIsOld )
150
+ callbackNeeded , reconfigureFunc , err := cn . ReconfigureRuntime ( h . Client , runtimeAPIsOld )
157
151
if err != nil {
158
152
e := misc .HandleError (err )
159
153
return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (e )
@@ -169,67 +163,6 @@ func (h *PostRawConfigurationHandlerImpl) Handle(params configuration.PostHAProx
169
163
return configuration .NewPostHAProxyConfigurationAccepted ().WithReloadID (rID ).WithPayload (params .Data )
170
164
}
171
165
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
-
233
166
func executeRuntimeActions (actionsStr string , client client_native.HAProxyClient ) error {
234
167
runtime , err := client .Runtime ()
235
168
if err != nil {
0 commit comments