9

Currently we are using ${HOME}/bin/kustomize edit set nameprefix prefix1

But it is adding nameprefix to all of our resources like deployment.yaml and service.yaml.

We want to apply nameprefix to deployment.yaml only and not apply it to service.yaml

coderatcloud9
  • 85
  • 1
  • 1
  • 7
  • 1
    Were you able to solve your problem. If not - please provide more details/yamls with your specific example. – Mark Mar 01 '21 at 11:10

3 Answers3

12

Posting for better visibility:

If you are using:

kustomize edit set nameprefix prefix1

This command will set namePrefix inside your current kustomization. As stated in the question - this is the way how it works, namePrefix will be used for all specified resources inside kustomization.yaml.

Please consider the following scenario using the idea of an overlay and base with kustomization.

Tested with:
kustomize/v4.0.1

Base declare resources and settings shared in common and overlay declare additional differences.

.
├── base
│   ├── [deployment.yaml]  Deployment nginx
│   ├── [kustomization.yaml]  Kustomization 
│   └── [service.yaml]  Service nginx
└── prod
    ├── [kustomization.yaml]  Kustomization 
    └── kustomizeconfig
        └── [deploy-prefix-transformer.yaml]  PrefixSuffixTransformer customPrefixer
  • base: common files
#deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      run: nginx

#service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    run: nginx

#kustomization.yaml
resources:
- deployment.yaml
- service.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
  • overlay/prod: kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
bases:
- ../../base
nameSuffix: -Suffix1
transformers:
- ./kustomizeconfig/deploy-prefix-transformer.yaml

  • overlay/prod/kustomizeconfig: deploy-prefix-transformer.yaml
apiVersion: builtin
kind: PrefixSuffixTransformer
metadata:
  name: customPrefixer
prefix: "deploymentprefix-"
fieldSpecs:
- kind: Deployment
  path: metadata/name

As you can see, using this structure and builtin plugin PrefixSuffixTransformer you can get the desired effect:

kustomize build overlay/prod/
apiVersion: v1
kind: Service
metadata:
  labels:
    run: nginx
  name: nginx-Suffix1
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploymentprefix-nginx-Suffix1
spec:
  selector:
    matchLabels:
      run: nginx

This configuration (overlay/prod/kustomization.yaml) will apply nameSuffix: -Suffix1 to all resources specified in base directory and using PrefixSuffixTransformer will add in this specific example prefix: "deploymentprefix-" to deployment.metadata.name

apiVersion: builtin
kind: PrefixSuffixTransformer
metadata:
  name: customPrefixer
prefix: "deploymentprefix-"
fieldSpecs:
- kind: Deployment
  path: metadata/name

 /kustomizeconfig/deploy-prefix-transformer.yaml

Mark
  • 3,644
  • 6
  • 23
  • Exactly what I needed for Zelando Postgres setup with config env with static name! – Marcello DeSales Apr 19 '22 at 20:17
  • Just battling a very similar issue with `PrefixSuffixTransformer` and trying to get it add a suffix onto the namespaces with no success. Using: apiVersion:builtin kind: PrefixSuffixTransformer metadata: name: namespace-suffix suffix: -mysuffixforNs fieldSpecs: - kind: Namespace path: metadata/name – John Fox Oct 24 '22 at 20:20
2

There is github issue about that

is it possible to have kustomization file avoid adding prefixes to few kinds ?

And there are 2 examples provided by @jbrette with which you can achieve what you need.

Additionally you can take a look at these pull requests:

Jakub
  • 8,189
  • 1
  • 17
  • 31
  • 2
    What version of `kustomize` is this supposed to be working with? I just tested this with `kustomize/v3.8.7` and it does not seem to work at all. Oh, I just noticed they closed both PRs without merging. Sad face. – milosgajdos Nov 19 '20 at 13:28
  • Does the `PrefixSuffixTransformer` work? tried it on namespace objects and not able to get it working. Able to get it to amend the namespace used in objects but not the manifests to create the namespaces. Using Kustomize v4.2.7 – John Fox Oct 25 '22 at 09:56
-1

For those who stumble across this, I had an issue get it to work with ServiceAccount type. Issue was that I needed to prevent suffix from being added. Apparently namePrefix "should" prevent that too but in actuality I had to add:

nameSuffix:
- path: metadata/name
  apiVersion: v1
  kind: serviceaccount
  skip: true

Note kind type with lowercase. Using standard kind for ServiceAccount makes it to fail.

iomv
  • 2,409
  • 18
  • 28
  • This doesn't seem to be valid syntax? `invalid type for kustomize.namePrefix: got "array", expected "string"` https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/namesuffix/ – TJ Zimmerman Mar 28 '23 at 18:04
  • If this was meant to be a `configuration`, then it also doesn't seem valid because `skip: true` is supported. :( – TJ Zimmerman Mar 31 '23 at 18:40