golang解析k8s输出的json

万州客 · · 1006 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

今天累了,先休息,目标:解析出namespace, pod,container三要素保存。

命令

kubectl  get pod -l name=hello-spring -l version=v0.1 -n default -o json

输出

{
    "apiVersion": "v1",
    "items": [
        {
            "apiVersion": "v1",
            "kind": "Pod",
            "metadata": {
                "creationTimestamp": "2020-05-13T13:19:23Z",
                "generateName": "hello-spring-5cbbc4b86b-",
                "labels": {
                    "name": "hello-spring",
                    "pod-template-hash": "5cbbc4b86b",
                    "version": "v0.1"
                },
                "name": "hello-spring-5cbbc4b86b-n8d9c",
                "namespace": "default",
                "ownerReferences": [
                    {
                        "apiVersion": "apps/v1",
                        "blockOwnerDeletion": true,
                        "controller": true,
                        "kind": "ReplicaSet",
                        "name": "hello-spring-5cbbc4b86b",
                        "uid": "b1a69b00-cf40-4f9c-99d7-084a8b4a4ca2"
                    }
                ],
                "resourceVersion": "18131",
                "selfLink": "/api/v1/namespaces/default/pods/hello-spring-5cbbc4b86b-n8d9c",
                "uid": "898cc614-0a06-4c87-ac9d-23713eb2c53b"
            },
            "spec": {
                "containers": [
                    {
                        "image": "192.168.1.111:8089/tmp/hello-spring:v0.1",
                        "imagePullPolicy": "IfNotPresent",
                        "name": "hello-spring",
                        "ports": [
                            {
                                "containerPort": 8899,
                                "protocol": "TCP"
                            }
                        ],
                        "resources": {},
                        "terminationMessagePath": "/dev/termination-log",
                        "terminationMessagePolicy": "File",
                        "volumeMounts": [
                            {
                                "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
                                "name": "default-token-hhzdg",
                                "readOnly": true
                            }
                        ]
                    },
                    {
                        "command": [
                            "top",
                            "-b"
                        ],
                        "image": "alpine:latest",
                        "imagePullPolicy": "IfNotPresent",
                        "name": "alpine",
                        "resources": {},
                        "terminationMessagePath": "/dev/termination-log",
                        "terminationMessagePolicy": "File",
                        "volumeMounts": [
                            {
                                "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
                                "name": "default-token-hhzdg",
                                "readOnly": true
                            }
                        ]
                    }
                ],
                "dnsPolicy": "ClusterFirst",
                "enableServiceLinks": true,
                "nodeName": "localhost.localdomain",
                "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-hhzdg",
                        "secret": {
                            "defaultMode": 420,
                            "secretName": "default-token-hhzdg"
                        }
                    }
                ]
            },
            "status": {
                "conditions": [
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2020-05-13T13:19:23Z",
                        "status": "True",
                        "type": "Initialized"
                    },
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2020-05-13T13:19:24Z",
                        "status": "True",
                        "type": "Ready"
                    },
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2020-05-13T13:19:24Z",
                        "status": "True",
                        "type": "ContainersReady"
                    },
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2020-05-13T13:19:23Z",
                        "status": "True",
                        "type": "PodScheduled"
                    }
                ],
                "containerStatuses": [
                    {
                        "containerID": "docker://14ef0ec60cd537da963a5ce15af3efccf7a1596ee8028e2157b12121577965b4",
                        "image": "alpine:latest",
                        "imageID": "docker-pullable://alpine@sha256:ab00606a42621fb68f2ed6ad3c88be54397f981a7b70a79db3d1172b11c4367d",
                        "lastState": {},
                        "name": "alpine",
                        "ready": true,
                        "restartCount": 0,
                        "started": true,
                        "state": {
                            "running": {
                                "startedAt": "2020-05-13T13:19:24Z"
                            }
                        }
                    },
                    {
                        "containerID": "docker://52ae464f3034258ea945623a4ad9546abe700aa2f35cea8729cfcef3b85740af",
                        "image": "192.168.1.111:8089/tmp/hello-spring:v0.1",
                        "imageID": "docker://sha256:5a09a33064f52dafa3ead756f3c1638f5bf63acdad72621b585e09179dc7ec17",
                        "lastState": {},
                        "name": "hello-spring",
                        "ready": true,
                        "restartCount": 0,
                        "started": true,
                        "state": {
                            "running": {
                                "startedAt": "2020-05-13T13:19:24Z"
                            }
                        }
                    }
                ],
                "hostIP": "192.168.1.111",
                "phase": "Running",
                "podIP": "10.128.0.19",
                "podIPs": [
                    {
                        "ip": "10.128.0.19"
                    }
                ],
                "qosClass": "BestEffort",
                "startTime": "2020-05-13T13:19:23Z"
            }
        },
        {
            "apiVersion": "v1",
            "kind": "Pod",
            "metadata": {
                "creationTimestamp": "2020-05-13T13:19:23Z",
                "generateName": "hello-spring-5cbbc4b86b-",
                "labels": {
                    "name": "hello-spring",
                    "pod-template-hash": "5cbbc4b86b",
                    "version": "v0.1"
                },
                "name": "hello-spring-5cbbc4b86b-lf7fp",
                "namespace": "default",
                "ownerReferences": [
                    {
                        "apiVersion": "apps/v1",
                        "blockOwnerDeletion": true,
                        "controller": true,
                        "kind": "ReplicaSet",
                        "name": "hello-spring-5cbbc4b86b",
                        "uid": "b1a69b00-cf40-4f9c-99d7-084a8b4a4ca2"
                    }
                ],
                "resourceVersion": "18136",
                "selfLink": "/api/v1/namespaces/default/pods/hello-spring-5cbbc4b86b-lf7fp",
                "uid": "7a424bee-632e-4217-a4ff-10680fcf2bb5"
            },
            "spec": {
                "containers": [
                    {
                        "image": "192.168.1.111:8089/tmp/hello-spring:v0.1",
                        "imagePullPolicy": "IfNotPresent",
                        "name": "hello-spring",
                        "ports": [
                            {
                                "containerPort": 8899,
                                "protocol": "TCP"
                            }
                        ],
                        "resources": {},
                        "terminationMessagePath": "/dev/termination-log",
                        "terminationMessagePolicy": "File",
                        "volumeMounts": [
                            {
                                "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
                                "name": "default-token-hhzdg",
                                "readOnly": true
                            }
                        ]
                    },
                    {
                        "command": [
                            "top",
                            "-b"
                        ],
                        "image": "alpine:latest",
                        "imagePullPolicy": "IfNotPresent",
                        "name": "alpine",
                        "resources": {},
                        "terminationMessagePath": "/dev/termination-log",
                        "terminationMessagePolicy": "File",
                        "volumeMounts": [
                            {
                                "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
                                "name": "default-token-hhzdg",
                                "readOnly": true
                            }
                        ]
                    }
                ],
                "dnsPolicy": "ClusterFirst",
                "enableServiceLinks": true,
                "nodeName": "localhost.localdomain",
                "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-hhzdg",
                        "secret": {
                            "defaultMode": 420,
                            "secretName": "default-token-hhzdg"
                        }
                    }
                ]
            },
            "status": {
                "conditions": [
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2020-05-13T13:19:23Z",
                        "status": "True",
                        "type": "Initialized"
                    },
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2020-05-13T13:19:25Z",
                        "status": "True",
                        "type": "Ready"
                    },
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2020-05-13T13:19:25Z",
                        "status": "True",
                        "type": "ContainersReady"
                    },
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2020-05-13T13:19:23Z",
                        "status": "True",
                        "type": "PodScheduled"
                    }
                ],
                "containerStatuses": [
                    {
                        "containerID": "docker://446cb1d39c3340d2582b86d56507fc6ddb585cd67b12709e4c8a8bd21a6a609c",
                        "image": "alpine:latest",
                        "imageID": "docker-pullable://alpine@sha256:ab00606a42621fb68f2ed6ad3c88be54397f981a7b70a79db3d1172b11c4367d",
                        "lastState": {},
                        "name": "alpine",
                        "ready": true,
                        "restartCount": 0,
                        "started": true,
                        "state": {
                            "running": {
                                "startedAt": "2020-05-13T13:19:24Z"
                            }
                        }
                    },
                    {
                        "containerID": "docker://f6157b465bcc0811c03e1c30b4a46dfbac58ad1926378b2979ece820931f0b73",
                        "image": "192.168.1.111:8089/tmp/hello-spring:v0.1",
                        "imageID": "docker://sha256:5a09a33064f52dafa3ead756f3c1638f5bf63acdad72621b585e09179dc7ec17",
                        "lastState": {},
                        "name": "hello-spring",
                        "ready": true,
                        "restartCount": 0,
                        "started": true,
                        "state": {
                            "running": {
                                "startedAt": "2020-05-13T13:19:24Z"
                            }
                        }
                    }
                ],
                "hostIP": "192.168.1.111",
                "phase": "Running",
                "podIP": "10.128.0.18",
                "podIPs": [
                    {
                        "ip": "10.128.0.18"
                    }
                ],
                "qosClass": "BestEffort",
                "startTime": "2020-05-13T13:19:23Z"
            }
        }
    ],
    "kind": "List",
    "metadata": {
        "resourceVersion": "",
        "selfLink": ""
    }
}


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:万州客

查看原文:golang解析k8s输出的json

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1006 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传