-
Notifications
You must be signed in to change notification settings - Fork 38.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
OpenAPIResourcesGetter allows lazy-loading OpenAPI V2 #116565
Conversation
/priority important-soon |
/cc @apelisse |
/retest |
/assign @apelisse |
@seans3 Can anyone else review this? |
I'm happy reviewing this, and did, but my comment is unanswered. I think @seans3 has switched to working on websocket and put that on the backburner. I'll try to re-open it when I find a chance. |
thanks @apelisse |
@@ -154,13 +154,8 @@ func (f *factoryImpl) Validator(validationDirective string) (validation.Schema, | |||
return validation.NullSchema{}, nil | |||
} | |||
|
|||
resources, err := f.OpenAPISchema() |
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.
Creation of the factory
does not download the Open API V2 now; the Open API V2 resources
are only downloaded when they are actually used.
Expect(err).ToNot(HaveOccurred()) | ||
validator = NewSchemaValidation(resources) | ||
validator = NewSchemaValidation(&fakeResourcesGetter{doc: doc}) |
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.
That test does nothing though. Can we add a test that shows the fakeResourceGetter isn't called at all, maybe with a PanicingResourceGetter?
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.
we would have to create a validator for that, that might work.
@seans3 I noticed some instances of OpenAPISchema() have not been updated to be loaded lazily, eg: https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go#L108. Does this PR plan to address all the invocations as well? I see three other instances of |
This PR does not affect those naked calls returning openapi resources. This PR will only address lazy-loading the openapi resources within the |
6e0d3bd
to
983ca55
Compare
/cc @Jefftree |
Currently, the only way to run apply without downloading the OpenAPI v2 is to do:
That is because the validation greedily wants the OpenAPI v2, and so does strategic-merge-patch. This change is specifically about the validation. Jeff, I think it's fine if you add the other part to your pr (#120707) |
983ca55
to
fe3ab71
Compare
resources, err := openapi.NewOpenAPIData(s) | ||
Expect(err).ToNot(HaveOccurred()) | ||
validator = NewSchemaValidation(resources) | ||
validator = NewSchemaValidation(&fakeResourcesGetter{}) |
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, I think if the fakeResourceGetter would fail on OpenAPISchema()
, then this test is probably just fine.
/sig cli |
/triage accepted |
// OpenAPIResourcesGetter represents a function to return | ||
// OpenAPI V2 resource specifications. Used for lazy-loading | ||
// these resource specifications. | ||
type OpenAPIResourcesGetter interface { |
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.
Can we put this in a common package so smp can also use it?
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 interface now lives in the k8s.io/kubectl/pkg/util/openapi
package (same as Resources
).
Why not bundle the invocation changes together? If we're introducing the interface for lazy fetching it would make sense to have all invocations of it be updated? This affects not just apply, but diff and explain as well. The changes would be almost identical to how the validator was updated https://github.com/kubernetes/kubernetes/pull/116565/files#diff-7cdf779b345ced699424395b0bf817e34af2a9fe68f9f6cd2c6e021d1de1d33eR56-R66 |
fe3ab71
to
c1f3be2
Compare
} | ||
} | ||
|
||
// retrieveResources gets (once) and caches the openapi.Resources, and | ||
// the error condition for the retrieval. | ||
func (v *schemaValidation) retrieveResources() (openapi.Resources, 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.
The code in the factory is not great, but I think this is actually already done in the factory, no?
https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/kubectl/pkg/util/openapi/openapi_getter.go#L56-L63
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.
Yes. You're right. We don't need to cache the Resources
, since they're cached already in the OpenAPIParser
(which is used for the OpenAPISchema()
call in the factory. We can remove this, and do a simple call on the OpenAPIResourcesGetter#OpenAPISchema()
.
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.
...the entire lazy-load mechanism is dependent on the implementation of the OpenAPISchema()
within factory_client_access.go
. Resources
are cached here in OpenAPIParser
:
type CachedOpenAPIParser struct {
openAPIClient discovery.OpenAPISchemaInterface
// Cached results
sync.Once
openAPIResources Resources
err error
}
/retest |
1 similar comment
/retest |
c1f3be2
to
6614a29
Compare
/approve |
LGTM label has been added. Git tree hash: 1f610b9b74fe9f5d2784b26cfdbec8f7235f17b9
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: apelisse, seans3 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 |
OpenAPIResourcesGetter
interface, and uses it within theschemaValidation
struct to lazy-load the OpenAPI V2 specifications for validation (using theValidator
from the factory).OpenAPISchema()
implementation infactory_client_access.go
./kind cleanup