Skip to content

Refactor Builder to make it easier to use #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

bergmannf
Copy link
Contributor

What type of PR is this?

Feature

What this PR does / Why we need it?

Reworks the ResourceBuilder to make it slightly easier to use - also updates the documentation to explain how to use it better.

Special notes for your reviewer

This PR was co-authored by Aider with Gemini.

Test Coverage

Guidelines for CAD investigations

  • New investgations should be accompanied by unit tests and/or step-by-step manual tests in the investigation README.
  • Actioning investigations should be locally tested in staging, and E2E testing is desired. See README for more info on investigation graduation process.

Test coverage checks

  • Added tests
  • Created jira card to add unit test
  • This PR may not need unit tests

Pre-checks (if applicable)

  • Ran unit tests locally
  • Validated the changes in a cluster
  • Included documentation changes with PR

@openshift-ci openshift-ci bot requested review from tnierman and zmird-r July 31, 2025 12:12
Copy link
Contributor

openshift-ci bot commented Jul 31, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: bergmannf

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 31, 2025
@codecov-commenter
Copy link

codecov-commenter commented Jul 31, 2025

Codecov Report

❌ Patch coverage is 7.89474% with 35 lines in your changes missing coverage. Please review.
✅ Project coverage is 32.94%. Comparing base (62aca9c) to head (e7b448c).

Files with missing lines Patch % Lines
pkg/investigations/investigation/investigation.go 4.54% 21 Missing ⚠️
cadctl/cmd/investigate/investigate.go 0.00% 10 Missing ⚠️
pkg/investigations/precheck/precheck.go 50.00% 2 Missing ⚠️
pkg/investigations/cpd/cpd.go 0.00% 1 Missing ⚠️
...e/machinehealthcheckunterminatedshortcircuitsre.go 0.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #523      +/-   ##
==========================================
+ Coverage   32.68%   32.94%   +0.25%     
==========================================
  Files          37       37              
  Lines        2472     2459      -13     
==========================================
+ Hits          808      810       +2     
+ Misses       1603     1588      -15     
  Partials       61       61              
Files with missing lines Coverage Δ
pkg/investigations/cpd/cpd.go 0.00% <0.00%> (ø)
...e/machinehealthcheckunterminatedshortcircuitsre.go 49.42% <0.00%> (ø)
pkg/investigations/precheck/precheck.go 41.66% <50.00%> (ø)
cadctl/cmd/investigate/investigate.go 0.00% <0.00%> (ø)
pkg/investigations/investigation/investigation.go 14.08% <4.54%> (+4.78%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bergmannf bergmannf changed the title Investigation architecture analysis Refactor Builder to make it easier to use Jul 31, 2025
@@ -68,7 +68,20 @@ As PagerDuty itself does not provide finer granularity for webhooks than service
To add a new alert investigation:

- run `make bootstrap-investigation` to generate boilerplate code in `pkg/investigations` (This creates the corresponding folder & .go file, and also appends the investigation to the `availableInvestigations` interface in `registry.go`.).
- investigation.Resources contain initialized clients for the clusters aws environment, ocm and more. See [Integrations](#integrations)
- The `Run` method of your investigation receives a `ResourceBuilder`. Use its `With...` methods to request the resources your investigation needs, then call `Build()` to get a `Resources` struct containing them. The builder automatically handles dependencies between resources (e.g., requesting an AWS client will also initialize the cluster object). For example:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated - I didn't know we had some scaffolding for investigations 🥳

@bergmannf bergmannf force-pushed the investigation-architecture-analysis branch from f426d63 to 681dd06 Compare July 31, 2025 14:03
@openshift-merge-robot openshift-merge-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 3, 2025
@typeid
Copy link
Member

typeid commented Aug 4, 2025

LGTM this just needs a rebase @bergmannf

This commit refactors the \`ResourceBuilder\` to be more user-friendly and robust. The previous implementation required callers to manage dependencies between resources manually and was prone to runtime errors if resources were not requested in the correct order.

The key changes include:
- A new \`NewResourceBuilder\` constructor now handles the initial setup of required components like OCM and PagerDuty clients, simplifying the builder's API.
- The \`Build()\` method now lazily initializes resources only when they are explicitly requested via \`With...\` methods. This includes deferring the \`GetClusterInfo\` call until necessary.
- Dependency management is now handled internally by the builder. For example, requesting an AWS client via \`WithAwsClient()\` will automatically ensure the cluster object is also built.
- Investigation \`Run\` methods have been updated to explicitly declare their resource dependencies using the new builder methods.

This refactoring resolves several build and runtime errors, improves the developer experience by providing a clearer and safer API, and makes investigations easier to write and maintain.

Co-authored-by: Aider <using Gemini>
Explain how to use the ResourceBuilder in a new investigation, including requesting resources and the automatic dependency handling.

Co-authored-by: Aider <using Gemini>
The error can convey more information than a simple bool.

If it's nil = all good, otherwise the error can be returned why the
investigation was stopped.
@bergmannf bergmannf force-pushed the investigation-architecture-analysis branch from 681dd06 to e7b448c Compare August 4, 2025 14:16
@openshift-merge-robot openshift-merge-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 4, 2025
Copy link
Contributor

openshift-ci bot commented Aug 4, 2025

@bergmannf: all tests passed!

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@openshift-merge-robot openshift-merge-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 9, 2025
@openshift-merge-robot
Copy link
Contributor

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants