Skip to content

Commit 58d6b0c

Browse files
fix code style
1 parent 4c9c726 commit 58d6b0c

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

kubernetes/utils/__init__.py

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

1515
from __future__ import absolute_import
1616

17-
from .operate_from_yaml import (FailToExecuteError, operate_from_dict,
18-
create_from_yaml, delete_from_yaml)
17+
from .operate_from_yaml import (FailToExecuteError, create_from_yaml,
18+
delete_from_yaml, operate_from_dict)
1919
from .quantity import parse_quantity

kubernetes/utils/operate_from_yaml.py

+35-25
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,23 @@
2020

2121
from kubernetes import client
2222

23+
2324
def create_from_yaml(k8s_client, yaml_file, verbose=False,
2425
namespace="default", **kwargs):
2526
operation = "create"
26-
operate_from_yaml(k8s_client, yaml_file, operation,verbose=False,
27-
namespace="default", **kwargs)
27+
operate_from_yaml(k8s_client, yaml_file, operation, verbose=False,
28+
namespace="default", **kwargs)
29+
2830

2931
def delete_from_yaml(k8s_client, yaml_file, verbose=False,
3032
namespace="default", **kwargs):
3133
operation = "delete"
32-
operate_from_yaml(k8s_client, yaml_file, operation,verbose=False,
33-
namespace="default", **kwargs)
34+
operate_from_yaml(k8s_client, yaml_file, operation, verbose=False,
35+
namespace="default", **kwargs)
3436

3537

3638
def operate_from_yaml(k8s_client, yaml_file, operation, verbose=False,
37-
namespace="default",**kwargs):
39+
namespace="default", **kwargs):
3840
"""
3941
Input:
4042
yaml_file: string. Contains the path to yaml file.
@@ -64,15 +66,15 @@ def operate_from_yaml(k8s_client, yaml_file, operation, verbose=False,
6466
for yml_document in yml_document_all:
6567
try:
6668
operate_from_dict(k8s_client, yml_document, operation, verbose,
67-
namespace=namespace, **kwargs)
69+
namespace=namespace, **kwargs)
6870
except FailToExecuteError as failure:
6971
failures.extend(failure.api_exceptions)
7072
if failures:
7173
raise FailToExecuteError(failures)
7274

7375

74-
def operate_from_dict(k8s_client, yml_document,operation, verbose,
75-
namespace="default",**kwargs):
76+
def operate_from_dict(k8s_client, yml_document, operation, verbose,
77+
namespace="default", **kwargs):
7678
"""
7779
Perform an operation kubernetes resource from a dictionary containing valid kubernetes
7880
API object (i.e. List, Service, etc).
@@ -99,8 +101,12 @@ def operate_from_dict(k8s_client, yml_document,operation, verbose,
99101
yml_doc["kind"] = kind
100102
try:
101103
operate_from_yaml_single_item(
102-
k8s_client, yml_doc, operation, verbose, namespace=namespace, **kwargs
103-
)
104+
k8s_client,
105+
yml_doc,
106+
operation,
107+
verbose,
108+
namespace=namespace,
109+
**kwargs)
104110
except client.rest.ApiException as api_exception:
105111
api_exceptions.append(api_exception)
106112
else:
@@ -116,8 +122,13 @@ def operate_from_dict(k8s_client, yml_document,operation, verbose,
116122
raise FailToExecuteError(api_exceptions)
117123

118124

119-
def operate_from_yaml_single_item(k8s_client,
120-
yml_document,operation,verbose=False,namespace="default", **kwargs):
125+
def operate_from_yaml_single_item(
126+
k8s_client,
127+
yml_document,
128+
operation,
129+
verbose=False,
130+
namespace="default",
131+
**kwargs):
121132
# get group and version from apiVersion
122133
group, _, version = yml_document["apiVersion"].partition("/")
123134
if version == "":
@@ -133,23 +144,26 @@ def operate_from_yaml_single_item(k8s_client,
133144
kind = yml_document["kind"]
134145
kind = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', kind)
135146
kind = re.sub('([a-z0-9])([A-Z])', r'\1_\2', kind).lower()
136-
if operation=="create":
137-
resp = create_k8s_object(k8s_api,yml_document,kind,namespace=namespace)
147+
if operation == "create":
148+
resp = create_k8s_object(
149+
k8s_api, yml_document, kind, namespace=namespace)
138150
if verbose:
139151
msg = "{0} created.".format(kind)
140152
if hasattr(resp, 'status'):
141153
msg += " status='{0}'".format(str(resp.status))
142154
print(msg)
143-
elif operation=="delete":
144-
resp = delete_k8s_object(k8s_api,yml_document,kind,namespace=namespace)
155+
elif operation == "delete":
156+
resp = delete_k8s_object(
157+
k8s_api, yml_document, kind, namespace=namespace)
145158
if verbose:
146159
msg = "{0} deleted.".format(kind)
147160
if hasattr(resp, 'status'):
148161
msg += " status='{0}'".format(str(resp.status))
149162
print(msg)
150-
151163

152-
def create_k8s_object(k8s_api, yml_document, kind,**kwargs):
164+
165+
def create_k8s_object(k8s_api, yml_document, kind, **kwargs):
166+
153167
if hasattr(k8s_api, "create_namespaced_{0}".format(kind)):
154168
if "namespace" in yml_document["metadata"]:
155169
namespace = yml_document["metadata"]["namespace"]
@@ -163,8 +177,8 @@ def create_k8s_object(k8s_api, yml_document, kind,**kwargs):
163177
return resp
164178

165179

166-
def delete_k8s_object(k8s_api, yml_document, kind,**kwargs):
167-
print(kwargs)
180+
def delete_k8s_object(k8s_api, yml_document, kind, **kwargs):
181+
168182
if hasattr(k8s_api, "create_namespaced_{0}".format(kind)):
169183
if "namespace" in yml_document["metadata"]:
170184
namespace = yml_document["metadata"]["namespace"]
@@ -185,12 +199,10 @@ def delete_k8s_object(k8s_api, yml_document, kind,**kwargs):
185199
return resp
186200

187201

188-
189-
190202
class FailToExecuteError(Exception):
191203
"""
192204
An exception class for handling error if an error occurred when
193-
handling a yaml file during deletion of the resource.
205+
handling a yaml file during creation or deletion of the resource.
194206
"""
195207

196208
def __init__(self, api_exceptions):
@@ -202,5 +214,3 @@ def __str__(self):
202214
msg += "Error from server ({0}):{1}".format(
203215
api_exception.reason, api_exception.body)
204216
return msg
205-
206-

0 commit comments

Comments
 (0)