Skip to content
This repository was archived by the owner on Jun 25, 2024. It is now read-only.

Commit b463a5f

Browse files
author
Yann-Gael GAUTHERON
committed
feat: get realtime pod logs in a new tab
1 parent dfad911 commit b463a5f

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

cmd/ktop.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ func (k *ktopCmd) run(cmd *cobra.Command, args []string) error {
132132
termui.NewCol(1./2, logo),
133133
termui.NewCol(1./2, hint),
134134
),
135-
termui.NewRow(6./12, monitor.GetPodTable()),
136-
termui.NewRow(4./12,
135+
termui.NewRow(3./12, monitor.GetPodTable()),
136+
termui.NewRow(5./12, monitor.GetLogs()),
137+
termui.NewRow(2./12,
137138
termui.NewCol(1./2, monitor.GetCPUGraph()),
138139
termui.NewCol(1./2, monitor.GetMemGraph()),
139140
),

pkg/ktop/ktop.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const (
4040
type Monitor struct {
4141
*kube.KubeClients
4242

43+
logs *ui.TextField
4344
table *ui.Table
4445
tableTypeCircle *ring.Ring
4546

@@ -67,6 +68,11 @@ func NewMonitor(kubeclients *kube.KubeClients, podQuery, containerQuery, nodeQue
6768
table.BorderStyle = termui.NewStyle(borderColor)
6869
table.CursorColor = selectedTableColor
6970

71+
// logs of pod
72+
logs := ui.NewTextField()
73+
logs.Text = `TEST TextField`
74+
logs.TextStyle = termui.NewStyle(termui.Color(244), termui.ColorClear)
75+
7076
// graph for cpu
7177
cpu := ui.NewGraph()
7278
cpu.Title = "⎈ CPU Usage ⎈"
@@ -86,6 +92,7 @@ func NewMonitor(kubeclients *kube.KubeClients, podQuery, containerQuery, nodeQue
8692
mem.LimitColor = graphLimitColor
8793

8894
monitor.table = table
95+
monitor.logs = logs
8996
monitor.cpuGraph = cpu
9097
monitor.memGraph = mem
9198
return monitor
@@ -149,6 +156,10 @@ func (m *Monitor) GetPodTable() *ui.Table {
149156
return m.table
150157
}
151158

159+
func (m *Monitor) GetLogs() *ui.TextField {
160+
return m.logs
161+
}
162+
152163
func (m *Monitor) Update() error {
153164
nodeList, err := m.GetNodeList(labels.Everything())
154165
if err != nil {
@@ -277,6 +288,12 @@ func (m *Monitor) fetchPodResources() ([]*resource.Resource, []*resource.Summari
277288
// filtered
278289
for _, podMetrics := range FilterPodMetrics(m.podQuery, podMetricsList.Items) {
279290
podName := podMetrics.Name
291+
podLogs, err := m.GetPodLogs(*m.Flags.Namespace, podName)
292+
// fmt.Print("podLogs")
293+
// fmt.Print(podLogs)
294+
if err != nil {
295+
fmt.Print(err)
296+
}
280297
pod := FindPod(podName, podList.Items)
281298
if pod == nil {
282299
continue
@@ -297,7 +314,7 @@ func (m *Monitor) fetchPodResources() ([]*resource.Resource, []*resource.Summari
297314
corev1.ResourceList{
298315
corev1.ResourceCPU: cpu,
299316
corev1.ResourceMemory: mem,
300-
})
317+
}, podLogs)
301318
summarizedResources = append(summarizedResources, summarizedResource)
302319
}
303320
return resources, summarizedResources, nil
@@ -330,6 +347,8 @@ func (m *Monitor) updateSummarizedGraph(nodeList *corev1.NodeList, summarized *r
330347
limitMemory := GetResourceValue(node.Status.Allocatable, corev1.ResourceMemory)
331348
limitMemoryStr := GetResourceValueString(node.Status.Allocatable, corev1.ResourceMemory)
332349

350+
m.logs.Text = summarized.GetLogs()
351+
333352
m.cpuGraph.LabelHeader = fmt.Sprintf("Name: %v", summarized.GetPodName())
334353
m.cpuGraph.Data = append(m.cpuGraph.Data, cpuUsage)
335354
m.cpuGraph.LabelData = fmt.Sprintf("Usage: %v", cpuUsageStr)

pkg/kube/clients.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package kube
22

33
import (
4+
"io"
5+
46
"github.com/pkg/errors"
57

68
corev1 "k8s.io/api/core/v1"
@@ -53,6 +55,40 @@ func (k *KubeClients) GetPodList(namespace string, labelSelector labels.Selector
5355
return k.clientset.CoreV1().Pods(namespace).List(metav1.ListOptions{LabelSelector: labelSelector.String()})
5456
}
5557

58+
func (k *KubeClients) GetPodLogs(namespace string, podName string) (string, error) {
59+
count := int64(100)
60+
follow := false
61+
message := ""
62+
podLogOptions := corev1.PodLogOptions{
63+
//Container: containerName,
64+
Follow: follow,
65+
TailLines: &count,
66+
}
67+
podLogRequest := k.clientset.CoreV1().
68+
Pods(namespace).
69+
GetLogs(podName, &podLogOptions)
70+
stream, err := podLogRequest.Stream()
71+
if err != nil {
72+
return "failure1", err
73+
}
74+
defer stream.Close()
75+
for {
76+
buf := make([]byte, 2000)
77+
numBytes, err := stream.Read(buf)
78+
if err == io.EOF {
79+
break
80+
}
81+
if err != nil {
82+
return "failure2", err
83+
}
84+
if numBytes == 0 {
85+
continue
86+
}
87+
message += string(buf[:numBytes])
88+
}
89+
return message, nil
90+
}
91+
5692
func (k *KubeClients) GetPodMetricsList(namespace string, labelSelector labels.Selector) (*metrics.PodMetricsList, error) {
5793
return k.metricsClient.getPodMetricsList(namespace, labelSelector)
5894
}

pkg/resource/summarized.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import (
99
type SummarizedResource struct {
1010
podName string
1111
nodeName string
12+
logs string
1213
usage corev1.ResourceList
1314
}
1415

15-
func NewSummarizedResource(p corev1.Pod, sumUsage corev1.ResourceList) *SummarizedResource {
16+
func NewSummarizedResource(p corev1.Pod, sumUsage corev1.ResourceList, logs string) *SummarizedResource {
1617
return &SummarizedResource{
1718
podName: p.Name,
1819
nodeName: p.Spec.NodeName,
20+
logs: logs,
1921
usage: sumUsage,
2022
}
2123
}
@@ -33,6 +35,10 @@ func (s *SummarizedResource) GetCpuUsage() (float64, string) {
3335
GetResourceValueString(s.usage, corev1.ResourceCPU)
3436
}
3537

38+
func (s *SummarizedResource) GetLogs() string {
39+
return s.logs
40+
}
41+
3642
func (s *SummarizedResource) GetMemoryUsage() (float64, string) {
3743
return GetResourceValue(s.usage, corev1.ResourceMemory),
3844
GetResourceValueString(s.usage, corev1.ResourceMemory)

0 commit comments

Comments
 (0)