@@ -60,18 +60,19 @@ def create_from_yaml(
60
60
yml_document_all = yaml .safe_load_all (f )
61
61
62
62
failures = []
63
-
64
63
for yml_document in yml_document_all :
65
64
try :
66
65
create_from_dict (k8s_client , yml_document , verbose ,
66
+ namespace = namespace ,
67
67
** kwargs )
68
68
except FailToCreateError as failure :
69
69
failures .extend (failure .api_exceptions )
70
70
if failures :
71
71
raise FailToCreateError (failures )
72
72
73
73
74
- def create_from_dict (k8s_client , data , verbose = False , ** kwargs ):
74
+ def create_from_dict (k8s_client , data , verbose = False , namespace = 'default' ,
75
+ ** kwargs ):
75
76
"""
76
77
Perform an action from a dictionary containing valid kubernetes
77
78
API object (i.e. List, Service, etc).
@@ -81,6 +82,11 @@ def create_from_dict(k8s_client, data, verbose=False, **kwargs):
81
82
data: a dictionary holding valid kubernetes objects
82
83
verbose: If True, print confirmation from the create action.
83
84
Default is False.
85
+ namespace: string. Contains the namespace to create all
86
+ resources inside. The namespace must preexist otherwise
87
+ the resource creation will fail. If the API object in
88
+ the yaml file already contains a namespace definition
89
+ this parameter has no effect.
84
90
85
91
Raises:
86
92
FailToCreateError which holds list of `client.rest.ApiException`
@@ -101,14 +107,15 @@ def create_from_dict(k8s_client, data, verbose=False, **kwargs):
101
107
yml_object ["kind" ] = kind
102
108
try :
103
109
create_from_yaml_single_item (
104
- k8s_client , yml_object , verbose , ** kwargs )
110
+ k8s_client , yml_object , verbose , namespace = namespace ,
111
+ ** kwargs )
105
112
except client .rest .ApiException as api_exception :
106
113
api_exceptions .append (api_exception )
107
114
else :
108
115
# This is a single object. Call the single item method
109
116
try :
110
117
create_from_yaml_single_item (
111
- k8s_client , data , verbose , ** kwargs )
118
+ k8s_client , data , verbose , namespace = namespace , ** kwargs )
112
119
except client .rest .ApiException as api_exception :
113
120
api_exceptions .append (api_exception )
114
121
@@ -135,17 +142,17 @@ def create_from_yaml_single_item(
135
142
kind = yml_object ["kind" ]
136
143
kind = re .sub ('(.)([A-Z][a-z]+)' , r'\1_\2' , kind )
137
144
kind = re .sub ('([a-z0-9])([A-Z])' , r'\1_\2' , kind ).lower ()
138
- # Decide which namespace we are going to put the object in,
139
- # if any
140
- if "namespace" in yml_object ["metadata" ]:
141
- namespace = yml_object ["metadata" ]["namespace" ]
142
- else :
143
- namespace = "default"
144
145
# Expect the user to create namespaced objects more often
145
146
if hasattr (k8s_api , "create_namespaced_{0}" .format (kind )):
147
+ # Decide which namespace we are going to put the object in,
148
+ # if any
149
+ if "namespace" in yml_object ["metadata" ]:
150
+ namespace = yml_object ["metadata" ]["namespace" ]
151
+ kwargs ['namespace' ] = namespace
146
152
resp = getattr (k8s_api , "create_namespaced_{0}" .format (kind ))(
147
- body = yml_object , namespace = namespace , ** kwargs )
153
+ body = yml_object , ** kwargs )
148
154
else :
155
+ kwargs .pop ('namespace' , None )
149
156
resp = getattr (k8s_api , "create_{0}" .format (kind ))(
150
157
body = yml_object , ** kwargs )
151
158
if verbose :
0 commit comments