/
queries.go
368 lines (313 loc) · 11.7 KB
/
queries.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 (
"context"
"os"
"path"
"time"
"github.com/vmware-tanzu/sonobuoy/pkg/config"
"github.com/vmware-tanzu/sonobuoy/pkg/dynamic"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
)
const (
// NSResourceLocation is the place under which namespaced API resources (pods, etc) are stored
NSResourceLocation = "resources/ns"
// ClusterResourceLocation is the place under which non-namespaced API resources (nodes, etc) are stored
ClusterResourceLocation = "resources/cluster"
// HostsLocation is the place under which host information (configz, healthz) is stored
HostsLocation = "hosts"
// MetaLocation is the place under which snapshot metadata (query times, config) is stored
MetaLocation = "meta"
// listVerb is the API verb we ensure resources respond to in order to try and call List()
listVerb = "list"
// secretResourceName is the value of the Name field on Secrets. We will implicitly filter those if the user
// tries to just query everything by not specifying a Resource list.
secretResourceName = "secrets"
)
type listQuery func() (*unstructured.UnstructuredList, error)
type objQuery func() (interface{}, error)
// timedListQuery performs a list query and serialize the results
func timedListQuery(outpath string, file string, f listQuery) (time.Duration, error) {
start := time.Now()
list, err := f()
duration := time.Since(start)
if err != nil {
return duration, err
}
if len(list.Items) > 0 {
err = errors.WithStack(SerializeObj(list.Items, outpath, file))
}
return duration, err
}
func timedObjectQuery(outpath string, file string, f objQuery) (time.Duration, error) {
start := time.Now()
obj, err := f()
duration := time.Since(start)
if err != nil {
return duration, err
}
return duration, errors.WithStack(SerializeObj(obj, outpath, file))
}
// timedQuery Wraps the execution of the function with a recorded timed snapshot
func timedQuery(recorder *QueryRecorder, name string, ns string, fn func() (time.Duration, error)) {
duration, fnErr := fn()
recorder.RecordQuery(name, ns, duration, fnErr)
}
func sliceContains(set []string, val string) bool {
for _, v := range set {
if v == val {
return true
}
}
return false
}
// given the filter options and a query against the given ns; what resources should we query? resourceNameList being empty means all. Only kept that for backwards compat.
func getResources(client *dynamic.APIHelper) (map[schema.GroupVersion][]metav1.APIResource, error) {
resourceMap, err := client.DiscoveryClient.ServerPreferredResources()
if err != nil {
return nil, err
}
// Some resources are ambiguously set in two or more groups. As kubectl
// does, we should just prefer the first one returned by discovery.
resourcesSeen := map[string]struct{}{}
versionResourceMap := map[schema.GroupVersion][]metav1.APIResource{}
for _, apiResourceList := range resourceMap {
version, err := schema.ParseGroupVersion(apiResourceList.GroupVersion)
if err != nil {
return nil, errors.Wrap(err, "parsing schema")
}
resources := []metav1.APIResource{}
for _, apiResource := range apiResourceList.APIResources {
// If we've seen the resource already, skip it.
if _, ok := resourcesSeen[apiResource.Name]; ok {
continue
}
resources = append(resources, apiResource)
resourcesSeen[apiResource.Name] = struct{}{}
continue
}
versionResourceMap[version] = resources
}
return versionResourceMap, nil
}
// QueryResources will query all the intended resources. If given a non-nil namespace
// it queries only namespaced objects; non-namespaced otherwise. Writing them out to
// <resultsdir>/resources/ns/<ns>/*.json or <resultsdir>/resources/cluster/*.json.
func QueryResources(
client *dynamic.APIHelper,
recorder *QueryRecorder,
resources []schema.GroupVersionResource,
ns *string,
cfg *config.Config) error {
// Early exit; avoid forming query or creating output directories.
if len(resources) == 0 {
return nil
}
if ns != nil {
logrus.Infof("Running ns query (%v)", *ns)
} else {
logrus.Info("Running cluster queries")
}
// 1. Create the parent directory we will use to store the results
outdir := path.Join(cfg.OutputDir(), ClusterResourceLocation)
if ns != nil {
outdir = path.Join(cfg.OutputDir(), NSResourceLocation, *ns)
}
if err := os.MkdirAll(outdir, 0755); err != nil {
return errors.WithStack(err)
}
// 2. Setup label filter if there is one.
opts := metav1.ListOptions{}
if len(cfg.Filters.LabelSelector) > 0 {
if _, err := labels.Parse(cfg.Filters.LabelSelector); err != nil {
logrus.Warningf("Labelselector %v failed to parse with error %v", cfg.Filters.LabelSelector, err)
} else {
opts.LabelSelector = cfg.Filters.LabelSelector
}
}
if ns != nil && len(*ns) > 0 {
opts.FieldSelector = "metadata.namespace=" + *ns
}
// 3. Execute the query
for _, gvr := range resources {
lister := func() (*unstructured.UnstructuredList, error) {
resourceClient := client.Client.Resource(gvr)
resources, err := resourceClient.List(context.TODO(), opts)
return resources, errors.Wrapf(err, "listing resource %v", gvr)
}
// The core group is just the empty string but for clarity and consistency, refer to it as core.
groupText := gvr.Group
if groupText == "" {
groupText = "core"
}
query := func() (time.Duration, error) {
return timedListQuery(
outdir+"/",
groupText+"_"+gvr.Version+"_"+gvr.Resource+".json",
lister,
)
}
// Get the pretty-print namespace and avoid dereference issues.
nsVal := ""
if ns != nil {
nsVal = *ns
}
timedQuery(recorder, gvr.Resource, nsVal, query)
}
return nil
}
// getAllFilteredResources figure out which resources we want to query for based on the filter list and whether
// or not we are considering namespaced objects or not.
func getAllFilteredResources(client *dynamic.APIHelper, wantResources []string) (clusterResources, nsResources []schema.GroupVersionResource, retErr error) {
groupResources, err := getResources(client)
if err != nil {
return nil, nil, errors.Wrap(err, "choosing resources to gather")
}
return filterResources(groupResources, false, wantResources),
filterResources(groupResources, true, wantResources),
nil
}
func filterResources(gvrs map[schema.GroupVersion][]metav1.APIResource, namespaced bool, wantResources []string) []schema.GroupVersionResource {
results := []schema.GroupVersionResource{}
for gv, resources := range gvrs {
for _, res := range resources {
// Get either namespaced or non-namespaced resources.
if namespaced != res.Namespaced {
continue
}
// Double check the resources are listable.
listable := false
for _, v := range res.Verbs {
if v == listVerb {
listable = true
break
}
}
if !listable {
continue
}
// Filter for explicit values
if wantResources != nil {
if !sliceContains(wantResources, res.Name) {
logrus.Infof("%v not specified in non-nil Resources. Skipping %v query.", res.Name, res.Name)
continue
}
} else {
// Filter out secrets by default to avoid accidental exposure.
if res.Name == secretResourceName {
logrus.Infof("Resources is not set explicitly implying query all resources, but skipping %v for safety. Specify the value explicitly in Resources to gather this data.", res.Name)
continue
}
}
results = append(results, gv.WithResource(res.Name))
}
}
return results
}
// QueryPodLogs gets the pod logs for each pod in the given namespace.
// If namespace is not provided, get pod logs using field selectors.
// VisitedPods will eliminate duplicate pods when execute overlapping queries,
// e.g. query by namespaces and query by fieldSelectors.
func QueryPodLogs(kubeClient kubernetes.Interface, recorder *QueryRecorder, ns string, cfg *config.Config,
visitedPods map[string]struct{}) error {
start := time.Now()
opts := metav1.ListOptions{}
if len(cfg.Limits.PodLogs.LabelSelector) > 0 {
if _, err := labels.Parse(cfg.Limits.PodLogs.LabelSelector); err != nil {
logrus.Warningf("Labelselector %v failed to parse with error %v", cfg.Limits.PodLogs.LabelSelector, err)
} else {
opts.LabelSelector = cfg.Limits.PodLogs.LabelSelector
}
}
if len(ns) > 0 {
logrus.Infof("Collecting Pod Logs by namespace (%v)", ns)
err := gatherPodLogs(kubeClient, ns, opts, cfg, visitedPods)
if err != nil {
return err
}
} else {
logrus.Infoln("Collecting Pod Logs by FieldSelectors", cfg.Limits.PodLogs.FieldSelectors)
for _, fieldSelector := range cfg.Limits.PodLogs.FieldSelectors {
opts.FieldSelector = fieldSelector
err := gatherPodLogs(kubeClient, ns, opts, cfg, visitedPods)
if err != nil {
return err
}
}
}
duration := time.Since(start)
recorder.RecordQuery("PodLogs", ns, duration, nil)
return nil
}
// QueryHostData gets the host data and records it.
func QueryHostData(kubeClient kubernetes.Interface, recorder *QueryRecorder, cfg *config.Config) error {
if cfg.Resources != nil && !sliceContains(cfg.Resources, "nodes") {
logrus.Info("nodes not specified in non-nil Resources. Skipping host data gathering.")
return nil
}
start := time.Now()
// TODO(chuckha) look at FieldSelector for list options{}
nodeList, err := kubeClient.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return errors.Wrap(err, "failed to get node list")
}
nodeNames := make([]string, len(nodeList.Items))
for i, node := range nodeList.Items {
nodeNames[i] = node.Name
}
err = gatherNodeData(nodeNames, kubeClient.CoreV1().RESTClient(), cfg)
duration := time.Since(start)
recorder.RecordQuery("Nodes", "", duration, err)
return nil
}
// QueryServerData gets the server version and server group data and records it.
func QueryServerData(kubeClient kubernetes.Interface, recorder *QueryRecorder, cfg *config.Config) error {
if err := queryServerVersion(kubeClient, recorder, cfg); err != nil {
return err
}
if err := queryServerGroups(kubeClient, recorder, cfg); err != nil {
return err
}
return nil
}
func queryServerVersion(kubeClient kubernetes.Interface, recorder *QueryRecorder, cfg *config.Config) error {
if cfg.Resources != nil && !sliceContains(cfg.Resources, "serverversion") {
logrus.Info("serverversion not specified in non-nil Resources. Skipping serverversion query.")
return nil
}
objqry := func() (interface{}, error) { return kubeClient.Discovery().ServerVersion() }
query := func() (time.Duration, error) {
return timedObjectQuery(cfg.OutputDir(), "serverversion.json", objqry)
}
timedQuery(recorder, "serverversion", "", query)
return nil
}
func queryServerGroups(kubeClient kubernetes.Interface, recorder *QueryRecorder, cfg *config.Config) error {
if cfg.Resources != nil && !sliceContains(cfg.Resources, "servergroups") {
logrus.Info("servergroups not specified in non-nil Resources. Skipping servergroups query.")
return nil
}
objqry := func() (interface{}, error) { return kubeClient.Discovery().ServerGroups() }
query := func() (time.Duration, error) {
return timedObjectQuery(cfg.OutputDir(), "servergroups.json", objqry)
}
timedQuery(recorder, "servergroups", "", query)
return nil
}