Skip to content

Commit

Permalink
Change create_from_yaml to accept string or file
Browse files Browse the repository at this point in the history
This keeps the function backwords compatible.

Changed the parameter yaml to yml in order not to mask
the imported module.
  • Loading branch information
Oz Tiram authored and oz123 committed Mar 20, 2019
1 parent 93e8902 commit 2fed56a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 46 deletions.
16 changes: 8 additions & 8 deletions kubernetes/e2e_test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def setUpClass(cls):

def test_app_yaml(self):
k8s_client = client.api_client.ApiClient(configuration=self.config)
k8s_api = utils.create_from_file(
k8s_api = utils.create_from_yaml(
k8s_client,
"kubernetes/e2e_test/test_yaml/apps-deployment.yaml")
self.assertEqual(
Expand All @@ -42,7 +42,7 @@ def test_app_yaml(self):

def test_extension_yaml(self):
k8s_client = client.api_client.ApiClient(configuration=self.config)
k8s_api = utils.create_from_file(
k8s_api = utils.create_from_yaml(
k8s_client,
"kubernetes/e2e_test/test_yaml/extensions-deployment.yaml")
self.assertEqual(
Expand All @@ -58,7 +58,7 @@ def test_extension_yaml(self):

def test_core_pod_yaml(self):
k8s_client = client.api_client.ApiClient(configuration=self.config)
k8s_api = utils.create_from_file(
k8s_api = utils.create_from_yaml(
k8s_client,
"kubernetes/e2e_test/test_yaml/core-pod.yaml")
self.assertEqual(
Expand All @@ -73,7 +73,7 @@ def test_core_pod_yaml(self):

def test_core_service_yaml(self):
k8s_client = client.api_client.ApiClient(configuration=self.config)
k8s_api = utils.create_from_file(
k8s_api = utils.create_from_yaml(
k8s_client,
"kubernetes/e2e_test/test_yaml/core-service.yaml")
self.assertEqual("v1", k8s_api.get_api_resources().group_version)
Expand All @@ -87,7 +87,7 @@ def test_core_service_yaml(self):

def test_core_namespace_yaml(self):
k8s_client = client.api_client.ApiClient(configuration=self.config)
k8s_api = utils.create_from_file(
k8s_api = utils.create_from_yaml(
k8s_client,
"kubernetes/e2e_test/test_yaml/core-namespace.yaml")
self.assertEqual("v1", k8s_api.get_api_resources().group_version)
Expand All @@ -97,13 +97,13 @@ def test_core_namespace_yaml(self):

def test_deployment_in_namespace(self):
k8s_client = client.ApiClient(configuration=self.config)
core_api = utils.create_from_file(
core_api = utils.create_from_yaml(
k8s_client,
"kubernetes/e2e_test/test_yaml/core-namespace-dep.yaml")
self.assertEqual("v1", core_api.get_api_resources().group_version)
nmsp = core_api.read_namespace(name="dep")
self.assertIsNotNone(nmsp)
dep_api = utils.create_from_file(
dep_api = utils.create_from_yaml(
k8s_client,
"kubernetes/e2e_test/test_yaml/extensions-deployment-dep.yaml")
dep = dep_api.read_namespaced_deployment(
Expand All @@ -117,7 +117,7 @@ def test_deployment_in_namespace(self):

def test_api_service(self):
k8s_client = client.api_client.ApiClient(configuration=self.config)
k8s_api = utils.create_from_file(
k8s_api = utils.create_from_yaml(
k8s_client,
"kubernetes/e2e_test/test_yaml/api-service.yaml")
self.assertEqual(
Expand Down
4 changes: 1 addition & 3 deletions kubernetes/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from .create_from_yaml import (create_from_yaml,
create_from_file,
create_from_map)
from .create_from_yaml import create_from_map, create_from_yaml
45 changes: 10 additions & 35 deletions kubernetes/utils/create_from_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
from kubernetes import client


def create_from_file(k8s_client, yaml_path, verbose=False, **kwargs):
def create_from_yaml(k8s_client, yml, verbose=False, **kwargs):
"""
Perform an action from a yaml file.
Perform an action from a yaml string or file.
:param k8s_client: an ApiClient object, initialized with the client args
:yaml_path: the path on the file system to the yaml file
:type yaml_path: str
:yml: a string containing valid yaml or a file name.
:type yml: str
:param verbose: print confimation information
:type verbose: bool
Expand All @@ -43,36 +43,11 @@ def create_from_file(k8s_client, yaml_path, verbose=False, **kwargs):
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_path)) as f:
k8s_api = create_from_yaml(k8s_client, f.read(), verbose=verbose,
**kwargs)

return k8s_api


def create_from_yaml(k8s_client, yaml_str, verbose=False, **kwargs):
"""
Perform an action from a yaml string.
:param k8s_client: an ApiClient object, initialized with the client args
:yaml_str: a string containing valid yaml or json.
:type yaml: str
:param verbose: print confimation information
:type verbose: bool
Available parameters for performing the subsequent action:
:param async_req:
:type async_req: bool
:param include_uninitialized: include partially initialized resources in
the response.
:type include_uninitialized: bool
:param str pretty: pretty print the output
:param str dry_run: When present, indicates that modifications should not
be persisted. An invalid or unrecognized dry_run 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
"""
stream = yaml.safe_load_all(yaml_str)
if path.exists(yml):
with open(path.abspath(yml)) as f:
stream = yaml.safe_load_all(f.read())
else:
stream = yaml.safe_load_all(yml)
for obj in stream:
k8s_api = create_from_map(k8s_client, obj, verbose, **kwargs)

Expand Down Expand Up @@ -123,7 +98,7 @@ def create_from_map(k8s_client, yml_object, verbose, **kwargs):
# Expect the user to create namespaced objects more often
if hasattr(k8s_api, "create_namespaced_{0}".format(kind)):
resp = getattr(k8s_api, "create_namespaced_{0}".format(kind))(
body=yml_object, namespace=namespace, **kwargs)
body=yml_object, namespace=namespace, **kwargs)
else:
resp = getattr(k8s_api, "create_{0}".format(kind))(
body=yml_object, **kwargs)
Expand Down

0 comments on commit 2fed56a

Please sign in to comment.