Skip to content

Commit

Permalink
Merge pull request #181 from bverschueren/haproxyParseDot
Browse files Browse the repository at this point in the history
ensure routes with a dot are parsed correctly
  • Loading branch information
bverschueren authored Sep 13, 2024
2 parents 500faf7 + 65e4999 commit 45e91af
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/haproxy/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func newBackendFromLine(raw []string) *backend {
}

func isBackendBlock(line string) *backend {
backendRe := `^backend ([a-z0-9\-\_]*):([a-z0-9\-\_]*):([a-z0-9\-\_]*)$`
backendRe := `^backend ([a-z0-9\-\_]*):([a-z0-9\-\_]*):([a-z0-9\-\_\.]*)$`
re := regexp.MustCompile(backendRe)

matches := re.FindStringSubmatch(line)
Expand Down
12 changes: 11 additions & 1 deletion cmd/haproxy/backends_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestParseHAProxyConfig(t *testing.T) {
includeOpenShiftNamespaces: false,
expected: []*backend{
&backend{namespace: "testdata", routeName: "rails-postgresql-example", ingressController: "default", service: &service{serviceName: "rails-postgresql-example", port: &port{portNr: 8080, portName: "web"}}, termination: "be_http"},
&backend{namespace: "testdata", routeName: "app.example.com", ingressController: "default", service: &service{serviceName: "hello-node", port: &port{portNr: 8080, portName: ""}}, termination: "be_http"},
&backend{namespace: "other-testdata", routeName: "hello-node-secure", ingressController: "default", service: &service{serviceName: "hello-node", port: &port{portNr: 8080, portName: ""}}, termination: "be_edge_http"}},
},
{
Expand All @@ -45,14 +46,17 @@ func TestParseHAProxyConfig(t *testing.T) {
expected: []*backend{
&backend{namespace: "openshift-monitoring", routeName: "thanos-querier", ingressController: "default", service: &service{serviceName: "thanos-querier", port: &port{portNr: 9091, portName: "web"}}, termination: "be_secure"},
&backend{namespace: "testdata", routeName: "rails-postgresql-example", ingressController: "default", service: &service{serviceName: "rails-postgresql-example", port: &port{portNr: 8080, portName: "web"}}, termination: "be_http"},
&backend{namespace: "testdata", routeName: "app.example.com", ingressController: "default", service: &service{serviceName: "hello-node", port: &port{portNr: 8080, portName: ""}}, termination: "be_http"},
&backend{namespace: "other-testdata", routeName: "hello-node-secure", ingressController: "default", service: &service{serviceName: "hello-node", port: &port{portNr: 8080, portName: ""}}, termination: "be_edge_http"}},
},
{
name: "Parse HAProxy config and extract backends matching a namespace",
configFile: "../../testdata/ingress_controllers/default/router-default-abc123-a1b1c3/haproxy.config",
wantedNamespace: "testdata",
includeOpenShiftNamespaces: true,
expected: []*backend{&backend{namespace: "testdata", routeName: "rails-postgresql-example", ingressController: "default", service: &service{serviceName: "rails-postgresql-example", port: &port{portNr: 8080, portName: "web"}}, termination: "be_http"}},
expected: []*backend{
&backend{namespace: "testdata", routeName: "rails-postgresql-example", ingressController: "default", service: &service{serviceName: "rails-postgresql-example", port: &port{portNr: 8080, portName: "web"}}, termination: "be_http"},
&backend{namespace: "testdata", routeName: "app.example.com", ingressController: "default", service: &service{serviceName: "hello-node", port: &port{portNr: 8080, portName: ""}}, termination: "be_http"}},
},
}

Expand Down Expand Up @@ -134,6 +138,12 @@ func TestIsBackendBlock(t *testing.T) {
includeOpenShift: true,
expected: &backend{termination: "be_edge_http", namespace: "testdata", routeName: "hello-node", ingressController: "", service: (*service)(nil)},
},
{
name: "return backend from valid backend block including dot in route",
line: "backend be_edge_http:testdata:hello-node.example.com",
includeOpenShift: true,
expected: &backend{termination: "be_edge_http", namespace: "testdata", routeName: "hello-node.example.com", ingressController: "", service: (*service)(nil)},
},
{
name: "return nil from invalid backend block",
line: "nonbackend be_edge_http:testdata:hello-node",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ backend be_http:testdata:rails-postgresql-example
cookie 94806193aae7eda5cffd44a87b61d794 insert indirect nocache httponly
server pod:rails-postgresql-example-1-vq49n:rails-postgresql-example:web:10.129.2.11:8080 10.129.2.11:8080 cookie bece57f8fd3ee9e776b3f1746960e4d5 weight 1

backend be_http:testdata:app.example.com
mode http
option redispatch
option forwardfor
balance random

timeout check 5000ms
http-request add-header X-Forwarded-Host %[req.hdr(host)]
http-request add-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto http if !{ ssl_fc }
http-request add-header X-Forwarded-Proto https if { ssl_fc }
http-request add-header X-Forwarded-Proto-Version h2 if { ssl_fc_alpn -i h2 }
http-request add-header Forwarded for=%[src];host=%[req.hdr(host)];proto=%[req.hdr(X-Forwarded-Proto)]
cookie 822b1a92d3efc1a6b2c276f5e2827b3c insert indirect nocache httponly
server pod:hello-node-8dd54cb99-6wsnt:hello-node::10.129.2.132:8080 10.129.2.132:8080 cookie 9d16ea4a111c1b03043d873e9efd86b1 weight 1

# Plain http backend or backend with TLS terminated at the edge or a
# secure backend with re-encryption.
backend be_edge_http:other-testdata:hello-node-secure
Expand Down

0 comments on commit 45e91af

Please sign in to comment.