@@ -55,6 +55,52 @@ def create_from_yaml(
5555 Valid values are: - All: all dry run stages will be processed
5656 """
5757
58+ create_namespaced_from_yaml (
59+ k8s_client ,
60+ yaml_file ,
61+ verbose ,
62+ namespace = "default" ,
63+ ** kwargs
64+ )
65+
66+
67+ def create_namespaced_from_yaml (
68+ k8s_client ,
69+ yaml_file ,
70+ verbose = False ,
71+ namespace = "default" ,
72+ ** kwargs ):
73+ """
74+ Perform an action from a yaml file. Pass True for verbose to
75+ print confirmation information.
76+ Input:
77+ yaml_file: string. Contains the path to yaml file.
78+ k8s_client: an ApiClient object, initialized with the client args.
79+ verbose: If True, print confirmation from the create action.
80+ Default is False.
81+ namespace: string. Contains the namespace to create all
82+ resources inside
83+
84+ Returns:
85+ An k8s api object or list of apis objects created from YAML.
86+ When a single object is generated, return type is dependent
87+ on output_list.
88+
89+ Throws a FailToCreateError exception if creation of any object
90+ fails with helpful messages from the server.
91+
92+ Available parameters for creating <kind>:
93+ :param async_req bool
94+ :param bool include_uninitialized: If true, partially initialized
95+ resources are included in the response.
96+ :param str pretty: If 'true', then the output is pretty printed.
97+ :param str dry_run: When present, indicates that modifications
98+ should not be persisted. An invalid or unrecognized dryRun
99+ directive will result in an error response and no further
100+ processing of the request.
101+ Valid values are: - All: all dry run stages will be processed
102+ """
103+
58104 with open (path .abspath (yaml_file )) as f :
59105 yml_document_all = yaml .safe_load_all (f )
60106 api_exceptions = []
@@ -72,15 +118,15 @@ def create_from_yaml(
72118 yml_object ["apiVersion" ] = yml_document ["apiVersion" ]
73119 yml_object ["kind" ] = kind
74120 try :
75- create_from_yaml_single_item (
76- k8s_client , yml_object , verbose , ** kwargs )
121+ create_namespaced_from_yaml_single_item (
122+ k8s_client , yml_object , verbose , namespace , ** kwargs )
77123 except client .rest .ApiException as api_exception :
78124 api_exceptions .append (api_exception )
79125 else :
80126 # This is a single object. Call the single item method
81127 try :
82- create_from_yaml_single_item (
83- k8s_client , yml_document , verbose , ** kwargs )
128+ create_namespaced_from_yaml_single_item (
129+ k8s_client , yml_document , verbose , namespace , ** kwargs )
84130 except client .rest .ApiException as api_exception :
85131 api_exceptions .append (api_exception )
86132 # In case we have exceptions waiting for us, raise them
@@ -89,7 +135,24 @@ def create_from_yaml(
89135
90136
91137def create_from_yaml_single_item (
92- k8s_client , yml_object , verbose = False , ** kwargs ):
138+ k8s_client ,
139+ yml_object ,
140+ verbose = False ,
141+ ** kwargs ):
142+ create_namespaced_from_yaml_single_item (
143+ k8s_client ,
144+ yml_object ,
145+ verbose ,
146+ namespace = "default" ,
147+ ** kwargs )
148+
149+
150+ def create_namespaced_from_yaml_single_item (
151+ k8s_client ,
152+ yml_object ,
153+ verbose = False ,
154+ namespace = "default" ,
155+ ** kwargs ):
93156 group , _ , version = yml_object ["apiVersion" ].partition ("/" )
94157 if version == "" :
95158 version = group
@@ -108,8 +171,6 @@ def create_from_yaml_single_item(
108171 # if any
109172 if "namespace" in yml_object ["metadata" ]:
110173 namespace = yml_object ["metadata" ]["namespace" ]
111- else :
112- namespace = "default"
113174 # Expect the user to create namespaced objects more often
114175 if hasattr (k8s_api , "create_namespaced_{0}" .format (kind )):
115176 resp = getattr (k8s_api , "create_namespaced_{0}" .format (kind ))(
0 commit comments