Skip to content
This repository has been archived by the owner on Mar 13, 2022. It is now read-only.

Support customizing “Accept” header #1428 #236

Merged
merged 1 commit into from
May 7, 2021

Conversation

Yashks1994
Copy link
Contributor

@Yashks1994 Yashks1994 commented Apr 27, 2021

What this PR does / why we need it:

We are adding the new header parameter in the dynamic client which helps in scenarios where the user only cares about object metadata and wants to reduce the client-side deserialisation time.

This changes will accept the header like 'application/json;as=PartialObjectMetadata;v=v1;g=meta.k8s.io' or 'application/json;as=Table;v=v1;g=meta.k8s.io'.

Which issue(s) this PR fixes:

Fixes #1428

Special notes for your reviewer:

Added the metadata field which is currently present in client-go. Now this feature will also be supporting in the python-client.

Does this PR introduce a user-facing change?

The dynamic client now supports customizing http "Accept" header through the `header_params` parameter, which can be used to customizing API server response, e.g. retrieving object metadata only. 

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added the do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. label Apr 27, 2021
@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


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-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Apr 27, 2021
@k8s-ci-robot
Copy link
Contributor

Welcome @Yashks1994!

It looks like this is your first PR to kubernetes-client/python-base 🎉. 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-client/python-base 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 k8s-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Apr 27, 2021
dynamic/client.py Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Apr 28, 2021
@codecov-commenter
Copy link

codecov-commenter commented Apr 28, 2021

Codecov Report

Merging #236 (3c488d0) into master (8a969ee) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #236   +/-   ##
=======================================
  Coverage   92.73%   92.73%           
=======================================
  Files          13       13           
  Lines        1692     1692           
=======================================
  Hits         1569     1569           
  Misses        123      123           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8a969ee...3c488d0. Read the comment docs.

])
if params.get('application/vnd.kubernetes.protobuf') is not None:
header_params['Accept'] = self.client.select_header_accept([
'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.

the Python client doesn't understand protobuf. What we want is g=meta.k8s.io;v=v1,application/json;as=PartialObjectMetadata

Copy link
Contributor

Choose a reason for hiding this comment

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

that's right. it's also why it's good to have a test to verify it works as expected

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Apr 29, 2021
@Yashks1994 Yashks1994 force-pushed the header-patch1 branch 2 times, most recently from 51e9bb8 to b0aae73 Compare April 30, 2021 08:14
@Yashks1994
Copy link
Contributor Author

Yashks1994 commented Apr 30, 2021

Hi @roycaihw, @yliaog
In order to accept the header params = 'PartialObjectMetadata', I have added a condition which is described below:

  1. First it is checking whether the header_params is not None. If it is not None then, next it will check if PartialObjectMetadata is present or not. Based on that it will set the header_params['Accept'] to params.
  2. If the header_params is None or {} then it will set the header to default i.e.. application/json.

I have added test case which is accepting the params as 'PartialObjectMetadata' and it is checking whether the kind is equal to the params or not.

dynamic/client.py Outdated Show resolved Hide resolved
'application/yaml',
])
if params.get('header_params') is not None:
partial_object = params.get('header_params')
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto, header_params can be used, instead of partial_object here

if params.get('header_params') is not None:
partial_object = params.get('header_params')
if 'PartialObjectMetadata' in partial_object.get('Accept'):
header_params['Accept'] = 'application/json;as=PartialObjectMetadata;v=v1;g=meta.k8s.io'
Copy link
Contributor

Choose a reason for hiding this comment

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

better to just take whatever is passed, instead of overwriting it here, i think. so if header_params['Accept'] is present, then do nothing, otherwise, set the default ['application/json', 'application/yaml']

Copy link
Member

Choose a reason for hiding this comment

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

+1 to what Yu said. Let's make the dynamic client dynamic. I'm interested to see how this feature will work with things like as=Table (what kubectl uses)

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels Apr 30, 2021
dynamic/test_client.py Outdated Show resolved Hide resolved
@roycaihw
Copy link
Member

