Skip to content

Feature Request: Target label selector patches should apply patches #1706

Description

@erikgb

According to https://fluxcd.io/flux/components/kustomize/kustomizations/#patches, it should be possible to apply patches to a Flux Kustomization using a target label selector. But I think there might be a bug somewhere.

$ tree
.
├── base
│   └── kustomization.yaml
└── kustomization.yaml

2 directories, 2 files
$ cat base/kustomization.yaml 
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
labels:
  - pairs:
      added-by-labels: "v"
configMapGenerator:
  - name: config
    literals:
      - KEY=v
    options:
      labels:
        added-by-options: "v"
$ cat kustomization.yaml 
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: stas
resources:
  - base
patches:
  - patch: |
      apiVersion: v1
      kind: ConfigMap
      metadata:
        name: not-used
      data:
        ADDED_BY_KUSTOMIZE_LABELS: "v"
    target:
      kind: ConfigMap
      labelSelector: added-by-labels=v
  - patch: |
      apiVersion: v1
      kind: ConfigMap
      metadata:
        name: not-used
      data:
        ADDED_BY_KUSTOMIZE_OPTIONS: "v"
    target:
      kind: ConfigMap
      labelSelector: added-by-labels=v

Then if I run kustomize build . it produces the expected output:

apiVersion: v1
data:
  ADDED_BY_KUSTOMIZE_LABELS: v
  ADDED_BY_KUSTOMIZE_OPTIONS: v
  KEY: v
kind: ConfigMap
metadata:
  labels:
    added-by-labels: v
    added-by-options: v
  name: config-49gc847c57
  namespace: stas

But if I do the same with a Flux Kustomization patch, it does not work:

---
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
  name: label-patch
spec:
  interval: 1h
  path: ./apps/label-patch/base
  prune: true
  sourceRef:
    kind: GitRepository
    name: tenant
  targetNamespace: stas
  patches:
    - patch: |
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: not-used
        data:
          ADDED_BY_KUSTOMIZE_LABELS: "v"
      target:
        kind: ConfigMap
        labelSelector: added-by-labels=v
    - patch: |
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: not-used
        data:
          ADDED_BY_KUSTOMIZE_OPTIONS: "v"
      target:
        kind: ConfigMap
        labelSelector: added-by-labels=v

Which in the cluster looks like this:

kind: ConfigMap
apiVersion: v1
metadata:
  name: config-2969fdbbtk
  namespace: stas
  uid: 386ae55f-4fc0-4e14-8a03-5ac8f5ee5411
  resourceVersion: '2736213362'
  creationTimestamp: '2026-07-09T18:49:34Z'
  labels:
    added-by-labels: v
    added-by-options: v
    kustomize.toolkit.fluxcd.io/name: label-patch
    kustomize.toolkit.fluxcd.io/namespace: stas
  annotations:
    config.kubernetes.io/origin: |
      configuredIn: kustomization.yaml
      configuredBy:
        apiVersion: builtin
        kind: ConfigMapGenerator
  managedFields:
    - manager: kustomize-controller
      operation: Apply
      apiVersion: v1
      time: '2026-07-09T18:49:34Z'
      fieldsType: FieldsV1
      fieldsV1:
        'f:data':
          'f:KEY': {}
        'f:metadata':
          'f:annotations':
            'f:config.kubernetes.io/origin': {}
          'f:labels':
            'f:added-by-labels': {}
            'f:added-by-options': {}
            'f:kustomize.toolkit.fluxcd.io/name': {}
            'f:kustomize.toolkit.fluxcd.io/namespace': {}
data:
  KEY: v

CC @matheuscscp

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions