Kubernetes + jq - 检索每个 pod 的容器列表产生笛卡尔积

Kubernetes + jq - retrieving containers list per pod yields cartesian product

我正在尝试在 kubernetes json 输出上使用 jq,以创建包含对象列表的新 json 对象 - 每个 pod 的容器和图像,但是我得到的是笛卡尔积。

我的输入数据(从敏感信息中截断):

{
    "apiVersion": "v1",
    "items": [
        {
            "apiVersion": "v1",
            "kind": "Pod",
            "metadata": {
                "creationTimestamp": "2021-06-30T12:45:40Z",
                "name": "pod-1",
                "namespace": "default",
                "resourceVersion": "757679286",
                "selfLink": "/api/v1/namespaces/default/pods/pod-1"
            },
            "spec": {
                "containers": [
                    {
                        "image": "image-1",
                        "imagePullPolicy": "Always",
                        "name": "container-1",
                        "resources": {},
                        "terminationMessagePath": "/dev/termination-log",
                        "terminationMessagePolicy": "File",
                        "volumeMounts": [
                            {
                                "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
                                "readOnly": true
                            }
                        ]
                    },
                    {
                        "image": "image-2",
                        "imagePullPolicy": "Always",
                        "name": "container-2",
                        "resources": {},
                        "terminationMessagePath": "/dev/termination-log",
                        "terminationMessagePolicy": "File",
                        "volumeMounts": [
                            {
                                "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
                                "readOnly": true
                            }
                        ]
                    }
                ],
                "dnsPolicy": "ClusterFirst",
                "enableServiceLinks": true,
                "priority": 0,
                "restartPolicy": "Always",
                "schedulerName": "default-scheduler",
                "securityContext": {},
                "serviceAccount": "default",
                "serviceAccountName": "default",
                "terminationGracePeriodSeconds": 30,
                "tolerations": [
                    {
                        "effect": "NoExecute",
                        "key": "node.kubernetes.io/not-ready",
                        "operator": "Exists",
                        "tolerationSeconds": 300
                    },
                    {
                        "effect": "NoExecute",
                        "key": "node.kubernetes.io/unreachable",
                        "operator": "Exists",
                        "tolerationSeconds": 300
                    }
                ],
                "volumes": [
                    {
                        "name": "default-token-b954f",
                        "secret": {
                            "defaultMode": 420,
                            "secretName": "default-token-b954f"
                        }
                    }
                ]
            },
            "status": {
                "conditions": [
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2021-06-30T12:45:40Z",
                        "status": "True",
                        "type": "Initialized"
                    },
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2021-06-30T12:45:40Z",
                        "message": "containers with unready status: [container-1 container-2]",
                        "reason": "ContainersNotReady",
                        "status": "False",
                        "type": "Ready"
                    },
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2021-06-30T12:45:40Z",
                        "message": "containers with unready status: [container-1 container-2]",
                        "reason": "ContainersNotReady",
                        "status": "False",
                        "type": "ContainersReady"
                    },
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2021-06-30T12:45:40Z",
                        "status": "True",
                        "type": "PodScheduled"
                    }
                ],
                "containerStatuses": [
                    {
                        "image": "image-1",
                        "imageID": "",
                        "lastState": {},
                        "name": "container-1",
                        "ready": false,
                        "restartCount": 0,
                        "started": false,
                        "state": {
                            "waiting": {
                                "message": "Back-off pulling image \"image-1\"",
                                "reason": "ImagePullBackOff"
                            }
                        }
                    },
                    {
                        "image": "image-2",
                        "imageID": "",
                        "lastState": {},
                        "name": "container-2",
                        "ready": false,
                        "restartCount": 0,
                        "started": false,
                        "state": {
                            "waiting": {
                                "message": "Back-off pulling image \"image-2\"",
                                "reason": "ImagePullBackOff"
                            }
                        }
                    }
                ],
                "qosClass": "BestEffort",
                "startTime": "2021-06-30T12:45:40Z"
            }
        }
    ],
    "kind": "List",
    "metadata": {
        "resourceVersion": "",
        "selfLink": ""
    }
}

我的命令:

jq '.items[] | { "name": .metadata.name, "containers": [{ "name": .spec.containers[].name, "image": .spec.containers[].image }]} '

期望的输出:

{
  "name": "pod_1",
  "containers": [
    {
      "name": "container_1",
      "image": "image_1"
    },
    {
      "name": "container_2",
      "image": "image_2"
    }
  ]
}

我得到的输出:

{
  "name": "pod-1",
  "containers": [
    {
      "name": "container-1",
      "image": "image-1"
    },
    {
      "name": "container-1",
      "image": "image-2"
    },
    {
      "name": "container-2",
      "image": "image-1"
    },
    {
      "name": "container-2",
      "image": "image-2"
    }
  ]
}

谁能解释一下我做错了什么?

最好的问候,Piotr。

问题是"name": .spec.containers[].name"image": .spec.containers[].image: 这两个表达式都为 nameimage 生成每个值的序列,然后将它们合并。

为什么会得到笛卡尔积的简化示例:

jq -c -n '{name: ("A", "B"), value: ("C", "D")}'

输出:

{"name":"A","value":"C"}
{"name":"A","value":"D"}
{"name":"B","value":"C"}
{"name":"B","value":"D"}

您在输入上使用此 jq 过滤器获得所需的输出:

jq '
  .items[]
  | {
      "name": .metadata.name,
      "containers": .spec.containers
                    | map({name, image})
    }'

输出:

{
  "name": "pod-1",
  "containers": [
    {
      "name": "container-1",
      "image": "image-1"
    },
    {
      "name": "container-2",
      "image": "image-2"
    }
  ]
}