Skip to content

annotations and labels set "null" instead of removed by strategic merge patch when using target #5995

@sooncj

Description

@sooncj

What happened?

Setting an annotation or label to null in a strategic merge patch when using target sets the value to "null" instead of removing the annotation.

Any combination of target selectors causes the issue.

This issue prevents using a single patch to target multiple resources for removal of annotations and labels.

What did you expect to happen?

Annotations and labels are removed by setting their value to null in a strategic merge patch.

Removing target and using default name reference behavior removes the annotation or label as expected.

e.g. this kustomization.yaml works.

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization  
resources:
- sa.yaml
patches:
- patch: |
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      labels:
        unwanted: null
      name: sa

How can we reproduce it (as minimally and precisely as possible)?

sa.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    unwanted: "true"
  name: sa

kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization  
resources:
- sa.yaml
patches:
- patch: |
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      labels:
        unwanted: null
      name: sa
  target:
    version: v1
    kind: ServiceAccount
    name: sa

Expected output

unwanted label removed.

apiVersion: v1
kind: ServiceAccount
metadata:
  labels: {}
  name: sa

Actual output

unwanted label retained, with value set to "null"

apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    unwanted: "null"
  name: sa

Kustomize version

5.7.1 (in kubectl 1.34.1)

Operating system

MacOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugCategorizes issue or PR as related to a bug.needs-triageIndicates an issue or PR lacks a `triage/foo` label and requires one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions