-
Notifications
You must be signed in to change notification settings - Fork 341
/
reader.go
408 lines (352 loc) · 11.9 KB
/
reader.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
Copyright 2018 Heptio Inc.
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 results
import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
"github.com/vmware-tanzu/sonobuoy/pkg/config"
goversion "github.com/hashicorp/go-version"
"github.com/pkg/errors"
)
const (
// PluginsDir defines where in the archive directories for plugin results are.
PluginsDir = "plugins/"
// ResultsDir defines where in the archive the plugin results are.
// Example: plugins/<name>/results
ResultsDir = "results/"
// ErrorsDir defines where in the archive the errors running the plugin get reported.
// These are the Sonobuoy reported errors, e.g. failure to start a plugin, timeout, etc.
// This is not the appropriate directory for things like test failures.
// Example: plugins/<name>/errors
ErrorsDir = "errors/"
// DefaultErrFile is the file name used when Sonobuoy is reporting an error running a plugin.
// Is written into the ErrorsDir directory.
DefaultErrFile = "error.json"
namespacedResourcesDir = "resources/ns/"
nonNamespacedResourcesDir = "resources/cluster/"
metadataDir = "meta/"
defaultNodesFile = "Nodes.json"
defaultServerVersionFile = "serverversion.json"
defaultServerGroupsFile = "servergroups.json"
// InfoFile contains data not that isn't strictly in another location
// but still relevent to post-processing or understanding the run in some way.
InfoFile = "info.json"
//Filename of the cluster health, relative to metadataDir
ClusterHealthFile = "clusterhealth.json"
)
// Versions corresponding to Kubernetes minor version values. We used to
// roughly version our results tarballs in sync with minor version patches
// and so checking the server version for one of these prefixes would be
// sufficient to inform the parser where certain files would be.
const (
// UnknownVersion lets the consumer know if this client can detect the archive version or not.
UnknownVersion = "v?.?"
VersionEight = "v0.8"
VersionNine = "v0.9"
VersionTen = "v0.10"
VersionFifteen = "v0.15"
)
var (
// v15 is the first version we started used a typed version. Allows more clean comparisons
// between versions.
v15 = goversion.Must(goversion.NewVersion("v0.15.0"))
// errStopWalk is a special cased error when using reader.Walk which will stop
// processing but will not be bubbled up. Used to prevent reading until EOF when you
// want to stop mid-reader.
errStopWalk = errors.New("stop")
)
// Reader holds a reader and a version. It uses the version to know where to
// find files within the archive.
type Reader struct {
io.Reader
Version string
}
// NewReaderWithVersion creates a results.Reader that interprets a results
// archive of the version passed in.
// Useful if the reader can be read only once and if the version of the data to
// read is known.
func NewReaderWithVersion(reader io.Reader, version string) *Reader {
return &Reader{
Reader: reader,
Version: version,
}
}
// NewReaderFromBytes is a helper constructor that will discover the version of the archive
// and return a new Reader with the correct version already populated.
func NewReaderFromBytes(data []byte) (*Reader, error) {
r := bytes.NewReader(data)
gzipReader, err := gzip.NewReader(r)
if err != nil {
return nil, errors.Wrap(err, "error creating new gzip reader")
}
version, err := DiscoverVersion(gzipReader)
if err != nil {
return nil, errors.Wrap(err, "error discovering version")
}
if _, err = r.Seek(0, io.SeekStart); err != nil {
return nil, errors.Wrap(err, "error seeking to start")
}
if err = gzipReader.Reset(r); err != nil {
return nil, errors.Wrap(err, "error reseting gzip reader")
}
return &Reader{
Reader: gzipReader,
Version: version,
}, nil
}
// DiscoverVersion takes a Sonobuoy archive stream and extracts just the
// version of the archive.
func DiscoverVersion(reader io.Reader) (string, error) {
r := &Reader{
Reader: reader,
}
conf := &config.Config{}
err := r.WalkFiles(func(path string, info os.FileInfo, err error) error {
return ExtractConfig(path, info, conf)
})
if err != nil {
return "", errors.Wrap(err, "error extracting config")
}
parsedVersion, err := goversion.NewVersion(conf.Version)
if err != nil {
return "", errors.Wrap(err, "parsing version")
}
segments := parsedVersion.Segments()
if len(segments) < 2 {
return "", fmt.Errorf("version %q only has %d segments, need at least 2", conf.Version, len(segments))
}
// Get rid of any of the extra version information that doesn't affect archive layout.
// Example: v0.10.0-a2b3d4
var version string
switch {
case strings.HasPrefix(conf.Version, VersionEight):
version = VersionEight
case strings.HasPrefix(conf.Version, VersionNine):
version = VersionNine
case strings.HasPrefix(conf.Version, VersionTen):
version = VersionTen
case parsedVersion.LessThan(v15):
version = VersionTen
default:
version = VersionFifteen
}
return version, nil
}
// tarFileInfo implements os.FileInfo and extends the Sys() method to
// return a reader to a file in a tar archive.
type tarFileInfo struct {
os.FileInfo
io.Reader
}
// Sys is going to be an io.Reader to a file in a tar archive.
// This is how data is extracted from the archive.
func (t *tarFileInfo) Sys() interface{} {
return t.Reader
}
// WalkFiles walks all of the files in the archive. Processing stops at the
// first error. The error is returned except in the special case of errStopWalk
// which will stop processing but nil will be returned.
func (r *Reader) WalkFiles(walkfn filepath.WalkFunc) error {
tr := tar.NewReader(r)
var err error
var header *tar.Header
for {
if err != nil {
break
}
header, err = tr.Next()
if err == io.EOF {
break
}
if err != nil {
return errors.Wrap(err, "error getting next file in archive")
}
info := &tarFileInfo{
header.FileInfo(),
tr,
}
err = walkfn(path.Clean(header.Name), info, err)
}
if err == errStopWalk || err == io.EOF {
return nil
}
return err
}
// Functions to be used within a walkfn.
// ExtractBytes pulls out bytes into a buffer for any path matching file.
func ExtractBytes(file string, path string, info os.FileInfo, buf *bytes.Buffer) error {
if file == path {
reader, ok := info.Sys().(io.Reader)
if !ok {
return errors.New("info.Sys() is not a reader")
}
_, err := buf.ReadFrom(reader)
if err != nil {
return errors.Wrap(err, "could not read from buffer")
}
}
return nil
}
// ExtractIntoStruct takes a predicate function and some file information
// and decodes the contents of the file that matches the predicate into the
// interface passed in (generally a pointer to a struct/slice).
func ExtractIntoStruct(predicate func(string) bool, path string, info os.FileInfo, object interface{}) error {
if predicate(path) {
reader, ok := info.Sys().(io.Reader)
if !ok {
return errors.New("info.Sys() is not a reader")
}
// TODO(chuckha) Perhaps find a more robust way to handle different data formats.
if strings.HasSuffix(path, "xml") {
decoder := xml.NewDecoder(reader)
if err := decoder.Decode(object); err != nil {
return errors.Wrap(err, "error decoding xml into object")
}
return nil
}
// If it's not xml it's probably json
decoder := json.NewDecoder(reader)
if err := decoder.Decode(object); err != nil {
return errors.Wrap(err, "error decoding json into object")
}
}
return nil
}
// ExtractFileIntoStruct is a helper for a common use case of extracting
// the contents of one file into the object.
func ExtractFileIntoStruct(file, path string, info os.FileInfo, object interface{}) error {
return ExtractIntoStruct(func(p string) bool {
return file == p
}, path, info, object)
}
// ExtractConfig populates the config object regardless of version.
func ExtractConfig(path string, info os.FileInfo, conf *config.Config) error {
return ExtractIntoStruct(func(file string) bool {
return path == ConfigFile(VersionTen) || path == ConfigFile(VersionEight)
}, path, info, conf)
}
// Functions for helping with backwards compatibility
// Metadata is the location of the metadata directory in the results archive.
func (r *Reader) Metadata() string {
return metadataDir
}
// ServerVersionFile is the location of the file that contains the Kubernetes
// version Sonobuoy ran on.
func (r *Reader) ServerVersionFile() string {
switch r.Version {
case VersionEight:
return path.Join("serverversion", "serverversion.json")
default:
return defaultServerVersionFile
}
}
// NamespacedResources returns the path to the directory that contains
// information about namespaced Kubernetes resources.
func (r *Reader) NamespacedResources() string {
return namespacedResourcesDir
}
// NonNamespacedResources returns the path to the non-namespaced directory.
func (r *Reader) NonNamespacedResources() string {
switch r.Version {
case VersionEight:
return path.Join("resources", "non-ns")
default:
return nonNamespacedResourcesDir
}
}
// NodesFile returns the path to the file that lists the nodes of the Kubernetes
// cluster.
func (r *Reader) NodesFile() string {
return path.Join(r.NonNamespacedResources(), defaultNodesFile)
}
// ServerGroupsFile returns the path to the groups the Kubernetes API supported at the time of the run.
func (r *Reader) ServerGroupsFile() string {
return defaultServerGroupsFile
}
// ConfigFile returns the path to the sonobuoy config file.
// This is not a method as it is used to determine the version of the archive.
func ConfigFile(version string) string {
switch version {
case VersionEight:
return "config.json"
default:
return path.Join("meta", "config.json")
}
}
// RunInfoFile returns the path to the Sonobuoy RunInfo file which is extra metadata about the run.
// This was added in v0.16.1. The function will return the same string even for earlier
// versions where that file does not exist.
func (r *Reader) RunInfoFile() string {
return path.Join(metadataDir, InfoFile)
}
// PluginResultsItem returns the results file from the given plugin if found, error otherwise.
func (r *Reader) PluginResultsItem(plugin string) (*Item, error) {
resultObj := &Item{}
reader, err := r.PluginResultsReader(plugin)
if err != nil {
return nil, err
}
decoder := yaml.NewDecoder(reader)
err = decoder.Decode(resultObj)
if err != nil {
return nil, errors.Wrapf(err, "failed to decode yaml results for plugin %v", plugin)
}
return resultObj, nil
}
// PluginResultsReader returns the results file from the given plugin if found, error otherwise.
func (r *Reader) PluginResultsReader(plugin string) (io.Reader, error) {
resultsPath := path.Join(PluginsDir, plugin, PostProcessedResultsFile)
return r.FileReader(resultsPath)
}
// FileReader returns a reader for a file in the archive.
func (r *Reader) FileReader(filename string) (io.Reader, error) {
var returnReader io.Reader
found := false
err := r.WalkFiles(
func(path string, info os.FileInfo, err error) error {
if err != nil || found {
return err
}
if path == filename {
found = true
reader, ok := info.Sys().(io.Reader)
if !ok {
return errors.New("info.Sys() is not a reader")
}
returnReader = reader
return errStopWalk
}
return nil
})
if err != nil {
return nil, errors.Wrapf(err, "failed to walk archive for file %v", filename)
}
if !found {
return nil, fmt.Errorf("failed to find file %q in archive", filename)
}
return returnReader, nil
}
// Return the full path of the ClusterHealthFile
func ClusterHealthFilePath() string {
return path.Join(metadataDir, ClusterHealthFile)
}