/
pod.go
295 lines (247 loc) · 7.65 KB
/
pod.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package k8sclient
import (
"bufio"
"context"
"fmt"
"net/url"
"path"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/Tencent/bk-bcs/bcs-services/bcs-monitor/pkg/config"
)
// 需要取指针, 不能用常量
var (
// 最多返回 10W 条日志
MAX_TAIL_LINES = int64(100 * 1000)
MAX_LIMIT_BYTES = 30 * 1024 * 1024 // 最多 30M 数据
// 默认返回 100 条日志
DEFAULT_TAIL_LINES = int64(100)
)
const (
// Containers ...
Containers = "Containers"
// InitContainers ...
InitContainers = "InitContainers"
)
// LogQuery 日志查询参数, 精简后的 v1.PodLogOptions
type LogQuery struct {
ContainerName string `form:"container_name" binding:"required"` // 必填参数
Previous bool `form:"previous"`
StartedAt string `form:"started_at"`
FinishedAt string `form:"finished_at"`
TailLines int64 `form:"-"`
LimitBytes int64 `form:"-"`
podLogOptions *v1.PodLogOptions `form:"-"`
}
func (q *LogQuery) makeOptions() (*v1.PodLogOptions, error) {
opt := &v1.PodLogOptions{
Container: q.ContainerName,
Previous: q.Previous,
Timestamps: true,
}
// 开始时间, 需要用做查询
if q.StartedAt != "" {
opt.TailLines = &MAX_TAIL_LINES
t, err := time.Parse(time.RFC3339Nano, q.StartedAt)
if err != nil {
return nil, err
}
opt.SinceTime = &metav1.Time{Time: t}
} else {
opt.TailLines = &DEFAULT_TAIL_LINES
}
// 日志下载参数
if q.TailLines > 0 {
opt.TailLines = &q.TailLines
}
if q.LimitBytes > 0 {
opt.LimitBytes = &q.LimitBytes
}
// 结束时间, 只做校验
if q.FinishedAt != "" {
if _, err := time.Parse(time.RFC3339Nano, q.FinishedAt); err != nil {
return nil, err
}
}
q.podLogOptions = opt
return opt, nil
}
// Log 格式化的日志
type Log struct {
Log string `json:"log"`
Time string `json:"time"`
}
// LogWithPreviousLink :
type LogWithPreviousLink struct {
Logs []*Log `json:"logs"`
Previous string `json:"previous"` // 向上翻页链接
}
// MakePreviousLink 计算向上翻页链接
func (l *LogWithPreviousLink) MakePreviousLink(projectId, clusterId, namespace, podname string, opt *LogQuery) error {
if len(l.Logs) <= 2 {
return nil
}
// 按日志行数, 如果返回没有超过需求行数, 说明已经包含全部日志
if opt.podLogOptions.TailLines != nil && int64(len(l.Logs)) < *opt.podLogOptions.TailLines {
return nil
}
// 计算翻页
startTime := l.Logs[0].Time
endTime := l.Logs[len(l.Logs)-1].Time
sinceTime, err := calcSinceTime(startTime, endTime)
if err != nil {
return err
}
u := *config.G.Web.BaseURL
u.Path = path.Join(u.Path, fmt.Sprintf("/%s/projects/%s/clusters/%s/namespaces/%s/pods/%s/logs",
config.APIServicePrefix, projectId, clusterId, namespace, podname))
query := url.Values{}
query.Set("started_at", sinceTime.Format(time.RFC3339Nano))
query.Set("finished_at", startTime) // 本次一次的开始时间做上一页的结束时间
query.Set("container_name", opt.ContainerName)
query.Set("previous", strconv.FormatBool(opt.Previous))
u.RawQuery = query.Encode()
l.Previous = u.String()
return nil
}
// calcSinceTime 计算下一次的开始时间
func calcSinceTime(startTime string, endTime string) (*time.Time, error) {
// 简单场景, 认为日志打印量是均衡的,通过计算时间差获取
start, err := time.Parse(time.RFC3339Nano, startTime)
if err != nil {
return nil, errors.Wrapf(err, "startTime: %s", startTime)
}
end, err := time.Parse(time.RFC3339Nano, endTime)
if err != nil {
return nil, errors.Wrapf(err, "endTime: %s", endTime)
}
duration := end.Sub(start)
sinceTime := start.Add(-duration)
return &sinceTime, nil
}
// Container 格式化的容器, 精简后的 v1.Container
type Container struct {
Name string `json:"name"`
ContainerType string `json:"container_type"`
}
// parseLog 解析Log
func parseLog(rawLog string) (*Log, error) {
item := strings.SplitN(rawLog, " ", 2)
if len(item) != 2 {
return nil, errors.Errorf("invalid log, %s", rawLog)
}
return &Log{Time: item[0], Log: item[1]}, nil
}
// GetPodContainers 获取 Pod 容器名称列表
func GetPodContainers(ctx context.Context, clusterId, namespace, podname string) ([]*Container, error) {
client, err := GetK8SClientByClusterId(clusterId)
if err != nil {
return nil, err
}
pod, err := client.CoreV1().Pods(namespace).Get(ctx, podname, metav1.GetOptions{})
if err != nil {
return nil, err
}
containers := make([]*Container, 0, len(pod.Spec.Containers))
for _, container := range pod.Spec.Containers {
containers = append(containers, &Container{Name: container.Name, ContainerType: Containers})
}
// 获取InitContainers name
for _, container := range pod.Spec.InitContainers {
containers = append(containers, &Container{Name: container.Name, ContainerType: InitContainers})
}
return containers, nil
}
// GetPodLogByte 获取日志
func GetPodLogByte(ctx context.Context, clusterId, namespace, podname string, opt *LogQuery) ([]byte, error) {
client, err := GetK8SClientByClusterId(clusterId)
if err != nil {
return nil, err
}
opts, err := opt.makeOptions()
if err != nil {
return nil, err
}
result := client.CoreV1().Pods(namespace).GetLogs(podname, opts).Do(ctx)
if result.Error() != nil {
// 错误以log的方式输出
return []byte(result.Error().Error()), nil
}
body, err := result.Raw()
if err != nil {
return nil, err
}
return body, nil
}
// GetPodLog 获取格式的日志列表
func GetPodLog(ctx context.Context, clusterId, namespace, podname string, opt *LogQuery) (*LogWithPreviousLink, error) {
result, err := GetPodLogByte(ctx, clusterId, namespace, podname, opt)
if err != nil {
return nil, err
}
logs := strings.Split(string(result), "\n")
logList := make([]*Log, 0, len(logs))
for _, logStr := range logs {
// 最新的日志有可能返回空行
if logStr == "" {
continue
}
log, err := parseLog(logStr)
if err != nil {
continue
}
// 只返回当前历史数据
if opt.FinishedAt != "" && log.Time >= opt.FinishedAt {
break
}
logList = append(logList, log)
}
logResult := &LogWithPreviousLink{Logs: logList}
return logResult, nil
}
// GetPodLogStream 获取日志流
func GetPodLogStream(ctx context.Context, clusterId, namespace, podname string, opt *LogQuery) (<-chan *Log, error) {
client, err := GetK8SClientByClusterId(clusterId)
if err != nil {
return nil, err
}
opts, err := opt.makeOptions()
if err != nil {
return nil, err
}
opts.Follow = true
reader, err := client.CoreV1().Pods(namespace).GetLogs(podname, opts).Stream(ctx)
if err != nil {
return nil, err
}
logChan := make(chan *Log)
// 会通过上面的 stream ctx 控制关闭 goroutine
go func() {
defer close(logChan)
defer reader.Close()
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
log, err := parseLog(scanner.Text())
if err != nil {
continue
}
logChan <- log
}
}()
return logChan, nil
}