kubernetes-sigs / kustomize

Customization of kubernetes YAML configurations
Apache License 2.0
10.99k stars 2.25k forks source link

Replacements in components does not behave as expected #5531

Open ecoupal-believe opened 8 months ago

ecoupal-believe commented 8 months ago

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

koba1t commented 8 months ago

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

k8s-ci-robot commented 8 months ago

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

In response to [this](https://github.com/kubernetes-sigs/kustomize/issues/5531#issuecomment-1944271273): >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](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
koba1t commented 8 months ago

/triage accepted

ecoupal-believe commented 8 months ago

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 ?

koba1t commented 8 months ago

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.

ecoupal-believe commented 8 months ago

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 ?

koba1t commented 8 months ago

Is there something like Pre and PostRenderedComponent ?

I believe we don't have.

Yingrjimsch commented 7 months ago

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?

koba1t commented 7 months ago

@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.