【问题标题】:Parsing Kubernetes ConfigMap in Mixed Data Formats以混合数据格式解析 Kubernetes ConfigMap
【发布时间】:2020-10-30 23:35:17
【问题描述】:

考虑当前 JSON 格式的命名空间配置:

$ kubectl get configmap config -n metallb-system -o json
{
    "apiVersion": "v1",
    "data": {
        "config": "address-pools:\n- name: default\n  protocol: layer2\n  addresses:\n  - 192.168.0.105-192.168.0.105\n  - 192.168.0.110-192.168.0.111\n"
    },
    "kind": "ConfigMap",
    "metadata": {
        "annotations": {
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"data\":{\"config\":\"address-pools:\\n- name: default\\n  protocol: layer2\\n  addresses:\\n  - 192.168.0.105-192.168.0.105\\n  - 192.168.0.110-192.168.0.111\\n\"},\"kind\":\"ConfigMap\",\"metadata\":{\"annotations\":{},\"name\":\"config\",\"namespace\":\"metallb-system\"}}\n"
        },
        "creationTimestamp": "2020-07-10T08:26:21Z",
        "managedFields": [
            {
                "apiVersion": "v1",
                "fieldsType": "FieldsV1",
                "fieldsV1": {
                    "f:data": {
                        ".": {},
                        "f:config": {}
                    },
                    "f:metadata": {
                        "f:annotations": {
                            ".": {},
                            "f:kubectl.kubernetes.io/last-applied-configuration": {}
                        }
                    }
                },
                "manager": "kubectl",
                "operation": "Update",
                "time": "2020-07-10T08:26:21Z"
            }
        ],
        "name": "config",
        "namespace": "metallb-system",
        "resourceVersion": "2086",
        "selfLink": "/api/v1/namespaces/metallb-system/configmaps/config",
        "uid": "c2cfd2d2-866c-466e-aa2a-f3f7ef4837ed"
    }
}

我只对配置的地址池感兴趣。根据kubectl cheat sheet,我可以这样做来获取所需的地址范围:

$ kubectl get configmap config -n metallb-system -o jsonpath='{.data.config}'
address-pools:
- name: default
  protocol: layer2
  addresses:
  - 192.168.0.105-192.168.0.105
  - 192.168.0.110-192.168.0.111

但是,我的要求是始终只使用 JSON 解析器,我无法解析上述输出,因为它是在 YAML 中。

由于我不愿意将上述yaml输出直接使用(或通过格式转换操作),有没有合适的方法可以从kubectl接口以JSON格式获取地址范围?

【问题讨论】:

  • ConfigMap 的内容(“data.address-pools”)是 yaml 格式的。对于 kubectl 它是......好吧,数据:),所以它不支持重新格式化它。它可以是任何东西。我不认为你可以在没有任何外部工具的情况下重新格式化它......

标签: json kubernetes yaml configmap


【解决方案1】:

你需要yqkubectl ...

检查您的 configmap 后,我理解了将其转换为 YAML 时的结构:

---
apiVersion: v1
data:
  config: |
    address-pools:
    - name: default
      protocol: layer2
      addresses:
      - 192.168.0.105-192.168.0.105
      - 192.168.0.110-192.168.0.111
kind: ConfigMap
metadata:
 name: config

你可以清楚地看到.data.config是一个多行字符串..但它也可以转换为YAML。

  • 用 kubectl 解析多行字符串
  • 使用 yq 将此字符串视为 yaml。

这就是你要找的东西:

# all addresses
kubectl -n metallb-system get cm config -o 'go-template={{index .data "config" }}' | \
    yq -r '.["address-pools"][0].addresses'

# first address only
kubectl -n metallb-system get cm config -o 'go-template={{index .data "config" }}' | \
    yq -r '.["address-pools"][0].addresses[0]'

# second address only
kubectl -n metallb-system get cm config -o 'go-template={{index .data "config" }}' | \
    yq -r '.["address-pools"][0].addresses[1]'

# so on ...

【讨论】:

  • 我很清楚它是 YAML 格式的。事实上,我可以使用-o yaml 来达到同样的目的。不过,我的要求是仅将其解析为 JSON 内容。
  • 相同的概念.. 但使用jq而不是yq
猜你喜欢
  • 2019-11-13
  • 2020-07-22
  • 1970-01-01
  • 2018-01-18
  • 2013-09-22
  • 2022-01-12
  • 2020-08-10
  • 2017-03-11
  • 2021-12-20
相关资源
最近更新 更多