-
Notifications
You must be signed in to change notification settings - Fork 39.7k
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
Fixes fake client generation for non-namespaced subresources #60445
Fixes fake client generation for non-namespaced subresources #60445
Conversation
02f30dc
to
57259db
Compare
@@ -95,6 +105,19 @@ func NewListSubresourceAction(resource schema.GroupVersionResource, name, subres | |||
return action | |||
} | |||
|
|||
func NewRootListSubresourceAction(resource schema.GroupVersionResource, name, subresource string, kind schema.GroupVersionKind, opts interface{}) ListActionImpl { |
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.
how do you list a subresource? subresources are always under a single resource
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.
Good question 🤔. The only reason I added this was because the template was wrong. I can remove it as well. Should I do that in this PR or a separate one?
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.
here please.
@@ -327,7 +330,7 @@ var listSubresourceTemplate = ` | |||
func (c *Fake$.type|publicPlural$) List($.type|private$Name string, opts $.ListOptions|raw$) (result *$.resultType|raw$List, err error) { |
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.
listSubresourceTemplate seems questionable, since subresource is always on a get
@@ -423,7 +426,7 @@ var createSubresourceTemplate = ` | |||
func (c *Fake$.type|publicPlural$) Create($.type|private$Name string, $.inputType|private$ *$.inputType|raw$) (result *$.resultType|raw$, err error) { | |||
obj, err := c.Fake. | |||
$if .namespaced$Invokes($.NewCreateSubresourceAction|raw$($.type|allLowercasePlural$Resource, $.type|private$Name, "$.subresourcePath$", c.ns, $.inputType|private$), &$.resultType|raw${}) | |||
$else$Invokes($.NewRootCreateAction|raw$($.inputType|allLowercasePlural$Resource, $.inputType|private$), &$.resultType|raw${})$end$ | |||
$else$Invokes($.NewRootCreateSubresourceAction|raw$($.type|allLowercasePlural$Resource, "$.subresourcePath$", $.inputType|private$), &$.inputType|raw${})$end$ |
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.
s/&$.inputType|raw${})$end$
/&$.resultType|raw${})$end$
?
@@ -327,7 +330,7 @@ var listSubresourceTemplate = ` | |||
func (c *Fake$.type|publicPlural$) List($.type|private$Name string, opts $.ListOptions|raw$) (result *$.resultType|raw$List, err error) { | |||
obj, err := c.Fake. | |||
$if .namespaced$Invokes($.NewListSubresourceAction|raw$($.type|allLowercasePlural$Resource, $.type|private$Name, "$.subresourcePath$", $.type|allLowercasePlural$Kind, c.ns, opts), &$.resultType|raw$List{}) | |||
$else$Invokes($.NewRootListAction|raw$($.type|allLowercasePlural$Resource, $.type|allLowercasePlural$Kind, opts), &$.resultType|raw$List{})$end$ | |||
$else$Invokes($.NewRootListSubresourceAction|raw$($.type|allLowercasePlural$Resource, $.type|private$Name), &$.resultType|raw${})$end$ |
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.
missed $List{}
in the end. but agree with @liggitt, subresource doesn't support list
/cc @nikhita |
@jhorwit2 can you update the PR so we can get it in? |
I'll get to it today @sttts! Sorry about the delay. |
57259db
to
b284718
Compare
@@ -449,7 +434,7 @@ var updateSubresourceTemplate = ` | |||
func (c *Fake$.type|publicPlural$) Update($.type|private$Name string, $.inputType|private$ *$.inputType|raw$) (result *$.resultType|raw$, err error) { | |||
obj, err := c.Fake. | |||
$if .namespaced$Invokes($.NewUpdateSubresourceAction|raw$($.type|allLowercasePlural$Resource, "$.subresourcePath$", c.ns, $.inputType|private$), &$.inputType|raw${}) | |||
$else$Invokes($.NewRootUpdateAction|raw$($.type|allLowercasePlural$Resource, $.type|private$), &$.type|raw${})$end$ | |||
$else$Invokes($.NewRootUpdateSubresourceAction|raw$($.type|allLowercasePlural$Resource, "$.subresourcePath$", $.inputType|private$), &$.resultType|raw${})$end$ |
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.
could you explain why the namespaced one (the line above) and the non-namespaced one (this line) differ at the end (&$.inputType|raw${})
vs &$.resultType|raw${})
)?
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.
it's a bug :)
It should be fixed in another PR
Because the result type is always the same as the input type currently, so although it's not technically correct it won't break until you have a scenario where the update call returns a different object.
Ex:
//+genclient:method=UpdateScale,verb=update,subresource=scale,input=k8s.io/kubernetes/pkg/apis/autoscaling.Scale,result=k8s.io/kubernetes/pkg/apis/autoscaling.Scale
Is anyone aware of a way I can add some tests for this? This is my first time messing w/ the code gen and don't see anything obvious. |
@jhorwit2 usually the are changes in the generated code. Why not here? |
@sttts Nothing in core uses a cluster scoped object with subresources besides /status which has special generation code (still not sure why but that's another convo). The reason I encountered this is because I have a cluster scoped custom object that I wanted to have /scale subresource. |
@jhorwit2 can you add an example to https://github.com/kubernetes/code-generator/tree/master/_examples ? |
b284718
to
b60c32a
Compare
@sttts Done! I added a cluster scoped resource with a scale sub-resource. |
Can you squash? |
b60c32a
to
0f62a8c
Compare
Done. |
@@ -146,9 +146,10 @@ func (g *genFakeForType) GenerateType(c *generator.Context, t *types.Type, w io. | |||
"NewRootWatchAction": c.Universe.Function(types.Name{Package: pkgClientGoTesting, Name: "NewRootWatchAction"}), | |||
"NewWatchAction": c.Universe.Function(types.Name{Package: pkgClientGoTesting, Name: "NewWatchAction"}), | |||
"NewCreateSubresourceAction": c.Universe.Function(types.Name{Package: pkgClientGoTesting, Name: "NewCreateSubresourceAction"}), | |||
"NewRootCreateSubresourceAction": c.Universe.Function(types.Name{Package: pkgClientGoTesting, Name: "NewRootCreateSubresourceAction"}), |
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 we use the term "root" elsewhere for cluster wide resources? I think it's either "cluster" or "NoneNamespaced".
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.
@sttts I was following the convention that already existed in the generation code. I'd prefer handling a rename like that in another PR.
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.
Right, "Root" is already used. So it's fine.
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.
To answer your question though, i've never seen it used else where in the code. I was a little confused at first :)
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: jhorwit2, sttts The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/retest Review the full test history for this PR. Silence the bot with an |
@jhorwit2: The following test failed, say
Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here. |
/retest Review the full test history for this PR. Silence the bot with an |
Automatic merge from submit-queue (batch tested with PRs 63291, 63490, 60445, 63507, 63524). If you want to cherry-pick this change to another branch, please follow the instructions here. |
What this PR does / why we need it:
Fixes code generation for non-namespaced subresources fake clients.
Which issue(s) this PR fixes (optional, in
fixes #<issue number>(, fixes #<issue_number>, ...)
format, will close the issue(s) when PR gets merged):Fixes #60444
Special notes for your reviewer:
Release note:
/cc @mfojtik @liggitt
I'm not sure the best way to add tests for this. Any pointers?