Skip to content

network policy rules are incorrectly updated with kustomization labels #5912

Open
@sdickhoven

Description

@sdickhoven

What happened?

kustomize incorrectly updates network policy rules (rather than just the podSelector) when using labels with includeSelectors: true or commonLabels in the kustomization.

What did you expect to happen?

i expect the network policy rules to remain unaltered.

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

kustomization.yaml:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: foo
labels:
- includeSelectors: true
  pairs:
    key1: val1
    key2: val2
- includeTemplates: true
  pairs:
    key3: val3
resources:
- netpol.yaml

netpol.yaml:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: my-app
spec:
  podSelector:
    matchLabels: {}
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: ingress-nginx
      podSelector:
        matchLabels:
          app.kubernetes.io/name: ingress-nginx
          app.kubernetes.io/component: controller
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: kube-system
      podSelector:
        matchLabels:
          app.kubernetes.io/name: prometheus
    ports:
    - protocol: TCP
      port: 8080

Expected output

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  labels:
    key1: val1
    key2: val2
    key3: val3
  name: my-app
  namespace: foo
spec:
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: ingress-nginx
      podSelector:
        matchLabels:
          app.kubernetes.io/component: controller
          app.kubernetes.io/name: ingress-nginx
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: kube-system
      podSelector:
        matchLabels:
          app.kubernetes.io/name: prometheus
    ports:
    - port: 8080
      protocol: TCP
  podSelector:
    matchLabels:
      key1: val1
      key2: val2

Actual output

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  labels:
    key1: val1
    key2: val2
    key3: val3
  name: my-app
  namespace: foo
spec:
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: ingress-nginx
      podSelector:
        matchLabels:
          app.kubernetes.io/component: controller
          app.kubernetes.io/name: ingress-nginx
          key1: val1    # <<<<<<<< incorrect!!!
          key2: val2    # <<<<<<<< incorrect!!!
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: kube-system
      podSelector:
        matchLabels:
          app.kubernetes.io/name: prometheus
          key1: val1    # <<<<<<<< incorrect!!!
          key2: val2    # <<<<<<<< incorrect!!!
    ports:
    - port: 8080
      protocol: TCP
  podSelector:
    matchLabels:
      key1: val1
      key2: val2

Kustomize version

v5.6.0

Operating system

None

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