-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Add kubectl create -f like feature #655
Conversation
/assign @roycaihw |
Fixes #349 |
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. |
This is awesome, thank you so much. Do you think it would be possible to have one for creating job from |
@nakulpathak3 From what I implemented I would expect a call to batch/vxx kind:Job to work. I did not implement an e2e test for it though. |
763b0fe
to
9e5a5b1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding this feature! I left a few nits, and one substantive comment around creating cluster-scoped resources.
@@ -0,0 +1,31 @@ | |||
# Copyright 2016 The Kubernetes Authors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/2016/2018
similar in other places under utils/
|
||
import yaml | ||
|
||
from kubernetes import client, config, utils |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can drop client
and yaml
imports?
kubernetes/e2e_test/test_utils.py
Outdated
|
||
def test_app_yaml(self): | ||
k8s_api = utils.create_from_yaml( | ||
"kubernetes/e2e_test/test_yaml/app.yaml", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe call the yaml files apps-deployment.yaml
and extensions-deployment.yaml
. We can use singular for the Kind, but usually we don't have singular v.s. plural for Group
kubernetes/e2e_test/test_utils.py
Outdated
self.assertEqual("apps/v1beta1", | ||
k8s_api.get_api_resources().group_version) | ||
deployments = k8s_api.list_namespaced_deployment( | ||
namespace="default").items |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use GET instead of LIST to test the creation:
python/kubernetes/client/apis/apps_v1beta1_api.py
Line 3091 in 529c0d3
def read_namespaced_deployment(self, name, namespace, **kwargs): |
kubernetes/utils/create_from_yaml.py
Outdated
|
||
from kubernetes import client | ||
|
||
def create_from_yaml(yaml_file, verbose=0, **kwargs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: please use verbose=False
and verbose=True
kubernetes/utils/create_from_yaml.py
Outdated
dep_namespace = dep["metadata"]["namespace"] | ||
else: | ||
dep_namespace = "default" | ||
resp = getattr(k8s_api, "create_namespaced_{0}".format(action_type.lower()))( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need to be able to create cluster-scoped resources (e.g. create a namespace)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM in general. I left a few nits. Please squash the commits before submitting.
kubernetes/utils/create_from_yaml.py
Outdated
|
||
def create_from_yaml(k8s_client, yaml_file, verbose=False, **kwargs): | ||
""" | ||
Perform an action from a yaml file. Pass 1 for verbose to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: s/1/True
kubernetes/utils/create_from_yaml.py
Outdated
with open(path.abspath(yaml_file)) as f: | ||
yml_object = yaml.load(f) | ||
#TODO: case of yaml file containing multiple objects | ||
api_type, _, api_version = yml_object["apiVersion"].partition("/") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use group
, version
and kind
instead of api_type
, api_version
and action_type
, following the Kubernetes API convention: https://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md
kubernetes/utils/create_from_yaml.py
Outdated
# Decide which namespace we are going to put the object in, | ||
# if any | ||
if "namespace" in yml_object["metadata"]: | ||
dep_namespace = yml_object["metadata"]["namespace"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: s/dep_namespace/namespace
/lgtm |
[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 |
: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 | ||
""" | ||
|
||
with open(path.abspath(yaml_file)) as f: |
There was a problem hiding this comment.
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 str while yaml.load(str)
can load the yaml from a string.
very useful if you need to change the yaml dynamically
Can you create argo workflows with this ? because I get |
Is it a CRD? If so see #740. This is in my to do list, but these features have to come one at a time. |
yeah it is a CRD. I am not sure how I missed that issue, thanks. |
This PR addresses #504.
Created a module kubernetes/utils with a function create_from_yaml(). Implemented with getattr().
e2e unit tests were added to test the functionalities on app_v1 / extension_v1beta1 (deployment), core_v1 (pod, service). The test yaml files are from kubernetes.io are included under e2e_test/test_yaml.
A minimal working example is added under examples/ folder.