Skip to content

Commit 1ac9398

Browse files
committed
fix(tenant): emit full ancestor label chain on tenant namespaces
The tenant.cozystack.io/<ancestor> namespace labels drive the <ancestor>-egress CiliumClusterwideNetworkPolicy, which lets an ancestor tenant reach a descendant namespace only when the descendant carries tenant.cozystack.io/<ancestor>. namespace.yaml derived that chain by splitting .Release.Namespace, which only reached one level up: a direct child of tenant-root got tenant-root, but a grandchild (tenant-ktj-htdev under tenant-ktj) got only tenant-ktj and dropped tenant-root entirely. The root ingress controller in tenant-root then could not reach nested tenants, surfacing as upstream timeouts. Derive the chain deterministically from the namespace name instead. Every tenant descends from tenant-root, and tenant.name builds each child as <parent-namespace>-<word>, so every progressive dash-prefix of the name is a real ancestor namespace. This needs no cluster lookup, renders offline (so it is unit-testable), and converges on every reconcile regardless of parent/child HelmRelease ordering. The ownerReference lookup is made non-fatal so the template still renders when the parent namespace lookup is empty; in a real reconcile the parent always exists and the ownerReference is always emitted. Signed-off-by: IvanHunters <[email protected]>
1 parent 71ca2b1 commit 1ac9398

3 files changed

Lines changed: 136 additions & 11 deletions

File tree

‎packages/apps/tenant/templates/_helpers.tpl‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,38 @@
5353
typing, so the "key absent" form is what distinguishes "unset"
5454
from explicit `false`.
5555
*/}}
56+
{{/*
57+
tenant.ancestorTenantLabels emits the full set of
58+
`tenant.cozystack.io/<ancestor>: ""` namespace labels for the tenant whose
59+
namespace name is passed as the single argument (e.g. "tenant-ktj-htdev").
60+
61+
Every tenant descends from tenant-root, so that label is always emitted.
62+
The remaining ancestors are encoded in the namespace name itself: tenant.name
63+
constructs each child namespace as `<parent-namespace>-<word>`, so every
64+
progressive dash-prefix of the name is a real ancestor namespace
65+
(tenant-ktj-htdev -> tenant-ktj, tenant-ktj-htdev). The last prefix is the
66+
tenant's own name, so its self-label is included too.
67+
68+
Deriving the chain from the name (rather than from a lookup of the parent
69+
namespace's labels) is deterministic: it needs no cluster state, renders
70+
identically offline, and converges on every reconcile regardless of the
71+
order in which parent and child HelmReleases reconcile. It replaces an
72+
earlier splitList over `.Release.Namespace` that only ever reached one level
73+
up and therefore dropped tenant-root for tenants at depth >= 2 — breaking the
74+
`<ancestor>-egress` CiliumClusterwideNetworkPolicy that grants an ancestor
75+
reachability to its descendants via the `tenant.cozystack.io/<ancestor>`
76+
namespace label.
77+
*/}}
78+
{{- define "tenant.ancestorTenantLabels" -}}
79+
{{- $parts := splitList "-" . -}}
80+
tenant.cozystack.io/tenant-root: ""
81+
{{- range $i, $v := $parts }}
82+
{{- if ne $i 0 }}
83+
{{ printf "tenant.cozystack.io/%s: \"\"" (join "-" (slice $parts 0 (add $i 1))) }}
84+
{{- end }}
85+
{{- end }}
86+
{{- end -}}
87+
5688
{{- define "tenant.gatewayEffective" -}}
5789
{{- if kindIs "invalid" .Values.gateway -}}
5890
false

‎packages/apps/tenant/templates/namespace.yaml‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
{{/* Lookup for namespace uid (needed for ownerReferences) */}}
1+
{{/*
2+
Lookup the parent namespace, used below to stamp the child namespace's
3+
ownerReference uid. During a real helm-controller reconcile the parent
4+
namespace (.Release.Namespace) always exists, so the ownerReference is
5+
always emitted. The lookup returns empty only when rendering offline
6+
(helm template / helm unittest), where the ownerReference is simply omitted
7+
so the rest of the template — notably the ancestor labels below, which are
8+
derived purely from the namespace name and need no cluster state — still
9+
renders and can be unit-tested.
10+
*/}}
211
{{- $existingNS := lookup "v1" "Namespace" "" .Release.Namespace }}
3-
{{- if not $existingNS }}
4-
{{- fail (printf "error lookup existing namespace: %s" .Release.Namespace) }}
5-
{{- end }}
612

