fix(tenant): allow etcd and ingress egress to all ancestors at any depth (#5442)

## What this PR does

The `<tenant>-egress` CiliumClusterwideNetworkPolicy builds the list of
ancestor namespaces for the etcd and ingress rules by walking
dash-prefixes of `.Release.Namespace`. A tenant HelmRelease lives in its
parent namespace, so for a tenant at depth >= 2 (e.g.
`tenant-acme-nonprod`, released into `tenant-acme`) the walk never
reaches `tenant-root`.

When an intermediate tenant has no etcd of its own, the child inherits
etcd from `tenant-root` (`namespace.cozystack.io/etcd: tenant-root`) but
is not allowed to reach it. The Kamaji control plane of a tenant
Kubernetes cluster in such a tenant times out on
`etcd.tenant-root.svc:2379` and never becomes ready. Ingress has the
same gap.

The chart already fixed this class of bug for namespace labels
(`tenant.ancestorTenantLabels`). This PR adds
`tenant.ancestorNamespaces`, which derives the chain from the tenant's
own namespace (`tenant-root` plus every dash-prefix except the tenant
itself), and uses it for the etcd and ingress rules. The set of allowed
destinations only grows: every previously reachable ancestor stays
reachable.

The vminsert/vlinsert rule is left untouched; #5257 fixes it separately.

Found while running a second-level tenant (`tenant-root` → `aepdev` →
`nonprod`) with a tenant Kubernetes cluster on v1.7.1.

### Screenshots

Not a UI change.

### Downstream repositories

- [x] No downstream repository is affected by this change

### Release note

```release-note
fix(tenant): tenants nested two or more levels deep can now reach etcd and ingress inherited from tenant-root, so tenant Kubernetes clusters in them start correctly.
```


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* Tenant network policies now correctly allow access to required
services in ancestor namespaces, including etcd, ingress, and vminsert.
* Ancestor access is handled consistently across tenant hierarchy
depths, including root tenants and cases where service ownership details
are unavailable.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Aleksei Sviridkin authored and GitHub committed 2026-12-02 03:42:07 +03:00
commit d27f8c950f
3 files changed
+364 -17

No files matched your search

@@ -14,6 +14,28 @@
{{- end }}
{{- end }}
{{/*
tenant.ancestorNamespaces encodes the strict ancestor chain as a JSON array
for callers using fromJsonArray. Like tenant.ancestorTenantLabels, it takes
the tenant's own namespace name: tenant-root is implicit, while the other
ancestors are dash-prefixes. The tenant itself is excluded, so tenant-root
has no ancestors. Service ownership does not restrict this chain: even a
tenant with its own ingress needs ancestor ingress for hairpin traffic.
*/}}
{{- define "tenant.ancestorNamespaces" -}}
{{- $ancestors := list -}}
{{- if ne . "tenant-root" -}}
{{- $ancestors = append $ancestors "tenant-root" -}}
{{- $parts := splitList "-" . -}}
{{- range $i, $v := $parts -}}
{{- if and (ne $i 0) (lt $i (sub (len $parts) 1)) -}}
{{- $ancestors = append $ancestors (join "-" (slice $parts 0 (add $i 1))) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $ancestors | toJson -}}
{{- end -}}
{{/*
tenant.gatewayEffective resolves whether THIS tenant should own a
dedicated Gateway resource. Returns the literal string "true" or
@@ -54,30 +54,19 @@ spec:
{{- end }}
{{- end }}
{{- end }}
{{- if ne (include "tenant.name" .) "tenant-root" }}
{{- $ancestors := include "tenant.ancestorNamespaces" (include "tenant.name" .) | fromJsonArray }}
{{- with $ancestors }}
- toEndpoints:
{{- if hasPrefix "tenant-" .Release.Namespace }}
{{- $parts := splitList "-" .Release.Namespace }}
{{- range $i, $v := $parts }}
{{- if ne $i 0 }}
{{- range . }}
- matchLabels:
"k8s:app.kubernetes.io/instance": "etcd"
"k8s:io.kubernetes.pod.namespace": {{ join "-" (slice $parts 0 (add $i 1)) }}
"k8s:io.kubernetes.pod.namespace": {{ . }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- if ne (include "tenant.name" .) "tenant-root" }}
- toEndpoints:
{{- if hasPrefix "tenant-" .Release.Namespace }}
{{- $parts := splitList "-" .Release.Namespace }}
{{- range $i, $v := $parts }}
{{- if ne $i 0 }}
{{- range . }}
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": {{ join "-" (slice $parts 0 (add $i 1)) }}
{{- end }}
{{- end }}
"k8s:io.kubernetes.pod.namespace": {{ . }}
{{- end }}
{{- end }}
---
@@ -0,0 +1,336 @@
suite: tenant etcd and ingress egress reaches all ancestors
templates:
- templates/networkpolicy.yaml
set:
etcd: false
ingress: false
tests:
- it: allows tenant-root at depth one
release:
name: tenant-team
namespace: tenant-root
documentSelector:
path: metadata.name
value: tenant-team-egress
asserts:
- equal:
path: spec.egress
value:
- toEndpoints:
- matchLabels:
"k8s:io.cilium.k8s.namespace.labels.tenant.cozystack.io/tenant-team": ""
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/name": vminsert
"k8s:io.kubernetes.pod.namespace": tenant-root
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-root
- toEndpoints:
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-root
- it: allows root and parent at depth two
release:
name: tenant-child
namespace: tenant-team
set:
_namespace.etcd: tenant-root
_namespace.ingress: tenant-root
documentSelector:
path: metadata.name
value: tenant-team-child-egress
asserts:
- equal:
path: spec.egress
value:
- toEndpoints:
- matchLabels:
"k8s:io.cilium.k8s.namespace.labels.tenant.cozystack.io/tenant-team-child": ""
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/name": vminsert
"k8s:io.kubernetes.pod.namespace": tenant-team
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-root
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-team
- toEndpoints:
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-root
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-team
- it: allows all three ancestors at depth three
release:
name: tenant-leaf
namespace: tenant-team-child
set:
_namespace.etcd: tenant-team
_namespace.ingress: tenant-team-child
documentSelector:
path: metadata.name
value: tenant-team-child-leaf-egress
asserts:
- equal:
path: spec.egress
value:
- toEndpoints:
- matchLabels:
"k8s:io.cilium.k8s.namespace.labels.tenant.cozystack.io/tenant-team-child-leaf": ""
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/name": vminsert
"k8s:io.kubernetes.pod.namespace": tenant-team
- matchLabels:
"k8s:app.kubernetes.io/name": vminsert
"k8s:io.kubernetes.pod.namespace": tenant-team-child
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-root
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-team
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-team-child
- toEndpoints:
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-root
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-team
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-team-child
- it: keeps root reachable with locally owned services at depth one
release:
name: tenant-team
namespace: tenant-root
set:
etcd: true
ingress: true
documentSelector:
path: metadata.name
value: tenant-team-egress
asserts:
- equal:
path: spec.egress
value:
- toEndpoints:
- matchLabels:
"k8s:io.cilium.k8s.namespace.labels.tenant.cozystack.io/tenant-team": ""
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/name": vminsert
"k8s:io.kubernetes.pod.namespace": tenant-root
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-root
- toEndpoints:
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-root
- it: keeps every ancestor reachable with locally owned services
release:
name: tenant-child
namespace: tenant-team
set:
etcd: true
ingress: true
documentSelector:
path: metadata.name
value: tenant-team-child-egress
asserts:
- equal:
path: spec.egress
value:
- toEndpoints:
- matchLabels:
"k8s:io.cilium.k8s.namespace.labels.tenant.cozystack.io/tenant-team-child": ""
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/name": vminsert
"k8s:io.kubernetes.pod.namespace": tenant-team
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-root
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-team
- toEndpoints:
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-root
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-team
- it: keeps ancestors reachable with local etcd
release:
name: tenant-child
namespace: tenant-team
set:
etcd: true
documentSelector:
path: metadata.name
value: tenant-team-child-egress
asserts:
- equal:
path: spec.egress
value:
- toEndpoints:
- matchLabels:
"k8s:io.cilium.k8s.namespace.labels.tenant.cozystack.io/tenant-team-child": ""
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/name": vminsert
"k8s:io.kubernetes.pod.namespace": tenant-team
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-root
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-team
- toEndpoints:
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-root
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-team
- it: keeps ancestors reachable with local ingress
release:
name: tenant-child
namespace: tenant-team
set:
ingress: true
documentSelector:
path: metadata.name
value: tenant-team-child-egress
asserts:
- equal:
path: spec.egress
value:
- toEndpoints:
- matchLabels:
"k8s:io.cilium.k8s.namespace.labels.tenant.cozystack.io/tenant-team-child": ""
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/name": vminsert
"k8s:io.kubernetes.pod.namespace": tenant-team
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-root
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-team
- toEndpoints:
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-root
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-team
- it: allows ancestors when owners are missing
release:
name: tenant-child
namespace: tenant-team
documentSelector:
path: metadata.name
value: tenant-team-child-egress
asserts:
- equal:
path: spec.egress
value:
- toEndpoints:
- matchLabels:
"k8s:io.cilium.k8s.namespace.labels.tenant.cozystack.io/tenant-team-child": ""
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/name": vminsert
"k8s:io.kubernetes.pod.namespace": tenant-team
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-root
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-team
- toEndpoints:
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-root
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-team
- it: allows ancestors with a null namespace map
release:
name: tenant-child
namespace: tenant-team
set:
_namespace: null
documentSelector:
path: metadata.name
value: tenant-team-child-egress
asserts:
- equal:
path: spec.egress
value:
- toEndpoints:
- matchLabels:
"k8s:io.cilium.k8s.namespace.labels.tenant.cozystack.io/tenant-team-child": ""
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/name": vminsert
"k8s:io.kubernetes.pod.namespace": tenant-team
- toEndpoints:
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-root
- matchLabels:
"k8s:app.kubernetes.io/instance": etcd
"k8s:io.kubernetes.pod.namespace": tenant-team
- toEndpoints:
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-root
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-team
- it: omits ancestor rules for tenant-root
release:
name: tenant-root
namespace: tenant-root
set:
etcd: true
ingress: true
documentSelector:
path: metadata.name
value: tenant-root-egress
asserts:
- equal:
path: spec.egress
value:
- toEndpoints:
- matchLabels:
"k8s:io.cilium.k8s.namespace.labels.tenant.cozystack.io/tenant-root": ""