diff --git a/examples/custom_object.py b/examples/custom_object.py index 55e359f5bf..a8ac5169ac 100644 --- a/examples/custom_object.py +++ b/examples/custom_object.py @@ -29,6 +29,19 @@ - name: v1 served: true storage: true + schema: + openAPIV3Schema: + type: object + properties: + spec: + type: object + properties: + cronSpec: + type: string + image: + type: string + replicas: + type: integer scope: Namespaced names: plural: crontabs @@ -80,6 +93,28 @@ def main(): print("Resource details:") pprint(resource) + # patch the resource + resource = api.patch_namespaced_custom_object( + group="stable.example.com", + version="v1", + name="my-new-cron-object", + namespace="default", + plural="crontabs", + body={'image': 'my-awesome-cron-image:2'}, + content_type="application/merge-patch+json" + ) + + # get the resource and print out data + resource = api.get_namespaced_custom_object( + group="stable.example.com", + version="v1", + name="my-new-cron-object", + namespace="default", + plural="crontabs", + ) + print("Resource details:") + pprint(resource) + # delete it api.delete_namespaced_custom_object( group="stable.example.com", diff --git a/kubernetes/client/api/core_v1_api.py b/kubernetes/client/api/core_v1_api.py index f6020c2d0f..940087f524 100644 --- a/kubernetes/client/api/core_v1_api.py +++ b/kubernetes/client/api/core_v1_api.py @@ -18250,6 +18250,7 @@ def patch_namespaced_config_map_with_http_info(self, name, namespace, body, **kw all_params.extend( [ 'async_req', + 'content_type', '_return_http_data_only', '_preload_content', '_request_timeout' @@ -18308,8 +18309,10 @@ def patch_namespaced_config_map_with_http_info(self, name, namespace, body, **kw ['application/json', 'application/yaml', 'application/vnd.kubernetes.protobuf']) # noqa: E501 # HTTP header `Content-Type` - header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501 - ['application/json-patch+json', 'application/merge-patch+json', 'application/strategic-merge-patch+json', 'application/apply-patch+yaml']) # noqa: E501 + header_params['Content-Type'] = local_var_params.get('content_type', + self.api_client.select_header_content_type(['application/json-patch+json', + 'application/merge-patch+json', 'application/strategic-merge-patch+json', 'application/apply-patch+yaml'], + 'PATCH', body_params)) # Authentication setting auth_settings = ['BearerToken'] # noqa: E501 @@ -20590,6 +20593,7 @@ def patch_namespaced_service_with_http_info(self, name, namespace, body, **kwarg all_params.extend( [ 'async_req', + 'content_type', '_return_http_data_only', '_preload_content', '_request_timeout' @@ -20648,8 +20652,10 @@ def patch_namespaced_service_with_http_info(self, name, namespace, body, **kwarg ['application/json', 'application/yaml', 'application/vnd.kubernetes.protobuf']) # noqa: E501 # HTTP header `Content-Type` - header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501 - ['application/json-patch+json', 'application/merge-patch+json', 'application/strategic-merge-patch+json', 'application/apply-patch+yaml']) # noqa: E501 + header_params['Content-Type'] = local_var_params.get('content_type', + self.api_client.select_header_content_type(['application/json-patch+json', + 'application/merge-patch+json', 'application/strategic-merge-patch+json', 'application/apply-patch+yaml'], + 'PATCH', body_params)) # Authentication setting auth_settings = ['BearerToken'] # noqa: E501 diff --git a/kubernetes/client/api/custom_objects_api.py b/kubernetes/client/api/custom_objects_api.py index 1186b0a0f5..337b4b680e 100644 --- a/kubernetes/client/api/custom_objects_api.py +++ b/kubernetes/client/api/custom_objects_api.py @@ -2336,6 +2336,7 @@ def patch_cluster_custom_object_with_http_info(self, group, version, plural, nam all_params.extend( [ 'async_req', + 'content_type', '_return_http_data_only', '_preload_content', '_request_timeout' @@ -2404,8 +2405,8 @@ def patch_cluster_custom_object_with_http_info(self, group, version, plural, nam ['application/json']) # noqa: E501 # HTTP header `Content-Type` - header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501 - ['application/merge-patch+json']) # noqa: E501 + header_params['Content-Type'] = local_var_params.get('content_type', self.api_client.\ + select_header_content_type(['application/merge-patch+json'], 'PATCH', body_params)) # Authentication setting auth_settings = ['BearerToken'] # noqa: E501 @@ -2505,6 +2506,7 @@ def patch_cluster_custom_object_scale_with_http_info(self, group, version, plura all_params.extend( [ 'async_req', + 'content_type', '_return_http_data_only', '_preload_content', '_request_timeout' @@ -2573,8 +2575,8 @@ def patch_cluster_custom_object_scale_with_http_info(self, group, version, plura ['application/json', 'application/yaml', 'application/vnd.kubernetes.protobuf']) # noqa: E501 # HTTP header `Content-Type` - header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501 - ['application/merge-patch+json']) # noqa: E501 + header_params['Content-Type'] = local_var_params.get('content_type', self.api_client.\ + select_header_content_type(['application/merge-patch+json'], 'PATCH', body_params)) # Authentication setting auth_settings = ['BearerToken'] # noqa: E501 @@ -2674,6 +2676,7 @@ def patch_cluster_custom_object_status_with_http_info(self, group, version, plur all_params.extend( [ 'async_req', + 'content_type', '_return_http_data_only', '_preload_content', '_request_timeout' @@ -2742,8 +2745,8 @@ def patch_cluster_custom_object_status_with_http_info(self, group, version, plur ['application/json', 'application/yaml', 'application/vnd.kubernetes.protobuf']) # noqa: E501 # HTTP header `Content-Type` - header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501 - ['application/merge-patch+json']) # noqa: E501 + header_params['Content-Type'] = local_var_params.get('content_type', self.api_client.\ + select_header_content_type(['application/merge-patch+json'], 'PATCH', body_params)) # Authentication setting auth_settings = ['BearerToken'] # noqa: E501 @@ -2846,6 +2849,7 @@ def patch_namespaced_custom_object_with_http_info(self, group, version, namespac all_params.extend( [ 'async_req', + 'content_type', '_return_http_data_only', '_preload_content', '_request_timeout' @@ -2920,8 +2924,8 @@ def patch_namespaced_custom_object_with_http_info(self, group, version, namespac ['application/json']) # noqa: E501 # HTTP header `Content-Type` - header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501 - ['application/merge-patch+json']) # noqa: E501 + header_params['Content-Type'] = local_var_params.get('content_type', self.api_client.\ + select_header_content_type(['application/merge-patch+json'], 'PATCH', body_params)) # Authentication setting auth_settings = ['BearerToken'] # noqa: E501 @@ -3024,6 +3028,7 @@ def patch_namespaced_custom_object_scale_with_http_info(self, group, version, na all_params.extend( [ 'async_req', + 'content_type', '_return_http_data_only', '_preload_content', '_request_timeout' @@ -3098,8 +3103,8 @@ def patch_namespaced_custom_object_scale_with_http_info(self, group, version, na ['application/json', 'application/yaml', 'application/vnd.kubernetes.protobuf']) # noqa: E501 # HTTP header `Content-Type` - header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501 - ['application/merge-patch+json']) # noqa: E501 + header_params['Content-Type'] = local_var_params.get('content_type', self.api_client.\ + select_header_content_type(['application/merge-patch+json'], 'PATCH', body_params)) # Authentication setting auth_settings = ['BearerToken'] # noqa: E501 @@ -3202,6 +3207,7 @@ def patch_namespaced_custom_object_status_with_http_info(self, group, version, n all_params.extend( [ 'async_req', + 'content_type', '_return_http_data_only', '_preload_content', '_request_timeout' @@ -3276,8 +3282,8 @@ def patch_namespaced_custom_object_status_with_http_info(self, group, version, n ['application/json', 'application/yaml', 'application/vnd.kubernetes.protobuf']) # noqa: E501 # HTTP header `Content-Type` - header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501 - ['application/merge-patch+json']) # noqa: E501 + header_params['Content-Type'] = local_var_params.get('content_type', self.api_client.\ + select_header_content_type(['application/merge-patch+json'], 'PATCH', body_params)) # Authentication setting auth_settings = ['BearerToken'] # noqa: E501 diff --git a/kubernetes/client/api_client.py b/kubernetes/client/api_client.py index ef56f4eb7d..d77c2a9783 100644 --- a/kubernetes/client/api_client.py +++ b/kubernetes/client/api_client.py @@ -494,7 +494,7 @@ def select_header_accept(self, accepts): else: return ', '.join(accepts) - def select_header_content_type(self, content_types): + def select_header_content_type(self, content_types, method=None, body=None): """Returns `Content-Type` based on an array of content_types provided. :param content_types: List of content-types. @@ -505,6 +505,21 @@ def select_header_content_type(self, content_types): content_types = [x.lower() for x in content_types] + if method and body and method == 'PATCH': + # try to select valid content_type + if 'application/json-patch+json' in content_types and \ + 'application/merge-patch+json' in content_types: + if isinstance(body, list): + return 'application/json-patch+json' + else: + # --- + # kubernetes specific - application/strategic-merge-patch+json + # applied as a patch or subclass + if 'application/strategic-merge-patch+json' in content_types: + return 'application/strategic-merge-patch+json' + # --- + return 'application/merge-patch+json' + if 'application/json' in content_types or '*/*' in content_types: return 'application/json' else: diff --git a/kubernetes/client/rest.py b/kubernetes/client/rest.py index 38fe5452df..544707e914 100644 --- a/kubernetes/client/rest.py +++ b/kubernetes/client/rest.py @@ -157,10 +157,6 @@ def request(self, method, url, query_params=None, headers=None, if query_params: url += '?' + urlencode(query_params) if re.search('json', headers['Content-Type'], re.IGNORECASE): - if headers['Content-Type'] == 'application/json-patch+json': - if not isinstance(body, list): - headers['Content-Type'] = \ - 'application/strategic-merge-patch+json' request_body = None if body is not None: request_body = json.dumps(body)