-
Notifications
You must be signed in to change notification settings - Fork 9
Improve watch setup in ServerMaintenance reconciler
#564
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
base: main
Are you sure you want to change the base?
Conversation
87f72f4 to
90445c3
Compare
90445c3 to
492e590
Compare
492e590 to
82b0964
Compare
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
WalkthroughRefactors the ServerMaintenance controller with consistent internal variable naming (renaming Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Areas requiring extra attention:
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
internal/controller/index_fields.go(1 hunks)internal/controller/servermaintenance_controller.go(8 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
internal/controller/index_fields.go (1)
api/v1alpha1/servermaintenance_types.go (1)
ServerMaintenance(88-94)
internal/controller/servermaintenance_controller.go (4)
api/v1alpha1/servermaintenance_types.go (5)
ServerMaintenance(88-94)ServerMaintenanceStatePending(71-71)ServerMaintenanceStateInMaintenance(73-73)ServerMaintenanceStateFailed(75-75)ServerMaintenanceState(67-67)api/v1alpha1/server_types.go (2)
Server(428-434)Power(13-13)internal/controller/helper.go (1)
GetServerByName(69-78)api/v1alpha1/serverclaim_types.go (1)
ServerClaim(69-75)
🔇 Additional comments (12)
internal/controller/servermaintenance_controller.go (12)
42-49: LGTM!The variable renaming from
serverMaintenancetomaintenanceis concise and the reconciliation flow is correct.
58-84: LGTM!The reconciliation flow improvements are well-structured:
- Added explicit logging at Line 59 for observability
- State initialization logic (lines 78-82) ensures the ServerMaintenance starts in Pending state
- Finalizer handling (lines 73-75) is correct
86-98: LGTM!The addition of a default case (lines 94-96) for unknown states is good defensive programming. Logging and skipping reconciliation for unknown states is a reasonable approach.
100-173: LGTM!The Pending state handler improvements are solid:
- Nil check for ServerRef (lines 101-103) prevents potential issues
- ServerClaim annotation patching (lines 135-144) uses the refactored helper method consistently
- Added logging (line 171) improves observability
175-220: LGTM!The InMaintenance state handler is well-structured with:
- Defensive nil check for ServerRef (lines 176-178)
- Enhanced logging at lines 189, 195, and 218 for better observability
- Correct handling of boot configuration states
289-311: LGTM!The delete method improvements are well done:
- Consistent parameter naming
- Enhanced logging at lines 301, 307, and 309 for better observability of the deletion flow
- Correct finalizer removal logic
361-371: LGTM!Removing the
logparameter simplifies the method signature. The logic correctly removes the boot config reference from the Server.
382-395: LGTM!The nil check addition (lines 383-385) is a good defensive programming practice. The method correctly patches the ServerMaintenance state and avoids unnecessary patches when the state already matches.
397-407: LGTM!Good improvements to this helper:
- Method renamed to plural form (more accurate since it sets multiple annotations)
- Nil check (lines 398-400) adds safety
- Uses
metautils.SetAnnotationsconsistently
409-451: LGTM!Excellent improvements to this enqueue handler:
- Type check with error logging (lines 412-416) prevents potential panics
- Deduplication via
processedNamesmap (line 419) prevents duplicate reconcile requests—this is a key correctness improvement- The handler now queries ServerMaintenances by indexed field (line 436), enabling efficient lookups via the newly added index in
index_fields.go
453-489: LGTM!Well-structured enqueue handler with good improvements:
- Type check with error logging (lines 456-460) prevents panics
- Early returns (lines 463-465, 467-469) optimize for common cases
- Leverages the indexed
serverRefField(line 472) for efficient lookups
491-499: The code is correct as-is; index registration timing is not an issue.The enqueue handlers (lines 496-497) are created during
SetupWithManager()but don't execute until runtime. The index registration fromRegisterIndexFields()happens beforemgr.Start()(line 538 before line 558), ensuring the index is available when the handlers actually invoke theirList()operations withMatchingFields{serverRefField}at runtime.
| if err := indexer.IndexField(ctx, &metalv1alpha1.ServerMaintenance{}, serverRefField, func(rawObj client.Object) []string { | ||
| maintenance := rawObj.(*metalv1alpha1.ServerMaintenance) | ||
| if maintenance.Spec.ServerRef != nil && maintenance.Spec.ServerRef.Name != "" { | ||
| return []string{maintenance.Spec.ServerRef.Name} | ||
| } | ||
| return nil | ||
| }); err != nil { | ||
| return err | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add nil-safety check on type assertion.
Line 60 performs a type assertion without checking the ok value, unlike the other indexers in this file (lines 21-24, 34-37, 47-50). This inconsistency could cause a panic if rawObj is not a ServerMaintenance.
🔎 Proposed fix
if err := indexer.IndexField(ctx, &metalv1alpha1.ServerMaintenance{}, serverRefField, func(rawObj client.Object) []string {
- maintenance := rawObj.(*metalv1alpha1.ServerMaintenance)
+ maintenance, ok := rawObj.(*metalv1alpha1.ServerMaintenance)
+ if !ok {
+ return nil
+ }
if maintenance.Spec.ServerRef != nil && maintenance.Spec.ServerRef.Name != "" {
return []string{maintenance.Spec.ServerRef.Name}
}
return nil
}); err != nil {
return err
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if err := indexer.IndexField(ctx, &metalv1alpha1.ServerMaintenance{}, serverRefField, func(rawObj client.Object) []string { | |
| maintenance := rawObj.(*metalv1alpha1.ServerMaintenance) | |
| if maintenance.Spec.ServerRef != nil && maintenance.Spec.ServerRef.Name != "" { | |
| return []string{maintenance.Spec.ServerRef.Name} | |
| } | |
| return nil | |
| }); err != nil { | |
| return err | |
| } | |
| if err := indexer.IndexField(ctx, &metalv1alpha1.ServerMaintenance{}, serverRefField, func(rawObj client.Object) []string { | |
| maintenance, ok := rawObj.(*metalv1alpha1.ServerMaintenance) | |
| if !ok { | |
| return nil | |
| } | |
| if maintenance.Spec.ServerRef != nil && maintenance.Spec.ServerRef.Name != "" { | |
| return []string{maintenance.Spec.ServerRef.Name} | |
| } | |
| return nil | |
| }); err != nil { | |
| return err | |
| } |
🤖 Prompt for AI Agents
In internal/controller/index_fields.go around lines 59 to 67, the type assertion
rawObj.(*metalv1alpha1.ServerMaintenance) is done without checking the ok result
which can panic; change it to the same nil-safe pattern used earlier: perform
the type assertion with the two-value form (maintenance, ok :=
rawObj.(*metalv1alpha1.ServerMaintenance)), return nil if !ok, then proceed to
check maintenance.Spec.ServerRef and Name and return the slice or nil as before.
Proposed Changes
ServerMaintenancereconcilerSummary by CodeRabbit
New Features
Bug Fixes & Improvements
✏️ Tip: You can customize this high-level summary in your review settings.