按需使用:未获取容器名称,命令和端口

时间:2019-05-30 09:07:20

标签: kubernetes client-go

我正在使用client-go来获取我的kubernetes集群中容器的元数据。

我尝试从containerstatus和containerspec两者获取名称字段,但是给出的名称不正确(它只是一个子字符串,并且我在副本集定义yml文件中指定了该名称)。

pods, err := clientset.CoreV1().Pods("ma").List(metav1.ListOptions{})
    if err != nil {
        panic(err.Error())
    }
    for _, pod := range pods.Items {
        containerStatuses := pod.Status.ContainerStatuses
if len(containerStatuses) > 0 {
            for _, containerStatus := range containerStatuses {
                fmt.Printf("Container Status: Name=%s, and Id=%s \n", containerStatus.Name, containerStatus.ContainerID)
            }
        } else {
            fmt.Printf("Empty container Status.")
        }
        containerSpecs := pod.Spec.Containers
        if len(containerSpecs) > 0 {
            for _, containerSpec := range containerSpecs {
                fmt.Printf("Container Spec.....: Name=%s, and Command=%s and ports=%s\n", containerSpec.Name, containerSpec.Command, containerSpec.Ports)
            }
        } else {
            fmt.Printf("Empty container Specs.")
        }

此代码显示输出,名称为=“ myagent”,其中名称应类似于以下内容(来自docker ps输出):

来自docker ps,这是容器的输出。

CONTAINER ID        IMAGE                                                                                                                                           COMMAND                   CREATED             STATUS              PORTS               NAMES
22b26a0f10f2        app-457@sha256:9ffa8a0ebee1051e6b3424cb2cecefb7efc57f161187a6df17ed137f364caab2        "/bin/sh -c \"./sta..."   2 weeks ago         Up 2 weeks                              k8s_myagent_replicaset1-test-458-bc-ssk4q_ma_042b3343-7621-11e9-9b24-02cb478680f6_0
f291344ab51b        app-457@sha256:9ffa8a0ebee1051e6b3424cb2cecefb7efc57f161187a6df17ed137f364caab2        "/bin/sh -c \"./sta..."   2 weeks ago         Up 2 weeks                              k8s_myagent_replicaset1-test-458-bc-p2hcx_ma_042c3ec7-7621-11e9-9b24-02cb478680f6_0

0 个答案:

没有答案