1

When using Istio with Kubernetes, a number of different manifests require the same environment-specific values. For example, the host address is required by both the Gateway (under spec/servers/hosts) and VirtualService (under spec/hosts). The typical approach to changing the address for different environments is to apply Kustomize patches. Is it possible to use a single patch to transform/insert the value into each manifest that needs it, or somehow maintain just one copy of the address that gets inserted where needed? The alternative is having to maintain multiple patch files with the same host address, and I would like to avoid duplication.

---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: foo
spec:
  selector:
    istio: bar
  servers:
    - port:
        number: 443
        name: https
        protocol: HTTPS
      tls:
        mode: SIMPLE
        credentialName: secret
      hosts:
        - test.acme.com

---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bazz
spec:
  hosts:
    - test.acme.com
  gateways:
    - foo
  http:
    - match:
      - uri:
          prefix: /api/v1
      route:
        - destination:
            host: blarg
            port:
              number: 80

Boon
  • 1,073
  • 1
  • 16
  • 42
  • This question is hard to answer without a [mcve]. It is possible for a patch to apply to multiple resources (see [here](https://github.com/kubernetes-sigs/kustomize/blob/master/examples/patchMultipleObjects.md)), but that only works when the value you want to modify is at the same path in all the target resources. So the answer is "maybe", because it depends on exactly what your manifests look like. – larsks Feb 03 '23 at 15:23
  • @larsks I have added an example to my post – Boon Feb 03 '23 at 15:33

1 Answers1

1

This isn't going to be possible with just Kustomize other than by using multiple patches. Because you're looking to change the value in objects of different types, this can't be done with a single patch. So you could do this:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- manifests.yaml

patches:
  - target:
      kind: Gateway
      name: foo
    patch: |
      - op: replace
        path: /spec/servers/0/hosts/0
        value: foo.acme.com
  - target:
      kind: VirtualService
      name: bazz
    patch: |
      - op: replace
        path: /spec/hosts/0
        value: foo.acme.com

If you find you need to do this frequently (maybe you have a bunch of similar services), you could move the manifests into a helm chart and then inflate that with kustomize.

larsks
  • 277,717
  • 41
  • 399
  • 399