After this is merged and included in the main repo, we should have an example for this feature.

/kind feature

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed needs-kind Indicates a PR lacks a `kind/foo` label and requires one. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Apr 30, 2021
@roycaihw
Copy link
Member

roycaihw commented May 6, 2021

/lgtm
/approve
Thanks!

@k8s-ci-robot k8s-ci-robot added the lgtm Indicates that a PR is ready to be merged. label May 6, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: roycaihw, Yashks1994

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 added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 6, 2021
@k8s-ci-robot k8s-ci-robot removed the lgtm Indicates that a PR is ready to be merged. label May 6, 2021
header_params['Accept'] = self.client.select_header_accept([

# User didn't specify the Accept header. Default it to json and yaml.
if not 'Accept' in header_params.keys():
Copy link
Member

Choose a reason for hiding this comment

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

you don't need .keys(), right?

Copy link
Contributor Author

@Yashks1994 Yashks1994 May 6, 2021

Choose a reason for hiding this comment

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

Yes we can remove the .keys() its not much appropriate. I will make the change.

@roycaihw
Copy link
Member

roycaihw commented May 6, 2021

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm Indicates that a PR is ready to be merged. label May 6, 2021
@yliaog
Copy link
Contributor

yliaog commented May 6, 2021

/hold

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label May 6, 2021
@yliaog
Copy link
Contributor

yliaog commented May 6, 2021

feel free to remove the hold once the case sensitivity is fixed

@k8s-ci-robot k8s-ci-robot removed the lgtm Indicates that a PR is ready to be merged. label May 6, 2021
header_params['Accept'] = self.client.select_header_accept([

# User didn't specify the Accept header. Default it to json and yaml.
if not 'Accept' in header_params:
Copy link
Member

@roycaihw roycaihw May 6, 2021

Choose a reason for hiding this comment

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

my bad. Header names are not case sensitive, so it should be:

if not 'Accept' in header_params or not 'accept' in header_params:

[edit] the above is wrong as well. We should allow things like aCCEpt

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 run both by passing 'Accept' and 'accept', it ran fine.

{'Accept': 'application/json;as=PartialObjectMetadataList;v=v1;g=meta.k8s.io'}
PartialObjectMetadataList
meta.k8s.io/v1

{'accept': 'application/json;as=PartialObjectMetadataList;v=v1;g=meta.k8s.io'}
PartialObjectMetadataList
meta.k8s.io/v1 

Still I will make the necessary change.

@roycaihw
Copy link
Member

roycaihw commented May 6, 2021

I think the next steps are:

  1. find a solution to do case insensitive search for a key in dict. There are some options in https://stackoverflow.com/questions/3296499/case-insensitive-dictionary-search
  2. add more test cases to make sure things like Accept accept aCCEpt etc work

@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 May 6, 2021
# Checking Accept header if True pass the user header else default it to json and yaml.
new_header_params = dict((key.lower(), value) for key, value in header_params.items())
if 'accept' in new_header_params:
header_params['Accept'] = new_header_params['accept']
Copy link
Member

Choose a reason for hiding this comment

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

if a user passes in an "accept" header, this will result in duplicated "Accept" and "accept" headers. You can simply do

if not 'accept' in new_header_params:
    header_params['Accept'] = self.client.select_header_accept([
        'application/json',
        'application/yaml',
    ])

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

@roycaihw
Copy link
Member

roycaihw commented May 7, 2021

/lgtm
/hold cancel

@k8s-ci-robot k8s-ci-robot added lgtm Indicates that a PR is ready to be merged. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. labels May 7, 2021
@k8s-ci-robot k8s-ci-robot merged commit b4d3aad into kubernetes-client:master May 7, 2021
@Yashks1994 Yashks1994 deleted the header-patch1 branch May 7, 2021 02:57
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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. kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. 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