Skip to content

Commit 78d90b6

Browse files
committed
BUG/MINOR: runtime: fix creating servers with HAProxy >= 3.0
In HAProxy 3.0, the keyword "enabled" is not accepted anymore when creating servers dynamically with the runtime socket (they are enabled by default). Dataplaneapi used to add "enabled" unless the server was marked as in maintenance.
1 parent cc259ef commit 78d90b6

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

handlers/runtime_server.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
client_native "github.com/haproxytech/client-native/v6"
2424
native_errors "github.com/haproxytech/client-native/v6/errors"
2525
"github.com/haproxytech/client-native/v6/models"
26+
cn_runtime "github.com/haproxytech/client-native/v6/runtime"
2627

2728
"github.com/haproxytech/dataplaneapi/misc"
2829
"github.com/haproxytech/dataplaneapi/operations/server"
@@ -168,7 +169,13 @@ func (h *AddRuntimeServerHandlerImpl) Handle(params server.AddRuntimeServerParam
168169
return server.NewAddRuntimeServerBadRequest().WithPayload(&models.Error{Code: &code, Message: &msg})
169170
}
170171

171-
err = runtime.AddServer(params.ParentName, params.Data.Name, SerializeRuntimeAddServer(params.Data))
172+
haversion, err := runtime.GetVersion()
173+
if err != nil {
174+
e := misc.HandleError(err)
175+
return server.NewAddRuntimeServerDefault(int(*e.Code)).WithPayload(e)
176+
}
177+
178+
err = runtime.AddServer(params.ParentName, params.Data.Name, SerializeRuntimeAddServer(params.Data, &haversion))
172179
if err != nil {
173180
msg := err.Error()
174181
switch {
@@ -236,7 +243,7 @@ func (h *DeleteRuntimeServerHandlerImpl) Handle(params server.DeleteRuntimeServe
236243
// SerializeRuntimeAddServer returns a string in the HAProxy config format, suitable
237244
// for the "add server" operation over the control socket.
238245
// Not all the Server attributes are available in this case.
239-
func SerializeRuntimeAddServer(srv *models.RuntimeAddServer) string { //nolint:cyclop,maintidx
246+
func SerializeRuntimeAddServer(srv *models.RuntimeAddServer, version *cn_runtime.HAProxyVersion) string { //nolint:cyclop,maintidx
240247
b := &strings.Builder{}
241248

242249
push := func(s string) {
@@ -311,7 +318,11 @@ func SerializeRuntimeAddServer(srv *models.RuntimeAddServer) string { //nolint:c
311318
case srv.Downinter != nil:
312319
pushi("downinter", srv.Downinter)
313320
case !enabled(srv.Maintenance):
314-
push("enabled")
321+
required := new(cn_runtime.HAProxyVersion)
322+
required.ParseHAProxyVersion("3.0.0")
323+
if !cn_runtime.IsBiggerOrEqual(required, version) {
324+
push("enabled")
325+
}
315326
case srv.ErrorLimit != nil:
316327
pushi("error-limit", srv.ErrorLimit)
317328
case srv.Fall != nil:

handlers/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ func (h *CreateServerHandlerImpl) Handle(parentType cnconstants.CnParentType, pa
145145
return server.NewCreateServerBackendCreated().WithPayload(params.Data)
146146
}
147147
if useRuntime {
148-
err = runtimeClient.AddServer(pName, params.Data.Name, SerializeRuntimeAddServer(ras))
148+
haversion, _ := runtimeClient.GetVersion()
149+
err = runtimeClient.AddServer(pName, params.Data.Name, SerializeRuntimeAddServer(ras, &haversion))
149150
if err == nil {
150151
// No need to reload.
151152
log.Debugf("backend %s: server %s added though runtime", pName, params.Data.Name)

0 commit comments

Comments
 (0)