Skip to content

Commit 18fb9a0

Browse files
committed
feat: add create from directory support
1 parent 1937611 commit 18fb9a0

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

kubernetes/utils/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
from __future__ import absolute_import
1616

1717
from .create_from_yaml import (FailToCreateError, create_from_dict,
18-
create_from_yaml)
18+
create_from_yaml, create_from_directory)
1919
from .quantity import parse_quantity

kubernetes/utils/create_from_yaml.py

+72-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
import re
17-
from os import path
17+
import os
1818

1919
import yaml
2020

@@ -24,6 +24,76 @@
2424
LOWER_OR_NUM_FOLLOWED_BY_UPPER_RE = re.compile('([a-z0-9])([A-Z])')
2525

2626

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+
2797
def create_from_yaml(
2898
k8s_client,
2999
yaml_file=None,
@@ -87,7 +157,7 @@ def create_with(objects):
87157
yml_document_all = yaml_objects
88158
return create_with(yml_document_all)
89159
elif yaml_file:
90-
with open(path.abspath(yaml_file)) as f:
160+
with open(os.path.abspath(yaml_file)) as f:
91161
yml_document_all = yaml.safe_load_all(f)
92162
return create_with(yml_document_all)
93163
else:

0 commit comments

Comments
 (0)