Add automatic reconnect on Ironic authentication failure#10
Conversation
WalkthroughThe OpenStack baremetal client now automatically recovers from authentication failures. The client stores a service client factory function, detects auth-related errors (HTTP 401 and reauthentication exceptions), and reconnects with a fresh client before retrying failed power-state operations in both GetPowerState and SetPowerState methods. ChangesAuthentication Failure Recovery with Reconnect and Retry
Sequence Diagram(s)No sequence diagram generated; the changes are localized error-handling enhancements with straightforward retry logic best understood through flowchart representation already provided in the review stack artifact. Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Complexity reasoning: This PR introduces a stateful reconnection mechanism with error-type detection, factory-based client rebuilding, and dual-retry paths in two methods. The changes span imports, struct definitions, initialization refactoring, helper functions, method implementations, and comprehensive test coverage. The logic is coherent but requires careful review of error classification, client state transitions, and retry semantics to ensure authentication recovery does not mask other failure types or leave the client in an inconsistent state. No high-density logic, but multiple interconnected concerns across the codebase. Possibly related PRs
Suggested reviewers
Caution Pre-merge checks failedPlease resolve all errors before merging. Addressing warnings is optional.
❌ Failed checks (1 error, 2 warnings)
✅ Passed checks (8 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 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 `@internal/management/openstack_internal_test.go`:
- Around line 127-140: Test currently only checks error text when reconnect
returns a client with empty Endpoint; also assert that the existing client
pointer remains unchanged: after calling c.reconnect(context.Background()) and
Expect(err).To(HaveOccurred()), add an assertion that c.client is still the
original oldSC (e.g., Expect(c.client).To(Equal(oldSC))) so the test verifies
the old client survives the failed swap in the OpenStackClient.reconnect path.
In `@internal/management/openstack.go`:
- Line 110: Remove the sensitive endpoint value from the reconnect success log:
update the log.Info call that currently logs "ironic service client reconnected
successfully" with the "endpoint", sc.Endpoint pair so it no longer includes
sc.Endpoint (either omit the "endpoint" key/value or replace it with a redacted
placeholder). Locate the log.Info invocation referencing sc.Endpoint in
internal/management/openstack.go (the reconnect success log) and modify it to
log only the success message (or a redacted endpoint) to avoid exposing internal
hostnames/URLs.
- Around line 27-29: The shared managementClient mutates c.client in reconnect
while GetPowerState and SetPowerState read it, causing a data race; protect
c.client with a sync.RWMutex on the managementClient struct (add a mutex field),
acquire a read lock when reading/copying c.client in GetPowerState/SetPowerState
and a write lock when swapping it in reconnect/newServiceClient, or copy the
client under lock then operate on the copy to avoid holding the lock long; also
remove the "endpoint", sc.Endpoint argument from the log call in reconnect to
avoid emitting internal hostnames.
🪄 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: osac-project/coderabbit/.coderabbit.yaml
Review profile: ASSERTIVE
Plan: Enterprise
Run ID: ca3d0c8d-6fc6-403f-b65a-54ee14ce8e2e
📒 Files selected for processing (2)
internal/management/openstack.gointernal/management/openstack_internal_test.go
Port the reconnect logic from the old internal/ironic package (PR osac-project#7) into the new internal/management layer. When gophercloud's built-in token refresh fails, the client detects auth errors (401, reauth failures), recreates the service client from scratch, and retries the operation once. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Issue tracked in #11 for future resolution.
Port the reconnect logic from the old internal/ironic package (PR #7) into the new internal/management layer. When gophercloud's built-in token refresh fails, the client detects auth errors (401, reauth failures), recreates the service client from scratch, and retries the operation once.
Part of osac-project/issues#394
Summary by CodeRabbit
Bug Fixes
Tests