|
23 | 23 | from kubernetes import client
|
24 | 24 |
|
25 | 25 |
|
26 |
| -def create_from_yaml(k8s_client, yaml_file, verbose=False, **kwargs): |
| 26 | +def create_from_yaml( |
| 27 | + k8s_client, |
| 28 | + yaml_file, |
| 29 | + verbose=False, |
| 30 | + output_list=False, |
| 31 | + **kwargs): |
27 | 32 | """
|
28 | 33 | Perform an action from a yaml file. Pass True for verbose to
|
29 | 34 | print confirmation information.
|
30 | 35 | Input:
|
31 | 36 | yaml_file: string. Contains the path to yaml file.
|
32 | 37 | k8s_cline: an ApiClient object, initialized with the client args.
|
| 38 | + verbose: If True, print confirmation from the create action. Default is False. |
| 39 | + output_list: compatibility option with v8.0.0. Default is False. |
| 40 | + Function returns a single api object when there is only one when set False. |
| 41 | + Does not affect when multiple objects are generated. |
33 | 42 |
|
34 |
| - Available parameters for performing the subsequent action: |
| 43 | + Returns: |
| 44 | + An k8s api object or list of apis objects created from YAML. |
| 45 | + When a single object is generated, return type is dependent on output_list. |
| 46 | +
|
| 47 | + Available parameters for creating <kind>: |
35 | 48 | :param async_req bool
|
36 | 49 | :param bool include_uninitialized: If true, partially initialized resources are included in the response.
|
37 | 50 | :param str pretty: If 'true', then the output is pretty printed.
|
38 | 51 | :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
|
39 | 52 | """
|
40 | 53 |
|
| 54 | + k8s_api_all = [] |
41 | 55 | 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 |
| 56 | + yml_document_all = yaml.load_all(f) |
| 57 | + # Load all documents from a single YAML file |
| 58 | + for yml_document in yml_document_all: |
| 59 | + # If it is a list type, will need to iterate its items |
| 60 | + if "List" in yml_document["kind"]: |
| 61 | + # Could be "List" or "Pod/Service/...List" |
| 62 | + # This is a list type. iterate within its items |
| 63 | + for yml_object in yml_document["items"]: |
| 64 | + k8s_api_all.append(create_from_yaml_single_item( |
| 65 | + k8s_client, yml_object, verbose, **kwargs)) |
| 66 | + else: |
| 67 | + # This is a single object. Call the single item method |
| 68 | + k8s_api_all.append(create_from_yaml_single_item( |
| 69 | + k8s_client, yml_document, verbose, **kwargs)) |
| 70 | + if output_list is False: |
| 71 | + if len(k8s_api_all) == 1: |
| 72 | + return k8s_api_all[0] |
| 73 | + return k8s_api_all |
| 74 | + |
| 75 | + |
| 76 | +def create_from_yaml_single_item( |
| 77 | + k8s_client, yml_object, verbose=False, **kwargs): |
| 78 | + group, _, version = yml_object["apiVersion"].partition("/") |
| 79 | + if version == "": |
| 80 | + version = group |
| 81 | + group = "core" |
| 82 | + # Take care for the case e.g. api_type is "apiextensions.k8s.io" |
| 83 | + # Only replace the last instance |
| 84 | + group = "".join(group.rsplit(".k8s.io", 1)) |
| 85 | + fcn_to_call = "{0}{1}Api".format(group.capitalize(), |
| 86 | + version.capitalize()) |
| 87 | + k8s_api = getattr(client, fcn_to_call)(k8s_client) |
| 88 | + # Replace CamelCased action_type into snake_case |
| 89 | + kind = yml_object["kind"] |
| 90 | + kind = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', kind) |
| 91 | + kind = re.sub('([a-z0-9])([A-Z])', r'\1_\2', kind).lower() |
| 92 | + # Decide which namespace we are going to put the object in, |
| 93 | + # if any |
| 94 | + if "namespace" in yml_object["metadata"]: |
| 95 | + namespace = yml_object["metadata"]["namespace"] |
| 96 | + else: |
| 97 | + namespace = "default" |
| 98 | + # Expect the user to create namespaced objects more often |
| 99 | + if hasattr(k8s_api, "create_namespaced_{0}".format(kind)): |
| 100 | + resp = getattr(k8s_api, "create_namespaced_{0}".format(kind))( |
| 101 | + body=yml_object, namespace=namespace, **kwargs) |
| 102 | + else: |
| 103 | + resp = getattr(k8s_api, "create_{0}".format(kind))( |
| 104 | + body=yml_object, **kwargs) |
| 105 | + if verbose: |
| 106 | + print("{0} created. status='{1}'".format(kind, str(resp.status))) |
| 107 | + return k8s_api |
0 commit comments