kustomize 如何在每个阵列中定位替换

kustomize how to target replacement in every array

根据示例https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/replacements/

可以像这样定位特定的数组项 spec.template.spec.containers.[name=hello].env.[name=SECRET_TOKEN].value

但是我们如何定位每个数组项?

已尝试 - 如下,但它仅替换了最后一次出现的结果。

fieldPaths:
     - spec.http.-.route.0.destination.host

这是我的 kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: ef
images:
- name: gateway-image
resources:
- configmap.yaml
- virtual-services.yaml
replacements:
- source:
    kind: ConfigMap
    fieldPath: data.HOST
  targets:
  - select:
      kind: VirtualService
    fieldPaths:
     - spec.http.-.route.0.destination.host
    options:
      create: true

这是我的目标 yaml 服务-entries.yaml - 我希望两个地方的 HOST 都被替换。

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: v-rules-1
spec:
  hosts:
    - EXTERNAL-HOST
  gateways:
    - istio-system/default-gateway
    - mesh
  http: # note these are ordered - first rule matching wins
  - match:
    - uri:
       prefix: /foo
    route:
      - destination:
          host: PLACEHOLDER1
  - match:
    - uri:
        prefix: /bar
    route:
      - destination:
          host: PLACEHOLDER2

但是正如您在下面看到的,结果只有最后一个值:-

kustomize build .

产生这个。如您所见,example.com 仅在最后一个条目中正确显示 - PLACEHOLDE1 不受影响:-

apiVersion: v1
data:
  HOST: example.com
kind: ConfigMap
metadata:
  name: gateway-configmap
  namespace: ef
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: v-rules-1
  namespace: ef
spec:
  gateways:
  - istio-system/default-gateway
  - mesh
  hosts:
  - EXTERNAL-HOST
  http:
  - match:
    - uri:
        prefix: /foo
    route:
    - destination:
        host: PLACEHOLDER1
  - match:
    - uri:
        prefix: /bar
    route:
    - destination:
        host: example.com

所以问题是我们如何替换每个数组元素? fieldPath 的语法是什么。

kustomize 版本为 4.2.0

答案是不能。但是,作者似乎已经打开了一个功能请求才能这样做。

https://github.com/kubernetes-sigs/kustomize/issues/4053