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
Resource Library #301
Resource Library #301
Conversation
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: ellenkorbes Assign the PR to them by writing 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 |
/assign @apelisse |
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.
This is great! Very clean. Thank you!
pkg/framework/resource/doc.go
Outdated
|
||
The following (admittedly ludicrous) example adds a filter to the example above that excludes all resources that do not start with the letter "n": | ||
|
||
type letterN struct { |
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.
Yeah, there is no reason not to have this as a compiled example.
pkg/framework/resource/doc.go
Outdated
|
||
For example: | ||
|
||
p := resource.NewParser(resources, discovery, apiGroup, apiVersion) |
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.
Same, I think this could be compiled. We can't test the output, but we can make sure it compiles.
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.
Since the examples won't pass testing, isn't that possibly gonna create a problem for us later on?
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.
if you don't have the // Output:
comment in your example, they won't run, they won't fail. They will be compiled to make sure that they are up-to-date though (which is still valuable to us).
pkg/framework/resource/doc.go
Outdated
} | ||
|
||
func (*letterN) Resource(r *resource.Resource) bool { | ||
return string(r.Resource.Name[0]) == "n" |
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'd be weird to have an empty resource name, but that could technically crash ;-)
pkg/framework/resource/doc.go
Outdated
type letterN struct { | ||
} | ||
|
||
func (*letterN) Resource(r *resource.Resource) bool { |
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.
Doesn't need to be pointer.
pkg/framework/resource/doc.go
Outdated
if err != nil { | ||
panic(err) | ||
} | ||
r = r.Filter(&letterN{}) |
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.
Doesn't have to be address (if you remove the two pointers above)
pkg/framework/resource/parse.go
Outdated
|
||
// resource returns the resource name and true if it is a resource (not a subresource). | ||
// It returns the resource name and false it if it is a subresource. | ||
func (p *parser) resource(resource *v1.APIResource) (string, bool) { |
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.
naming return parameters?
pkg/framework/resource/parse.go
Outdated
} | ||
|
||
// isGroupVersionMatch returns false if either group or version doesn't match with the API. | ||
func (p *parser) isGroupVersionMatch(group, version string) bool { |
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'm wondering why we need that? If we could get rid of that, that'd simplify the creation of the parser. @pwittrock
|
||
// Resource is an API Resource. | ||
type Resource struct { | ||
Resource v1.APIResource |
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 think we'd rather make these private by default, and change them to public as needed.
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.
You sure? If I were to do this it'd break my filter examples (they depend on r.Resource.Name).
I tried it anyway and the IDE gave me:
Rename failed: /home/ellen/go/src/k8s.io/kubectl/pkg/framework/resource/resource.go:26:6: renaming this type "Resource" to "resource" would make it unexported /home/ellen/go/src/k8s.io/kubectl/pkg/framework/resource/poc/main.go:38:38: breaking references from packages such as "k8s.io/kubectl/pkg/framework/resource/poc" [...]
I did it by hand then and indeed it broke everything.
undefined: "k8s.io/kubectl/pkg/framework/resource".Resource
There's probably a way to do the filtering differently, but do we really have to?
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.
Sorry if I was unclear. By "these", I meant "all these fields". If this one in particular is needed, let's keep it public, what about the others?
|
||
// SubResource is an API subresource. | ||
type SubResource struct { | ||
Resource v1.APIResource |
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.
public -> private?
pkg/framework/resource/parse.go
Outdated
} | ||
|
||
// indexResources indexes into maps the resources from the API resource list. It returns a map indexed by resource name, and a map indexed by GroupVersionResource objects. | ||
func (p *parser) indexResources(gvs []*v1.APIResourceList) (map[string][]*Resource, map[schema.GroupVersionResource]*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.
Should the type be "Resources" here (first returned type)?
Yeah, you should run |
pkg/framework/resource/parse.go
Outdated
return true | ||
} | ||
|
||
// getOpenAPI retrieves a schema object from the API based on a GroupVersionResource triplet. |
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'm not sure why this returns the Schema and bool. It's safe and sufficient to check if Schema is nil.
This is great. Please fix the test so that we can merge it! Thanks! |
b88a3e6
to
50140c8
Compare
/retest |
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.
Just had time for a cursory look, but I think it's pretty good. I have a bunch of nit, but I don't want these to block.
tst "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing" | ||
) | ||
|
||
var fakeSchema = tst.Fake{Path: filepath.Join("..", "..", "..", "..", "..", "api", "openapi-spec", "swagger.json")} |
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.
This can't work :-/
pkg/framework/openapi/testing/BUILD
Outdated
@@ -0,0 +1,33 @@ | |||
package(default_visibility = ["//visibility:public"]) |
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 think you can drop these files in general (bazel)
@@ -0,0 +1,141 @@ | |||
/* |
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'm not sure you need this package at all.
pkg/framework/resource/resource.go
Outdated
Resource v1.APIResource | ||
apiGroupVersion schema.GroupVersion | ||
schema openapi.Schema | ||
SubResources []*SubResource |
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.
Try to sort public and then private fields for readability
b596d8a
to
d79f38d
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.
Good job Ellen! Thank you so much 🎆
Package resource implements tools for the discovery and filtering of resources in an API server.
Tests to be implemented in a subsequent PR.