fix(tenant): ship sub-tenant logs and metrics to the ancestor monitoring (#5257)

## What this PR does

A tenant (and the guest clusters inside it) whose monitoring resolves to
an ancestor runs monitoring agents (Fluent Bit + VMAgent) that write to
the owner tenant's `vlinsert` (logs) and `vminsert` (metrics). The
generated tenant Cilium egress dropped part of this traffic:

- The egress never opened `vlinsert`, so log shipping was always dropped
at the source (fluent-bit `no upstream connections available to
vlinsert-generic.<owner>.svc:9481`).
- For metrics, the egress derived the target by walking the namespace
prefix. The walk reaches an ancestor only when its namespace is a prefix
of the writer's, so it never reaches `tenant-root` from below the first
level. In the common layout where only `tenant-root` has monitoring and
descendants inherit it, a guest two levels deep had its metrics dropped
on egress (a first-level tenant, or an intermediate monitoring owner
that is a prefix, still worked).

This change replaces the prefix walk with a single egress rule that
targets the monitoring owner namespace directly. The owner is the value
already computed in `namespace.yaml` and inherited through
`_namespace.monitoring`, so it resolves to the real owner at any depth
and covers both `vminsert` and `vlinsert` in one rule. The rule is
skipped when the tenant owns its monitoring (writes stay in-namespace)
or when no ancestor owns it.

No ingress policy is added on the owner side: the owner tenant already
accepts the traffic through its `allow-external-communication` rule
(`fromEntities: [world, cluster]`), which matches every pod in the
cluster.

Helm unittests cover the owner-targeted egress one level down, the deep
case targeting `tenant-root`, the self-owned case (the `monitoring:
true` override wins over the inherited owner, no cross-namespace rule),
and the no-owner case (monitoring disabled with the inherited owner
absent or empty, no rule rendered).

Fixes #5256

### Downstream repositories

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

### Release note

```release-note
fix(tenant): sub-tenant monitoring agents now ship logs and metrics to the monitoring owner tenant's VictoriaLogs/VictoriaMetrics at any nesting depth. Previously the generated Cilium egress never opened vlinsert, so logs were always dropped, and it reached vminsert only by walking the namespace prefix, so metrics were dropped when the monitoring owner was tenant-root and the writer sat below the first level.
```


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

## Summary by CodeRabbit

* **Updates**
* Tenant monitoring write traffic now targets the configured monitoring
owner’s namespace for `vminsert` and `vlinsert`, including when the
owner is not the tenant’s parent.
* Tenants that own their monitoring, or have no configured monitoring
owner, do not receive a separate cross-namespace monitoring egress rule.
* Network access rules continue to allow required connections to
ancestor namespaces, including etcd and ingress endpoints.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Aleksei Sviridkin authored and GitHub committed 2026-12-03 17:43:20 +03:00
commit d380f2e8fc
3 files changed
+217 -50

No files matched your search

@@ -41,19 +41,31 @@ spec:
- toEndpoints:
- matchLabels:
"k8s:io.cilium.k8s.namespace.labels.tenant.cozystack.io/{{ include "tenant.name" . }}": ""
{{- if ne (include "tenant.name" .) "tenant-root" }}
{{- /*
Monitoring agents in this tenant (and the guest clusters inside it) that
resolve monitoring to an ancestor ship metrics (vminsert) and logs
(vlinsert) to that owner. The owner namespace is the one computed in
namespace.yaml and inherited through _namespace.monitoring, so this reaches
the real owner at any depth (including tenant-root), unlike a namespace
prefix walk, which never reaches tenant-root from below the first level.
Skipped when this tenant owns its monitoring (writes stay in-namespace) or
when no ancestor does.
*/}}
{{- $monitoringOwner := (.Values._namespace | default dict).monitoring | default "" }}
{{- if .Values.monitoring }}
{{- $monitoringOwner = include "tenant.name" . }}
{{- end }}
{{- if and $monitoringOwner (ne $monitoringOwner (include "tenant.name" .)) }}
- toEndpoints:
{{- if hasPrefix "tenant-" .Release.Namespace }}
{{- $parts := splitList "-" .Release.Namespace }}
{{- range $i, $v := $parts }}
{{- if ne $i 0 }}
- matchLabels:
"k8s:app.kubernetes.io/name": "vminsert"
"k8s:io.kubernetes.pod.namespace": {{ join "-" (slice $parts 0 (add $i 1)) }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
"k8s:io.kubernetes.pod.namespace": {{ $monitoringOwner }}
matchExpressions:
- key: "k8s:app.kubernetes.io/name"
operator: In
values:
- vminsert
- vlinsert
{{- end }}
{{- $ancestors := include "tenant.ancestorNamespaces" (include "tenant.name" .) | fromJsonArray }}
{{- with $ancestors }}
- toEndpoints:
@@ -19,10 +19,6 @@ tests:
- 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
@@ -49,10 +45,6 @@ tests:
- 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
@@ -85,13 +77,6 @@ tests:
- 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
@@ -130,10 +115,6 @@ tests:
- 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
@@ -160,10 +141,6 @@ tests:
- 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
@@ -195,10 +172,6 @@ tests:
- 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
@@ -230,10 +203,6 @@ tests:
- 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
@@ -263,10 +232,6 @@ tests:
- 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
@@ -298,10 +263,6 @@ tests:
- 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
@@ -0,0 +1,194 @@
suite: tenant monitoring ingest egress reaches the monitoring owner at any depth
# A tenant (and the guest clusters inside it) that resolves its monitoring to an
# ancestor must be able to write both metrics (vminsert) and logs (vlinsert) to
# that owner's ingest endpoints. The owner is inherited through
# _namespace.monitoring, so the egress targets the real owner directly rather
# than walking the namespace prefix (which never reaches tenant-root from below
# the first level). The ancestor already accepts the ingress via its
# allow-external-communication policy (fromEntities: cluster), so no extra
# ingress policy is rendered here. The etcd and ingress ancestor rules come from
# tenant.ancestorNamespaces (every ancestor including tenant-root); the no-owner
# cases assert the exact list so a stray owner rule cannot slip in.
templates:
- templates/networkpolicy.yaml
release:
name: tenant-ktj
namespace: tenant-root
set:
_cluster:
bundle-name: paas-full
tests:
- it: egresses to the parent owner's vminsert and vlinsert in one rule
release:
name: tenant-sub
namespace: tenant-ktj
set:
_cluster:
bundle-name: paas-full
_namespace:
monitoring: tenant-ktj
documentSelector:
path: metadata.name
value: tenant-ktj-sub-egress
asserts:
- isKind:
of: CiliumClusterwideNetworkPolicy
- contains:
path: spec.egress
content:
toEndpoints:
- matchLabels:
"k8s:io.kubernetes.pod.namespace": tenant-ktj
matchExpressions:
- key: "k8s:app.kubernetes.io/name"
operator: In
values:
- vminsert
- vlinsert
- it: targets the owning tenant-root even when the owner is not the parent
# Common layout: monitoring lives only in tenant-root and every descendant
# inherits it. A namespace-prefix walk from tenant-ktj-a would emit
# tenant-ktj and tenant-ktj-a and never tenant-root, dropping the traffic.
release:
name: tenant-b
namespace: tenant-ktj-a
set:
_cluster:
bundle-name: paas-full
_namespace:
monitoring: tenant-root
documentSelector:
path: metadata.name
value: tenant-ktj-a-b-egress
asserts:
- isKind:
of: CiliumClusterwideNetworkPolicy
- contains:
path: spec.egress
content:
toEndpoints:
- matchLabels:
"k8s:io.kubernetes.pod.namespace": tenant-root
matchExpressions:
- key: "k8s:app.kubernetes.io/name"
operator: In
values:
- vminsert
- vlinsert
- it: renders no monitoring egress rule when the tenant owns its monitoring
# The inherited owner points at an ancestor (_namespace.monitoring is never
# empty in a real cluster; cozystack-basics sets it on tenant-root and every
# child inherits it), but monitoring: true overrides it to this tenant, so
# writes stay in-namespace (covered by allow-internal-communication) and no
# cross-namespace rule is added. The set below is what makes the override
# load-bearing: without _namespace.monitoring the owner is empty either way.
set:
monitoring: true
_cluster:
bundle-name: paas-full
_namespace:
monitoring: tenant-root
documentSelector:
path: metadata.name
value: tenant-ktj-egress
asserts:
- isKind:
of: CiliumClusterwideNetworkPolicy
- equal:
path: spec.egress
value:
- toEndpoints:
- matchLabels:
"k8s:io.cilium.k8s.namespace.labels.tenant.cozystack.io/tenant-ktj": ""
- 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: renders no monitoring egress rule when no ancestor owns monitoring
# Monitoring disabled and nothing inherited: the owner resolves to empty and
# the rule must not render. monitoring: false is set explicitly so the case
# does not depend on the chart default, and _namespace.monitoring is left
# absent. Asserting the exact egress list keeps a rule with an empty owner
# namespace from slipping through unnoticed; the etcd and ingress rules reach
# every ancestor (tenant-root included) via tenant.ancestorNamespaces.
release:
name: tenant-sub
namespace: tenant-ktj
set:
monitoring: false
_cluster:
bundle-name: paas-full
documentSelector:
path: metadata.name
value: tenant-ktj-sub-egress
asserts:
- isKind:
of: CiliumClusterwideNetworkPolicy
- equal:
path: spec.egress
value:
- toEndpoints:
- matchLabels:
"k8s:io.cilium.k8s.namespace.labels.tenant.cozystack.io/tenant-ktj-sub": ""
- 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-ktj
- toEndpoints:
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-root
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-ktj
- it: renders no monitoring egress rule when the inherited owner is empty
# The empty-string owner (an explicit _namespace.monitoring: "") must be
# treated the same as absent, so the exact egress list carries no owner rule.
# monitoring: false is set explicitly here too, so the result does not depend
# on the chart default.
release:
name: tenant-sub
namespace: tenant-ktj
set:
monitoring: false
_cluster:
bundle-name: paas-full
_namespace:
monitoring: ""
documentSelector:
path: metadata.name
value: tenant-ktj-sub-egress
asserts:
- isKind:
of: CiliumClusterwideNetworkPolicy
- equal:
path: spec.egress
value:
- toEndpoints:
- matchLabels:
"k8s:io.cilium.k8s.namespace.labels.tenant.cozystack.io/tenant-ktj-sub": ""
- 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-ktj
- toEndpoints:
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-root
- matchLabels:
cozystack.io/service: ingress
"k8s:io.kubernetes.pod.namespace": tenant-ktj