Skip to content

Component not correctly honoring fields from parent Kustomization #5953

@Joker9944

Description

@Joker9944

What happened?

I'm trying to write a Component which generates a Certificate which needs some replacements with the given namespace from the parent Kustomization.

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: garage
components:
  - ../../../components/namespace-cert
---
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
resources:
  - manifests/certificate.yaml
replacements:
  - source:
      kind: Certificate
      fieldPath: metadata.namespace
    targets:
      - select:
          kind: Certificate
          name: wildcard-PLACEHOLDER
        fieldPaths:
          - spec.dnsNames.0
          - spec.dnsNames.1
        options:
          delimiter: .
          index: 1
      - select:
          kind: Certificate
          name: wildcard-PLACEHOLDER
        fieldPaths:
          - spec.secretName
          - metadata.name
        options:
          delimiter: '-'
          index: 1
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: wildcard-PLACEHOLDER
spec:
  dnsNames:
    - "*.PLACEHOLDER.svc.cluster.local"
    - "*.PLACEHOLDER"
  issuerRef:
    group: cert-manager.io
    kind: ClusterIssuer
    name: nyx-intermediate-ca
  secretName: wildcard-PLACEHOLDER-cert

This leads to the following error, implying that the namespace has not been set:

$ kubectl kustomize
error: accumulating components: accumulateDirectory: "recursed accumulation of path 'k8s-config/components/namespace-cert': fieldPath `metadata.namespace` is missing for replacement source Certificate.[noVer].[noGrp]/[noName].[noNs]"

What did you expect to happen?

However when I add a test namespace to the Component the generation works flawlessly.

---
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
namespace: test
resources:
  - manifests/certificate.yaml
replacements:
  - source:
      kind: Certificate
      fieldPath: metadata.namespace
    targets:
      - select:
          kind: Certificate
          name: wildcard-PLACEHOLDER
        fieldPaths:
          - spec.dnsNames.0
          - spec.dnsNames.1
        options:
          delimiter: .
          index: 1
      - select:
          kind: Certificate
          name: wildcard-PLACEHOLDER
        fieldPaths:
          - spec.secretName
          - metadata.name
        options:
          delimiter: '-'
          index: 1
$ kubectl kustomize
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: wildcard-test
  namespace: garage
spec:
  dnsNames:
  - '*.test.svc.cluster.local'
  - '*.test'
  issuerRef:
    group: cert-manager.io
    kind: ClusterIssuer
    name: nyx-intermediate-ca
  secretName: wildcard-test-cert

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

# app/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: test
components:
  - ../component
# component/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
resources:
  - certificate.yaml
replacements:
  - source:
      kind: Certificate
      fieldPath: metadata.namespace
    targets:
      - select:
          kind: Certificate
          name: wildcard-PLACEHOLDER
        fieldPaths:
          - spec.dnsNames.0
          - spec.dnsNames.1
        options:
          delimiter: .
          index: 1
      - select:
          kind: Certificate
          name: wildcard-PLACEHOLDER
        fieldPaths:
          - spec.secretName
          - metadata.name
        options:
          delimiter: '-'
          index: 1
# component/certificate.yaml
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: wildcard-PLACEHOLDER
spec:
  dnsNames:
    - "*.PLACEHOLDER.svc.cluster.local"
    - "*.PLACEHOLDER"
  issuerRef:
    group: cert-manager.io
    kind: ClusterIssuer
    name: nyx-intermediate-ca
  secretName: wildcard-PLACEHOLDER-cert
cd app
kubectl kustomize

Expected output

apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: wildcard-garage
  namespace: garage
spec:
  dnsNames:
  - '*.garage.svc.cluster.local'
  - '*.garage'
  issuerRef:
    group: cert-manager.io
    kind: ClusterIssuer
    name: nyx-intermediate-ca
  secretName: wildcard-garage-cert

Actual output

error: accumulating components: accumulateDirectory: "recursed accumulation of path '/home/joker9944/Workspace/k8s-config/components/namespace-cert': fieldPath `metadata.namespace` is missing for replacement source Certificate.[noVer].[noGrp]/[noName].[noNs]"

Kustomize version

v5.6.0

Operating system

Linux

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