Selectively apply nameprefix/namesuffix in kustomize

16.6k views Asked by At

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

3

There are 3 answers

2
Jakub On

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:

2
Mark On

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

2
iomv On

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.