Skip to content

Commit 1492302

Browse files
committed
fix(tcp-balancer): rename backends and pin HAProxy so the chart starts on 3.3+
haproxy:latest now resolves to 3.3 and newer, where a backend named like a frontend is a fatal config error, so every tcp-balancer pod fails at config parse. Backends get a _backend suffix and the image follows the chart appVersion on the 3.4 LTS line instead of a floating tag. The whitelist rules render inside one guard. On the kubernetes and talos frontends an empty whitelist means no filtering; whitelistHTTP with an empty whitelist fails the render, because a security opt-in that quietly became a no-op would not be noticed the way a crash is. The unittest suite pins the rendered config for the default, whitelist and failure cases and the image pin, so a template refactor cannot bring the collision or the floating tag back. Fixes #1966 Signed-off-by: Yan Bondarenko <[email protected]>
1 parent 1c88014 commit 1492302

6 files changed

Lines changed: 191 additions & 18 deletions

File tree

‎packages/apps/tcp-balancer/Chart.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ description: Layer4 load balancer service
44
icon: /logos/haproxy.svg
55
type: application
66
version: 0.0.0 # Placeholder, the actual version will be automatically set during the build process
7-
appVersion: "2.9.7"
7+
appVersion: "3.4.4"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
include ../../../hack/package.mk
22

3+
.PHONY: generate test
4+
35
generate:
46
cozyvalues-gen -m 'tcpbalancer' -v values.yaml -s values.schema.json -r README.md -g ../../../api/apps/v1alpha1/tcpbalancer/types.go
57
../../../hack/update-crd.sh
8+
9+
test:
10+
helm unittest .

‎packages/apps/tcp-balancer/templates/configmap.yaml‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,23 @@ data:
3434
timeout check 10s
3535
3636
{{- with .Values.httpAndHttps }}
37+
{{- if and $.Values.whitelistHTTP (not $.Values.whitelist) }}
38+
{{- fail "whitelistHTTP is enabled but whitelist is empty" }}
39+
{{- end }}
3740
{{- if .targetPorts.http }}
3841
frontend http
3942
bind :::8080 v4v6
4043
mode tcp
41-
default_backend http
44+
default_backend http_backend
4245
{{- if $.Values.whitelistHTTP }}
43-
{{- with $.Values.whitelist }}
44-
acl whitelist src{{ range . }} {{ . }}{{ end }}
45-
{{- end }}
46+
acl whitelist src{{ range $.Values.whitelist }} {{ . }}{{ end }}
4647
acl all src 0.0.0.0
4748
tcp-request content accept if whitelist
4849
tcp-request content reject
4950
{{- end }}
5051
tcp-request content set-dst-port int(80)
5152
52-
backend http
53+
backend http_backend
5354
{{- include "backendoptions" (default "tcp" .mode) | nindent 8 }}
5455
{{- range $num, $ep := .endpoints }}
5556
server srv{{ $num }} {{ $ep }}:{{ $.Values.httpAndHttps.targetPorts.http }} check
@@ -64,18 +65,16 @@ data:
6465
{{- else }}
6566
mode tcp
6667
{{- end }}
67-
default_backend https
68+
default_backend https_backend
6869
{{- if $.Values.whitelistHTTP }}
69-
{{- with $.Values.whitelist }}
70-
acl whitelist src{{ range . }} {{ . }}{{ end }}
71-
{{- end }}
70+
acl whitelist src{{ range $.Values.whitelist }} {{ . }}{{ end }}
7271
acl all src 0.0.0.0
7372
tcp-request content accept if whitelist
7473
tcp-request content reject
7574
{{- end }}
7675
tcp-request content set-dst-port int(443)
7776
78-
backend https
77+
backend https_backend
7978
{{- include "backendoptions" (default "tcp" .mode) | nindent 8 }}
8079
{{- range $num, $ep := .endpoints }}
8180
server srv{{ $num }} {{ $ep }}:{{ $.Values.httpAndHttps.targetPorts.https }} check
@@ -89,32 +88,32 @@ data:
8988
frontend kubernetes
9089
bind :::6443 v4v6
9190
mode tcp
92-
default_backend kubernetes
91+
default_backend kubernetes_backend
9392
{{- with $.Values.whitelist }}
9493
acl whitelist src{{ range . }} {{ . }}{{ end }}
95-
{{- end }}
9694
acl all src 0.0.0.0
9795
tcp-request content accept if whitelist
9896
tcp-request content reject
97+
{{- end }}
9998
10099
frontend talos
101100
bind :::50000 v4v6
102101
mode tcp
103-
default_backend talos
102+
default_backend talos_backend
104103
{{- with $.Values.whitelist }}
105104
acl whitelist src{{ range . }} {{ . }}{{ end }}
106-
{{- end }}
107105
acl all src 0.0.0.0
108106
tcp-request content accept if whitelist
109107
tcp-request content reject
108+
{{- end }}
110109
111-
backend kubernetes
110+
backend kubernetes_backend
112111
{{- include "backendoptions" (default "tcp" .mode) | nindent 8 }}
113112
{{- range $num, $ep := .endpoints }}
114113
server srv{{ $num }} {{ $ep }}:6443 check
115114
{{- end }}
116115
117-
backend talos
116+
backend talos_backend
118117
{{- include "backendoptions" (default "tcp" .mode) | nindent 8 }}
119118
{{- range $num, $ep := .endpoints }}
120119
server srv{{ $num }} {{ $ep }}:50000 check

