-
Notifications
You must be signed in to change notification settings - Fork 346
/
dump.go
254 lines (229 loc) · 8.33 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
// 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 (
"encoding/json"
"fmt"
"io"
"os"
"path"
"path/filepath"
"runtime/pprof"
"strings"
"github.com/spf13/afero"
"k8s.io/utils/exec"
agentquerier "github.com/vmware-tanzu/antrea/pkg/agent/querier"
clusterinformationv1beta1 "github.com/vmware-tanzu/antrea/pkg/apis/clusterinformation/v1beta1"
controllerquerier "github.com/vmware-tanzu/antrea/pkg/controller/querier"
"github.com/vmware-tanzu/antrea/pkg/ovs/ovsctl"
"github.com/vmware-tanzu/antrea/pkg/querier"
"github.com/vmware-tanzu/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)
}
// fileCopy copies files under the srcDir to the targetDir. Only files whose
// name matches the prefixFilter will be copied. Copied files will be located
// under the same relative path.
func fileCopy(fs afero.Fs, targetDir string, srcDir string, prefixFilter string) 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 !strings.HasPrefix(info.Name(), prefixFilter) {
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()
_, 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
}
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 fileCopy(d.fs, path.Join(basedir, "logs", "controller"), logDir, "antrea-controller")
}
func (d *controllerDumper) DumpHeapPprof(basedir string) error {
return DumpHeapPprof(d.fs, basedir)
}
func NewControllerDumper(fs afero.Fs, executor exec.Interface) ControllerDumper {
return &controllerDumper{
fs: fs,
executor: executor,
}
}
type agentDumper struct {
fs afero.Fs
executor exec.Interface
ovsCtlClient ovsctl.OVSCtlClient
aq agentquerier.AgentQuerier
npq querier.AgentNetworkPolicyInfoQuerier
}
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) AgentDumper {
return &agentDumper{
fs: fs,
executor: executor,
ovsCtlClient: ovsCtlClient,
aq: aq,
npq: npq,
}
}