Search code examples
amazon-web-serviceskuberneteskubectlamazon-ekskustomize

Kustomize: Patch multiple resources that starts with same name


I want to patch multiple deployments that starts with same namePrefix instead of targeting specific resource.

For example, I have 2 deployments of nginx deployment-v1.yaml and deployment-v2.yaml. I want to patch both the deployment using nginx- prefix.

deployment-v1.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-v1
  labels:
    app: web
spec:
  selector:
    matchLabels:
      app: web
  replicas: 5
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
       —name: nginx
          image: nginx
          ports:
           —containerPort: 80 

deployment-v2.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: web
spec:
  selector:
    matchLabels:
      app: web
  replicas: 5
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
       —name: nginx
          image: nginx
          resources:
            limits:
              memory: 200Mi
            requests:
              cpu: 100m
              memory: 200Mi
          ports:
           —containerPort: 80

Now I want to overlay both the deployments with common overlay-patch. I am trying something like this.

overlay.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  namePrefix: nginx-
spec:
  replicas: 10

kustomization.yml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - deployment-v1.yaml
  - deployment-v2.yaml
patchesStrategicMerge:
  - overlay.yaml

But it is not working as it is expecting a name to match the target and totally ignoring namePrefix. Any help is appreciated.


Solution

  • You can apply a patch to multiple resources using the target attribute in a patch. Given your examples (after fixing the errors I pointed out in my comment), we can write a kustomization.yaml like this:

    apiVersion: kustomize.config.k8s.io/v1beta1
    kind: Kustomization
    resources:
    - deployment-v1.yaml
    - deployment-v2.yaml
    
    patches:
      - target:
          kind: Deployment
        patch: |
          apiVersion: apps/v1
          kind: Deployment
          metadata:
            name: this_value_is_ignored
          spec:
            replicas: 10
    

    The target attribute controls to what resources this patch will apply. With the above configuration, running kustomize build results in:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: web
      name: nginx-deployment
    spec:
      replicas: 10
    .
    .
    .
    

    and:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: web
      name: nginx-v1
    spec:
      replicas: 10
    .
    .
    .
    

    The above configuration would apply the patch to all deployments in your kustomization. If you wanted to limit the patching to only deployments matching a specific name prefix, you could write:

    apiVersion: kustomize.config.k8s.io/v1beta1
    kind: Kustomization
    resources:
    - deployment-v1.yaml
    - deployment-v2.yaml
    
    patches:
      - target:
          kind: Deployment
          name: nginx.*
        patch: |
          apiVersion: apps/v1
          kind: Deployment
          metadata:
            name: this_value_is_ignored
          spec:
            replicas: 10
    

    Note that the name pattern is regular expression, not a shell-style glob.