自定义如何在每个数组中定位替换

bha*_*tol 4 kustomize

根据示例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
Run Code Online (Sandbox Code Playgroud)

这是我的 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

Run Code Online (Sandbox Code Playgroud)

这是我的目标 yaml service-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

Run Code Online (Sandbox Code Playgroud)

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

kustomize build .
Run Code Online (Sandbox Code Playgroud)

产生这个。如您所见,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

Run Code Online (Sandbox Code Playgroud)

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

kustomize版本是4.2.0

小智 6

您现在可以用作*通配符:

spec.http.*.route.0.destination.host
Run Code Online (Sandbox Code Playgroud)

这是在 2022 年添加的: https ://github.com/kubernetes-sigs/kustomize/issues/4053