dywan666
dywan666

Reputation: 445

Kustomize - patch every item in a list

I'm trying to use patching in Kustomize to modify Kubernetes resources and I'm wondering if there is a neat way to update every item of a list.

Here is the yaml that I'd like to customize:

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
  annotations:
  name: validating-webhook-configuration
webhooks:
- admissionReviewVersions:
  - v1
  clientConfig:
    service:
      name: webhook-service
      namespace: rabbitmq-system
      path: /validate-rabbitmq-com-v1beta1-binding
- admissionReviewVersions:
  - v1
  clientConfig:
    service:
      name: webhook-service
      namespace: rabbitmq-system
      path: /validate-rabbitmq-com-v1beta1-exchange
- admissionReviewVersions:
  - v1
  clientConfig:
    service:
      name: webhook-service
      namespace: rabbitmq-system
      path: /validate-rabbitmq-com-v1beta1-federation

And here is the end effect that I want to achieve (see NEWVALUE):

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
  annotations:
  name: validating-webhook-configuration
webhooks:
- admissionReviewVersions:
  - v1
  clientConfig:
    NEWVALUE: value
    service:
      name: webhook-service
      namespace: rabbitmq-system
      path: /validate-rabbitmq-com-v1beta1-binding
- admissionReviewVersions:
  - v1
  clientConfig:
    NEWVALUE: value
    service:
      name: webhook-service
      namespace: rabbitmq-system
      path: /validate-rabbitmq-com-v1beta1-exchange
- admissionReviewVersions:
  - v1
  clientConfig:
    NEWVALUE: value
    service:
      name: webhook-service
      namespace: rabbitmq-system
      path: /validate-rabbitmq-com-v1beta1-federation

I'm fully aware of a Json6902 patch but it only allows me to modify one index at a time:

- op: add
  path: /webhooks/0/clientConfig/NEWVALUe
  value: value

Is there a way to modify every element in a fashion similar to a for loop or do I have to specify each index manually?

Upvotes: 6

Views: 7776

Answers (1)

Yoni Romm
Yoni Romm

Reputation: 61

According to what discussed here they don't have support for such case yet. when they add it you can do something like this:

kind: Kustomization
apiVersion: kustomize.config.k8s.io/v1beta1

resources:
- validating-webhook.yaml
- configmap.yaml

replacements:
- source:
    kind: ConfigMap
    name: cofignmap
    fieldPath: data.NEWVALUE
  targets:
  - select:
      name: validating-webhook-configuration
      kind: ValidatingWebhookConfiguration
    fieldPaths:
      - webhooks.*.clientConfig.NEWVALUE
    options:
      create: true

configMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: cofignmap
data:
  NEWVALUE: value

you can read about replacements in the docs

Upvotes: 6

Related Questions