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

Extend YAML load functionality to *LIST and multi-resources #673

Merged
merged 1 commit into from
Mar 19, 2019

Conversation

micw523
Copy link
Contributor

@micw523 micw523 commented Nov 1, 2018

Fixes #671
This PR addresses #504 #671, which is an extension of #655.

It allows direct loading of YAMLs such as in
https://github.com/kubernetes/kubernetes/blob/master/hack/testdata/multi-resource-yaml.yaml
and
https://github.com/kubernetes/kubernetes/blob/master/hack/testdata/list.yaml

Important changes:

  • The function will no longer return an API object after processing the YAML file due to concerns of partial / complete failure during the creation process.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Nov 1, 2018
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 1, 2018
@micw523
Copy link
Contributor Author

micw523 commented Nov 1, 2018

/cc @roycaihw
/this-is-fine

@k8s-ci-robot
Copy link
Contributor

@micw523: dog image

In response to this:

/cc @roycaihw
/this-is-fine

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.

@micw523
Copy link
Contributor Author

micw523 commented Nov 2, 2018

/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 Nov 2, 2018
@micw523 micw523 force-pushed the yaml-list branch 3 times, most recently from b6b93ec to d52c7ac Compare November 2, 2018 22:46
@micw523
Copy link
Contributor Author

micw523 commented Nov 2, 2018

/hold cancel
Merge conflicts solved

@micw523
Copy link
Contributor Author

micw523 commented Nov 2, 2018

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Nov 2, 2018
Copy link
Member

@roycaihw roycaihw left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Please separate updating python-base submodule into another commit

kubernetes/utils/create_from_yaml.py Outdated Show resolved Hide resolved
kubernetes/utils/create_from_yaml.py Outdated Show resolved Hide resolved
if output_list is False:
if len(k8s_api_all) == 1:
return k8s_api_all[0]
return k8s_api_all
Copy link
Member

Choose a reason for hiding this comment

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

Have you verified kubectl's behavior? What if we failed creating some of the items?

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 think I only tested on 404 with an invalid API version and 409 with conflicts. These exceptions are handled by API internally.

I probably should go test with a multi resource yaml with one of the resources being 404/409. I’ll keep you updated.

@micw523
Copy link
Contributor Author

micw523 commented Nov 7, 2018

The base module update is an overlook. I should fix that...

@micw523 micw523 closed this Nov 7, 2018
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 7, 2018
@micw523
Copy link
Contributor Author

micw523 commented Nov 7, 2018

/open

@micw523 micw523 reopened this Nov 7, 2018
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed approved Indicates a PR has been approved by an approver from all required OWNERS files. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Nov 7, 2018
@micw523 micw523 force-pushed the yaml-list branch 2 times, most recently from 2018925 to dd62474 Compare November 7, 2018 19:54
@fejta
Copy link

fejta commented Nov 10, 2018

/meow psychedelic

@k8s-ci-robot
Copy link
Contributor

@fejta: Bad category. Please see https://api.thecatapi.com/api/categories/list

In response to this:

/meow psychedelic

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.

@fejta
Copy link

fejta commented Nov 10, 2018

noooooooooooo when psychedelic stopped work?!

Dunno... We just send it to thecatapi, not sure why it's including it in the list but not returning a valid image. https://api.thecatapi.com/api/images/get?format=json&results_per_page=1&category=psychedelic returns an empty list is the problem

@micw523
Copy link
Contributor Author

micw523 commented Nov 10, 2018

@fejta I still want my code reviewed here😂
On a side note, I replied in
kubernetes/test-infra#10111
Available categories are 5, 6, 15, 9, 3, 1, 10, 14, 2, 4, 7 from curl. The api list is out of date. No boys, no psychedelic, no girls, no breaded.
/meowvie sunglasses

@k8s-ci-robot
Copy link
Contributor

@micw523: Bad category. Please see https://api.thecatapi.com/api/categories/list

In response to this:

@fetja I still want my code reviewed here😂
On a side note, I replied in
kubernetes/test-infra#10111
Available categories are 5, 6, 15, 9, 3, 1, 10, 14, 2, 4, 7 from curl. The api list is out of date. No boys, no psychedelic, no girls, no breaded.
/meowvie sunglasses

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.

@micw523
Copy link
Contributor Author

micw523 commented Nov 10, 2018

/meow sunglasses

@k8s-ci-robot
Copy link
Contributor

@micw523: cat image

In response to this:

/meow sunglasses

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.

