|
14 | 14 |
|
15 | 15 |
|
16 | 16 | import re
|
17 |
| -import sys |
18 | 17 | from os import path
|
19 | 18 |
|
20 | 19 | import yaml
|
21 |
| -from six import iteritems |
22 | 20 |
|
23 | 21 | from kubernetes import client
|
24 | 22 |
|
25 | 23 |
|
26 |
| -def create_from_yaml(k8s_client, yaml_file, verbose=False, **kwargs): |
| 24 | +def create_from_file(k8s_client, yaml_path, verbose=False, **kwargs): |
27 | 25 | """
|
28 |
| - Perform an action from a yaml file. Pass True for verbose to |
29 |
| - print confirmation information. |
30 |
| - Input: |
31 |
| - yaml_file: string. Contains the path to yaml file. |
32 |
| - k8s_cline: an ApiClient object, initialized with the client args. |
| 26 | + Perform an action from a yaml file. |
| 27 | +
|
| 28 | + :param k8s_client: an ApiClient object, initialized with the client args |
| 29 | + :yaml_path: the path on the file system to the yaml file |
| 30 | + :type yaml_path: str |
| 31 | + :param verbose: print confimation information |
| 32 | + :type verbose: bool |
| 33 | +
|
| 34 | + Available parameters for performing the subsequent action: |
| 35 | + :param async_req: |
| 36 | + :type async_req: bool |
| 37 | + :param include_uninitialized: include partially initialized resources in |
| 38 | + the response. |
| 39 | + :type include_uninitialized: bool |
| 40 | + :param str pretty: pretty print the output |
| 41 | + :param str dry_run: When present, indicates that modifications should not |
| 42 | + be persisted. An invalid or unrecognized dry_run directive will result in |
| 43 | + an error response and no further processing of the request. |
| 44 | + Valid values are: - All: all dry run stages will be processed |
| 45 | + """ |
| 46 | + with open(path.abspath(yaml_path)) as f: |
| 47 | + k8s_api = create_from_yaml(k8s_client, f.read(), verbose=verbose, |
| 48 | + **kwargs) |
| 49 | + |
| 50 | + return k8s_api |
| 51 | + |
| 52 | + |
| 53 | +def create_from_yaml(k8s_client, yaml_str, verbose=False, **kwargs): |
| 54 | + """ |
| 55 | + Perform an action from a yaml string. |
| 56 | +
|
| 57 | + :param k8s_client: an ApiClient object, initialized with the client args |
| 58 | + :yaml_str: a string containing valid yaml or json. |
| 59 | + :type yaml: str |
| 60 | + :param verbose: print confimation information |
| 61 | + :type verbose: bool |
33 | 62 |
|
34 | 63 | Available parameters for performing the subsequent action:
|
35 |
| - :param async_req bool |
36 |
| - :param bool include_uninitialized: If true, partially initialized resources are included in the response. |
37 |
| - :param str pretty: If 'true', then the output is pretty printed. |
38 |
| - :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 |
| 64 | + :param async_req: |
| 65 | + :type async_req: bool |
| 66 | + :param include_uninitialized: include partially initialized resources in |
| 67 | + the response. |
| 68 | + :type include_uninitialized: bool |
| 69 | + :param str pretty: pretty print the output |
| 70 | + :param str dry_run: When present, indicates that modifications should not |
| 71 | + be persisted. An invalid or unrecognized dry_run directive will result in |
| 72 | + an error response and no further processing of the request. |
| 73 | + Valid values are: - All: all dry run stages will be processed |
| 74 | + """ |
| 75 | + stream = yaml.safe_load_all(yaml_str) |
| 76 | + for obj in stream: |
| 77 | + k8s_api = create_from_map(k8s_client, obj, verbose, **kwargs) |
| 78 | + |
| 79 | + return k8s_api |
| 80 | + |
| 81 | + |
| 82 | +def create_from_map(k8s_client, yml_object, verbose, **kwargs): |
39 | 83 | """
|
| 84 | + performe an action from a valid parsed yaml object (as dict). |
| 85 | +
|
| 86 | + :param k8s_client: an ApiClient objcet initialized with the client args |
| 87 | + :yml_object dict: a parsed yaml object |
| 88 | + :param verbose: print confimation information |
| 89 | + :type verbose: bool |
40 | 90 |
|
41 |
| - with open(path.abspath(yaml_file)) as f: |
42 |
| - yml_object = yaml.load(f) |
43 |
| - # TODO: case of yaml file containing multiple objects |
44 |
| - group, _, version = yml_object["apiVersion"].partition("/") |
45 |
| - if version == "": |
46 |
| - version = group |
47 |
| - group = "core" |
48 |
| - # Take care for the case e.g. api_type is "apiextensions.k8s.io" |
49 |
| - # Only replace the last instance |
50 |
| - group = "".join(group.rsplit(".k8s.io", 1)) |
51 |
| - fcn_to_call = "{0}{1}Api".format(group.capitalize(), |
52 |
| - version.capitalize()) |
53 |
| - k8s_api = getattr(client, fcn_to_call)(k8s_client) |
54 |
| - # Replace CamelCased action_type into snake_case |
55 |
| - kind = yml_object["kind"] |
56 |
| - kind = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', kind) |
57 |
| - kind = re.sub('([a-z0-9])([A-Z])', r'\1_\2', kind).lower() |
58 |
| - # Decide which namespace we are going to put the object in, |
59 |
| - # if any |
60 |
| - if "namespace" in yml_object["metadata"]: |
61 |
| - namespace = yml_object["metadata"]["namespace"] |
62 |
| - else: |
63 |
| - namespace = "default" |
64 |
| - # Expect the user to create namespaced objects more often |
65 |
| - if hasattr(k8s_api, "create_namespaced_{0}".format(kind)): |
66 |
| - resp = getattr(k8s_api, "create_namespaced_{0}".format(kind))( |
67 |
| - body=yml_object, namespace=namespace, **kwargs) |
68 |
| - else: |
69 |
| - resp = getattr(k8s_api, "create_{0}".format(kind))( |
70 |
| - body=yml_object, **kwargs) |
71 |
| - if verbose: |
72 |
| - print("{0} created. status='{1}'".format(kind, str(resp.status))) |
73 |
| - return k8s_api |
| 91 | + Available parameters for performing the subsequent action: |
| 92 | + :param async_req: |
| 93 | + :type async_req: bool |
| 94 | + :param include_uninitialized: include partially initialized resources in |
| 95 | + the response. |
| 96 | + :type include_uninitialized: bool |
| 97 | + :param str pretty: pretty print the output |
| 98 | + :param str dry_run: When present, indicates that modifications should not |
| 99 | + be persisted. An invalid or unrecognized dry_run directive will result in |
| 100 | + an error response and no further processing of the request. |
| 101 | + Valid values are: - All: all dry run stages will be processed |
| 102 | + """ |
| 103 | + group, _, version = yml_object["apiVersion"].partition("/") |
| 104 | + if version == "": |
| 105 | + version = group |
| 106 | + group = "core" |
| 107 | + # Take care for the case e.g. api_type is "apiextensions.k8s.io" |
| 108 | + # Only replace the last instance |
| 109 | + group = "".join(group.rsplit(".k8s.io", 1)) |
| 110 | + fcn_to_call = "{0}{1}Api".format(group.capitalize(), |
| 111 | + version.capitalize()) |
| 112 | + k8s_api = getattr(client, fcn_to_call)(k8s_client) |
| 113 | + # Replace CamelCased action_type into snake_case |
| 114 | + kind = yml_object["kind"] |
| 115 | + kind = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', kind) |
| 116 | + kind = re.sub('([a-z0-9])([A-Z])', r'\1_\2', kind).lower() |
| 117 | + # Decide which namespace we are going to put the object in, |
| 118 | + # if any |
| 119 | + if "namespace" in yml_object["metadata"]: |
| 120 | + namespace = yml_object["metadata"]["namespace"] |
| 121 | + else: |
| 122 | + namespace = "default" |
| 123 | + # Expect the user to create namespaced objects more often |
| 124 | + if hasattr(k8s_api, "create_namespaced_{0}".format(kind)): |
| 125 | + resp = getattr(k8s_api, "create_namespaced_{0}".format(kind))( |
| 126 | + body=yml_object, namespace=namespace, **kwargs) |
| 127 | + else: |
| 128 | + resp = getattr(k8s_api, "create_{0}".format(kind))( |
| 129 | + body=yml_object, **kwargs) |
| 130 | + if verbose: |
| 131 | + print("{0} created. status='{1}'".format(kind, str(resp.status))) |
| 132 | + return k8s_api |
0 commit comments