kustomize icon indicating copy to clipboard operation
kustomize copied to clipboard

Replacements in components does not behave as expected

Open ecoupal-believe opened this issue 1 year ago • 9 comments

What happened?

I want to add a metadata.label containing the metadata.name added by the namePrefix of an overlay. The replacement is working but it takes the name from the base instead of the one of the overlay. If it's working as intented can you suggest me something ?

Thanks

What did you expect to happen?

The replacements should take the rendered name including the prefix from the overlay.

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

#base/deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: service
spec:
  template:
    spec:
      containers:
        - name: service
          image: service
#component/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component

replacements:
- source:
    kind: Deployment
    fieldPath: metadata.name
  targets:
  - select:
      kind: Deployment
    fieldPaths:
      - metadata.labels.foo
    options:
      create: true
#overlay/foo/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namePrefix: foo-

resources:
- ../base

components:
- ../component

Complete code is here: https://gist.github.com/ecoupal-believe/1c143045499afbd196505b5911343254

Expected output

apiVersion: apps/v1
kind: Deployment
metadata:
  name: foo-service
  labels:
    foo:  foo-service
spec:
  template:
    spec:
      containers:
        - name: service
          image: service

Actual output

apiVersion: apps/v1
kind: Deployment
metadata:
  name: foo-service
  labels:
    foo:  service
spec:
  template:
    spec:
      containers:
        - name: service
          image: service

Kustomize version

v5.3.0

Operating system

Linux

ecoupal-believe avatar Feb 08 '24 14:02 ecoupal-believe

Hi @ecoupal-believe Now, kustomize apply components before applying every transformer( that contains the namePrefix transformer). So, I think it is an expected behavior.

But, I think we need to add a document about the order in kustomization.yaml.

/triage accept /kind documentation

koba1t avatar Feb 14 '24 17:02 koba1t

@koba1t: The label(s) triage/accept cannot be applied, because the repository doesn't have them.

In response to this:

Hi @ecoupal-believe Now, kustomize apply components before applying every transformer( that contains the namePrefix transformer). So, I think it is an expected behavior.

But, I think we need to add a document about the order in kustomization.yaml.

/triage accept /kind documentation

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/test-infra repository.

k8s-ci-robot avatar Feb 14 '24 17:02 k8s-ci-robot

/triage accepted

koba1t avatar Feb 14 '24 17:02 koba1t

Hi @koba1t, thank you for your answer.

That's what I thought from looking around the documentations/issues. I understand that component are applied before the transformers but that doesn't help me so much, is there any way arround for my use case ?

Just for context I try to apply common behaviors (such as adding labels, patching resources) to my already rendered overlays (services in my case). How can I do so ?

ecoupal-believe avatar Feb 15 '24 08:02 ecoupal-believe

Hi @ecoupal-believe I think you can control the order if you add a new component that only contains the namePrefix. It's just an idea. Maybe you can find a better way.

koba1t avatar Feb 15 '24 08:02 koba1t

I also got this answer in another thread. That would require to add a new component for each of my actual overlays (foo-service and bar-service and so on) just to hold the static namePrefix. Anything cleaner ? Is there something like Pre and PostRenderedComponent ?

ecoupal-believe avatar Feb 15 '24 09:02 ecoupal-believe

Is there something like Pre and PostRenderedComponent ?

I believe we don't have.

koba1t avatar Feb 15 '24 11:02 koba1t

Two questions @koba1t maybe you can answer I have similar problems with components + patches so what is the order of execution components > patches? Could it be done that components with replacements are still executed at the end?

Yingrjimsch avatar Mar 08 '24 14:03 Yingrjimsch

@Yingrjimsch

I have similar problems with components + patches

I believe components are executed before running the patches transformer.

Could it be done that components with replacements are still executed at the end?

Please make more than one directory and the kusomization.yaml that contains what you want to run early and read with the resources field from another kusomization.yaml that contains the components field.

koba1t avatar Mar 13 '24 17:03 koba1t