Skip to content

Commit

Permalink
BUG/MEDIUM: fix add server endpoint for other resources than backend.
Browse files Browse the repository at this point in the history
Moreover, the convertion to the socket param (ie  was always failling because ras was nil.
Though runtime socket, servers can only be added on backend.

Signed-off-by: Vincent Gramer <[email protected]>
  • Loading branch information
vgramer committed Mar 6, 2024
1 parent 78a4e05 commit 0e3db1c
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 19 deletions.
45 changes: 44 additions & 1 deletion e2e/tests/servers/create.bats
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,50 @@ load '../../libs/version'

load 'utils/_helpers'

@test "servers: Add a new server" {
@test "servers: Add a new server to backend" {
resource_post "$_SERVER_BASE_PATH" "data/post.json" "backend=test_backend&force_reload=true"
assert_equal "$SC" 201
}

@test "servers: Add a new server to backend thought runtime with deprecated backend param" {
haproxy_version_ge 2.6 || skip "requires HAProxy 2.6+"
pre_logs_count=$(dpa_docker_exec 'cat /var/log/dataplaneapi.log' | wc -l)

resource_post "$_SERVER_BASE_PATH" "data/post.json" "backend=test_backend"
assert_equal "$SC" 201

# check that server has been added thought runtime socket
post_logs_count=$(dpa_docker_exec 'sh /var/log/dataplaneapi.log' | wc -l)
new_logs_count=$(( $pre_logs_count - $post_logs_count ))
new_logs=$(dpa_docker_exec 'cat /var/log/dataplaneapi.log' | tail -n $new_logs_count)

echo "$new_logs" # this will help debugging if the test fails
assert echo -e "$new_logs" | grep -q "backend test_backend: server test_server added though runtime"
}

@test "servers: Add a new server to backend thought runtime with parent_type/ parent_name" {
haproxy_version_ge 2.6 || skip "requires HAProxy 2.6+"
pre_logs_count=$(dpa_docker_exec 'cat /var/log/dataplaneapi.log' | wc -l)

resource_post "$_SERVER_BASE_PATH" "data/post.json" "parent_type=backend&parent_name=test_backend"
assert_equal "$SC" 201

# check that server has been added thought runtime socket
post_logs_count=$(dpa_docker_exec 'sh /var/log/dataplaneapi.log' | wc -l)
new_logs_count=$(( $pre_logs_count - $post_logs_count ))
new_logs=$(dpa_docker_exec 'cat /var/log/dataplaneapi.log' | tail -n $new_logs_count)

echo "$new_logs" # this will help debugging if the test fails
assert echo -e "$new_logs" | grep -q "backend test_backend: server test_server added though runtime"
}

@test "servers: Add a new server to peer" {
resource_post "$_SERVER_BASE_PATH" "data/post.json" "parent_type=peers&parent_name=fusion"
assert_equal "$SC" 202
}

@test "servers: Add a new server to ring" {
haproxy_version_ge 2.2 || skip "requires HAProxy 2.2+"
resource_post "$_SERVER_BASE_PATH" "data/post.json" "parent_type=ring&parent_name=logbuffer"
assert_equal "$SC" 202
}
3 changes: 3 additions & 0 deletions e2e/tests/servers/data/haproxy.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ defaults
timeout http-keep-alive 10s
retries 3

peers fusion
peer peer1 127.0.0.1:5108

backend test_backend
mode http
balance roundrobin
Expand Down
50 changes: 50 additions & 0 deletions e2e/tests/servers/data/haproxy_2_2.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
global
chroot /var/lib/haproxy
user haproxy
group haproxy
maxconn 4000
pidfile /var/run/haproxy.pid
stats socket /var/lib/haproxy/stats level admin
log 127.0.0.1 local2

defaults
mode http
maxconn 3000
log global
option httplog
option redispatch
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
timeout http-request 10s
timeout check 10s
timeout connect 10s
timeout client 1m
timeout queue 1m
timeout server 1m
timeout http-keep-alive 10s
retries 3

peers fusion
peer peer1 127.0.0.1:5108

ring logbuffer
description "buffer for logs"
format rfc5424
maxlen 1500
size 65536
timeout connect 10s
timeout server 20s

# Sends outgoing messages via TCP
server logserver 192.168.1.100:514

backend test_backend
mode http
balance roundrobin
option forwardfor
server server_01 10.1.1.1:8080 check weight 80
server server_02 10.1.1.2:8080 check weight 80
server server_03 10.1.1.2:8080 check weight 80
server server_ipv6 [fd00:6:48:c85:deb:f:62:4]:80 check
server server_04 192.168.1.1:80 check resolve-opts allow-dup-ip,ignore-weight resolve-net 10.0.0.0/8,10.200.200.0/12
42 changes: 24 additions & 18 deletions handlers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package handlers
import (
"fmt"

"github.com/haproxytech/client-native/v6/runtime"

"github.com/go-openapi/runtime/middleware"
client_native "github.com/haproxytech/client-native/v6"
"github.com/haproxytech/client-native/v6/models"
Expand Down Expand Up @@ -108,25 +110,28 @@ func (h *CreateServerHandlerImpl) Handle(params server.CreateServerParams, princ
return server.NewCreateServerDefault(int(*e.Code)).WithPayload(e)
}

// Try to create the new server dynamically. This is only possible if no `default_server`
// Try to create the new server dynamically. This is only possible if parentType is `backend` and no `default_server`
// was defined in the current backend or in the `defaults` section.
useRuntime := false
var ras *models.RuntimeAddServer
_, defaults, err := configuration.GetDefaultsConfiguration(t)
if err != nil {
e := misc.HandleError(err)
return server.NewCreateServerDefault(int(*e.Code)).WithPayload(e)
}
_, backend, err := configuration.GetBackend(pName, t)
if err != nil {
e := misc.HandleError(err)
return server.NewCreateServerDefault(int(*e.Code)).WithPayload(e)
}
runtime, err := h.Client.Runtime()
if err == nil && defaults.DefaultServer == nil && backend.DefaultServer == nil {
// Also make sure the server attributes are supported by the runtime API.
err = misc.ConvertStruct(params.Data, ras)
useRuntime = err == nil
ras := &models.RuntimeAddServer{}
var runtimeClient runtime.Runtime
if pType == "backend" {
_, defaults, errRuntime := configuration.GetDefaultsConfiguration(t)
if errRuntime != nil {
e := misc.HandleError(errRuntime)
return server.NewCreateServerDefault(int(*e.Code)).WithPayload(e)
}
_, backend, errRuntime := configuration.GetBackend(pName, t)
if errRuntime != nil {
e := misc.HandleError(errRuntime)
return server.NewCreateServerDefault(int(*e.Code)).WithPayload(e)
}
runtimeClient, errRuntime = h.Client.Runtime()
if errRuntime == nil && defaults.DefaultServer == nil && backend.DefaultServer == nil {
// Also make sure the server attributes are supported by the runtime API.
errRuntime = misc.ConvertStruct(params.Data, ras)
useRuntime = errRuntime == nil
}
}

if params.TransactionID == nil {
Expand All @@ -139,9 +144,10 @@ func (h *CreateServerHandlerImpl) Handle(params server.CreateServerParams, princ
return server.NewCreateServerCreated().WithPayload(params.Data)
}
if useRuntime {
err = runtime.AddServer(pName, params.Data.Name, SerializeRuntimeAddServer(ras))
err = runtimeClient.AddServer(pName, params.Data.Name, SerializeRuntimeAddServer(ras))
if err == nil {
// No need to reload.
log.Debugf("backend %s: server %s added though runtime", pName, params.Data.Name)
return server.NewCreateServerCreated().WithPayload(params.Data)
}
log.Warning("failed to add server through runtime:", err)
Expand Down

0 comments on commit 0e3db1c

Please sign in to comment.