Skip to content

Add support for template-based label/annotation matching #193

@tamoreton

Description

@tamoreton

Use case

When creating namespace configurations, there are scenarios where we want to match labels/annotations based on dynamic values derived from the namespace itself.

For example, a common pattern is to have ArgoCD instances managing namespaces where the instance name is derived from the namespace name:

metadata:
  name: my-project
  labels:
    argocd.argoproj.io/managed-by: my-project-argo

Currently, we can't create a NamespaceConfig that matches based on this pattern without requiring additional trigger labels.

Proposed solution

Add a new field to the NamespaceConfig spec for template-based matching:

apiVersion: redhatcop.redhat.io/v1alpha1
kind: NamespaceConfig
metadata:
  name: gitops-config
spec:
  labelMatchTemplate:
    argocd.argoproj.io/managed-by: "{{ .Name }}-argo"
  templates:
    - objectTemplate: |
        apiVersion: v1
        kind: Namespace
        metadata:
          name: {{ .Name }}-argo

This would allow the operator to:

  1. Evaluate the template expressions against the namespace
  2. Check if the resulting key-value pairs match the namespace's actual labels/annotations

Benefits

  • More intuitive configurations that can use self-referential patterns
  • Reduction in redundant labeling (no need for separate trigger labels)
  • Better support for common patterns like ArgoCD management where instance names follow a convention
  • More maintainable configurations as relationships are more explicit

Alternatives considered

  1. Using standard label selectors with Exists operator - less precise, can't validate naming convention
  2. Using separate trigger labels - works but adds unnecessary complexity
  3. Using regexp matching - possible but less intuitive and harder to maintain

Would this feature be valuable to the project? Happy to provide more details or discuss alternative approaches.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions