-
Notifications
You must be signed in to change notification settings - Fork 919
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
add APIVersion and Kind checks for helper.ToUnstructured() #6028
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #6028 +/- ##
==========================================
- Coverage 48.37% 47.98% -0.39%
==========================================
Files 665 668 +3
Lines 54836 55303 +467
==========================================
+ Hits 26526 26537 +11
- Misses 26592 27038 +446
- Partials 1718 1728 +10
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Signed-off-by: zach593 <[email protected]>
b9d880b
to
3afc187
Compare
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.
/assign
return | ||
} | ||
|
||
if unstructuredOldObj.GetGeneration() == unstructuredNewObj.GetGeneration() { | ||
klog.V(4).Infof("Ignore update event of object (kind=%s, %s/%s) as specification no change", unstructuredOldObj.GetKind(), unstructuredOldObj.GetNamespace(), unstructuredOldObj.GetName()) |
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.
Does this unstructuredOldObj.GetKind()
always return ""? If yes, is there any bad effect in addition to the log?
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.
Does this unstructuredOldObj.GetKind() always return ""?
Yes.
Bad effects are minimal because this function only deal with rb and crb, and with or without namespace is enough to explain what is happening here. The reason I needed to change it because I update the ToUnstructured()
which was obviously affected it.
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.
OK. I will leave my question to ToUnstructured() part.
I'm trying to understand what bug this PR fixes.
Do you mean tests are invalid? always returning success due to this issue? Can you help to elaborate on it? |
Signed-off-by: zach593 <[email protected]>
[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.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-namespace", | ||
Labels: map[string]string{ | ||
"overridden": "false", | ||
}, |
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.
Do you mean tests are invalid? always returning success due to this issue? Can you help to elaborate on it?
I should have explained the invalid test in more detail, I apologize for that.
This test is typical, the reason I needed to add this label is because without typeMeta, after casting to unstructured.Unstructured
, it will lose the type data and the override will not work at all, making this test meaningless.
I just updated the test to make it more precise. @RainbowMango
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.
Thank you for the explanation, I understand now. This test attempted to enable OverridePolicy when building the Work object. However, since the APIVersion/Kind were not set in the target object, the OverridePolicy never actually took effect. Additionally, the test still passed because there were no validation checks for the OverridePolicy behavior.
unstructuredObj := &unstructured.Unstructured{Object: uncastObj} | ||
if unstructuredObj.GetKind() == "" { | ||
return nil, runtime.NewMissingKindErr(klog.KObj(unstructuredObj).String()) | ||
} | ||
if unstructuredObj.GetAPIVersion() == "" { | ||
return nil, runtime.NewMissingVersionErr(klog.KObj(unstructuredObj).String()) | ||
} |
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 still can't understand why this ToUnstructured
needs to be so picky, requiring apiVersion/kind to be set. In other words, what problem would arise without this?
Basically, I'm asking why we need it
, note that missing apiVersion/kind is not ToUnstructured
's fault, it just do the conversion job.
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.
note that missing apiVersion/kind is not ToUnstructured's fault, it just do the conversion job.
I thought so too. But the problem is that the root cause of this kind of error is too obscure, and even if I fix that test, there is no way to prevent the next person from writing the next invalid test.
From the perspective of preventing it from happening, if there is a way to completely prevent such errors from happening, I would welcome it no matter what it is, so I chose to modify this function, which seems to be the lowest cost solution.
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 thought so too. But the problem is that the root cause of this kind of error is too obscure, and even if I fix that test, there is no way to prevent the next person from writing the next invalid test.
I agree.
Another question, is doing all this just to prevent writing ineffective unit tests? Could business code potentially run into this issue due to oversight?
By the way, the issue tracks this as well kubernetes/kubernetes#80609 |
What type of PR is this?
/kind bug
What this PR does / why we need it:
This issue blocks me to continue #6017.
In function
helper.ToUnstructured()
, karmada will convert other types of object tounstructured.Unstructured
, but if object is missedTypeMeta
, the generatedunstructured.Unstructured
will also missedAPIVersion
andKind
, of course in most of the time these are harmful.Missing
TypeMeta
objects usually come from typed clients or objects that manually created in code.In kubernetes issue kubernetes/client-go#541 talked about typed client , looks like these guys won't fix this.
This issue affected many tests, and caused some tests to falsely report success. I fixed all the affected code I could see, but there may be still some I haven't noticed. This may also have an impact in non-test code, which I need to confirm further.
After fixing all the affected codes, I think adding an APIVersion and Kind checks to helper.ToUnstructured() can prevent the impact from expanding further. However, this check obviously needs to be added after ensuring that all the affected codes have been modified to avoid causing a stronger impact.
Which issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
Does this PR introduce a user-facing change?: