fix(tenant): ship sub-tenant logs and metrics to the monitoring owner at any depth

A tenant whose monitoring resolves to an ancestor runs Fluent Bit and
VMAgent that write logs to the owner's vlinsert and metrics to its
vminsert. The generated Cilium egress dropped part of this traffic: it
never opened vlinsert, so log shipping was always dropped at the source,
and it reached vminsert only by walking the namespace prefix, which never
reaches tenant-root from below the first level, so a guest two levels deep
under a tenant-root-owned monitoring lost its metrics.

Replace the prefix walk with a single egress rule that targets the
monitoring owner namespace directly for both vminsert and vlinsert. The
owner is already computed in namespace.yaml and inherited through
_namespace.monitoring, so it resolves at any depth, and the rule is
skipped when the tenant owns its monitoring or when no ancestor owns it.
The owner already admits the traffic through allow-external-communication
(fromEntities: cluster), so no ingress policy is added.

The etcd and ingress ancestor egress now comes from
tenant.ancestorNamespaces (merged separately onto main), so the monitoring
tests assert that shape and the ancestor-egress suite no longer asserts the
removed vminsert prefix-walk rule.

Assisted-by: LLM
Signed-off-by: IvanHunters <xorokhotnikov@gmail.com>
This commit is contained in:
IvanHunters committed 2026-12-03 11:45:34 +03:00
1 parent 8e430c011f
commit 0b119ea18b
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