-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix nil pointer panic in application failover controller #7127
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: master
Are you sure you want to change the base?
Fix nil pointer panic in application failover controller #7127
Conversation
Add nil check for TolerationSeconds in syncBinding() to prevent controller panic when the field is not set. Also add webhook defaulting for TolerationSeconds (300s) to match CRD schema. Signed-off-by: goyalpalak18 <goyalpalak1806@gmail.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
Welcome @goyalpalak18! It looks like this is your first PR to karmada-io/karmada 🎉 |
Summary of ChangesHello @goyalpalak18, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical nil pointer panic in the application failover controllers that occurred when "decisionConditions.tolerationSeconds" was unset, leading to controller crash loops during cluster failure scenarios. The changes introduce robust nil checks within the controllers and establish consistent defaulting via webhooks, significantly enhancing the reliability of application failover mechanisms. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request effectively addresses a critical nil pointer panic in the application failover controllers by introducing robust nil checks for tolerationSeconds and ensuring a default value of 300 seconds is applied when it's not explicitly set. The changes also include a new helper function SetDefaultTolerationSeconds in pkg/util/helper/policy.go to consistently apply this default during admission, which is a good defense-in-depth strategy. Comprehensive unit tests have been added for the new helper, and existing webhook tests were updated to reflect the new defaulting behavior. Overall, this is a well-implemented and crucial fix that significantly improves the reliability of application failover during cluster failure scenarios.
| // If tolerationSeconds is not set, use the default value of 300 seconds | ||
| // to avoid nil pointer dereference in detectFailure. | ||
| if tolerationSeconds == nil { | ||
| tolerationSeconds = ptr.To[int32](300) |
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.
| // If tolerationSeconds is not set, use the default value of 300 seconds | ||
| // to avoid nil pointer dereference in detectFailure. | ||
| if tolerationSeconds == nil { | ||
| tolerationSeconds = ptr.To[int32](300) |
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.
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7127 +/- ##
==========================================
- Coverage 46.55% 46.54% -0.01%
==========================================
Files 700 700
Lines 48133 48142 +9
==========================================
+ Hits 22406 22407 +1
- Misses 24041 24047 +6
- Partials 1686 1688 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@mszacillo @XiShanYongYe-Chang Maybe you can take a look. |
mszacillo
left a 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.
Thanks for finding this. Could you advise on how to reproduce this bug? You mention this only happens when generating the propagationpolicy programmatically or when updating from a previous version - which version upgrades cause an issue?
| // If tolerationSeconds is not set, use the default value of 300 seconds | ||
| // to avoid nil pointer dereference in detectFailure. | ||
| if tolerationSeconds == nil { | ||
| tolerationSeconds = ptr.To[int32](300) |
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.
I don't have an issue including an additional fallback for the tolerationSeconds if this is somehow unset, but your additional to the mutation webhook should be enough to safeguard against this field being empty. If we keep this, can we please add a warning log to notify the user that this field is not set, and that we expect it to be?
|
@RainbowMango Another comment that you may have more context for. Is there a historical reason as to why we let tolerationSeconds be optional? Was this to make the use of the application failover API a bit easier for the user? |
Problem summary
I found a crash scenario in the Application Failover controllers (
RBApplicationFailoverControllerandCRBApplicationFailoverController) whendecisionConditions.tolerationSecondsis not set.TolerationSecondsis an optional*int32300sis not applied for resources created programmatically or migrated from older versionsThis panic happens exactly during cluster failure scenarios, which makes it particularly risky.
What I changed
1. Controller-side safety (defense in depth)
I added a nil check in both application failover controllers so
tolerationSecondsis always initialized before use:pkg/controllers/applicationfailover/rb_application_failover_controller.gopkg/controllers/applicationfailover/crb_application_failover_controller.goIf
tolerationSecondsis nil, it now defaults to 300 seconds, which prevents panics even when webhook defaulting is skipped.2. Webhook defaulting
I added a shared helper function to apply the same default during admission:
SetDefaultTolerationSeconds()inpkg/util/helper/policy.gopropagationpolicy/mutating.goclusterpropagationpolicy/mutating.goThis ensures newly created policies get the expected default value.
3. Unit tests
I added unit tests for the new helper to verify:
tolerationSecondsvalues are not overwritten300is applied when the field is missingWhy this fix is complete
tolerationSeconds300sThis makes application failover more reliable during real cluster failure scenarios.