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

Fixes fake client generation for non-namespaced subresources #60445

Merged

Conversation

jhorwit2
Copy link
Contributor

@jhorwit2 jhorwit2 commented Feb 26, 2018

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:

Fixes fake client generation for non-namespaced subresources

/cc @mfojtik @liggitt

I'm not sure the best way to add tests for this. Any pointers?

@k8s-ci-robot k8s-ci-robot added the release-note Denotes a PR that will be considered when it comes time to generate release notes. label Feb 26, 2018
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Feb 26, 2018
@jhorwit2 jhorwit2 force-pushed the for/upstream/master/60444 branch 2 times, most recently from 02f30dc to 57259db Compare February 26, 2018 19:17
@@ -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 {
Copy link
Member

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

Copy link
Contributor Author

@jhorwit2 jhorwit2 Feb 27, 2018

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?

Copy link
Contributor

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) {
Copy link
Member

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

@mbohlool
Copy link
Contributor

mbohlool commented Mar 1, 2018

/cc @roycaihw @jennybuckley

@@ -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$
Copy link
Member

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$
Copy link
Member

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

@sttts
Copy link
Contributor

sttts commented Apr 6, 2018

/cc @nikhita

@sttts
Copy link
Contributor

sttts commented Apr 6, 2018

@jhorwit2 can you update the PR so we can get it in?

@jhorwit2
Copy link
Contributor Author

jhorwit2 commented Apr 6, 2018

I'll get to it today @sttts! Sorry about the delay.

@@ -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$
Copy link
Member

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${}))?

Copy link
Contributor Author

@jhorwit2 jhorwit2 Apr 6, 2018

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

@jhorwit2
Copy link
Contributor Author

jhorwit2 commented Apr 6, 2018

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.

@sttts
Copy link
Contributor

sttts commented Apr 9, 2018

@jhorwit2 usually the are changes in the generated code. Why not here?

@jhorwit2
Copy link
Contributor Author

jhorwit2 commented Apr 9, 2018

@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.

@sttts
Copy link
Contributor

sttts commented Apr 10, 2018

@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Apr 19, 2018
@jhorwit2
Copy link
Contributor Author

@sttts Done! I added a cluster scoped resource with a scale sub-resource.

@sttts
Copy link
Contributor

sttts commented Apr 26, 2018

Can you squash?

@jhorwit2
Copy link
Contributor Author

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"}),
Copy link
Contributor

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".

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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 :)

@sttts
Copy link
Contributor

sttts commented May 8, 2018

/approve
/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 8, 2018
@k8s-ci-robot
Copy link
Contributor

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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 8, 2018
@fejta-bot
Copy link

/retest
This bot automatically retries jobs that failed/flaked on approved PRs (send feedback to fejta).

Review the full test history for this PR.

Silence the bot with an /lgtm cancel comment for consistent failures.

@k8s-ci-robot
Copy link
Contributor

k8s-ci-robot commented May 8, 2018

@jhorwit2: The following test failed, say /retest to rerun them all:

Test name Commit Details Rerun command
pull-kubernetes-unit 57259db5efe7c909697d313bad6ef4a76a167cf5 link /test pull-kubernetes-unit

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.

@fejta-bot
Copy link

/retest
This bot automatically retries jobs that failed/flaked on approved PRs (send feedback to fejta).

Review the full test history for this PR.

Silence the bot with an /lgtm cancel comment for consistent failures.

@k8s-github-robot
Copy link

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.

@k8s-github-robot k8s-github-robot merged commit 8203d35 into kubernetes:master May 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Code generator generates invalid fake clients for nonnamespaced subresources
8 participants