713
{{- if ne (include "tenant.name" .) "tenant-root" }}
814
{{/* Compute namespace values once for use in both Secret and labels */}}
@@ -75,13 +81,7 @@ metadata:
7581
name: {{ $tenantName }}
7682
{{- if hasPrefix "tenant-" .Release.Namespace }}
7783
labels:
78-
tenant.cozystack.io/{{ $tenantName }}: ""
79-
{{- $parts := splitList "-" .Release.Namespace }}
80-
{{- range $i, $v := $parts }}
81-
{{- if ne $i 0 }}
82-
tenant.cozystack.io/{{ join "-" (slice $parts 0 (add $i 1)) }}: ""
83-
{{- end }}
84-
{{- end }}
84+
{{- include "tenant.ancestorTenantLabels" $tenantName | nindent 4 }}
8585
{{/* Labels for network policies */}}
8686
namespace.cozystack.io/etcd: {{ $etcd | quote }}
8787
namespace.cozystack.io/ingress: {{ $ingress | quote }}
@@ -93,6 +93,7 @@ metadata:
9393
scheduler.cozystack.io/scheduling-class: {{ . | quote }}
9494
{{- end }}
9595
alpha.kubevirt.io/auto-memory-limits-ratio: "1.0"
96+
{{- if $existingNS }}
9697
ownerReferences:
9798
- apiVersion: v1
9899
blockOwnerDeletion: true
@@ -101,6 +102,7 @@ metadata:
101102
name: {{ .Release.Namespace }}
102103
uid: {{ $existingNS.metadata.uid }}
103104
{{- end }}
105+
{{- end }}
104106
---
105107
apiVersion: v1
106108
kind: Secret
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
suite: tenant namespace ancestor labels
2+
# Regression cover for the tenant.cozystack.io/<ancestor> namespace labels
3+
# emitted by namespace.yaml. These labels drive the <ancestor>-egress
4+
# CiliumClusterwideNetworkPolicy (networkpolicy.yaml): an ancestor tenant is
5+
# allowed to reach any namespace carrying tenant.cozystack.io/<ancestor>, so
6+
# every tenant namespace MUST carry the label of each of its ancestors,
7+
# tenant-root included, at every depth.
8+
#
9+
# The labels are derived from the namespace name by tenant.ancestorTenantLabels
10+
# (see _helpers.tpl) with no cluster lookup, so helm-unittest can render them
11+
# offline — the ownerReference lookup is the only cluster-coupled part of
12+
# namespace.yaml and is skipped when the parent namespace lookup is empty.
13+
templates:
14+
- templates/namespace.yaml
15+
set:
16+
_cluster:
17+
root-host: example.com
18+
tests:
19+
- it: depth-1 tenant (direct child of root) carries tenant-root and its own label
20+
release:
21+
name: tenant-ktj
22+
namespace: tenant-root
23+
asserts:
24+
- documentIndex: 0
25+
isKind:
26+
of: Namespace
27+
- documentIndex: 0
28+
equal:
29+
path: metadata.name
30+
value: tenant-ktj
31+
- documentIndex: 0
32+
equal:
33+
path: metadata.labels["tenant.cozystack.io/tenant-root"]
34+
value: ""
35+
- documentIndex: 0
36+
equal:
37+
path: metadata.labels["tenant.cozystack.io/tenant-ktj"]
38+
value: ""
39+
40+
- it: depth-2 tenant carries the full ancestor chain including tenant-root
41+
release:
42+
name: tenant-htdev
43+
namespace: tenant-ktj
44+
asserts:
45+
- documentIndex: 0
46+
equal:
47+
path: metadata.name
48+
value: tenant-ktj-htdev
49+
- documentIndex: 0
50+
equal:
51+
path: metadata.labels["tenant.cozystack.io/tenant-root"]
52+
value: ""
53+
- documentIndex: 0
54+
equal:
55+
path: metadata.labels["tenant.cozystack.io/tenant-ktj"]
56+
value: ""
57+
- documentIndex: 0
58+
equal:
59+
path: metadata.labels["tenant.cozystack.io/tenant-ktj-htdev"]
60+
value: ""
61+
62+
- it: depth-3 tenant carries every ancestor from root down to itself
63+
release:
64+
name: tenant-foo
65+
namespace: tenant-ktj-htdev
66+
asserts:
67+
- documentIndex: 0
68+
equal:
69+
path: metadata.name
70+
value: tenant-ktj-htdev-foo
71+
- documentIndex: 0
72+
equal:
73+
path: metadata.labels["tenant.cozystack.io/tenant-root"]
74+
value: ""
75+
- documentIndex: 0
76+
equal:
77+
path: metadata.labels["tenant.cozystack.io/tenant-ktj"]
78+
value: ""
79+
- documentIndex: 0
80+
equal:
81+
path: metadata.labels["tenant.cozystack.io/tenant-ktj-htdev"]
82+
value: ""
83+
- documentIndex: 0
84+
equal:
85+
path: metadata.labels["tenant.cozystack.io/tenant-ktj-htdev-foo"]
86+
value: ""
87+
# A tenant must NOT claim descendants it does not have: the depth-3
88+
# namespace is not an ancestor of any deeper tenant, so no stray label.
89+
- documentIndex: 0
90+
notExists:
91+
path: metadata.labels["tenant.cozystack.io/tenant-ktj-htdev-foo-bar"]

0 commit comments

Comments
 (0)