Selectively apply nameprefix/namesuffix in kustomize Selectively apply nameprefix/namesuffix in kustomize kubernetes kubernetes

Selectively apply nameprefix/namesuffix in kustomize


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.yamlapiVersion: apps/v1kind: Deploymentmetadata:  name: nginxspec:  selector:    matchLabels:      run: nginx#service.yamlapiVersion: v1kind: Servicemetadata:  name: nginx  labels:    run: nginx#kustomization.yamlresources:- deployment.yaml- service.yamlapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomization
  • overlay/prod: kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationbases:- ../../basenameSuffix: -Suffix1transformers:- ./kustomizeconfig/deploy-prefix-transformer.yaml
  • overlay/prod/kustomizeconfig: deploy-prefix-transformer.yaml
apiVersion: builtinkind: PrefixSuffixTransformermetadata:  name: customPrefixerprefix: "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: v1kind: Servicemetadata:  labels:    run: nginx  name: nginx-Suffix1---apiVersion: apps/v1kind: Deploymentmetadata:  name: deploymentprefix-nginx-Suffix1spec:  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: builtinkind: PrefixSuffixTransformermetadata:  name: customPrefixerprefix: "deploymentprefix-"fieldSpecs:- kind: Deployment  path: metadata/name /kustomizeconfig/deploy-prefix-transformer.yaml


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: