From 4d65c7fa746e5bac5f4a9cd4d1718d2eba803366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20W=C3=B6ckinger?= Date: Fri, 23 Sep 2022 10:46:17 +0200 Subject: [PATCH] Fixes #419: Allow configuration of haproxy option persist --- .../haproxy/conf/haproxy-config.template | 3 ++ pkg/router/router_test.go | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/images/router/haproxy/conf/haproxy-config.template b/images/router/haproxy/conf/haproxy-config.template index 618ee89dc..93ddf3eaa 100644 --- a/images/router/haproxy/conf/haproxy-config.template +++ b/images/router/haproxy/conf/haproxy-config.template @@ -472,6 +472,9 @@ backend openshift_default backend {{ genBackendNamePrefix $cfg.TLSTermination }}:{{ $cfgIdx }} mode http option redispatch + {{- if isTrue (index $cfg.Annotations "haproxy.router.openshift.io/persist") }} + option persist + {{- end }} {{- with $setHeaders := firstMatch $setForwardedHeadersPattern (index $cfg.Annotations $setForwardedHeadersAnnotation) $setForwardedHeadersDefaultValue }} {{- if eq $setHeaders "append" }} option forwardfor diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index 544e817d1..77f94c99a 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -244,6 +244,26 @@ func TestConfigTemplate(t *testing.T) { }, }, }, + "Forced persistence on down servers": { + mustCreateWithConfig{ + mustCreate: mustCreate{ + name: "p", + host: "pexample.com", + path: "", + time: start, + annotations: map[string]string{ + "haproxy.router.openshift.io/persist": "true", + }, + tlsTermination: routev1.TLSTerminationEdge, + }, + mustMatchConfig: mustMatchConfig{ + section: "backend", + sectionName: edgeBackendName(h.namespace, "p"), + attribute: "option", + value: "persist", + }, + }, + }, "Simple HSTS header": { mustCreateWithConfig{ mustCreate: mustCreate{ @@ -514,6 +534,18 @@ type mustMatchConfig struct { } func (m mustMatchConfig) Match(parser haproxyconfparser.Parser) error { + if m.attribute == "option" { + cfg := fmt.Sprintf("%s", parser) + start :=strings.Index(cfg, m.sectionName) + backends := cfg[start:] + end := strings.Index(backends, "backend") + backend := backends[:end] + if strings.Contains(backend, "option " + m.value) { + return nil + } + return fmt.Errorf("unable to find option [%s]", m.value) + } + data, err := parser.Get(haproxyconfparser.Section(m.section), m.sectionName, m.attribute) if err != nil { if m.notFound {