Perform an action from a dictionary containing valid kubernetes API object (i.e. List, Service, etc). Input: k8s_client: an ApiClient object, initialized with the client args. data: a dictionary holding valid kubernetes objects verbose: If True, print confirmation from the
(
k8s_client, data, verbose=False, namespace="default", apply=False, **kwargs
)
| 182 | |
| 183 | |
| 184 | def create_from_dict( |
| 185 | k8s_client, data, verbose=False, namespace="default", apply=False, **kwargs |
| 186 | ): |
| 187 | """ |
| 188 | Perform an action from a dictionary containing valid kubernetes |
| 189 | API object (i.e. List, Service, etc). |
| 190 | |
| 191 | Input: |
| 192 | k8s_client: an ApiClient object, initialized with the client args. |
| 193 | data: a dictionary holding valid kubernetes objects |
| 194 | verbose: If True, print confirmation from the create action. |
| 195 | Default is False. |
| 196 | namespace: string. Contains the namespace to create all |
| 197 | resources inside. The namespace must preexist otherwise |
| 198 | the resource creation will fail. If the API object in |
| 199 | the yaml file already contains a namespace definition |
| 200 | this parameter has no effect. |
| 201 | apply: bool. If True, use server-side apply for creating resources. |
| 202 | |
| 203 | Returns: |
| 204 | The created kubernetes API objects. |
| 205 | |
| 206 | Raises: |
| 207 | FailToCreateError which holds list of `client.rest.ApiException` |
| 208 | instances for each object that failed to create. |
| 209 | """ |
| 210 | # If it is a list type, will need to iterate its items |
| 211 | api_exceptions = [] |
| 212 | k8s_objects = [] |
| 213 | |
| 214 | if "List" in data["kind"]: |
| 215 | # Could be "List" or "Pod/Service/...List" |
| 216 | # This is a list type. iterate within its items |
| 217 | kind = data["kind"].replace("List", "") |
| 218 | for yml_object in data["items"]: |
| 219 | # Mitigate cases when server returns a xxxList object |
| 220 | # See kubernetes-client/python#586 |
| 221 | if kind != "": |
| 222 | yml_object["apiVersion"] = data["apiVersion"] |
| 223 | yml_object["kind"] = kind |
| 224 | try: |
| 225 | created = create_from_yaml_single_item( |
| 226 | k8s_client, |
| 227 | yml_object, |
| 228 | verbose, |
| 229 | namespace=namespace, |
| 230 | apply=apply, |
| 231 | **kwargs, |
| 232 | ) |
| 233 | k8s_objects.append(created) |
| 234 | except client.rest.ApiException as api_exception: |
| 235 | api_exceptions.append(api_exception) |
| 236 | else: |
| 237 | # This is a single object. Call the single item method |
| 238 | try: |
| 239 | created = create_from_yaml_single_item( |
| 240 | k8s_client, data, verbose, namespace=namespace, apply=apply, **kwargs |
| 241 | ) |
no test coverage detected