Skip to content

Commit 36fc461

Browse files
authored
Merge pull request #1683 from dingyiyi0226/master
feat: add create from directory support
2 parents 1271465 + c9e11d0 commit 36fc461

File tree

5 files changed

+127
-3
lines changed

5 files changed

+127
-3
lines changed

Diff for: kubernetes/e2e_test/test_utils.py

+22
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,28 @@ def test_create_implicit_service_list_from_yaml_with_conflict(self):
288288
core_api.delete_namespaced_service(name="mock-4",
289289
namespace="default", body={})
290290

291+
# Tests for creating multi-resource from directory
292+
293+
def test_create_multi_resource_from_directory(self):
294+
"""
295+
Should be able to create a service and a replication controller
296+
from a directory
297+
"""
298+
k8s_client = client.api_client.ApiClient(configuration=self.config)
299+
utils.create_from_directory(
300+
k8s_client, self.path_prefix + "multi-resource/")
301+
core_api = client.CoreV1Api(k8s_client)
302+
svc = core_api.read_namespaced_service(name="mock",
303+
namespace="default")
304+
self.assertIsNotNone(svc)
305+
ctr = core_api.read_namespaced_replication_controller(
306+
name="mock", namespace="default")
307+
self.assertIsNotNone(ctr)
308+
core_api.delete_namespaced_replication_controller(
309+
name="mock", namespace="default", propagation_policy="Background")
310+
core_api.delete_namespaced_service(name="mock",
311+
namespace="default", body={})
312+
291313
# Tests for multi-resource yaml objects
292314

293315
def test_create_from_multi_resource_yaml(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: v1
2+
kind: ReplicationController
3+
metadata:
4+
name: mock
5+
spec:
6+
replicas: 1
7+
selector:
8+
app: mock
9+
template:
10+
metadata:
11+
labels:
12+
app: mock
13+
spec:
14+
containers:
15+
- name: mock-container
16+
image: k8s.gcr.io/pause:2.0
17+
ports:
18+
- containerPort: 9949
19+
protocol: TCP
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: mock
5+
labels:
6+
app: mock
7+
spec:
8+
ports:
9+
- port: 99
10+
protocol: TCP
11+
targetPort: 9949
12+
selector:
13+
app: mock

Diff for: 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

Diff for: 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)