Client-go: Not getting container name, command and port

5/30/2019

I am using client-go to fetch the metadata of the containers in my kubernetes cluster.

I tried getting the name field from containerstatus and containerspec both, but the name which is given is not right (its only a substring and which i specified in the replicaset definition yml file).

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.")
        }

This code prints the output such that name is = "myagent" where as name should be something like below (from docker ps output):

From docker ps, this is the output of the containers.

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
-- Venkat Teki
client-go
kubernetes

0 Answers