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 application/vnd.kubernetes.protobuf as content-type if possible #1149

Merged

Conversation

FillZpp
Copy link
Contributor

@FillZpp FillZpp commented Sep 2, 2020

Signed-off-by: Siyu Wang FillZpp.pub@gmail.com

Use application/vnd.kubernetes.protobuf as content-type if it is possible, currently for those native resources defined in client-go.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Sep 2, 2020
@k8s-ci-robot
Copy link
Contributor

Welcome @FillZpp!

It looks like this is your first PR to kubernetes-sigs/controller-runtime 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/controller-runtime has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot
Copy link
Contributor

Hi @FillZpp. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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.

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Sep 2, 2020
@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Sep 2, 2020
@FillZpp
Copy link
Contributor Author

FillZpp commented Sep 2, 2020

/assign @alvaroaleman

@@ -93,5 +102,8 @@ func createRestConfig(gvk schema.GroupVersionKind, baseConfig *rest.Config) *res
if cfg.UserAgent == "" {
cfg.UserAgent = rest.DefaultKubernetesUserAgent()
}
if cfg.ContentType == "" && localScheme.Recognizes(gvk) {
Copy link
Member

Choose a reason for hiding this comment

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

So you copy the client-go scheme so this is only used for built-in types? Are you sure protobuf works for all built-in types on all kube server versions? Does upstream have any docs on this?

Copy link
Member

Choose a reason for hiding this comment

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

Do you know what happens if we request protobuf for a type that doesn't support it, is there maybe some way to negotiate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So you copy the client-go scheme so this is only used for built-in types?

Yes, this is only used for built-in resources. For custom resources, CRDs can not support Protocol Buffers but Aggregated API can. You can see it in this table.

Are you sure protobuf works for all built-in types on all kube server versions? Does upstream have any docs on this?

All built-in resources are guaranteed to implement Protocal Buffers:

  • This design doc for contributors says protobuf schema for each API group and version in k8s.io/api will be generated from the objects in that API group and version.
  • hack/update-generated-protobuf.sh in Kubernetes repo will find all packages contains +k8s:protobuf-gen=package and generate protobuf schema for them. And all API definition package must contain a doc.go which has these comments:
// +k8s:deepcopy-gen=package
// +k8s:protobuf-gen=packag

Copy link
Contributor

Choose a reason for hiding this comment

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

missed this comment. I think this should probably be a bit less magic, since this ignores aggregated api servers or any future improvements to CRDs. This is probably fine in the short run, but in the long run I think we want to check discovery or something to make sure that this is actually true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Understand, but currently there is no way to check if a resource type supports protobuf or not. I have added a TODO to describe this.

@@ -93,5 +102,8 @@ func createRestConfig(gvk schema.GroupVersionKind, baseConfig *rest.Config) *res
if cfg.UserAgent == "" {
cfg.UserAgent = rest.DefaultKubernetesUserAgent()
}
if cfg.ContentType == "" && localScheme.Recognizes(gvk) {
cfg.ContentType = "application/vnd.kubernetes.protobuf"
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Can you use runtime.ContentTypeProtobuf here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

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

/approve
/hold
/assign @DirectXMan12

@k8s-ci-robot k8s-ci-robot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Sep 14, 2020
@alvaroaleman
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Sep 14, 2020
@FillZpp FillZpp force-pushed the use-protobuf-for-native-resources branch 2 times, most recently from 9077fb6 to a8946c3 Compare September 15, 2020 04:59
@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 Sep 15, 2020
@FillZpp FillZpp force-pushed the use-protobuf-for-native-resources branch from a8946c3 to 5a22a5f Compare September 15, 2020 08:21
)

func init() {
_ = clientgoscheme.AddToScheme(localScheme)
Copy link
Contributor

Choose a reason for hiding this comment

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

this should not be using some sort of magic private scheme -- we should be using the user's passed-in scheme. That way, if the user also defines protobuf support for their types (e.g. aggregated api servers, if we get proto support in the future for CRDs, etc) it'll just work.

EDIT: missed the comment above, responding up there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

Choose a reason for hiding this comment

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

Nit: Can we panic here if this fails?

_, isUnstructuredList := obj.(*unstructured.UnstructuredList)
isUnstructured = isUnstructured || isUnstructuredList

resourceByType := c.structuredResourceByType
Copy link
Contributor

Choose a reason for hiding this comment

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

move this so that it's under the lock

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@FillZpp FillZpp force-pushed the use-protobuf-for-native-resources branch 4 times, most recently from 9176b46 to 3abb8c6 Compare October 14, 2020 12:50
Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

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

Some nits, other than that it looks good. Also this change is breaking due to the signature change of RESTClientForGVK.

)

func init() {
_ = clientgoscheme.AddToScheme(localScheme)
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Can we panic here if this fails?

"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
)

var (
localScheme = runtime.NewScheme()
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Can we rename to "protobufScheme"? The current name is a bit too generic IMHO

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All done

@FillZpp FillZpp force-pushed the use-protobuf-for-native-resources branch from 3abb8c6 to 00b4b34 Compare October 19, 2020 10:07
Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

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

/lgtm
leaving hold cancel to @DirectXMan12

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 19, 2020
@alvaroaleman
Copy link
Member

and thanks for your work, this is a great change!

Copy link
Member

@vincepri vincepri left a comment

Choose a reason for hiding this comment

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

LGTM
/milestone v0.7.x

@k8s-ci-robot k8s-ci-robot added this to the v0.7.x milestone Oct 19, 2020
@FillZpp
Copy link
Contributor Author

FillZpp commented Oct 23, 2020

PTAL @DirectXMan12 when you have time

@alvaroaleman
Copy link
Member

Retitling because this PR includes a breaking change in RESTClientForGVK
/retitle ⚠️ Use application/vnd.kubernetes.protobuf as content-type if possible

@k8s-ci-robot k8s-ci-robot changed the title ✨ Use application/vnd.kubernetes.protobuf as content-type if possible ⚠️ Use application/vnd.kubernetes.protobuf as content-type if possible Oct 23, 2020
@vincepri
Copy link
Member

Are we ok unholding this change? @alvaroaleman @DirectXMan12

"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
)

var (
protobufScheme = runtime.NewScheme()
Copy link
Member

Choose a reason for hiding this comment

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

Actually one nit: Can we add a setter for this so projects that have additional apis that do support protobuf (metrics-server, all the openshift apis that are served via its aggregated apiserver) can add themselves here?

Copy link
Member

Choose a reason for hiding this comment

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

I am also fine doing a quick follow-up for that and merging this PR as-is

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually one nit: Can we add a setter for this so projects that have additional apis that do support protobuf (metrics-server, all the openshift apis that are served via its aggregated apiserver) can add themselves here?

Sure. How about provide a function in pkg/client/apiutil/apimachinery.go?

func AdditionalProtobufScheme(builder runtime.SchemeBuilder) error {
    return builder.AddToScheme(protobufScheme)
}

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good to me. Could you also add a small godoc comment explaining it?

Copy link
Member

Choose a reason for hiding this comment

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

How about AddToProtobufScheme to make the experience familiar with the other AddToScheme methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have add this func and a RWMutex to avoid race modification of protobufScheme.
PTAL @alvaroaleman @vincepri

Signed-off-by: Siyu Wang <FillZpp.pub@gmail.com>
@FillZpp FillZpp force-pushed the use-protobuf-for-native-resources branch from 00b4b34 to 7c26bc0 Compare November 24, 2020 03:33
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 24, 2020
@k8s-ci-robot
Copy link
Contributor

@FillZpp: The following test failed, say /retest to rerun all failed tests:

Test name Commit Details Rerun command
pull-controller-runtime-apidiff-master 7c26bc0 link /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.

Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 24, 2020
Copy link
Member

@vincepri vincepri left a comment

Choose a reason for hiding this comment

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

/approve

@alvaroaleman good to unhold?

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alvaroaleman, FillZpp, vincepri

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:
  • OWNERS [alvaroaleman,vincepri]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@alvaroaleman
Copy link
Member

/hold cancel

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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants