Resource not found while reading logs for kubernetes pod in client go

9/28/2018

Creating a pod deployment first

if pod, err = clientset.CoreV1().Pods(namespace).Create(&v1.Pod{
        TypeMeta: metav1.TypeMeta{
            Kind:       "Deployment",
            APIVersion: "apps/v1",
        },
        ObjectMeta: metav1.ObjectMeta{
            Name:      "mithu",
            Namespace: namespace,
        },
        Spec: v1.PodSpec{
            Containers: []v1.Container{
                v1.Container{
                    Name:            "mithu",
                    Image:           "test-app",
                    ImagePullPolicy: "IfNotPresent",
                    Ports: []v1.ContainerPort{
                        v1.ContainerPort{
                            Name:          "local",
                            HostPort:      8081,
                            ContainerPort: 8081,
                            Protocol:      v1.ProtocolTCP,
                        },
                    },
                    Env: []v1.EnvVar{
                        v1.EnvVar{
                            Name:  "ASD",
                            Value: "",
                        },
                    },
                },
            },
            RestartPolicy: v1.RestartPolicyOnFailure,
        },
}); err == nil {
    fmt.Println("Pod created succesfuly with status", pod.Name)
} else {
    fmt.Println("Error1: ", err)
}

time.Sleep(5000)

Now the pod has been deployed, I am now reading the logs

req := clientset.RESTClient().Get().
    Namespace(namespace).
    Name(pod.Name).
    Resource("pods").
    SubResource("log").
    Param("follow", "").
    Param("container", "").
    Param("previous", "").
    Param("timestamps", "")

readCloser, err := req.Stream()
if err != nil {
    fmt.Println("Error2: ", err)
} else {
    buf := new(bytes.Buffer)
    _, err = io.Copy(buf, readCloser)
    fmt.Println("log : ", buf.String())
}

Followed reading of logs from here Pod is being created successfully but the client cant seem to find the resources getting the error as Error2: the server could not find the requested resource (get pods.meta.k8s.io mithu)

If I try to get logs from command line, there is no problems. I think the major issue is between pods.meta.k8s.io and the command line args pods

-- mohd.gadi
kubernetes
kubernetes-go-client

1 Answer

9/29/2018

This worked for me:

req := clientset.CoreV1().Pods(namespace).GetLogs(
    pod.Name, 
    &v1.PodLogOptions{},
)

readCloser, err := req.Stream()
if err != nil {
        fmt.Println("Error2: ", err)
} else {
        buf := new(bytes.Buffer)
        _, err = io.Copy(buf, readCloser)
        fmt.Println("log : ", buf.String())
}
-- Tushar Dudani
Source: StackOverflow