Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

zach593
Copy link
Contributor

@zach593 zach593 commented Jan 8, 2025

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 to unstructured.Unstructured, but if object is missed TypeMeta, the generated unstructured.Unstructured will also missed APIVersion and Kind, 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?:

NONE

@karmada-bot karmada-bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. kind/bug Categorizes issue or PR as related to a bug. labels Jan 8, 2025
@karmada-bot karmada-bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jan 8, 2025
@codecov-commenter
Copy link

codecov-commenter commented Jan 8, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 26.31579% with 14 lines in your changes missing coverage. Please review.

Project coverage is 47.98%. Comparing base (72b6bd7) to head (0d376ca).
Report is 82 commits behind head on master.

Files with missing lines Patch % Lines
pkg/scheduler/event_handler.go 27.27% 6 Missing and 2 partials ⚠️
pkg/util/helper/unstructured.go 25.00% 4 Missing and 2 partials ⚠️

❗ 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     
Flag Coverage Δ
unittests 47.98% <26.31%> (-0.39%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zach593 zach593 changed the title [WIP]add APIVersion and Kind checks for helper.ToUnstructured() add APIVersion and Kind checks for helper.ToUnstructured() Jan 14, 2025
@karmada-bot karmada-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jan 14, 2025
Copy link
Member

@RainbowMango RainbowMango left a 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())
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

@RainbowMango
Copy link
Member

I'm trying to understand what bug this PR fixes.

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.

Do you mean tests are invalid? always returning success due to this issue? Can you help to elaborate on it?

@karmada-bot
Copy link
Collaborator

[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 ask for approval from rainbowmango. For more information see the Kubernetes Code Review Process.

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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

ObjectMeta: metav1.ObjectMeta{
Name: "test-namespace",
Labels: map[string]string{
"overridden": "false",
},
Copy link
Contributor Author

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

Copy link
Member

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.

Comment on lines +66 to +72
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())
}
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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?

@RainbowMango
Copy link
Member

In kubernetes issue kubernetes/client-go#541 talked about typed client , looks like these guys won't fix this.

By the way, the issue tracks this as well kubernetes/kubernetes#80609

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants