|
14 | 14 |
|
15 | 15 |
|
16 | 16 | import re
|
17 |
| -from os import path |
| 17 | +import os |
18 | 18 |
|
19 | 19 | import yaml
|
20 | 20 |
|
|
24 | 24 | LOWER_OR_NUM_FOLLOWED_BY_UPPER_RE = re.compile('([a-z0-9])([A-Z])')
|
25 | 25 |
|
26 | 26 |
|
| 27 | +def create_from_directory( |
| 28 | + k8s_client, |
| 29 | + yaml_dir=None, |
| 30 | + verbose=False, |
| 31 | + namespace="default", |
| 32 | + **kwargs): |
| 33 | + """ |
| 34 | + Perform an action from files from a directory. Pass True for verbose to |
| 35 | + print confirmation information. |
| 36 | +
|
| 37 | + Input: |
| 38 | + k8s_client: an ApiClient object, initialized with the client args. |
| 39 | + yaml_dir: string. Contains the path to directory. |
| 40 | + verbose: If True, print confirmation from the create action. |
| 41 | + Default is False. |
| 42 | + namespace: string. Contains the namespace to create all |
| 43 | + resources inside. The namespace must preexist otherwise |
| 44 | + the resource creation will fail. If the API object in |
| 45 | + the yaml file already contains a namespace definition |
| 46 | + this parameter has no effect. |
| 47 | +
|
| 48 | + Available parameters for creating <kind>: |
| 49 | + :param async_req bool |
| 50 | + :param bool include_uninitialized: If true, partially initialized |
| 51 | + resources are included in the response. |
| 52 | + :param str pretty: If 'true', then the output is pretty printed. |
| 53 | + :param str dry_run: When present, indicates that modifications |
| 54 | + should not be persisted. An invalid or unrecognized dryRun |
| 55 | + directive will result in an error response and no further |
| 56 | + processing of the request. |
| 57 | + Valid values are: - All: all dry run stages will be processed |
| 58 | +
|
| 59 | + Returns: |
| 60 | + The list containing the created kubernetes API objects. |
| 61 | +
|
| 62 | + Raises: |
| 63 | + FailToCreateError which holds list of `client.rest.ApiException` |
| 64 | + instances for each object that failed to create. |
| 65 | + """ |
| 66 | + |
| 67 | + if not yaml_dir: |
| 68 | + raise ValueError( |
| 69 | + '`yaml_dir` argument must be provided') |
| 70 | + elif not os.path.isdir(yaml_dir): |
| 71 | + raise ValueError( |
| 72 | + '`yaml_dir` argument must be a path to directory') |
| 73 | + |
| 74 | + files = [os.path.join(yaml_dir, i) for i in os.listdir(yaml_dir) |
| 75 | + if os.path.isfile(os.path.join(yaml_dir, i))] |
| 76 | + if not files: |
| 77 | + raise ValueError( |
| 78 | + '`yaml_dir` contains no files') |
| 79 | + |
| 80 | + failures = [] |
| 81 | + k8s_objects_all = [] |
| 82 | + |
| 83 | + for file in files: |
| 84 | + try: |
| 85 | + k8s_objects = create_from_yaml(k8s_client, file, |
| 86 | + verbose=verbose, |
| 87 | + namespace=namespace, |
| 88 | + **kwargs) |
| 89 | + k8s_objects_all.append(k8s_objects) |
| 90 | + except FailToCreateError as failure: |
| 91 | + failures.extend(failure.api_exceptions) |
| 92 | + if failures: |
| 93 | + raise FailToCreateError(failures) |
| 94 | + return k8s_objects_all |
| 95 | + |
| 96 | + |
27 | 97 | def create_from_yaml(
|
28 | 98 | k8s_client,
|
29 | 99 | yaml_file=None,
|
@@ -87,7 +157,7 @@ def create_with(objects):
|
87 | 157 | yml_document_all = yaml_objects
|
88 | 158 | return create_with(yml_document_all)
|
89 | 159 | elif yaml_file:
|
90 |
| - with open(path.abspath(yaml_file)) as f: |
| 160 | + with open(os.path.abspath(yaml_file)) as f: |
91 | 161 | yml_document_all = yaml.safe_load_all(f)
|
92 | 162 | return create_with(yml_document_all)
|
93 | 163 | else:
|
|
0 commit comments