From a51e3c7b48a90aff2937bf12ee1f61d58916bcb6 Mon Sep 17 00:00:00 2001 From: Fabiano Parente Date: Sat, 2 Sep 2023 08:07:24 -0300 Subject: [PATCH] BUG/MINOR: prevents port conflicts on startup when using hostNetwork --- deploy/tests/tnr/routeacl/suite_test.go | 5 ----- fs/usr/local/etc/haproxy/haproxy.cfg | 5 ----- pkg/controller/controller.go | 6 +++--- pkg/haproxy/api/api.go | 1 + pkg/haproxy/api/frontend.go | 9 +++++++++ 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/deploy/tests/tnr/routeacl/suite_test.go b/deploy/tests/tnr/routeacl/suite_test.go index cd8231a6..ae5d5eef 100644 --- a/deploy/tests/tnr/routeacl/suite_test.go +++ b/deploy/tests/tnr/routeacl/suite_test.go @@ -71,29 +71,24 @@ stats socket /var/run/haproxy-runtime-api.sock level admin expose-fd listeners default-path config peers localinstance - peer local 127.0.0.1:10000 frontend https mode http -bind 127.0.0.1:8080 name v4 http-request set-var(txn.base) base use_backend %[var(txn.path_match),field(1,.)] frontend http mode http -bind 127.0.0.1:4443 name v4 http-request set-var(txn.base) base use_backend %[var(txn.path_match),field(1,.)] frontend healthz -bind 127.0.0.1:1042 name v4 mode http monitor-uri /healthz option dontlog-normal frontend stats mode http - bind *:1024 name stats stats enable stats uri / stats refresh 10s diff --git a/fs/usr/local/etc/haproxy/haproxy.cfg b/fs/usr/local/etc/haproxy/haproxy.cfg index c2fd2ff4..04fcdd4d 100644 --- a/fs/usr/local/etc/haproxy/haproxy.cfg +++ b/fs/usr/local/etc/haproxy/haproxy.cfg @@ -23,29 +23,24 @@ defaults timeout http-keep-alive 60000 peers localinstance - peer local 127.0.0.1:10000 frontend https mode http - bind 127.0.0.1:8080 name v4 http-request set-var(txn.base) base use_backend %[var(txn.path_match),field(1,.)] frontend http mode http - bind 127.0.0.1:4443 name v4 http-request set-var(txn.base) base use_backend %[var(txn.path_match),field(1,.)] frontend healthz - bind 127.0.0.1:1042 name v4 mode http monitor-uri /healthz option dontlog-normal frontend stats mode http - bind *:1024 name stats http-request set-var(txn.base) base http-request use-service prometheus-exporter if { path /metrics } stats enable diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 82add6fb..4eb1b282 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -195,7 +195,7 @@ func (c *HAProxyController) updateHAProxy() { func (c *HAProxyController) setToReady() { healthzPort := c.osArgs.HealthzBindPort logger.Panic(c.clientAPIClosure(func() error { - return c.haproxy.FrontendBindEdit("healthz", + return c.haproxy.FrontendBindCreate("healthz", models.Bind{ BindParams: models.BindParams{ Name: "v4", @@ -218,7 +218,7 @@ func (c *HAProxyController) setToReady() { logger.Panic(c.clientAPIClosure(func() error { ip := "127.0.0.1" - return c.haproxy.PeerEntryEdit("localinstance", + return c.haproxy.PeerEntryCreate("localinstance", models.PeerEntry{ Name: "local", Address: &ip, @@ -228,7 +228,7 @@ func (c *HAProxyController) setToReady() { })) logger.Panic(c.clientAPIClosure(func() error { - return c.haproxy.FrontendBindEdit("stats", + return c.haproxy.FrontendBindCreate("stats", models.Bind{ BindParams: models.BindParams{ Name: "stats", diff --git a/pkg/haproxy/api/api.go b/pkg/haproxy/api/api.go index 35ac90dc..f1d50e57 100644 --- a/pkg/haproxy/api/api.go +++ b/pkg/haproxy/api/api.go @@ -61,6 +61,7 @@ type HAProxyClient interface { //nolint:interfacebloat GlobalPushConfiguration(models.Global) error GlobalCfgSnippet(snippet []string) error GetMap(mapFile string) (*models.Map, error) + PeerEntryCreate(peerSection string, peer models.PeerEntry) error PeerEntryEdit(peerSection string, peer models.PeerEntry) error RefreshBackends() (deleted []string, err error) SetMapContent(mapFile string, payload []string) error diff --git a/pkg/haproxy/api/frontend.go b/pkg/haproxy/api/frontend.go index b339bfdc..d9b31715 100644 --- a/pkg/haproxy/api/frontend.go +++ b/pkg/haproxy/api/frontend.go @@ -233,3 +233,12 @@ func (c *clientNative) PeerEntryEdit(peerSection string, peerEntry models.PeerEn c.activeTransactionHasChanges = true return configuration.EditPeerEntry(peerEntry.Name, peerSection, &peerEntry, c.activeTransaction, 0) } + +func (c *clientNative) PeerEntryCreate(peerSection string, peerEntry models.PeerEntry) error { + configuration, err := c.nativeAPI.Configuration() + if err != nil { + return err + } + c.activeTransactionHasChanges = true + return configuration.CreatePeerEntry(peerSection, &peerEntry, c.activeTransaction, 0) +}