MCPcopy
hub / github.com/vmware-tanzu/sonobuoy / QueryResources

Function QueryResources

pkg/discovery/query.go:260–337  ·  view source on GitHub ↗

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 /resources/ns/ /*.json or /resources/cluster/*.json.

(
	client *dynamic.APIHelper,
	recorder *QueryRecorder,
	resources []schema.GroupVersionResource,
	ns *string,
	cfg *config.Config)

Source from the content-addressed store, hash-verified

258// it queries only namespaced objects; non-namespaced otherwise. Writing them out to
259// <outputDir>/resources/ns/<ns>/*.json or <outputDir>/resources/cluster/*.json.
260func QueryResources(
261 client *dynamic.APIHelper,
262 recorder *QueryRecorder,
263 resources []schema.GroupVersionResource,
264 ns *string,
265 cfg *config.Config) error {
266
267 // Early exit; avoid forming query or creating output directories.
268 if len(resources) == 0 {
269 return nil
270 }
271
272 if ns != nil {
273 logrus.Infof("Running ns query (%v)", *ns)
274 } else {
275 logrus.Info("Running cluster queries")
276 }
277
278 // Create the parent directory we will use to store the results
279 outdir := filepath.Join(cfg.QueryOutputDir(), ClusterResourceLocation)
280 if ns != nil {
281 outdir = filepath.Join(cfg.QueryOutputDir(), NSResourceLocation, *ns)
282 }
283
284 if err := os.MkdirAll(outdir, 0755); err != nil {
285 return errors.WithStack(err)
286 }
287
288 // Setup label filter if there is one.
289 opts := metav1.ListOptions{}
290 if len(cfg.Filters.LabelSelector) > 0 {
291 if _, err := labels.Parse(cfg.Filters.LabelSelector); err != nil {
292 logrus.Warningf("Labelselector %v failed to parse with error %v", cfg.Filters.LabelSelector, err)
293 } else {
294 opts.LabelSelector = cfg.Filters.LabelSelector
295 }
296 }
297
298 // Execute the query
299 for _, gvr := range resources {
300 lister := func() (*unstructured.UnstructuredList, error) {
301 resourceClient := client.Client.Resource(gvr)
302 if ns != nil && len(*ns) > 0 {
303 // Use a namespaced query rather than using a metadata.namespace filter to
304 // avoid permissions issues.
305 objs, err := resourceClient.Namespace(*ns).List(context.TODO(), opts)
306 return objs, errors.Wrapf(err, "listing resource %v", gvr)
307 } else {
308 objs, err := resourceClient.List(context.TODO(), opts)
309 return objs, errors.Wrapf(err, "listing resource %v", gvr)
310 }
311 }
312
313 // The core group is just the empty string but for clarity and consistency, refer to it as core.
314 groupText := gvr.Group
315 if groupText == "" {
316 groupText = "core"
317 }

Callers 1

QueryClusterFunction · 0.85

Calls 6

timedListQueryFunction · 0.85
timedQueryFunction · 0.85
QueryOutputDirMethod · 0.80
ResourceMethod · 0.80
ListMethod · 0.80
NamespaceMethod · 0.65

Tested by

no test coverage detected