@micw523 micw523 mentioned this pull request Jan 8, 2019
:param async_req bool
:param bool include_uninitialized: If true, partially initialized resources are included in the response.
:param str pretty: If 'true', then the output is pretty printed.
:param str dry_run: When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed
"""

k8s_api_all = []
with open(path.abspath(yaml_file)) as f:
Copy link

Choose a reason for hiding this comment

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

path.abspath(yaml_file)
will crash on string input while yaml.load(str) can load the yaml from a string.
very useful if you need to change the yaml dynamically

ill suggest to support yaml_file as string and a file

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 don’t think this is necessary. The feature requested is to replicate kubectl create -f which of course would not support strings. YAML files can be load as a dictionary and passed as the body parameter when the user calls the corresponding APIs. I don’t see the benefit of changing a YAML on the fly - we’re not doing kubectl apply -f here.

If you want this feature to parse as a string, please file a separate issue.

Copy link
Member

@roycaihw roycaihw left a comment

Choose a reason for hiding this comment

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

I tried running the example but hit following error

$ python examples/create_deployment_from_yaml.py 
Traceback (most recent call last):
  File "examples/create_deployment_from_yaml.py", line 17, in <module>
    from kubernetes import client, config, utils
ImportError: cannot import name utils

did I miss something?

try:
k8s_api_all.append(create_from_yaml_single_item(
k8s_client, yml_document, verbose, **kwargs))
except client.rest.ApiException as api_exception:
Copy link
Member

Choose a reason for hiding this comment

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

it is great that the util runs through the entire list even when there is a failure, like kubectl does

however kubectl returns error code 1 to stderr when failure happens, I'd expect we raise error to upstream after we finish the entire list, to make it easier for automation

Copy link
Member

Choose a reason for hiding this comment

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

instead of print each failure. I'd like to see we concatenate the errors and raise in the end

for question about what the raised error should look like, as long as the error clearly identifies failed objects and is well-formatted to parse (for upstream automation) when necessary, I'm okay with having either a string or a new class for the error message. @mbohlool @yliaog Do you have suggestion on what's the best practice?

below is what kubectl does

$ kubectl create -f hack/testdata/list.yaml 1>/dev/null
Error from server (AlreadyExists): services "list-service-test" already exists
Error from server (AlreadyExists): deployments.apps "list-deployment-test" already exists

namespace="default", body={})

def test_multi_resource_with_conflict(self):
Copy link
Member

Choose a reason for hiding this comment

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

need to document the tests. it's hard to follow what's expected from reading the test and file names

@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 9, 2019
Copy link
Member

@roycaihw roycaihw left a comment

Choose a reason for hiding this comment

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

it'd be better if we have dynamic client, to reuse code and maintain these tests

kubernetes/e2e_test/test_utils.py Show resolved Hide resolved
kubernetes/e2e_test/test_utils.py Show resolved Hide resolved
kubernetes/e2e_test/test_utils.py Show resolved Hide resolved
kubernetes/e2e_test/test_utils.py Show resolved Hide resolved
kubernetes/e2e_test/test_utils.py Show resolved Hide resolved
Using core, extensions API
Using yaml file core-namespace-dep.yaml
Using yaml file extensions-deployment-dep.yaml
"""
Copy link
Member

Choose a reason for hiding this comment

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

def test_create_deployment_in_namespace_from_yaml(self):
    """
    Should be able to create a deployment in a non-default namespace.
    """

Check the program raises CreationFailedError.
Using apiregistration API
Using yaml file api-service.yaml
"""
Copy link
Member

Choose a reason for hiding this comment

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

    def test_create_apiservice_from_yaml_with_conflict(self):
        """
        Creating the same apiservice twice should fail due to conflict.
        """

with self.assertRaises(utils.FailToCreateError):
Copy link
Member

Choose a reason for hiding this comment

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

could you also verify the error message from apiserver?

Using core, extensions API
Using yaml file list.yaml
"""
Copy link
Member

Choose a reason for hiding this comment

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

suggest

    def test_create_from_yaml_list(self):
        """
        Should be able to create multiple objects from a yaml of list kind.
        """

namespace="default")
self.assertIsNotNone(svc)
ext_api = k8s_api[1]
Copy link
Member

Choose a reason for hiding this comment

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

I don't see the point of doing so. It's typed client anyway

it's not a good pattern to iterate through a list of arbitrary api objects and call typed api functions imo

@roycaihw
Copy link
Member

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 19, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

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

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 Mar 19, 2019
@k8s-ci-robot k8s-ci-robot merged commit 84c057b into kubernetes-client:master Mar 19, 2019
@thiagosantosleite
Copy link

Any plans to delete and apply?

kubectl delete -f
kubectl apply -f

@micw523 micw523 deleted the yaml-list branch September 24, 2019 20:36
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. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extend #655 with a List Parser
7 participants