karmada
karmada copied to clipboard
operator/pkg: mitigate null pointer dereference in `Validate()` and `newRunData()` functions
Description
In this commit, we mitigate the null pointer dereference issue in the deinit operator pkg by adding defensive checks on
opt.Karmada.Spec.HostCluster in newRunData func and on opt.Karmada.Spec.Components in Validate in init.go.
Motivation and Context
While unit testing the Karmada Init and DeInit in the operator package (#5613), deliberately passing empty components on those fields triggers a null pointer exception error. This PR resolves this issue by adding defensive checks.
While testing
What type of PR is this?
/kind cleanup
Does this PR introduce a user-facing change?:
NONE
Hey @chaosi-zju,
Just checking in on this PR. Any feedback you might have would be greatly appreciated!
It looks like PR #5613 is waiting on this one. Thanks!
@mohamedawnallah Could you please rebase this PR? There is a conflict needs to be resolved. Also, what's the benefit of this? Only for ease of the unit tests?
cc @zhzhuang-zju @jabellard
While unit testing the Karmada Init and DeInit in the operator package (https://github.com/karmada-io/karmada/pull/5613), deliberately passing empty components on those fields triggers a null pointer exception error. This PR resolves this issue by adding defensive checks.
https://github.com/karmada-io/karmada/blob/c31c8e4a58430bc8bdf4b28fa7232dffbc3fce49/operator/pkg/apis/operator/v1alpha1/register.go#L49-L54
We registered a method in the init function to set default values for Karmada, which will be invoked when Default() is called. So in practice, there won’t be any null pointer issues because Default() is called before this code segment. When writing unit tests (UT), calling operatorscheme.Scheme.Default(karmada) can achieve the same effect.
Of course, adding some defensive checks can also prevent panics in case operatorscheme.Scheme.Default(karmada) is not called correctly in the code.
[APPROVALNOTIFIER] This PR is NOT APPROVED
This pull-request has been approved by: Once this PR has been reviewed and has the lgtm label, please assign lonelycz for approval. For more information see the Kubernetes Code Review Process.
The full list of commands accepted by this bot can be found here.
Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment
It looks like some of those defensive checks already added in that PR #5814. Updated this PR to handle when karmada *operatorv1alpha1.Karmada is nil and when opt.Karmada.Spec.HostCluster is nil part of newRunData.
:warning: Please install the to ensure uploads and comments are reliably processed by Codecov.
Codecov Report
Attention: Patch coverage is 0% with 8 lines in your changes missing coverage. Please review.
Project coverage is 48.42%. Comparing base (
2f6ff56) to head (05ac259). Report is 5 commits behind head on master.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| operator/pkg/init.go | 0.00% | 5 Missing :warning: |
| operator/pkg/controller/karmada/validating.go | 0.00% | 3 Missing :warning: |
:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@ Coverage Diff @@
## master #5617 +/- ##
==========================================
+ Coverage 48.27% 48.42% +0.14%
==========================================
Files 677 677
Lines 56067 56074 +7
==========================================
+ Hits 27068 27153 +85
+ Misses 27229 27139 -90
- Partials 1770 1782 +12
| Flag | Coverage Δ | |
|---|---|---|
| unittests | 48.42% <0.00%> (+0.14%) |
:arrow_up: |
Flags with carried forward coverage won't be shown. Click here to find out more.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.