Skip to content

Commit 301a7fe

Browse files
authored
fix(nats): merge config.merge.accounts with the generated accounts map (#4134)
## What this PR does Fixes #4084. With `users` set, `templates/nats.yaml` rendered `config.merge.accounts.A.users` from the generated passwords and then appended `.Values.config.merge` verbatim. A tenant `config.merge` that also carried `accounts`, which is the only place to set `accounts.A.jetstream` for the generated account, produced the key twice in the HelmRelease values, and the install failed at post-render with `mapping key "accounts" already defined`. The template now builds a single `merge` map: a deep copy of `.Values.config.merge` with the generated users merged in under `accounts.A.users` via `mergeOverwrite`. The generated user list wins for account `A`; other keys under `accounts.A`, other accounts and top-level keys such as `max_payload` are kept; a `config.merge` without `users` passes through unchanged; and no `merge` block is rendered when neither is set. The values contract does not change. `helm unittest`: 67/67 in the chart. The new `tests/config_merge_test.yaml` covers the six shapes above; against the previous template the same suite fails on the three cases that combine `users` with `config.merge.accounts`. ### Downstream repositories Template and tests only: no values, schema, README or defaults change. - [x] No downstream repository is affected by this change - [ ] [cozystack/website](https://github.com/cozystack/website) - follow-up: - [ ] [cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack) - follow-up: - [ ] [cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack) - follow-up: - [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up: - [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up: - [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) - follow-up: - [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) - follow-up: - [ ] [cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server) - follow-up: - [ ] [cozystack/external-apps-example](https://github.com/cozystack/external-apps-example) - follow-up: - [ ] [cozystack/examples](https://github.com/cozystack/examples) - follow-up: - [ ] [cozystack/community](https://github.com/cozystack/community) - follow-up: ### Release note ```release-note fix(nats): merge config.merge.accounts with the generated accounts instead of rendering the key twice ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Improved NATS configuration merging so generated user credentials and custom account settings are combined consistently. - Preserved tenant-listed users, additional accounts, and top-level configuration during merges. - Prevented empty merge sections from being rendered when no users or custom merge settings are configured. - **Tests** - Added coverage for user generation, account merging, configuration preservation, and empty-configuration scenarios. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents a04887d + 334f6ad commit 301a7fe

2 files changed

Lines changed: 144 additions & 12 deletions

File tree

‎packages/apps/nats/templates/nats.yaml‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
{{- end }}
1818
{{- end }}
1919

20+
{{- $merge := dict }}
21+
{{- with .Values.config.merge }}
22+
{{- $merge = deepCopy . }}
23+
{{- end }}
24+
{{- if $passwords }}
25+
{{- $users := list }}
26+
{{- range $username, $password := $passwords }}
27+
{{- $users = append $users (dict "user" $username "password" $password) }}
28+
{{- end }}
29+
{{- $merge = mergeOverwrite $merge (dict "accounts" (dict "A" (dict "users" $users))) }}
30+
{{- end }}
31+
2032
{{- if .Values.users }}
2133
apiVersion: v1
2234
kind: Secret
@@ -64,20 +76,9 @@ spec:
6476
secretName: {{ .Release.Name }}-ca
6577
{{- end }}
6678
config:
67-
{{- if or $passwords .Values.config.merge }}
79+
{{- with $merge }}
6880
merge:
69-
{{- if $passwords }}
70-
accounts:
71-
A:
72-
users:
73-
{{- range $username, $password := $passwords }}
74-
- user: "{{ $username }}"
75-
password: "{{ $password }}"
76-
{{- end }}
77-
{{- end }}
78-
{{- with .Values.config.merge }}
7981
{{- toYaml . | nindent 10 }}
80-
{{- end }}
8182
{{- end }}
8283
{{- with .Values.config.resolver }}
8384
resolver:
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
suite: nats config.merge and generated accounts
2+
3+
release:
4+
name: my-nats
5+
namespace: tenant-test
6+
7+
set:
8+
_cluster:
9+
cluster-domain: cozy.local
10+
11+
templates:
12+
- templates/nats.yaml
13+
14+
tests:
15+
- it: renders the generated users under accounts.A when users are set
16+
set:
17+
users:
18+
app:
19+
password: secret
20+
documentSelector:
21+
path: kind
22+
value: HelmRelease
23+
asserts:
24+
- equal:
25+
path: spec.values.nats.config.merge.accounts.A.users
26+
value:
27+
- user: app
28+
password: secret
29+
- notExists:
30+
path: spec.values.nats.config.merge.jetstream
31+
32+
- it: merges a tenant accounts block into the generated accounts map
33+
set:
34+
users:
35+
app:
36+
password: secret
37+
config:
38+
merge:
39+
accounts:
40+
A:
41+
jetstream: enabled
42+
documentSelector:
43+
path: kind
44+
value: HelmRelease
45+
asserts:
46+
- equal:
47+
path: spec.values.nats.config.merge.accounts.A.jetstream
48+
value: enabled
49+
- equal:
50+
path: spec.values.nats.config.merge.accounts.A.users
51+
value:
52+
- user: app
53+
password: secret
54+
- lengthEqual:
55+
path: spec.values.nats.config.merge.accounts.A.users
56+
count: 1
57+
58+
- it: keeps the generated users when the tenant block also lists users for account A
59+
set:
60+
users:
61+
app:
62+
password: secret
63+
config:
64+
merge:
65+
accounts:
66+
A:
67+
users:
68+
- user: stray
69+
password: stray
70+
documentSelector:
71+
path: kind
72+
value: HelmRelease
73+
asserts:
74+
- equal:
75+
path: spec.values.nats.config.merge.accounts.A.users
76+
value:
77+
- user: app
78+
password: secret
79+
80+
- it: keeps other accounts and top-level merge keys next to the generated one
81+
set:
82+
users:
83+
app:
84+
password: secret
85+
config:
86+
merge:
87+
max_payload: 4MB
88+
accounts:
89+
B:
90+
jetstream: enabled
91+
documentSelector:
92+
path: kind
93+
value: HelmRelease
94+
asserts:
95+
- equal:
96+
path: spec.values.nats.config.merge.max_payload
97+
value: 4MB
98+
- equal:
99+
path: spec.values.nats.config.merge.accounts.B.jetstream
100+
value: enabled
101+
- equal:
102+
path: spec.values.nats.config.merge.accounts.A.users[0].user
103+
value: app
104+
105+
- it: passes config.merge through unchanged when there are no users
106+
set:
107+
config:
108+
merge:
109+
max_payload: 4MB
110+
accounts:
111+
A:
112+
jetstream: enabled
113+
documentSelector:
114+
path: kind
115+
value: HelmRelease
116+
asserts:
117+
- equal:
118+
path: spec.values.nats.config.merge
119+
value:
120+
max_payload: 4MB
121+
accounts:
122+
A:
123+
jetstream: enabled
124+
125+
- it: renders no merge block when neither users nor config.merge are set
126+
documentSelector:
127+
path: kind
128+
value: HelmRelease
129+
asserts:
130+
- notExists:
131+
path: spec.values.nats.config.merge

0 commit comments

Comments
 (0)