Skip to content

[ROSAENG-62213] pkg/pagerduty: refactor and fix cluster ID extraction - #892

Merged
openshift-merge-bot[bot] merged 1 commit into
openshift:mainfrom
bergmannf:rosaeng-62213-hcpnpud
Jul 30, 2026
Merged

openshift-merge-bot[bot] merged 1 commit into
openshift:mainfrom
bergmannf:rosaeng-62213-hcpnpud

Conversation

@bergmannf

@bergmannf bergmannf commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Add dedicated extractor functions, and add a third parse path that extracts cluster_id from the free-text 'firing' field to handle HCPNodepoolUpgradeDelay alerts that set neither 'cluster_id' nor 'notes'.

What type of PR is this?

bug

What this PR does / Why we need it?

Special notes for your reviewer

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

Summary by CodeRabbit

  • Bug Fixes

    • Improved extraction of cluster IDs from PagerDuty alert bodies, including multiline firing text support and safer parsing to avoid false matches.
    • Enhanced behavior when optional alert sections are missing, with clearer error outcomes when required information is absent.
  • Tests

    • Expanded test coverage for node pool upgrade delay alerts, including multiline scenarios.
    • Added negative coverage to ensure hosted_cluster_id is not mistaken for cluster_id, and adjusted missing-field expectations.

@openshift-ci openshift-ci Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 28, 2026
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: openshift/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: c237445e-9bd7-460f-904b-6551d81ad4d5

📥 Commits

Reviewing files that changed from the base of the PR and between bf12470 and b3d7863.

📒 Files selected for processing (2)
  • pkg/pagerduty/pagerduty.go
  • pkg/pagerduty/pagerduty_test.go

Walkthrough

PagerDuty cluster ID extraction now checks direct alert details, notes YAML, and firing text containing cluster_id = .... Tests cover single-line and multiline firing payloads and update the missing-field error case.

Changes

PagerDuty cluster ID extraction

Layer / File(s) Summary
Multi-source extraction pipeline
pkg/pagerduty/pagerduty.go
The extractor checks details.cluster_id, notes YAML, and multiline details.firing text in sequence, returning aggregated errors when no strategy succeeds.
Firing-text extraction validation
pkg/pagerduty/pagerduty_test.go
Tests verify single-line and multiline extraction, reject hosted_cluster_id, and require both details and firing to be absent for the missing-fields error case.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested labels: approved

Suggested reviewers: rolandmkunkel, nikokolas3270

