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

OpenAPIResourcesGetter allows lazy-loading OpenAPI V2 #116565

Merged
merged 1 commit into from
Oct 30, 2023

Conversation

seans3
Copy link
Contributor

@seans3 seans3 commented Mar 14, 2023

  • Creates OpenAPIResourcesGetter interface, and uses it within the schemaValidation struct to lazy-load the OpenAPI V2 specifications for validation (using the Validator from the factory).
  • Depends on the lazy-load mechanism within the OpenAPISchema() implementation in factory_client_access.go.
  • Can reduce memory consumption, since there are only some validation cases in which the OpenAPI V2 is needed.
  • Helps effort to move from OpenAPI V2 -> V3.

/kind cleanup

NONE

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. release-note-none Denotes a PR that doesn't merit a release note. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Mar 14, 2023
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. area/kubectl sig/cli Categorizes an issue or PR as relevant to SIG CLI. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Mar 14, 2023
@seans3
Copy link
Contributor Author

seans3 commented Mar 14, 2023

/priority important-soon

@k8s-ci-robot k8s-ci-robot added priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. and removed needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Mar 14, 2023
@seans3
Copy link
Contributor Author

seans3 commented Mar 14, 2023

/cc @apelisse

@seans3
Copy link
Contributor Author

seans3 commented Mar 14, 2023

/retest

@dims
Copy link
Member

dims commented Apr 24, 2023

/assign @apelisse

@dims
Copy link
Member

dims commented Jun 6, 2023

@seans3 Can anyone else review this?

@apelisse
Copy link
Member

apelisse commented Jun 6, 2023

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.

@dims
Copy link
Member

dims commented Jun 15, 2023

thanks @apelisse

@@ -154,13 +154,8 @@ func (f *factoryImpl) Validator(validationDirective string) (validation.Schema,
return validation.NullSchema{}, nil
}

resources, err := f.OpenAPISchema()
Copy link
Contributor Author

@seans3 seans3 Oct 17, 2023

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

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?

Copy link
Member

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.

@Jefftree
Copy link
Member

Jefftree commented Oct 24, 2023

@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 f.OpenAPISchema(), one in apply.go, one in diff.go, and one in explain.go.

@seans3
Copy link
Contributor Author

seans3 commented Oct 26, 2023

Sean Sullivan 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 f.OpenAPISchema(), one in apply.go, one in diff.go, and one in explain.go.

This PR does not affect those naked calls returning openapi resources. This PR will only address lazy-loading the openapi resources within the Validator.

@seans3
Copy link
Contributor Author

seans3 commented Oct 26, 2023

/cc @Jefftree

@apelisse
Copy link
Member

apelisse commented Oct 26, 2023

Currently, the only way to run apply without downloading the OpenAPI v2 is to do:

kubectl apply -f some.yaml --validate=false --server-side=true

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)

resources, err := openapi.NewOpenAPIData(s)
Expect(err).ToNot(HaveOccurred())
validator = NewSchemaValidation(resources)
validator = NewSchemaValidation(&fakeResourcesGetter{})
Copy link
Member

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.

@seans3
Copy link
Contributor Author

seans3 commented Oct 26, 2023

/sig cli

@seans3
Copy link
Contributor Author

seans3 commented Oct 26, 2023

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Oct 26, 2023
// OpenAPIResourcesGetter represents a function to return
// OpenAPI V2 resource specifications. Used for lazy-loading
// these resource specifications.
type OpenAPIResourcesGetter interface {
Copy link
Member

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?

Copy link
Contributor Author

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

@Jefftree
Copy link
Member

Currently, the only way to run apply without downloading the OpenAPI v2 is to do:

kubectl apply -f some.yaml --validate=false --server-side=true

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)

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

}
}

// retrieveResources gets (once) and caches the openapi.Resources, and
// the error condition for the retrieval.
func (v *schemaValidation) retrieveResources() (openapi.Resources, error) {
Copy link
Member

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

Copy link
Contributor Author

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().

Copy link
Contributor Author

@seans3 seans3 Oct 27, 2023

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
}

@seans3
Copy link
Contributor Author

seans3 commented Oct 27, 2023

/retest

1 similar comment
@seans3
Copy link
Contributor Author

seans3 commented Oct 27, 2023

/retest

@apelisse
Copy link
Member

/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 Oct 30, 2023
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 1f610b9b74fe9f5d2784b26cfdbec8f7235f17b9

@k8s-ci-robot
Copy link
Contributor

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

@k8s-ci-robot k8s-ci-robot merged commit ef9587c into kubernetes:master Oct 30, 2023
14 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.29 milestone Oct 30, 2023
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. area/kubectl cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. release-note-none Denotes a PR that doesn't merit a release note. sig/cli Categorizes an issue or PR as relevant to SIG CLI. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

5 participants