【问题标题】:Retrieve information from a configmap using jsonpath使用 jsonpath 从配置映射中检索信息
【发布时间】:2021-08-03 10:24:35
【问题描述】:

我有以下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]}

【问题讨论】:

  • 这是一个字符串"domain.yaml": "dns: \n - 127.0.0.1\n - 127.0.0.1\n"

标签: kubernetes jsonpath configmap


【解决方案1】:

这不是很简单,因为

dns:
- 127.0.0.1
- 127.0.0.2

被解释为单个 json 值。

例如kubectl get cm testcm -o jsonpath='{.data}' 返回以下输出{"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' 

将返回127.0.0.1

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 2011-03-26
    • 1970-01-01
    • 2017-04-27
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多