Skip to content

PodLogs template incompatible with v1alpha2 API - namespaceSelector rendered incorrectly #19737

@elcomtik

Description

@elcomtik

Describe the bug
The Loki Helm chart's PodLogs template for self-monitoring doesn't correctly support the v1alpha2 API version. When monitoring.selfMonitoring.podLogs.apiVersion is set to monitoring.grafana.com/v1alpha2, the rendered PodLogs CR has an empty namespaceSelector: {} instead of properly restricting to the Loki namespace.

When using v1alpha2 API version for PodLogs, the chart template renders:

apiVersion: monitoring.grafana.com/v1alpha2
kind: PodLogs
metadata:
  name: loki
  namespace: observability
spec:
  namespaceSelector: {}  # ❌ Empty - should restrict to observability namespace
  selector:
    matchLabels:
      app.kubernetes.io/instance: loki
      app.kubernetes.io/name: loki
  # ... rest of spec

However, the v1alpha2 CRD schema doesn't include the matchNames field. The v1alpha2 schema uses standard Kubernetes LabelSelector format with only:

  • matchExpressions (list of label selector requirements)
  • matchLabels (map of label key-value pairs)

When Kubernetes validates the PodLogs CR against the v1alpha2 schema, it strips the unknown matchNames field, resulting in an empty namespaceSelector: {}.

To Reproduce

  1. Deploy Loki with self-monitoring enabled:
monitoring:
  selfMonitoring:
    enabled: true
    podLogs:
      apiVersion: monitoring.grafana.com/v1alpha2
  1. Check the rendered PodLogs CR:
kubectl get podlogs/loki -n observability -o yaml
  1. Observe empty namespaceSelector: {}

  2. Check Alloy/grafana-agent logs - it will be collecting logs from all pods cluster-wide

Expected behavior
The namespaceSelector should be properly populated to restrict log collection to the Loki namespace using v1alpha2 format:

namespaceSelector:
  matchLabels:
    kubernetes.io/metadata.name: observability

Environment:

  • Kubernetes Version: 1.31+
  • Grafana Alloy: v1.11.2 (with PodLogs CRD v1alpha2 support)

Proposed Solution

Update the chart template to conditionally render the correct format based on API version:

{{- if eq .Values.monitoring.selfMonitoring.podLogs.apiVersion "monitoring.grafana.com/v1alpha2" }}
  namespaceSelector:
    matchLabels:
      kubernetes.io/metadata.name: {{ include "loki.namespace" $ }}
{{- else }}
  namespaceSelector:
    matchNames:
      - {{ include "loki.namespace" $ }}
{{- end }}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/helmtype/bugSomehing is not working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions