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)
| 258 | // it queries only namespaced objects; non-namespaced otherwise. Writing them out to |
| 259 | // <outputDir>/resources/ns/<ns>/*.json or <outputDir>/resources/cluster/*.json. |
| 260 | func 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 | } |
no test coverage detected