‎packages/apps/tcp-balancer/templates/deployment.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ spec:
3232
- {{ .Release.Name }}-haproxy
3333
topologyKey: kubernetes.io/hostname
3434
containers:
35-
- image: haproxy:latest
35+
- image: haproxy:{{ .Chart.AppVersion }}
3636
name: haproxy
3737
resources: {{- include "cozy-lib.resources.defaultingSanitize" (list .Values.resourcesPreset .Values.resources $) | nindent 10 }}
3838
ports:
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
suite: haproxy deployment
2+
3+
templates:
4+
- templates/deployment.yaml
5+
- templates/configmap.yaml
6+
7+
tests:
8+
- it: pins the haproxy image to the chart appVersion
9+
template: templates/deployment.yaml
10+
asserts:
11+
- equal:
12+
path: spec.template.spec.containers[0].image
13+
value: haproxy:3.4.4
14+
- notMatchRegex:
15+
path: spec.template.spec.containers[0].image
16+
pattern: ':latest$'
17+
18+
- it: follows the chart appVersion when it changes
19+
template: templates/deployment.yaml
20+
chart:
21+
appVersion: 9.9.9
22+
asserts:
23+
- equal:
24+
path: spec.template.spec.containers[0].image
25+
value: haproxy:9.9.9
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
suite: haproxy config
2+
3+
templates:
4+
- templates/configmap.yaml
5+
6+
tests:
7+
- it: names backends so they never collide with frontends
8+
asserts:
9+
- matchRegex:
10+
path: data["haproxy.cfg"]
11+
pattern: '(?m)^frontend http$'
12+
- matchRegex:
13+
path: data["haproxy.cfg"]
14+
pattern: '(?m)^backend http_backend$'
15+
- matchRegex:
16+
path: data["haproxy.cfg"]
17+
pattern: '(?m)^\s+default_backend http_backend$'
18+
- notMatchRegex:
19+
path: data["haproxy.cfg"]
20+
pattern: '(?m)^backend http$'
21+
- matchRegex:
22+
path: data["haproxy.cfg"]
23+
pattern: '(?m)^frontend https$'
24+
- matchRegex:
25+
path: data["haproxy.cfg"]
26+
pattern: '(?m)^backend https_backend$'
27+
- matchRegex:
28+
path: data["haproxy.cfg"]
29+
pattern: '(?m)^\s+default_backend https_backend$'
30+
- notMatchRegex:
31+
path: data["haproxy.cfg"]
32+
pattern: '(?m)^backend https$'
33+
34+
- it: renders http frontends without whitelist rules by default
35+
asserts:
36+
- notMatchRegex:
37+
path: data["haproxy.cfg"]
38+
pattern: 'acl whitelist'
39+
- notMatchRegex:
40+
path: data["haproxy.cfg"]
41+
pattern: 'tcp-request content accept if whitelist'
42+
43+
- it: fails when whitelistHTTP is enabled with an empty whitelist
44+
set:
45+
whitelistHTTP: true
46+
whitelist: []
47+
asserts:
48+
- failedTemplate:
49+
errorPattern: 'whitelistHTTP is enabled but whitelist is empty'
50+
51+
- it: filters http and https when whitelistHTTP is enabled with a whitelist
52+
set:
53+
whitelistHTTP: true
54+
whitelist:
55+
- 10.0.0.0/8
56+
- 192.168.1.1
57+
asserts:
58+
- matchRegex:
59+
path: data["haproxy.cfg"]
60+
pattern: '(?m)^frontend http$\n(?:.*\n){0,5}?\s+acl whitelist src 10\.0\.0\.0/8 192\.168\.1\.1$'
61+
- matchRegex:
62+
path: data["haproxy.cfg"]
63+
pattern: '(?m)^frontend https$\n(?:.*\n){0,8}?\s+acl whitelist src 10\.0\.0\.0/8 192\.168\.1\.1$'
64+
- matchRegex:
65+
path: data["haproxy.cfg"]
66+
pattern: '(?m)^\s+tcp-request content accept if whitelist$'
67+
- matchRegex:
68+
path: data["haproxy.cfg"]
69+
pattern: '(?m)^\s+tcp-request content reject$'
70+
71+
- it: renames kubernetes and talos backends
72+
set:
73+
kubernetesAndTalos:
74+
endpoints:
75+
- 10.1.1.1
76+
- 10.1.1.2
77+
asserts:
78+
- matchRegex:
79+
path: data["haproxy.cfg"]
80+
pattern: '(?m)^frontend kubernetes$'
81+
- matchRegex:
82+
path: data["haproxy.cfg"]
83+
pattern: '(?m)^\s+default_backend kubernetes_backend$'
84+
- matchRegex:
85+
path: data["haproxy.cfg"]
86+
pattern: '(?m)^backend kubernetes_backend$'
87+
- notMatchRegex:
88+
path: data["haproxy.cfg"]
89+
pattern: '(?m)^backend kubernetes$'
90+
- matchRegex:
91+
path: data["haproxy.cfg"]
92+
pattern: '(?m)^frontend talos$'
93+
- matchRegex:
94+
path: data["haproxy.cfg"]
95+
pattern: '(?m)^\s+default_backend talos_backend$'
96+
- matchRegex:
97+
path: data["haproxy.cfg"]
98+
pattern: '(?m)^backend talos_backend$'
99+
- notMatchRegex:
100+
path: data["haproxy.cfg"]
101+
pattern: '(?m)^backend talos$'
102+
- matchRegex:
103+
path: data["haproxy.cfg"]
104+
pattern: '(?m)^\s+server srv0 10\.1\.1\.1:6443 check\s*$'
105+
- matchRegex:
106+
path: data["haproxy.cfg"]
107+
pattern: '(?m)^\s+server srv1 10\.1\.1\.2:50000 check\s*$'
108+
109+
- it: renders kubernetes and talos without whitelist rules when the whitelist is empty
110+
set:
111+
kubernetesAndTalos:
112+
endpoints:
113+
- 10.1.1.1
114+
asserts:
115+
- notMatchRegex:
116+
path: data["haproxy.cfg"]
117+
pattern: 'acl whitelist'
118+
- notMatchRegex:
119+
path: data["haproxy.cfg"]
120+
pattern: 'tcp-request content accept if whitelist'
121+
- notMatchRegex:
122+
path: data["haproxy.cfg"]
123+
pattern: 'tcp-request content reject'
124+
125+
- it: filters kubernetes and talos when a whitelist is set
126+
set:
127+
kubernetesAndTalos:
128+
endpoints:
129+
- 10.1.1.1
130+
whitelist:
131+
- 10.0.0.0/8
132+
asserts:
133+
- matchRegex:
134+
path: data["haproxy.cfg"]
135+
pattern: '(?m)^frontend kubernetes$\n(?:.*\n){0,3}?\s+acl whitelist src 10\.0\.0\.0/8$'
136+
- matchRegex:
137+
path: data["haproxy.cfg"]
138+
pattern: '(?m)^frontend talos$\n(?:.*\n){0,3}?\s+acl whitelist src 10\.0\.0\.0/8$'
139+
- matchRegex:
140+
path: data["haproxy.cfg"]
141+
pattern: '(?m)^\s+tcp-request content accept if whitelist$'
142+
- matchRegex:
143+
path: data["haproxy.cfg"]
144+
pattern: '(?m)^\s+tcp-request content reject$'

0 commit comments

Comments
 (0)