From 3b130d8ccaf2509eb56e0a4295e78e31a2c80d3b Mon Sep 17 00:00:00 2001 From: Andrey Chernykh Date: Fri, 18 Oct 2019 14:48:16 -0700 Subject: [PATCH] Add ability to patch status of an object --- lib/kubeclient/common.rb | 17 +++++++++++++++++ test/test_service.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/kubeclient/common.rb b/lib/kubeclient/common.rb index a51a4de9..4ce881b8 100644 --- a/lib/kubeclient/common.rb +++ b/lib/kubeclient/common.rb @@ -245,6 +245,11 @@ def define_entity_methods patch_entity(entity.resource_name, name, patch, 'strategic-merge-patch', namespace) end + define_singleton_method("patch_#{entity.method_names[0]}_status") \ + do |name, patch, namespace = nil| + patch_entity_status(entity.resource_name, name, patch, 'strategic-merge-patch', namespace) + end + define_singleton_method("json_patch_#{entity.method_names[0]}") \ do |name, patch, namespace = nil| patch_entity(entity.resource_name, name, patch, 'json-patch', namespace) @@ -395,6 +400,18 @@ def update_entity(resource_name, entity_config) format_response(@as, response.body) end + def patch_entity_status(resource_name, name, patch, strategy, namespace) + ns_prefix = build_namespace_prefix(namespace) + response = handle_exception do + rest_client[ns_prefix + resource_name + "/#{name}/status"] + .patch( + patch.to_json, + { 'Content-Type' => "application/#{strategy}+json" }.merge(@headers) + ) + end + format_response(@as, response.body) + end + def patch_entity(resource_name, name, patch, strategy, namespace) ns_prefix = build_namespace_prefix(namespace) response = handle_exception do diff --git a/test/test_service.rb b/test/test_service.rb index bd89f8f3..01f41c25 100644 --- a/test/test_service.rb +++ b/test/test_service.rb @@ -274,6 +274,40 @@ def test_patch_service end end + def test_patch_service_status + service = Kubeclient::Resource.new + name = 'my_service' + + service.metadata = {} + service.metadata.name = name + service.metadata.namespace = 'development' + + stub_core_api_list + expected_url = "http://localhost:8080/api/v1/namespaces/development/services/#{name}/status" + stub_request(:patch, expected_url) + .to_return(body: open_test_file('service_patch.json'), status: 200) + + patch = { + status: { + conditions: [ + { + type: 'StatusType', + status: 'True', + } + ] + } + } + + client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1') + service = client.patch_service_status(name, patch, 'development') + assert_kind_of(RecursiveOpenStruct, service) + + assert_requested(:patch, expected_url, times: 1) do |req| + data = JSON.parse(req.body) + data['status']['conditions'].find { |c| c['type'] == 'StatusType' }['status'] == 'True' + end + end + def test_json_patch_service service = Kubeclient::Resource.new name = 'my-service'