-
Notifications
You must be signed in to change notification settings - Fork 363
/
dump.go
336 lines (299 loc) · 10.8 KB
/
dump.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright 2021 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 support
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"path"
"path/filepath"
"runtime/pprof"
"strconv"
"strings"
"time"
"github.com/spf13/afero"
"k8s.io/utils/exec"
agentquerier "antrea.io/antrea/pkg/agent/querier"
clusterinformationv1beta1 "antrea.io/antrea/pkg/apis/crd/v1beta1"
controllerquerier "antrea.io/antrea/pkg/controller/querier"
"antrea.io/antrea/pkg/ovs/ovsctl"
"antrea.io/antrea/pkg/querier"
"antrea.io/antrea/pkg/util/logdir"
)
// AgentDumper is the interface for dumping runtime information of the agent. Its
// functions should only work in an agent Pod or a Windows Node which has an agent
// installed.
type AgentDumper interface {
// DumpFlows should create files that contains flows under the basedir.
DumpFlows(basedir string) error
// DumpHostNetworkInfo should create files that contains host network
// information under the basedir. Host network information should include
// links, routes, addresses and etc.
DumpHostNetworkInfo(basedir string) error
// DumpLog should create files that contains container logs of the agent
// Pod under the basedir.
DumpLog(basedir string) error
// DumpAgentInfo should create a file that contains AgentInfo of the agent Pod
// under the basedir.
DumpAgentInfo(basedir string) error
// DumpNetworkPolicyResources should create files that contains networkpolicy
// resources on the agent Pod under the base dir.
DumpNetworkPolicyResources(basedir string) error
// DumpHeapPprof should create a pprof file of heap usage of the agent.
DumpHeapPprof(basedir string) error
// DumpOVSPorts should create file that contains OF port descriptions under the basedir.
DumpOVSPorts(basedir string) error
}
// ControllerDumper is the interface for dumping runtime information of the
// controller. Its functions should only work in the controller Pod.
type ControllerDumper interface {
// DumpLog should create files that contains container logs of the controller
// Pod under the basedir.
DumpLog(basedir string) error
// DumpControllerInfo should create a file that contains ControllerInfo of
// the controller Pod under the basedir.
DumpControllerInfo(basedir string) error
// DumpNetworkPolicyResources should create files that contains networkpolicy
// resources on the controller Pod under the base dir.
DumpNetworkPolicyResources(basedir string) error
// DumpHeapPprof should create a pprof file of the heap usage of the controller.
DumpHeapPprof(basedir string) error
}
func DumpHeapPprof(fs afero.Fs, basedir string) error {
f, err := fs.Create(filepath.Join(basedir, "memprofile"))
if err != nil {
return err
}
defer f.Close()
return pprof.WriteHeapProfile(f)
}
func dumpAntctlGet(fs afero.Fs, executor exec.Interface, name, basedir string) error {
output, err := executor.Command("antctl", "-oyaml", "get", name).CombinedOutput()
if err != nil {
return fmt.Errorf("error when dumping %s: %w", name, err)
}
return writeFile(fs, filepath.Join(basedir, name), name, output)
}
func dumpNetworkPolicyResources(fs afero.Fs, executor exec.Interface, basedir string) error {
if err := dumpAntctlGet(fs, executor, "networkpolicies", basedir); err != nil {
return err
}
if err := dumpAntctlGet(fs, executor, "appliedtogroups", basedir); err != nil {
return err
}
return dumpAntctlGet(fs, executor, "addressgroups", basedir)
}
func timestampFilter(since string) *time.Time {
var timeFilter *time.Time
if since != "" {
duration, _ := time.ParseDuration(since)
start := time.Now().Add(-duration)
timeFilter = &start
}
return timeFilter
}
// parseTimeFromFileName parse time from log file name.
// example log file format: <component>.<hostname>.<user>.log.<level>.<yyyymmdd>-<hhmmss>.1
func parseTimeFromFileName(name string) (time.Time, error) {
ss := strings.Split(name, ".")
ts := ss[len(ss)-2]
return time.Parse("20060102-150405", ts)
}
// parseTimeFromLogLine parse timestamp from the log line.
// example(kubelet/agent/controller): "I0817 06:55:10.804384 1 shared_informer.go:270] caches populated"
// example(ovs): "2021-06-02T16:18:52.285Z|00004|reconnect|INFO|unix:/var/run/openvswitch/db.sock: connecting..."
// the first char indicates the log level.
func parseTimeFromLogLine(log string, year string, prefix string) (time.Time, error) {
ss := strings.Split(log, ".")
if ss[0] == "" {
return time.Time{}, fmt.Errorf("log line is empty")
}
dateStr := year + ss[0][1:]
layout := "20060102 15:04:05"
if prefix == "ovs" {
dateStr = ss[0]
layout = "2006-01-02T15:04:05"
}
return time.Parse(layout, dateStr)
}
// directoryCopy copies files under the srcDir to the targetDir. Only files whose name matches
// the prefixFilter will be copied. If prefixFiler is "", no filter is performed. At the same time, if the timeFilter is set,
// only files whose modTime is later than the timeFilter will be copied. If a file contains both older logs and matched logs, only
// the matched logs will be copied. Copied files will be located under the same relative path.
func directoryCopy(fs afero.Fs, targetDir string, srcDir string, prefixFilter string, timeFilter *time.Time) error {
err := fs.MkdirAll(targetDir, os.ModePerm)
if err != nil {
return fmt.Errorf("error when creating target dir: %w", err)
}
return afero.Walk(fs, srcDir, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if !info.Mode().IsRegular() {
return nil
}
if prefixFilter != "" && !strings.HasPrefix(info.Name(), prefixFilter) {
return nil
}
if timeFilter != nil && info.ModTime().Before(*timeFilter) {
return nil
}
targetPath := path.Join(targetDir, info.Name())
targetFile, err := fs.Create(targetPath)
if err != nil {
return err
}
defer targetFile.Close()
srcFile, err := fs.Open(filePath)
if err != nil {
return err
}
defer srcFile.Close()
startTime, err := parseTimeFromFileName(info.Name())
if timeFilter != nil {
// if name contains timestamp, use it to find the first matched file. If not, such as ovs log file,
// just parse the log file (usually there is only one log file for each component)
if err == nil && startTime.Before(*timeFilter) || err != nil {
data := ""
scanner := bufio.NewScanner(srcFile)
for scanner.Scan() {
// the size limit of single log line is 64k. marked it as known issue and fix it if
// error occurs
line := scanner.Text()
if data != "" {
data += line + "\n"
} else {
ts, err := parseTimeFromLogLine(line, strconv.Itoa(timeFilter.Year()), prefixFilter)
if err == nil {
if !ts.Before(*timeFilter) {
data += line + "\n"
}
}
}
}
_, err = targetFile.WriteString(data)
return err
}
}
_, err = io.Copy(targetFile, srcFile)
return err
})
}
// writeFile writes the given data to the specified filePath. Param "resource" is used to identify the type of the given
// data in the error message.
func writeFile(fs afero.Fs, filePath string, resource string, data []byte) error {
err := afero.WriteFile(fs, filePath, data, 0644)
if err != nil {
return fmt.Errorf("error when writing %s to file: %w", resource, err)
}
return nil
}
type controllerDumper struct {
fs afero.Fs
executor exec.Interface
cq controllerquerier.ControllerQuerier
since string
}
func (d *controllerDumper) DumpControllerInfo(basedir string) error {
return dumpAntctlGet(d.fs, d.executor, "controllerinfo", basedir)
}
func (d *controllerDumper) DumpNetworkPolicyResources(basedir string) error {
return dumpNetworkPolicyResources(d.fs, d.executor, basedir)
}
func (d *controllerDumper) DumpLog(basedir string) error {
logDir := logdir.GetLogDir()
return directoryCopy(d.fs, path.Join(basedir, "logs", "controller"), logDir, "antrea-controller", timestampFilter(d.since))
}
func (d *controllerDumper) DumpHeapPprof(basedir string) error {
return DumpHeapPprof(d.fs, basedir)
}
func NewControllerDumper(fs afero.Fs, executor exec.Interface, since string) ControllerDumper {
return &controllerDumper{
fs: fs,
executor: executor,
since: since,
}
}
type agentDumper struct {
fs afero.Fs
executor exec.Interface
ovsCtlClient ovsctl.OVSCtlClient
aq agentquerier.AgentQuerier
npq querier.AgentNetworkPolicyInfoQuerier
since string
}
func (d *agentDumper) DumpAgentInfo(basedir string) error {
ci := new(clusterinformationv1beta1.AntreaAgentInfo)
d.aq.GetAgentInfo(ci, false)
f, err := d.fs.Create(filepath.Join(basedir, "agentinfo"))
if err != nil {
return err
}
defer f.Close()
encoder := json.NewEncoder(f)
encoder.SetIndent("", " ")
return encoder.Encode(ci)
}
func (d *agentDumper) DumpNetworkPolicyResources(basedir string) error {
dump := func(o interface{}, name string) error {
f, err := d.fs.Create(filepath.Join(basedir, "agentinfo"))
if err != nil {
return err
}
defer f.Close()
encoder := json.NewEncoder(f)
encoder.SetIndent("", " ")
return encoder.Encode(o)
}
if err := dump(d.npq.GetAddressGroups(), "addressgroups"); err != nil {
return err
}
if err := dump(d.npq.GetNetworkPolicies(&querier.NetworkPolicyQueryFilter{}), "networkpolicies"); err != nil {
return err
}
return dump(d.npq.GetAppliedToGroups(), "appliedtogroups")
}
func (d *agentDumper) DumpFlows(basedir string) error {
flows, err := d.ovsCtlClient.DumpFlows()
if err != nil {
return fmt.Errorf("error when dumping flows: %w", err)
}
return writeFile(d.fs, filepath.Join(basedir, "flows"), "flows", []byte(strings.Join(flows, "\n")))
}
func (d *agentDumper) DumpHeapPprof(basedir string) error {
return DumpHeapPprof(d.fs, basedir)
}
func (d *agentDumper) DumpOVSPorts(basedir string) error {
portsDesc, err := d.ovsCtlClient.DumpPortsDesc()
if err != nil {
return fmt.Errorf("error when dumping ports desc: %w", err)
}
portData := make([]string, len(portsDesc))
for idx := range portsDesc {
portData[idx] = strings.Join(portsDesc[idx], "\n")
}
return writeFile(d.fs, filepath.Join(basedir, "ovsports"), "ports", []byte(strings.Join(portData, "\n")))
}
func NewAgentDumper(fs afero.Fs, executor exec.Interface, ovsCtlClient ovsctl.OVSCtlClient, aq agentquerier.AgentQuerier, npq querier.AgentNetworkPolicyInfoQuerier, since string) AgentDumper {
return &agentDumper{
fs: fs,
executor: executor,
ovsCtlClient: ovsCtlClient,
aq: aq,
npq: npq,
since: since,
}
}