-
Notifications
You must be signed in to change notification settings - Fork 341
/
discovery.go
368 lines (311 loc) · 11 KB
/
discovery.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
/*
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 discovery
import (
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"time"
"github.com/heptio/sonobuoy/pkg/client/results"
"github.com/heptio/sonobuoy/pkg/config"
"github.com/heptio/sonobuoy/pkg/dynamic"
"github.com/heptio/sonobuoy/pkg/errlog"
"github.com/heptio/sonobuoy/pkg/plugin"
pluginaggregation "github.com/heptio/sonobuoy/pkg/plugin/aggregation"
"github.com/pkg/errors"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
"github.com/viniciuschiele/tarx"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
const (
pluginDefinitionFilename = "defintion.json"
)
// Run is the main entrypoint for discovery.
func Run(restConf *rest.Config, cfg *config.Config) (errCount int) {
// Adjust QPS/Burst so that the queries execute as quickly as possible.
restConf.QPS = float32(cfg.QPS)
restConf.Burst = cfg.Burst
apiHelper, err := dynamic.NewAPIHelperFromRESTConfig(restConf)
if err != nil {
errlog.LogError(err)
return errCount + 1
}
kubeClient, err := kubernetes.NewForConfig(restConf)
if err != nil {
errlog.LogError(errors.Wrap(err, "could not create kubernetes client"))
return errCount + 1
}
t := time.Now()
// 1. Create the directory which will store the results, including the
// `meta` directory inside it (which we always need regardless of
// config)
outpath := path.Join(cfg.ResultsDir, cfg.UUID)
metapath := path.Join(outpath, MetaLocation)
err = os.MkdirAll(metapath, 0755)
if err != nil {
errlog.LogError(errors.Wrap(err, "could not create directory to store results"))
return errCount + 1
}
// Write logs to the configured results location. All log levels
// should write to the same log file
pathmap := make(lfshook.PathMap)
logfile := path.Join(metapath, "run.log")
for _, level := range logrus.AllLevels {
pathmap[level] = logfile
}
hook := lfshook.NewHook(pathmap, &logrus.JSONFormatter{})
logrus.AddHook(hook)
// Unset all hooks as we exit the Run function
defer func() {
logrus.StandardLogger().Hooks = make(logrus.LevelHooks)
}()
// closure used to collect and report errors.
trackErrorsFor := func(action string) func(error) {
return func(err error) {
if err != nil {
errCount++
errlog.LogError(errors.Wrapf(err, "error %v", action))
}
}
}
// Set initial annotation stating the pod is running. Ensures the annotation
// exists sooner for user/polling consumption and prevents issues were we try
// to patch a non-existant status later.
trackErrorsFor("setting initial pod status")(
setStatus(kubeClient, cfg.Namespace,
&pluginaggregation.Status{
Status: pluginaggregation.RunningStatus,
}),
)
// 2. Get the list of namespaces and apply the regex filter on the namespace
logrus.Infof("Filtering namespaces based on the following regex:%s", cfg.Filters.Namespaces)
nslist, err := FilterNamespaces(kubeClient, cfg.Filters.Namespaces)
if err != nil {
errlog.LogError(errors.Wrap(err, "could not filter namespaces"))
return errCount + 1
}
// 3. Dump the config.json we used to run our test
if blob, err := json.Marshal(cfg); err == nil {
if err = ioutil.WriteFile(path.Join(metapath, "config.json"), blob, 0644); err != nil {
errlog.LogError(errors.Wrap(err, "could not write config.json file"))
return errCount + 1
}
}
// 4. Run the plugin aggregator
trackErrorsFor("running plugins")(
pluginaggregation.Run(kubeClient, cfg.LoadedPlugins, cfg.Aggregation, cfg.Namespace, outpath),
)
// 5. Run the queries
recorder := NewQueryRecorder()
clusterResources, nsResources, err := getAllFilteredResources(apiHelper, cfg.Resources)
trackErrorsFor("querying cluster resources")(
QueryHostData(kubeClient, recorder, cfg),
)
trackErrorsFor("querying cluster resources")(
QueryResources(apiHelper, recorder, clusterResources, nil, cfg),
)
trackErrorsFor("querying server info")(
QueryServerData(kubeClient, recorder, cfg),
)
for _, ns := range nslist {
trackErrorsFor("querying resources under namespace " + ns)(
QueryResources(apiHelper, recorder, nsResources, &ns, cfg),
)
}
// query pod logs
if cfg.Resources == nil || sliceContains(cfg.Resources, "podlogs") {
// Eliminate duplicate pods when query by namespaces and query by fieldSelectors
visitedPods := make(map[string]struct{})
nsFilter := getPodLogNamespaceFilter(cfg)
if len(nsFilter) > 0 {
nsListLogs, _ := FilterNamespaces(kubeClient, nsFilter)
for _, ns := range nsListLogs {
trackErrorsFor("querying pod logs under namespace " + ns)(
QueryPodLogs(kubeClient, recorder, ns, cfg, visitedPods),
)
}
}
trackErrorsFor("querying pod logs by field selectors")(
QueryPodLogs(kubeClient, recorder, "", cfg, visitedPods),
)
} else {
logrus.Infof("podlogs not specified in non-nil Resources, skipping getting podlogs")
}
// 6. Dump the query times
trackErrorsFor("recording query times")(
recorder.DumpQueryData(path.Join(metapath, "query-time.json")),
)
// 7. Clean up after the plugins
pluginaggregation.Cleanup(kubeClient, cfg.LoadedPlugins)
// Postprocessing before we create the tarball.
for _, p := range cfg.LoadedPlugins {
item, err := results.PostProcessPlugin(p, outpath)
if err != nil {
logrus.Errorf("Error processing plugin %v: %v", p.GetName(), err)
}
if !item.Empty() {
if err := results.SaveProcessedResults(p.GetName(), outpath, item); err != nil {
logrus.Errorf("Unable to save results for plugin %v: %v", p.GetName(), err)
}
// Update the plugin status with this post-processed information.
statusInfo := map[string]int{}
statusCounts(&item, statusInfo)
updatePluginStatus(kubeClient, cfg.Namespace, p.GetName(), item.Status, statusInfo)
}
}
// Saving plugin definitions in their respective folders for easy reference.
for _, p := range cfg.LoadedPlugins {
trackErrorsFor("saving plugin info")(
dumpPlugin(p, outpath),
)
}
// 8. tarball up results YYYYMMDDHHMM_sonobuoy_UID.tar.gz
tb := cfg.ResultsDir + "/" + t.Format("200601021504") + "_sonobuoy_" + cfg.UUID + ".tar.gz"
err = tarx.Compress(tb, outpath, &tarx.CompressOptions{Compression: tarx.Gzip})
if err == nil {
defer os.RemoveAll(outpath)
}
trackErrorsFor("assembling results tarball")(err)
tarInfo, err := getFileInfo(tb)
trackErrorsFor("recording tarball info")(err)
// 9. Mark final annotation stating the results are available and status is completed.
trackErrorsFor("updating pod status")(
updateStatus(
kubeClient,
cfg.Namespace,
pluginaggregation.CompleteStatus,
&tarInfo,
),
)
logrus.Infof("Results available at %v", tb)
return errCount
}
func statusCounts(item *results.Item, startingCounts map[string]int) {
if item == nil {
return
}
if len(item.Items) > 0 {
for _, v := range item.Items {
statusCounts(&v, startingCounts)
}
return
}
startingCounts[item.Status]++
return
}
func getFileInfo(path string) (pluginaggregation.TarInfo, error) {
fi, err := os.Stat(path)
if err != nil {
return pluginaggregation.TarInfo{}, err
}
f, err := os.Open(path)
if err != nil {
return pluginaggregation.TarInfo{}, err
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return pluginaggregation.TarInfo{}, err
}
return pluginaggregation.TarInfo{
Name: filepath.Base(path),
Size: fi.Size(),
SHA256: fmt.Sprintf("%x", h.Sum(nil)),
CreatedAt: time.Now(),
}, nil
}
// dumpPlugin will marshal the plugin to the appropriate location in the outputDir:
// plugins/<name>/definition.json. This makes the data more clear for any consumer
// looking at the tarball about what was.
func dumpPlugin(p plugin.Interface, outputDir string) error {
b, err := json.Marshal(p)
if err != nil {
return errors.Wrapf(err, "encoding plugin %v definition to yaml", p.GetName())
}
err = ioutil.WriteFile(
filepath.Join(outputDir, results.PluginsDir, p.GetName(), pluginDefinitionFilename),
b,
os.FileMode(0644),
)
return errors.Wrapf(err, "writing plugin %v definition to yaml", p.GetName())
}
// Targeted namespaces will be specified by cfg.Limits.PodLogs.Namespaces OR cfg.Limits.PodLogs.SonobuoyNamespace.
func getPodLogNamespaceFilter(cfg *config.Config) string {
nsfilter := cfg.Limits.PodLogs.Namespaces
if cfg.Limits.PodLogs.SonobuoyNamespace != nil && *cfg.Limits.PodLogs.SonobuoyNamespace {
if len(nsfilter) > 0 {
nsfilter = fmt.Sprintf("%s|%s", nsfilter, cfg.Namespace)
} else {
nsfilter = cfg.Namespace
}
}
return nsfilter
}
// updateStatus changes the summary status of the sonobuoy pod in order to
// effect the finalized status the user sees. This does not change the
// status of individual plugins.
func updateStatus(client kubernetes.Interface, namespace string, status string, tarInfo *pluginaggregation.TarInfo) error {
podStatus, err := pluginaggregation.GetStatus(client, namespace)
if err != nil {
return errors.Wrap(err, "failed to get the existing status")
}
// Update status
podStatus.Status = status
if tarInfo != nil {
podStatus.Tarball = *tarInfo
}
return setStatus(client, namespace, podStatus)
}
func updatePluginStatus(client kubernetes.Interface, namespace string, pluginType string, pluginResultStatus string, pluginResultCounts map[string]int) error {
podStatus, err := pluginaggregation.GetStatus(client, namespace)
if err != nil {
return errors.Wrap(err, "failed to get the existing status")
}
for i := range podStatus.Plugins {
if podStatus.Plugins[i].Plugin == pluginType {
podStatus.Plugins[i].ResultStatus = pluginResultStatus
podStatus.Plugins[i].ResultStatusCounts = pluginResultCounts
break
}
}
return setStatus(client, namespace, podStatus)
}
// setStatus sets the status on the pod via an annotation. It will overwrite the
// existing status.
func setStatus(client kubernetes.Interface, namespace string, status *pluginaggregation.Status) error {
// Marshal back into json, inject into the patch, then serialize again.
statusBytes, err := json.Marshal(status)
if err != nil {
return errors.Wrap(err, "failed to marshal the status")
}
patch := pluginaggregation.GetPatch(string(statusBytes))
patchBytes, err := json.Marshal(patch)
if err != nil {
return errors.Wrap(err, "failed to marshal the patch")
}
// Determine sonobuoy pod name
podName, err := pluginaggregation.GetAggregatorPodName(client, namespace)
if err != nil {
return errors.Wrap(err, "failed to get the name of the aggregator pod to set the status on")
}
_, err = client.CoreV1().Pods(namespace).Patch(podName, types.MergePatchType, patchBytes)
return err
}