-
Notifications
You must be signed in to change notification settings - Fork 341
/
queries.go
385 lines (354 loc) · 13.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
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 (
"os"
"path"
"time"
"github.com/heptio/sonobuoy/pkg/config"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/conversion"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
)
// ObjQuery is a query function that returns a kubernetes object
type ObjQuery func() (runtime.Object, error)
// UntypedQuery is a query function that return an untyped array of objs
type UntypedQuery func() (interface{}, error)
// UntypedListQuery is a query function that return an untyped array of objs
type UntypedListQuery func() ([]interface{}, error)
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"
)
// objListQuery performs a list query and serialize the results
func objListQuery(outpath string, file string, f ObjQuery) (time.Duration, error) {
start := time.Now()
listObj, err := f()
duration := time.Since(start)
if err != nil {
return duration, err
}
if listObj == nil {
return duration, errors.Errorf("got invalid response from API server")
}
listPtr, err := meta.GetItemsPtr(listObj)
if err != nil {
return duration, errors.WithStack(err)
}
items, err := conversion.EnforcePtr(listPtr)
if err != nil {
return duration, errors.WithStack(err)
}
if items.Len() > 0 {
err = errors.WithStack(SerializeObj(listPtr, outpath, file))
}
return duration, err
}
// untypedQuery performs a untyped query and serialize the results
func untypedQuery(outpath string, file string, f UntypedQuery) (time.Duration, error) {
start := time.Now()
Obj, err := f()
duration := time.Since(start)
if err == nil && Obj != nil {
err = SerializeObj(Obj, outpath, file)
}
return duration, err
}
// untypedListQuery performs a untyped list query and serialize the results
func untypedListQuery(outpath string, file string, f UntypedListQuery) (time.Duration, error) {
start := time.Now()
listObj, err := f()
duration := time.Since(start)
if err == nil && listObj != nil {
err = SerializeArrayObj(listObj, outpath, file)
}
return duration, err
}
// 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)
}
// queryNsResource performs the appropriate namespace-scoped query according to its input args
func queryNsResource(ns string, resourceKind string, opts metav1.ListOptions, kubeClient kubernetes.Interface) (runtime.Object, error) {
switch resourceKind {
case "ConfigMaps":
return kubeClient.CoreV1().ConfigMaps(ns).List(opts)
case "ControllerRevisions":
lst, err := kubeClient.AppsV1().ControllerRevisions(ns).List(opts)
if apierrors.IsNotFound(err) {
lst, err := kubeClient.AppsV1beta2().ControllerRevisions(ns).List(opts)
if apierrors.IsNotFound(err) {
return kubeClient.AppsV1beta1().ControllerRevisions(ns).List(opts)
}
return lst, err
}
return lst, err
case "CronJobs":
lst, err := kubeClient.BatchV1beta1().CronJobs(ns).List(opts)
if apierrors.IsNotFound(err) {
return kubeClient.BatchV2alpha1().CronJobs(ns).List(opts)
}
return lst, err
case "DaemonSets":
lst, err := kubeClient.AppsV1().DaemonSets(ns).List(opts)
if apierrors.IsNotFound(err) {
lst, err := kubeClient.AppsV1beta2().DaemonSets(ns).List(opts)
if apierrors.IsNotFound(err) {
return kubeClient.ExtensionsV1beta1().DaemonSets(ns).List(opts)
}
return lst, err
}
return lst, err
case "Deployments":
lst, err := kubeClient.AppsV1().Deployments(ns).List(opts)
if apierrors.IsNotFound(err) {
lst, err := kubeClient.AppsV1beta2().Deployments(ns).List(opts)
if apierrors.IsNotFound(err) {
return kubeClient.AppsV1beta1().Deployments(ns).List(opts)
}
return lst, err
}
return lst, err
case "Endpoints":
return kubeClient.CoreV1().Endpoints(ns).List(opts)
case "Events":
return kubeClient.CoreV1().Events(ns).List(opts)
case "HorizontalPodAutoscalers":
return kubeClient.AutoscalingV1().HorizontalPodAutoscalers(ns).List(opts)
case "Ingresses":
return kubeClient.ExtensionsV1beta1().Ingresses(ns).List(opts)
case "Jobs":
return kubeClient.BatchV1().Jobs(ns).List(opts)
case "LimitRanges":
return kubeClient.CoreV1().LimitRanges(ns).List(opts)
case "NetworkPolicies":
return kubeClient.NetworkingV1().NetworkPolicies(ns).List(opts)
case "PersistentVolumeClaims":
return kubeClient.CoreV1().PersistentVolumeClaims(ns).List(opts)
case "Pods":
return kubeClient.CoreV1().Pods(ns).List(opts)
case "PodDisruptionBudgets":
return kubeClient.PolicyV1beta1().PodDisruptionBudgets(ns).List(opts)
case "PodPresets":
return kubeClient.SettingsV1alpha1().PodPresets(ns).List(opts)
case "PodTemplates":
return kubeClient.CoreV1().PodTemplates(ns).List(opts)
case "ReplicaSets":
lst, err := kubeClient.AppsV1().ReplicaSets(ns).List(opts)
if apierrors.IsNotFound(err) {
lst, err := kubeClient.AppsV1beta2().ReplicaSets(ns).List(opts)
if apierrors.IsNotFound(err) {
return kubeClient.ExtensionsV1beta1().ReplicaSets(ns).List(opts)
}
return lst, err
}
return lst, err
case "ReplicationControllers":
return kubeClient.CoreV1().ReplicationControllers(ns).List(opts)
case "ResourceQuotas":
return kubeClient.CoreV1().ResourceQuotas(ns).List(opts)
case "RoleBindings":
lst, err := kubeClient.RbacV1().RoleBindings(ns).List(opts)
if apierrors.IsNotFound(err) {
lst, err := kubeClient.RbacV1beta1().RoleBindings(ns).List(opts)
if apierrors.IsNotFound(err) {
return kubeClient.RbacV1alpha1().RoleBindings(ns).List(opts)
}
return lst, err
}
return lst, err
case "Roles":
lst, err := kubeClient.RbacV1().Roles(ns).List(opts)
if apierrors.IsNotFound(err) {
lst, err := kubeClient.RbacV1beta1().Roles(ns).List(opts)
if apierrors.IsNotFound(err) {
return kubeClient.RbacV1alpha1().Roles(ns).List(opts)
}
return lst, err
}
return lst, err
case "Secrets":
return kubeClient.CoreV1().Secrets(ns).List(opts)
case "ServiceAccounts":
return kubeClient.CoreV1().ServiceAccounts(ns).List(opts)
case "Services":
return kubeClient.CoreV1().Services(ns).List(opts)
case "StatefulSets":
lst, err := kubeClient.AppsV1().StatefulSets(ns).List(opts)
if apierrors.IsNotFound(err) {
lst, err := kubeClient.AppsV1beta2().StatefulSets(ns).List(opts)
if apierrors.IsNotFound(err) {
return kubeClient.AppsV1beta1().StatefulSets(ns).List(opts)
}
return lst, err
}
return lst, err
default:
return nil, errors.Errorf("Unsupported resource %v", resourceKind)
}
}
// queryNonNsResource performs the appropriate non-namespace-scoped query according to its input args
func queryNonNsResource(resourceKind string, kubeClient kubernetes.Interface) (runtime.Object, error) {
switch resourceKind {
case "CertificateSigningRequests":
return kubeClient.CertificatesV1beta1().CertificateSigningRequests().List(metav1.ListOptions{})
case "ClusterRoleBindings":
lst, err := kubeClient.RbacV1().ClusterRoleBindings().List(metav1.ListOptions{})
if apierrors.IsNotFound(err) {
lst, err := kubeClient.RbacV1beta1().ClusterRoleBindings().List(metav1.ListOptions{})
if apierrors.IsNotFound(err) {
return kubeClient.RbacV1alpha1().ClusterRoleBindings().List(metav1.ListOptions{})
}
return lst, err
}
return lst, err
case "ClusterRoles":
lst, err := kubeClient.RbacV1().ClusterRoles().List(metav1.ListOptions{})
if apierrors.IsNotFound(err) {
lst, err := kubeClient.RbacV1beta1().ClusterRoles().List(metav1.ListOptions{})
if apierrors.IsNotFound(err) {
return kubeClient.RbacV1alpha1().ClusterRoles().List(metav1.ListOptions{})
}
return lst, err
}
return lst, err
case "ComponentStatuses":
return kubeClient.CoreV1().ComponentStatuses().List(metav1.ListOptions{})
case "CustomResourceDefinitions":
// TODO : This should get cleaned up in future revisions.
apiextensionsclientset := apiextensionsclient.New(kubeClient.CoreV1().RESTClient())
if apiextensionsclientset != nil {
return apiextensionsclientset.ApiextensionsV1beta1().CustomResourceDefinitions().List(metav1.ListOptions{})
}
return nil, errors.Errorf("Failed to create extensions client for CRDs")
case "Nodes":
return kubeClient.CoreV1().Nodes().List(metav1.ListOptions{})
case "PersistentVolumes":
return kubeClient.CoreV1().PersistentVolumes().List(metav1.ListOptions{})
case "PodSecurityPolicies":
return kubeClient.ExtensionsV1beta1().PodSecurityPolicies().List(metav1.ListOptions{})
case "StorageClasses":
lst, err := kubeClient.StorageV1().StorageClasses().List(metav1.ListOptions{})
if apierrors.IsNotFound(err) {
return kubeClient.StorageV1beta1().StorageClasses().List(metav1.ListOptions{})
}
return lst, err
default:
return nil, errors.Errorf("don't know how to handle non-namespaced resource %v", resourceKind)
}
}
// QueryNSResources will query namespace-specific resources in the cluster,
// writing them out to <resultsdir>/resources/ns/<ns>/*.json
// TODO: Eliminate dependencies from config.Config and pass in data
func QueryNSResources(kubeClient kubernetes.Interface, recorder *QueryRecorder, ns string, cfg *config.Config) error {
logrus.Infof("Running ns query (%v)", ns)
// 1. Create the parent directory we will use to store the results
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
}
}
var resources []string
if cfg.Namespace == ns {
resources = config.NamespacedResources
} else {
resources = cfg.FilterResources(config.NamespacedResources)
}
// 3. Execute the ns-query
for _, resourceKind := range resources {
switch resourceKind {
case "PodLogs":
start := time.Now()
err := gatherPodLogs(kubeClient, ns, opts, cfg)
if err != nil {
return err
}
duration := time.Since(start)
recorder.RecordQuery("PodLogs", ns, duration, err)
default:
lister := func() (runtime.Object, error) { return queryNsResource(ns, resourceKind, opts, kubeClient) }
query := func() (time.Duration, error) { return objListQuery(outdir+"/", resourceKind+".json", lister) }
timedQuery(recorder, resourceKind, ns, query)
}
}
return nil
}
// QueryClusterResources queries non-namespace resources in the cluster, writing
// them out to <resultsdir>/resources/non-ns/*.json
// TODO: Eliminate dependencies from config.Config and pass in data
func QueryClusterResources(kubeClient kubernetes.Interface, recorder *QueryRecorder, cfg *config.Config) error {
logrus.Infof("Running non-ns query")
resources := cfg.FilterResources(config.ClusterResources)
// 1. Create the parent directory we will use to store the results
outdir := path.Join(cfg.OutputDir(), ClusterResourceLocation)
if len(resources) > 0 {
if err := os.MkdirAll(outdir, 0755); err != nil {
return errors.WithStack(err)
}
}
// 2. Execute the non-ns-query
for _, resourceKind := range resources {
switch resourceKind {
case "ServerVersion":
objqry := func() (interface{}, error) { return kubeClient.Discovery().ServerVersion() }
query := func() (time.Duration, error) {
return untypedQuery(cfg.OutputDir(), "serverversion.json", objqry)
}
timedQuery(recorder, "serverversion", "", query)
case "ServerGroups":
objqry := func() (interface{}, error) { return kubeClient.Discovery().ServerGroups() }
query := func() (time.Duration, error) {
return untypedQuery(cfg.OutputDir(), "servergroups.json", objqry)
}
timedQuery(recorder, "servergroups", "", query)
case "Nodes":
// cfg.Nodes configures whether users want to gather the Nodes resource in the
// cluster, but we also use that option to guide whether we get node data such
// as configz and healthz endpoints.
// NOTE: Node data collection is an aggregated time b/c propagating that detail back up
// is odd and would pollute some of the output.
start := time.Now()
err := gatherNodeData(kubeClient, cfg)
duration := time.Since(start)
recorder.RecordQuery("Nodes", "", duration, err)
fallthrough
default:
lister := func() (runtime.Object, error) { return queryNonNsResource(resourceKind, kubeClient) }
query := func() (time.Duration, error) { return objListQuery(outdir+"/", resourceKind+".json", lister) }
timedQuery(recorder, resourceKind, "", query)
}
}
return nil
}