-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
157 lines (131 loc) · 3.84 KB
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package testwave
import (
"bufio"
"context"
"fmt"
"io"
"log"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/celestiaorg/testwave/pkg/logs"
"github.com/celestiaorg/testwave/pkg/deployer"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// ANSI escape codes for colors
const (
colorGreen = "\x1b[32m"
colorBlue = "\x1b[94m"
colorRed = "\x1b[91m"
colorReset = "\x1b[0m"
)
var flagsLogs struct {
kubeConfig string
namespace string
timeout int
}
func logsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: LogsCmd,
Short: "shows the logs interactively",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientset, err := deployer.KubeClientset(flagsStart.kubeConfig)
if err != nil {
logrus.Errorf("Error building kubeconfig: %v", err)
os.Exit(1)
}
cLog := logs.Logs{
Clientset: clientset,
Namespace: flagsLogs.namespace,
}
scanner := bufio.NewScanner(os.Stdin)
for {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(flagsLogs.timeout)*time.Minute)
defer cancel()
pods, err := cLog.AllPods(ctx)
if err != nil {
logrus.Errorf("Error getting pods: %v", err)
os.Exit(1)
}
type container struct {
name string
pod string
}
searchMap := make(map[int64]container) // key: menu number, value: container+pod name
counter := int64(1)
for _, p := range pods {
pName := p.Name + " - " + timeAgo(p.CreationTimestamp.Time)
fmt.Printf("pod: %s%s%s\n", colorGreen, pName, colorReset)
for _, c := range p.Status.ContainerStatuses {
searchMap[counter] = container{name: c.Name, pod: p.Name}
var status string
if c.State.Running != nil {
status = "Running"
} else if c.State.Terminated != nil {
status = fmt.Sprintf("Terminated (Exit Code: %d)", c.State.Terminated.ExitCode)
} else if c.State.Waiting != nil {
status = fmt.Sprintf("Waiting (%s)", c.State.Waiting.Reason)
} else {
status = "Unknown"
}
fmt.Printf("%s[%3d] - %s - %s%s\n", colorBlue, counter, c.Name, colorReset, status)
counter++
}
}
fmt.Printf("\n%s[%3d] - Exit%s\n", colorRed, 0, colorReset)
fmt.Print("Enter the container number to see the logs (press Enter to refresh): ")
scanner.Scan()
input := scanner.Text()
// empty string Enter key press
if strings.TrimSpace(input) == "" {
fmt.Println("Refreshing menu...")
continue
}
conNumber, err := strconv.ParseInt(input, 10, 64)
if err != nil {
log.Printf("Invalid input: %v", err)
continue
}
if conNumber == 0 {
return nil
}
conInfo, ok := searchMap[conNumber]
if !ok {
logrus.Errorf("Invalid container number: %d", conNumber)
continue
}
logrus.Infof("Logs for pod: %s container: %s", conInfo.pod, conInfo.name)
logsReader, err := cLog.LogsReader(context.Background(), conInfo.pod, conInfo.name)
if err != nil {
logrus.Errorf("Error getting logs reader: %v", err)
continue
}
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
go func() {
_, err := io.Copy(os.Stdout, logsReader)
if err != nil {
logrus.Errorf("Error copying logs: %v", err)
}
}()
<-stop // Wait for the stop signal
logsReader.Close()
}
},
}
homedir, err := os.UserHomeDir()
if err != nil {
logrus.Errorf("Error getting user home dir: %v", err)
os.Exit(1)
}
defaultKubeConfig := homedir + "/.kube/config"
cmd.Flags().StringVar(&flagsLogs.kubeConfig, flagKubeConfig, defaultKubeConfig, "Path to kubeconfig file")
cmd.Flags().StringVar(&flagsLogs.namespace, flagNamespace, "default", "Kubernetes namespace")
cmd.Flags().IntVar(&flagsLogs.timeout, flagTimeout, 1, "Timeout for fetching data (in minutes)")
return cmd
}