/
log.go
210 lines (173 loc) · 4.86 KB
/
log.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2021 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rpaas
import (
"context"
"fmt"
"regexp"
"sort"
"strings"
"text/template"
"time"
"github.com/fatih/color"
"github.com/stern/stern/stern"
nginxv1alpha1 "github.com/tsuru/nginx-operator/api/v1alpha1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
var (
logFuncs = map[string]interface{}{
"color": func(color color.Color, text string) string {
return color.SprintFunc()(text)
},
"time": func(msg string) string {
idx := strings.IndexRune(msg, ' ')
return msg[:idx]
},
"message": func(msg string) string {
idx := strings.IndexRune(msg, ' ')
return msg[idx+1:]
},
}
logsTemplate = template.Must(template.New("rpaasv2.log").
Funcs(logFuncs).
Parse("{{ printf `%s [%s][%s]` (time .Message) .PodName .ContainerName }}: {{ message .Message }}"))
logsWithColor = template.Must(template.New("rpaasv2.log").
Funcs(logFuncs).
Parse("{{ color .PodColor (printf `%s [%s][%s]` (time .Message) .PodName .ContainerName) }}: {{ message .Message }}"))
)
func (m *k8sRpaasManager) Log(ctx context.Context, instanceName string, args LogArgs) error {
instance, err := m.GetInstance(ctx, instanceName)
if err != nil {
return err
}
nginx, err := m.getNginx(ctx, instance)
if err != nil {
return err
}
if args.Since == nil {
args.Since = func(n int64) *int64 { return &n }(60 * 60 * 24) // last 24 hours
}
args.template = logsTemplate
if args.Color {
args.template = logsWithColor
}
if args.Follow {
return m.watchLogs(ctx, nginx, args)
}
return m.listLogs(ctx, nginx, args)
}
func (m *k8sRpaasManager) listLogs(ctx context.Context, nginx *nginxv1alpha1.Nginx, args LogArgs) error {
pods, err := m.getPods(ctx, nginx)
if err != nil {
return err
}
sort.Slice(pods, func(i, j int) bool { return pods[i].Name < pods[j].Name })
var tails []*stern.Tail
for _, p := range pods {
if args.Pod != "" && args.Pod != p.Name {
continue
}
for _, c := range p.Spec.Containers {
if args.Container != "" && args.Container != c.Name {
continue
}
tails = append(tails, stern.NewTail(m.kcs.CoreV1(), p.Spec.NodeName, p.Namespace, p.Name, c.Name, args.template, args.Stdout, args.Stderr, &stern.TailOptions{
Timestamps: true,
SinceSeconds: *args.Since,
TailLines: args.Lines,
Location: time.UTC,
}))
}
}
for _, t := range tails {
t.Start(ctx)
}
return nil
}
func (m *k8sRpaasManager) watchLogs(ctx context.Context, nginx *nginxv1alpha1.Nginx, args LogArgs) error {
added := make(chan *stern.Target)
removed := make(chan *stern.Target)
errCh := make(chan error)
defer close(added)
defer close(errCh)
defer close(removed)
podRegexp := regexp.MustCompile(`.*`)
if args.Pod != "" {
podRegexp = regexp.MustCompile(fmt.Sprintf("^%s$", regexp.QuoteMeta(args.Pod)))
}
containerRegexp := regexp.MustCompile(`.*`)
if args.Container != "" {
containerRegexp = regexp.MustCompile(fmt.Sprintf("^%s$", regexp.QuoteMeta(args.Container)))
}
labelSet, err := labels.ConvertSelectorToLabelsMap(nginx.Status.PodSelector)
if err != nil {
return err
}
tails := make(map[string]*stern.Tail)
a, r, err := stern.Watch(ctx, m.kcs.CoreV1().Pods(nginx.Namespace),
podRegexp, nil,
containerRegexp, nil,
false, false,
[]stern.ContainerState{stern.RUNNING, stern.WAITING, stern.TERMINATED},
labelSet.AsSelector(),
fields.Everything(),
)
if err != nil {
return err
}
go routeTarget(ctx, a, r, added, removed, errCh)
go addTail(ctx, m.kcs.CoreV1(), added, tails, args)
go removeTail(removed, tails)
select {
case e := <-errCh:
return e
case <-ctx.Done():
return nil
}
}
func addTail(ctx context.Context, client v1.CoreV1Interface, added chan *stern.Target, tails map[string]*stern.Tail, args LogArgs) {
for p := range added {
tail := stern.NewTail(client, p.Node, p.Namespace, p.Pod, p.Container, args.template, args.Stdout, args.Stderr, &stern.TailOptions{
Timestamps: true,
SinceSeconds: *args.Since,
TailLines: args.Lines,
Follow: true,
Location: time.UTC,
})
tails[p.GetID()] = tail
go func(t *stern.Tail) { t.Start(ctx) }(tail)
}
}
func removeTail(removed chan *stern.Target, tails map[string]*stern.Tail) {
for p := range removed {
t, ok := tails[p.GetID()]
if !ok {
continue
}
t.Close()
delete(tails, p.GetID())
}
}
func routeTarget(ctx context.Context, wAdded, wRemoved, toAdd, toRemove chan *stern.Target, errCh chan error) {
for {
select {
case v, ok := <-wAdded:
if !ok {
errCh <- fmt.Errorf("lost watch connection")
return
}
toAdd <- v
case v, ok := <-wRemoved:
if !ok {
errCh <- fmt.Errorf("lost watch connection")
return
}
toRemove <- v
case <-ctx.Done():
return
}
}
}