使用 jsonpath 从 configmap 中检索信息

Retrieve information from a configmap using jsonpath

我有以下 configmap 我想在其中使用 jsonpath 检索 IP 如何做到这一点?

apiVersion: v1
data:
  domain.yaml: |
    dns:
      - 127.0.0.1
      - 127.0.0.2

我尝试了以下方法,但没有用:kubectl get cm domain -o jsonpath={.domain.yaml.dns[0]}

这不是很简单,因为

dns:
- 127.0.0.1
- 127.0.0.2

被解释为单个 json 值。

例如kubectl get cm testcm -o jsonpath='{.data}'returns如下输出{"domain.yaml":"dns:\n - 127.0.0.1\n - 127.0.0.2\n"} 如您所见,它以“domain.yaml”为键,其余是一个简单的字符串值。

为了获取ips,我们可以使用jq和cut magic。例如

kubectl get cm testcm -o jsonpath='{.data.*}' | cut -d$'\n' -f2 | sed 's/  - //g' 

会return127.0.0.1