Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nested namePrefix configuration behaviour #5870

Open
penekk opened this issue Feb 28, 2025 · 2 comments
Open

nested namePrefix configuration behaviour #5870

penekk opened this issue Feb 28, 2025 · 2 comments
Labels
kind/bug Categorizes issue or PR as related to a bug. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.

Comments

@penekk
Copy link

penekk commented Feb 28, 2025

What happened?

Let me begin with that I'm not entirely sure this is actually a bug but considering the unintuitive nature of this behaviour I'll go with that.

In order to get my point across let's consider this simplified (in a proper scenario there would be for example more 'bars' and more resources) structure:

Case 1:

apps/foo
├── bar
│   ├── ingress.yaml
│   ├── kustomization.yaml
│   ├── prefixed-resources.yaml
│   └── service-account.yaml
├── generators.yaml
├── jobs.yaml
├── kustomization.yaml
├── prefixed-resources.yaml
└── secrets.enc.yaml

and relevant files:

❯ cat apps/foo/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: foo-
namespace: apps
resources:
  - bar
  - jobs.yaml
generators:
  - generators.yaml
configurations:
  - prefixed-resources.yaml

❯ cat apps/foo/bar/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: bar-
namespace: apps
resources:
  - service-account.yaml
  - ingress.yaml

❯ cat apps/foo/prefixed-resources.yaml
namePrefix:
  - path: metadata/name
    kind: Job

❯ cat apps/foo/jobs.yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: migrations
spec:
  template:
    spec:
      containers: []

In foo there's a namePrefix with explicit config for kinds and in bar there is namePrefix applied on everything.

This results in for example Job: migrations having prefixed applied twice:

---
apiVersion: batch/v1
kind: Job
metadata:
  name: foo-foo-migrations
  namespace: apps
spec:
  template:
    spec:
      containers: []

Case 2:

The difference compared to Case 1 is we do not apply prefix to Job resources in foo but decide to apply it to Job resources in bar:

❯ cat apps/foo/prefixed-resources.yaml
namePrefix:
  # - path: metadata/name
  #   kind: Job
  - path: metadata/name
    kind: Secret
❯ cat apps/foo/bar/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: bar-
namespace: apps
resources:
  - service-account.yaml
  - ingress.yaml
configurations:
  - prefixed-resources.yaml
❯ cat apps/foo/bar/prefixed-resources.yaml
namePrefix:
  - path: metadata/name
    kind: IngressRoute
  - path: metadata/name
    kind: ServiceAccount
  - path: metadata/name
    kind: Job

Which results with Job: migrations having prefix applied once but with value 'from another namePrefix configuration' (so to speak):

---
apiVersion: batch/v1
kind: Job
metadata:
  name: foo-migrations
  namespace: apps
spec:
  template:
    spec:
      containers: []

What did you expect to happen?

Case 1: Job should have prefix foo applied once.

Case 2: Job should not have any prefix applied.

Perhaps I'm misunderstanding something fundamental over here but I would've expected to be able to have a granular control over prefix transformer used but things are gettin mixed/confusing here.

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

.

Expected output

No response

Actual output

No response

Kustomize version

v5.4.1

Operating system

Linux

@penekk penekk added the kind/bug Categorizes issue or PR as related to a bug. label Feb 28, 2025
@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Feb 28, 2025
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

SIG CLI takes a lead on issue triage for this repo, but any Kubernetes member can accept issues by applying the triage/accepted label.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@sda399
Copy link

sda399 commented Mar 2, 2025

Hi,
my thoughts

by definition: "NamePrefix will add a prefix to the names of all resources mentioned in the Kustomization file including generated resources such as ConfigMaps and Secrets."

So namePrefix adds a prefix to all metadata/name paths and the references to these modified names -- references searched in a list of predefined 'default' paths.
for custom paths we use transformerconfigs.

Case1

metadata/name transformation is expected, but there is also a config for it, so it modifies it again, we end up with double prefix.

Case2

metadata/name is transformed, by definition of namePrefix.

Examples

#! /bin/sh

[ ! -d foo ] || rm -r foo
mkdir -p foo/bar

cat <<EOF > foo/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: foo-
namespace: apps
resources:
  - bar
  - jobs.yaml

configurations:
  - prefix-conf.yaml
EOF

cat <<EOF > foo/jobs.yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: migrations
spec:
  custom-name-path0: migrations
  custom-name-path1: migrations
EOF

cat <<EOF > foo/prefix-conf.yaml
namePrefix:
  - path: metadata/custom-name-path
    kind: Job
    create: true
  - path: spec/custom-name-path1
    kind: Job
    create: true

EOF

cat <<EOF > foo/bar/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: bar-
namespace: apps
EOF

cat <<EOF > expected.yaml
apiVersion: batch/v1
kind: Job
metadata:
  custom-name-path: foo-
  name: foo-migrations
  namespace: apps
spec:
  custom-name-path0: migrations
  custom-name-path1: foo-migrations
EOF
#---
kustomize build foo > result.yaml
printf "%-64s%s\n" expected.yaml result.yaml
printf "%130s\n" " " | tr ' ' '-'
diff -W130 -y expected.yaml result.yaml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.
Projects
None yet
Development

No branches or pull requests

3 participants