Common Transformers
Common Transformers
-
How can we use
kustomizeto modify and transform Kubernetes configs. -
These are done using
kustomize transformers.- Here, we will focus on a sub-group called
common transformations.
- Here, we will focus on a sub-group called
-
In the following examples, we have these
yamlfiles: -
db-depl.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 1
selector:
matchLabels:
component: api
template:
metadata:
labels:
component: api
spec:
containers:
- name: nginx
image: nginx
db-service.yaml:
apiVersion: v1
kind: Service
metadata:
name: db-service
spec:
selector:
component: db-depl
ports:
- protocol: "TCP"
port: 27017
targetPort: 27017
type: LoadBalancer
-
We want to apply a common configuration across all
yamlfiles. For example, we want to add a label to each of the aboveyamlfiles fororg: KodeKloud -
Or even append
-devto the end ofapi-deploymentas an example. -
We can easily make common configuration changes across all Kubernetes resources.
-
Types of Common Transformations:
-
commonLabel- adds a label to all Kubernetes resources. -
namePrefix/Suffix- adds a common prefix-suffix to all resource names. -
Namespace- adds a common namespace to all resources. -
commonAnnotations- adds an annotation to all resources.
-
-
Regarding the
commonLabeltransformer.-
We want to add a label to all resources.
-
Go into the
Kustomization.yamlfile and add the following:
-
commonLabels:
org: KodeKloud
* The above then adds the label to all Kubernetes resources.
-
Namespace Transformer.
-
Puts all Kubernetes resources under a particular namespace.
- This looks like this:
namespace: lab
- This looks like this:
-
-
For the Prefix/suffix Transformer, can add in the following:
namePrefix: KodeKloud-
nameSuffix: -dev
* This will map to the prefix of the name and the suffix of the name as well.
-
CommonAnnotations Transformer
- If you want to add commonAnnotations, add the following lines:
commonAnnotations:
branch: master
- This will then be added to all Kubernetes objects.