This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
forked from Mirantis/virtlet
/
diag.go
292 lines (269 loc) · 7.12 KB
/
diag.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
/*
Copyright 2018 Mirantis
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 tools
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
"github.com/Mirantis/virtlet/pkg/diag"
"github.com/Mirantis/virtlet/pkg/version"
)
const (
maxVirtletPodLogLines = 20000
sonobuoyPluginsConfigMapName = "sonobuoy-plugins-cm"
virtletSonobuoyPluginFileName = "virtlet.yaml"
sonobuoyPluginYaml = `sonobuoy-config:
driver: Job
plugin-name: virtlet
result-type: virtlet
spec:
command:
- /bin/bash
- -c
- /sonobuoy.sh && sleep 3600
env:
- name: RESULTS_DIR
value: /tmp/results
image: mirantis/virtlet$TAG
name: sonobuoy-virtlet
volumeMounts:
- mountPath: /tmp/results
name: results
readOnly: false
`
)
type diagDumpCommand struct {
client KubeClient
out io.Writer
outDir string
useJSON bool
}
// NewDiagDumpCommand returns a new cobra.Command that dumps
// diagnostic information
func NewDiagDumpCommand(client KubeClient, out io.Writer) *cobra.Command {
d := &diagDumpCommand{
client: client,
out: out,
}
cmd := &cobra.Command{
Use: "dump output_dir",
Short: "Dump Virtlet diagnostics information",
Long: "Pull Virtlet diagnostics information from the nodes and dump it as a directory tree or JSON",
RunE: func(cmd *cobra.Command, args []string) error {
switch {
case !d.useJSON && len(args) != 1:
return errors.New("Must specify output directory or --json")
case !d.useJSON:
d.outDir = args[0]
case len(args) != 0:
return errors.New("This command does not accept arguments")
}
return d.Run()
},
}
cmd.Flags().BoolVar(&d.useJSON, "json", false, "Use JSON output")
return cmd
}
func (d *diagDumpCommand) diagResult() (diag.Result, error) {
dr := diag.Result{
IsDir: true,
Name: "nodes",
Children: make(map[string]diag.Result),
}
podNames, nodeNames, err := d.client.GetVirtletPodAndNodeNames()
if err != nil {
return diag.Result{}, err
}
for n, podName := range podNames {
nodeName := nodeNames[n]
var buf bytes.Buffer
exitCode, err := d.client.ExecInContainer(
podName, "virtlet", "kube-system", nil,
&buf, os.Stderr, []string{"virtlet", "--diag"})
var cur diag.Result
switch {
case err != nil:
cur = diag.Result{
Ext: "err",
Error: fmt.Sprintf("node %q: error getting version from Virtlet pod %q: %v", nodeName, podName, err),
}
case exitCode != 0:
cur = diag.Result{
Ext: "err",
Error: fmt.Sprintf("node %q: error getting version from Virtlet pod %q: exit code %d", nodeName, podName, exitCode),
}
default:
cur, err = diag.DecodeDiagnostics(buf.Bytes())
if err != nil {
cur = diag.Result{
Ext: "err",
Error: fmt.Sprintf("error unmarshalling the diagnostics: %v", err),
}
}
}
if cur.IsDir {
if sub, found := cur.Children["diagnostics"]; found && len(dr.Children) == 1 {
cur = sub
}
}
if cur.IsDir {
d.dumpLogs(&cur, podName, "virtlet")
d.dumpLogs(&cur, podName, "libvirt")
}
cur.Name = nodeName
dr.Children[nodeName] = cur
}
return dr, nil
}
func (d *diagDumpCommand) dumpLogs(dr *diag.Result, podName, containerName string) {
cur := diag.Result{
Name: "virtlet-pod-" + containerName,
Ext: "log",
}
logs, err := d.client.PodLogs(podName, containerName, "kube-system", maxVirtletPodLogLines)
if err != nil {
cur.Error = err.Error()
} else {
cur.Data = string(logs)
}
dr.Children[cur.Name] = cur
}
func (d *diagDumpCommand) Run() error {
dr, err := d.diagResult()
if err != nil {
return err
}
if d.useJSON {
d.out.Write(dr.ToJSON())
return nil
}
return dr.Unpack(d.outDir)
}
type diagUnpackCommand struct {
in io.Reader
outDir string
}
// NewDiagUnpackCommand returns a new cobra.Command that unpacks
// diagnostic information
func NewDiagUnpackCommand(in io.Reader) *cobra.Command {
d := &diagUnpackCommand{in: in}
return &cobra.Command{
Use: "unpack output_dir",
Short: "Unpack Virtlet diagnostics information",
Long: "Read Virtlet diagnostics information as JSON from stdin and unpacks into a directory tree",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("Must specify output directory")
}
d.outDir = args[0]
return d.Run()
},
}
}
func (d *diagUnpackCommand) Run() error {
data, err := ioutil.ReadAll(d.in)
if err != nil {
return err
}
dr, err := diag.DecodeDiagnostics(data)
if err != nil {
return err
}
return dr.Unpack(d.outDir)
}
type diagSonobuoyCommand struct {
in io.Reader
out io.Writer
tag string
}
// NewDiagSonobuoyCommand returns a new cobra.Command that adds
// Virtlet plugin to sonobuoy-generated yaml
func NewDiagSonobuoyCommand(in io.Reader, out io.Writer) *cobra.Command {
d := &diagSonobuoyCommand{in: in, out: out}
cmd := &cobra.Command{
Use: "sonobuoy",
Short: "Add Virtlet sonobuoy plugin to the sonobuoy output",
Long: "Find and patch sonobuoy configmap in the yaml that's read from stdin to include Virtlet sonobuoy plugin",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return errors.New("This command does not accept arguments")
}
return d.Run()
},
}
cmd.Flags().StringVar(&d.tag, "tag", version.Get().ImageTag, "Set virtlet image tag for the plugin")
return cmd
}
func (d *diagSonobuoyCommand) getYaml() ([]byte, error) {
bs, err := ioutil.ReadAll(d.in)
if err != nil {
return nil, err
}
objs, err := LoadYaml(bs)
if err != nil {
return nil, err
}
if len(objs) == 0 {
return nil, errors.New("source yaml is empty")
}
found := false
for _, o := range objs {
cfgMap, ok := o.(*v1.ConfigMap)
if !ok {
continue
}
if cfgMap.Name != sonobuoyPluginsConfigMapName {
continue
}
found = true
tagStr := ""
if d.tag != "" {
tagStr = ":" + d.tag
}
yaml := strings.Replace(sonobuoyPluginYaml, "$TAG", tagStr, -1)
cfgMap.Data[virtletSonobuoyPluginFileName] = yaml
}
if !found {
return nil, fmt.Errorf("ConfigMap not found: %q", sonobuoyPluginsConfigMapName)
}
return ToYaml(objs)
}
func (d *diagSonobuoyCommand) Run() error {
bs, err := d.getYaml()
if err != nil {
return err
}
if _, err := d.out.Write(bs); err != nil {
return err
}
return nil
}
// NewDiagCommand returns a new cobra.Command that handles Virtlet
// diagnostics.
func NewDiagCommand(client KubeClient, in io.Reader, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "diag",
Short: "Virtlet diagnostics",
Long: "Retrieve and unpack Virtlet diagnostics information",
}
cmd.AddCommand(NewDiagDumpCommand(client, out))
cmd.AddCommand(NewDiagUnpackCommand(in))
cmd.AddCommand(NewDiagSonobuoyCommand(in, out))
return cmd
}