|
|
@@ -1,7 +1,10 @@
|
|
|
package kubernetes
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
"context"
|
|
|
+ "fmt"
|
|
|
+ "io"
|
|
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
@@ -23,3 +26,24 @@ func (a *Agent) ListNamespaces() (*v1.NamespaceList, error) {
|
|
|
metav1.ListOptions{},
|
|
|
)
|
|
|
}
|
|
|
+
|
|
|
+// GetPodLogs streams real-time logs from a given pod.
|
|
|
+func (a *Agent) GetPodLogs(pod *v1.Pod) (string, error) {
|
|
|
+ podLogOpts := v1.PodLogOptions{}
|
|
|
+ req := a.Clientset.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &podLogOpts)
|
|
|
+ podLogs, err := req.Stream(context.TODO())
|
|
|
+ if err != nil {
|
|
|
+ return "Error: Cannot open log stream.", fmt.Errorf("Cannot open log stream for pod %s", pod.Name)
|
|
|
+ }
|
|
|
+ defer podLogs.Close()
|
|
|
+
|
|
|
+ buf := new(bytes.Buffer)
|
|
|
+ _, err = io.Copy(buf, podLogs)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return "Error: Cannot encode Pod logs.", fmt.Errorf("Cannot copy logs from pod %s to buf", pod.Name)
|
|
|
+ }
|
|
|
+ str := buf.String()
|
|
|
+
|
|
|
+ return str, nil
|
|
|
+}
|