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

Use proto only if we have proto types #1638

Closed
wants to merge 1 commit into from

Conversation

justinsb
Copy link
Contributor

✨ WIP: Use proto only if we have proto definitions

Even if the apiserver supports proto for a type, if we don't know the
proto schema definition we can't use it.

Therefore, detect if the type implements proto.Message when deciding
whether to use proto encoding.

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 20, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: justinsb
To complete the pull request process, please assign vincepri after the PR has been reviewed.
You can assign the PR to them by writing /assign @vincepri in a comment when ready.

The full list of commands accepted by this bot can be found 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 size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Aug 20, 2021
@justinsb justinsb force-pushed the use_proto_if_proto branch 2 times, most recently from 513e6cc to 8067a1d Compare August 20, 2021 15:39
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Aug 20, 2021
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Aug 20, 2021
if gvkType.Kind() == reflect.Struct {
gvkType = reflect.PtrTo(gvkType)
}
return gvkType.Implements(protoMessageType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this slow enough that we should cache it? I know reflect operations can get pretty cranky sometimes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It did involve a linear scan, so I added memoization - thanks!

if gvkType.Kind() == reflect.Struct {
gvkType = reflect.PtrTo(gvkType)
}
return gvkType.Implements(protoMessageType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid the gvk type has proto definition in scheme doesn't mean the API Server could accept its requests via Protobuf. Actually for a gvk type defined by CRD, API Server can only accept JSON requests even if we implement its proto types in project.

Copy link
Contributor Author

@justinsb justinsb Aug 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if there's a way to discover if the apiserver supports proto (for a given kind)? I think we need destination-type-is-proto and apiserver-supports-proto-for-this-kind, because if someone queries a PodList into an UnstructuredList we still want to not use proto (?).

BTW the reason that I'm looking at this code is that I'd like to eliminate most of the imports of the kubernetes API types, for controllers that generally just use unstructured.Unstructured.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can see this PR Use application/vnd.kubernetes.protobuf as content-type if possible I merged before, which brings Protobuf support into controller-runtime.

// Currently only enabled for built-in resources which are guaranteed to implement Protocol Buffers.
// For custom resources, CRDs can not support Protocol Buffers but Aggregated API can.
// See doc: https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/#advanced-features-and-flexibility

So if you want to extend the no-built-in types to use Protobuf, you have to find out whether they are defined by CRD or Aggregated API, which is hard to check. Since all types created via kubebuilder/operator-sdk are in CRD definition, I think the built-in types are enough for most ppl using controller-runtime.

WDYT @alvaroaleman

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I guess it is possible (and something I would love to try out!) to use proto to define CRDs locally.

I tweaked the logic therefore, there's a list of built-in types, and we have a unit test to make sure that the core kubernetes types are recognized appropriately. This should detect if there's a new built-in library added upstream, and I added a comment so that it's obvious how to add the newly-added built-in group.

This also means that we can continue to support AddToProtobufScheme.

@alvaroaleman
Copy link
Member

@justinsb what problem does this PR solve? I agree it is currently a bit hard to discover how to register your api for proto usage, however the only case where this could be wanted is an extension api server that supports proto. To my knowledge, there aren't a lot of them, so this is a bit of a niche problem.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 26, 2021
@justinsb justinsb changed the title WIP: Use proto if we have proto types Use proto only if we have proto types Nov 13, 2021
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 13, 2021
@justinsb justinsb force-pushed the use_proto_if_proto branch 2 times, most recently from 602bb3c to 0780d4f Compare November 13, 2021 22:34
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 13, 2021
@justinsb
Copy link
Contributor Author

justinsb commented Nov 13, 2021

Thanks for the reviews and comment @alvaroaleman and @coderanger . The intention here is actually the other way round; I want to eliminate all dependencies on kubernetes/scheme. Merely importing that package pulls in all the k8s API types, and that adds up to a lot more memory usage than is really needed (>500 entries in the scheme, instead of around a dozen that are really needed in a typical controller). It also causes the binary size to be much larger, though that is a secondary concern for me.

This is by no means the last place we need this, but ... it's a journey!

We should not try to use proto if the scheme does not have the
protobuf representation in the go type; we won't be able to
deserialize it.
@k8s-ci-robot
Copy link
Contributor

k8s-ci-robot commented Nov 13, 2021

@justinsb: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-controller-runtime-apidiff-master 9f1aac0 link false /test pull-controller-runtime-apidiff-master

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.

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Feb 11, 2022
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Mar 14, 2022
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue or PR with /reopen
  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

@k8s-ci-robot
Copy link
Contributor

@k8s-triage-robot: Closed this PR.

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue or PR with /reopen
  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants