Selectively apply nameprefix/namesuffix in kustomize

10/6/2020

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
kubernetes
kustomize

2 Answers

3/2/2021

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
Source: StackOverflow

10/6/2020

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
Source: StackOverflow