-1

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.

Pradyskumar
  • 296
  • 1
  • 3
  • 15
  • It helps if the examples you post in your question are syntactically valid. There are indentation problems with these manifests and the character `—` is not the same as `-`. These problems make these files invalid. – larsks Jul 13 '23 at 12:28

1 Answers1

1

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.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • Thanks. It is working. However, I am not sure about the regular expression. It seems to be targeting all the Deployment manifests. – Pradyskumar Jul 18 '23 at 07:42
  • The regex absolutely works as intended. [Here](https://github.com/larsks/so-example-76678256-patch-multiple) is a demo repository; run `kustomize build` and you'll see that the patch is only applied to the `web.*` deployments. – larsks Jul 18 '23 at 13:26