如何在k8s yaml文件中引用标签值

问题描述 投票:1回答:2

我想在k8s yaml文件中引用VirtualService规范部分中的标签值。我使用$ {metadata.labels [component]}来表示下面的位置。有没有办法实现我的想法?

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: istio-ingress-version
  namespace: netops
  labels:
    component: version
spec:
  hosts:
  - "service.api.com" 
  gateways:
  - public-inbound-gateway 
  http:
  - match: 
    - uri:
        prefix: /${metadata.labels[component]}/
      headers: 
        referer:
          regex: ^https://[^\s/]*a.api.com[^\s]* 
    rewrite:
      uri: "/"
    route:
    - destination:
        host: ${metadata.labels[component]}.3da.svc.cluster.local  
  - match: 
    - uri:
        prefix: /${metadata.labels[component]}/
      headers: 
        referer:
          regex: ^https://[^\s/]*b.api.com[^\s]* 
    rewrite:
      uri: "/"
    route:
    - destination:
        host: ${metadata.labels[component]}.3db.svc.cluster.local  
  - match: 
    - uri:
        prefix: /${metadata.labels[component]}/
    rewrite:
      uri: "/"
    route:
    - destination:
        host: ${metadata.labels[component]}.3db.svc.cluster.local
kubernetes yaml istio
2个回答
2
投票

这不是Kubernetes本身的功能,但是存在可以帮助您解决此问题的其他工具。

其中最主要的是Helm。它允许您创建可以跨多个不同YAML文件共享的变量,允许您共享值甚至完全参数化您的部署。


1
投票

Look at downwardAPI to inject pod metadata like labels and annotations to pods at runtime.

apiVersion: v1
kind: Pod
metadata:
  name: kubernetes-downwardapi-volume-example
  labels:
    zone: us-est-coast
    cluster: test-cluster1
    rack: rack-22
  annotations:
    build: two
    builder: john-doe
spec:
  containers:
    - name: client-container
      image: gcr.io/google_containers/busybox
      command: ["sh", "-c", "while true; do if [[ -e /etc/labels ]]; then cat /etc/labels; fi; if [[ -e /etc/annotations ]]; then cat /etc/annotations; fi; sleep 5; done"]
      volumeMounts:
        - name: podinfo
          mountPath: /etc
          readOnly: false
  volumes:
    - name: podinfo
      downwardAPI:
        items:
          - path: "labels"
            fieldRef:
              fieldPath: metadata.labels
          - path: "annotations"
            fieldRef:
              fieldPath: metadata.annotations
© www.soinside.com 2019 - 2024. All rights reserved.