🚥 Pre-merge checks | ✅ 14 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (14 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: refactoring cluster ID extraction and adding a fix for the PagerDuty parser.
Description check ✅ Passed The description covers PR type, summary, test coverage, and pre-checks; only the reviewer notes section is left blank.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Stable And Deterministic Test Names ✅ Passed Added Ginkgo titles are static strings; no dynamic IDs, timestamps, or generated values appear in test names.
Test Structure And Quality ✅ Passed New RetrieveClusterID unit cases are isolated, use existing BeforeEach/AfterEach cleanup, have no cluster waits/timeouts, and follow repo patterns.
Microshift Test Compatibility ✅ Passed The new Ginkgo tests are unit tests using httptest/PagerDuty payloads only; they don’t touch OpenShift APIs or MicroShift-unsupported features.
Single Node Openshift (Sno) Test Compatibility ✅ Passed Only pagerduty unit tests were added; they parse alert JSON/text and make no node/topology assumptions, so SNO compatibility isn’t impacted.
Topology-Aware Scheduling Compatibility ✅ Passed PASS: Changes are limited to PagerDuty parsing/tests; no manifests, controllers, node selectors, affinity, spread constraints, or replica logic were added.
Ote Binary Stdout Contract ✅ Passed No stdout writes were added in process-level code; changed files only add parsing helpers/tests, and suite setup still just calls RunSpecs.
Ipv6 And Disconnected Network Test Compatibility ✅ Passed New Ginkgo tests are unit tests using httptest.Server/server.URL; no hardcoded IPv4, IP parsing, or public internet dependencies were found.
No-Weak-Crypto ✅ Passed Touched code only adds regex-based alert parsing; no weak crypto, custom crypto, or secret comparisons were introduced.
Container-Privileges ✅ Passed PR only changes pagerduty code/tests; no container/K8s manifests or privilege fields like privileged, hostPID, or allowPrivilegeEscalation are present.
No-Sensitive-Data-In-Logs ✅ Passed New logs only announce parsing steps; they don’t print tokens, PII, or alert contents. The only full-body debug log is unchanged.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
pkg/pagerduty/pagerduty_test.go (1)

344-370: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Good coverage for the new firing-text path; consider adding an "all fields empty" case.

These two cases solidly cover single-line and multi-line firing extraction. One gap: there's no test where details exists but none of cluster_id, notes, or firing are populated — the path that exercises the lastErr fallthrough across all three extractors in extractClusterIDFromAlertBody. Adding it would guard the loop's error-selection behavior (see the related comment on pkg/pagerduty/pagerduty.go lines 422-441) against regressions.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/pagerduty/pagerduty_test.go` around lines 344 - 370, Add a test alongside
the existing RetrieveClusterID cases for an alert whose details object exists
but has empty cluster_id, notes, and firing fields. Assert that
RetrieveClusterID returns an error and no cluster ID, exercising the lastErr
fallthrough across all extractors in extractClusterIDFromAlertBody.
pkg/pagerduty/pagerduty.go (1)

422-441: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

lastErr can mask the real root cause.

Since lastErr is overwritten on every iteration regardless of which extractor produced it, the error ultimately returned is always from whichever extractor ran last (parseClusterIdFromFiring) if none succeed — even when an earlier extractor hit a more specific and actionable error (e.g., malformed notes YAML from parseClusterIdFromNotes). A caller/log consumer debugging a failed extraction would see "could not find firing field" or "no cluster_id found in firing field" instead of the actual YAML decode error, hiding the true cause.

♻️ Proposed fix: aggregate/preserve all errors
 	type extractor = func(map[string]interface{}) (string, error)
 	extractors := []extractor{parseClusterIdFromField, parseClusterIdFromNotes, parseClusterIdFromFiring}
-	var lastErr error
-	for _, extractor := range extractors {
-		id, err := extractor(details)
+	var errs []error
+	for _, ext := range extractors {
+		id, err := ext(details)
 		if err != nil {
-			lastErr = err
+			errs = append(errs, err)
 		}
 		if id != "" {
 			return id, nil
 		}
 	}
-	return "", lastErr
+	return "", errors.Join(errs...)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/pagerduty/pagerduty.go` around lines 422 - 441, Update
extractClusterIDFromAlertBody so errors from all failed extractors are preserved
instead of returning only the final extractor’s error. Aggregate the errors from
parseClusterIdFromField, parseClusterIdFromNotes, and parseClusterIdFromFiring,
then return the combined error when no extractor returns an ID while preserving
the existing successful extraction behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/pagerduty/pagerduty.go`:
- Around line 424-426: Update the not-found error in extractAlertDetails to stop
embedding the full data alert body in the error message. Return an error
containing only a safe, relevant field identifier or a generic description,
while preserving the existing error path and context.
- Around line 414-420: Update clusterIDFromFiringRe to require a whitespace or
start-of-field boundary before cluster_id, preventing matches within names such
as hosted_cluster_id while still extracting standalone cluster_id values from
multi-line firing text.

---

Nitpick comments:
In `@pkg/pagerduty/pagerduty_test.go`:
- Around line 344-370: Add a test alongside the existing RetrieveClusterID cases
for an alert whose details object exists but has empty cluster_id, notes, and
firing fields. Assert that RetrieveClusterID returns an error and no cluster ID,
exercising the lastErr fallthrough across all extractors in
extractClusterIDFromAlertBody.

In `@pkg/pagerduty/pagerduty.go`:
- Around line 422-441: Update extractClusterIDFromAlertBody so errors from all
failed extractors are preserved instead of returning only the final extractor’s
error. Aggregate the errors from parseClusterIdFromField,
parseClusterIdFromNotes, and parseClusterIdFromFiring, then return the combined
error when no extractor returns an ID while preserving the existing successful
extraction behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: openshift/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 1fad5080-205a-4f94-973a-5e7e8b51d07d

📥 Commits

Reviewing files that changed from the base of the PR and between 4b9e7e4 and 3fd1500.

📒 Files selected for processing (2)
  • pkg/pagerduty/pagerduty.go
  • pkg/pagerduty/pagerduty_test.go

Comment thread pkg/pagerduty/pagerduty.go Outdated
Comment thread pkg/pagerduty/pagerduty.go
@codecov-commenter

codecov-commenter commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 45.08%. Comparing base (b36756e) to head (b3d7863).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
pkg/pagerduty/pagerduty.go 88.88% 1 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #892      +/-   ##
==========================================
+ Coverage   44.94%   45.08%   +0.13%     
==========================================
  Files          73       73              
  Lines        8753     8775      +22     
==========================================
+ Hits         3934     3956      +22     
  Misses       4594     4594              
  Partials      225      225              
Files with missing lines Coverage Δ
pkg/pagerduty/pagerduty.go 64.68% <88.88%> (+3.14%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bergmannf
bergmannf force-pushed the rosaeng-62213-hcpnpud branch from 3fd1500 to bf12470 Compare July 29, 2026 08:56

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/pagerduty/pagerduty_test.go`:
- Line 348: Handle the ignored fmt.Fprint errors in all three new mock handlers
in pkg/pagerduty/pagerduty_test.go at lines 348-348, 362-362, and 375-375: check
each fixture write result and fail the corresponding test or handler
consistently when writing fails, rather than discarding the error.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: openshift/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 2b1f776d-e78d-4ef6-ade6-aa5f89194c6d

📥 Commits

Reviewing files that changed from the base of the PR and between 3fd1500 and bf12470.

📒 Files selected for processing (2)
  • pkg/pagerduty/pagerduty.go
  • pkg/pagerduty/pagerduty_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/pagerduty/pagerduty.go

Comment thread pkg/pagerduty/pagerduty_test.go
Add dedicated extractor functions, and add a third parse path that
extracts cluster_id from the free-text 'firing' field to handle
HCPNodepoolUpgradeDelay alerts that set neither 'cluster_id' nor
'notes'.
@bergmannf
bergmannf force-pushed the rosaeng-62213-hcpnpud branch from bf12470 to b3d7863 Compare July 30, 2026 09:25
@gvnnn

gvnnn commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

/lgtm

@openshift-ci openshift-ci Bot added the lgtm Indicates that a PR is ready to be merged. label Jul 30, 2026
@openshift-ci

openshift-ci Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: bergmannf, gvnnn

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

The pull request process is described here

Details 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 commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

@bergmannf: all tests passed!

Full PR test history. Your PR dashboard.

Details

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-bot
openshift-merge-bot Bot merged commit ba15d3f into openshift:main Jul 30, 2026
10 checks passed
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. lgtm Indicates that a PR is ready to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants