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 |
This is great! Very clean. Thank you! |
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 { |
apelisse
Feb 17, 2018
Member
Yeah, there is no reason not to have this as a compiled example.
Yeah, there is no reason not to have this as a compiled example.
For example: | ||
p := resource.NewParser(resources, discovery, apiGroup, apiVersion) |
apelisse
Feb 17, 2018
Member
Same, I think this could be compiled. We can't test the output, but we can make sure it compiles.
Same, I think this could be compiled. We can't test the output, but we can make sure it compiles.
ellenkorbes
Feb 20, 2018
Author
Contributor
Since the examples won't pass testing, isn't that possibly gonna create a problem for us later on?
Since the examples won't pass testing, isn't that possibly gonna create a problem for us later on?
apelisse
Feb 20, 2018
Member
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).
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).
} | ||
func (*letterN) Resource(r *resource.Resource) bool { | ||
return string(r.Resource.Name[0]) == "n" |
apelisse
Feb 17, 2018
Member
It'd be weird to have an empty resource name, but that could technically crash ;-)
It'd be weird to have an empty resource name, but that could technically crash ;-)
type letterN struct { | ||
} | ||
func (*letterN) Resource(r *resource.Resource) bool { |
apelisse
Feb 17, 2018
Member
Doesn't need to be pointer.
Doesn't need to be pointer.
if err != nil { | ||
panic(err) | ||
} | ||
r = r.Filter(&letterN{}) |
apelisse
Feb 17, 2018
Member
Doesn't have to be address (if you remove the two pointers above)
Doesn't have to be address (if you remove the two pointers above)
|
||
// 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) { |
apelisse
Feb 17, 2018
Member
naming return parameters?
naming return parameters?
} | ||
|
||
// isGroupVersionMatch returns false if either group or version doesn't match with the API. | ||
func (p *parser) isGroupVersionMatch(group, version string) bool { |
apelisse
Feb 17, 2018
Member
I'm wondering why we need that? If we could get rid of that, that'd simplify the creation of the parser. @pwittrock
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 |
apelisse
Feb 17, 2018
Member
I think we'd rather make these private by default, and change them to public as needed.
I think we'd rather make these private by default, and change them to public as needed.
ellenkorbes
Feb 20, 2018
Author
Contributor
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?
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?
apelisse
Feb 20, 2018
Member
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?
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 |
apelisse
Feb 17, 2018
Member
public -> private?
public -> private?
} | ||
|
||
// 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) { |
apelisse
Feb 17, 2018
Member
Should the type be "Resources" here (first returned type)?
Should the type be "Resources" here (first returned type)?
Yeah, you should run |
return true | ||
} | ||
|
||
// getOpenAPI retrieves a schema object from the API based on a GroupVersionResource triplet. |
apelisse
Feb 17, 2018
Member
I'm not sure why this returns the Schema and bool. It's safe and sufficient to check if Schema is nil.
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
/retest |
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")} |
apelisse
Feb 27, 2018
Member
This can't work :-/
This can't work :-/
@@ -0,0 +1,33 @@ | |||
package(default_visibility = ["//visibility:public"]) |
apelisse
Feb 27, 2018
Member
I think you can drop these files in general (bazel)
I think you can drop these files in general (bazel)
@@ -0,0 +1,141 @@ | |||
/* |
apelisse
Feb 27, 2018
Member
I'm not sure you need this package at all.
I'm not sure you need this package at all.
Resource v1.APIResource | ||
apiGroupVersion schema.GroupVersion | ||
schema openapi.Schema | ||
SubResources []*SubResource |
apelisse
Feb 27, 2018
Member
Try to sort public and then private fields for readability
Try to sort public and then private fields for readability
b596d8a
to
d79f38d
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.