-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
log_options.go
97 lines (85 loc) 路 2.1 KB
/
log_options.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
package dao
import (
"fmt"
"strings"
"time"
"github.com/derailed/k9s/internal/client"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// LogOptions represent logger options.
type LogOptions struct {
Path string
Container string
Lines int64
Previous bool
SingleContainer bool
MultiPods bool
ShowTimestamp bool
SinceTime string
SinceSeconds int64
In, Out string
}
// Info returns the option pod and container info.
func (o LogOptions) Info() string {
return fmt.Sprintf("%q::%q", o.Path, o.Container)
}
// HasContainer checks if a container is present.
func (o LogOptions) HasContainer() bool {
return o.Container != ""
}
// ToPodLogOptions returns pod log options.
func (o LogOptions) ToPodLogOptions() *v1.PodLogOptions {
opts := v1.PodLogOptions{
Follow: true,
Timestamps: true,
Container: o.Container,
Previous: o.Previous,
TailLines: &o.Lines,
}
if o.SinceSeconds < 0 {
return &opts
}
if o.SinceSeconds != 0 {
opts.SinceSeconds = &o.SinceSeconds
return &opts
}
if o.SinceTime == "" {
return &opts
}
if t, err := time.Parse(time.RFC3339, o.SinceTime); err == nil {
opts.SinceTime = &metav1.Time{Time: t.Add(time.Second)}
}
return &opts
}
// FixedSizeName returns a normalize fixed size pod name if possible.
func (o LogOptions) FixedSizeName() string {
_, n := client.Namespaced(o.Path)
tokens := strings.Split(n, "-")
if len(tokens) < 3 {
return n
}
var s []string
for i := 0; i < len(tokens)-1; i++ {
s = append(s, tokens[i])
}
return Truncate(strings.Join(s, "-"), 15) + "-" + tokens[len(tokens)-1]
}
// DecorateLog add a log header to display po/co information along with the log message.
func (o LogOptions) DecorateLog(bytes []byte) *LogItem {
item := NewLogItem(bytes)
if len(bytes) == 0 {
return item
}
item.SingleContainer = o.SingleContainer
if item.SingleContainer {
item.Container = o.Container
}
if o.MultiPods {
_, pod := client.Namespaced(o.Path)
item.Pod, item.Container = pod, o.Container
} else {
item.Container = o.Container
}
return item
}