From 49d74ab165ce79496bd0c73c7cb131cfb917c004 Mon Sep 17 00:00:00 2001 From: Beni Cherniavsky-Paskin Date: Tue, 20 Jun 2017 05:20:14 +0300 Subject: [PATCH 01/10] Simplify service fetching relevant pods parse_endpoint() is already storing :container_groups to a list of parsed pods, so the 2nd lookup in parse_service() was rebuilding the same list. --- .../kubernetes/container_manager/refresh_parser.rb | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb index b2b3554892..2ed44a0796 100644 --- a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb +++ b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb @@ -532,19 +532,10 @@ def parse_service(service) if new_result[:ems_ref].nil? # Typically this happens for kubernetes services new_result[:ems_ref] = "#{new_result[:namespace]}_#{new_result[:name]}" end - container_groups = [] - endpoint_container_groups = @data_index.fetch_path( + container_groups = @data_index.fetch_path( :container_endpoints, :by_namespace_and_name, new_result[:namespace], new_result[:name], :container_groups) - endpoint_container_groups ||= [] - - endpoint_container_groups.each do |group| - cg = @data_index.fetch_path( - path_for_entity("pod"), :by_namespace_and_name, group[:namespace], - group[:name]) - container_groups << cg unless cg.nil? - end labels = parse_labels(service) new_result.merge!( From 2b348b32ef137b154c4204b7c32ec4225fd8d069 Mon Sep 17 00:00:00 2001 From: Beni Cherniavsky-Paskin Date: Tue, 20 Jun 2017 05:33:08 +0300 Subject: [PATCH 02/10] Lift pod->node linking into get_pods() --- .../kubernetes/container_manager/refresh_parser.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb index 2ed44a0796..f1327b9472 100644 --- a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb +++ b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb @@ -87,6 +87,8 @@ def get_pods(inventory) key = path_for_entity("pod") process_collection(inventory["pod"], key) { |n| parse_pod(n) } @data[key].each do |cg| + node_name = cg.delete(:container_node_name) + cg[:container_node] = node_name && @data_index.fetch_path(path_for_entity("node"), :by_name, node_name) @data_index.store_path(key, :by_namespace_and_name, cg[:namespace], cg[:name], cg) end @@ -575,16 +577,12 @@ def parse_pod(pod) :phase => pod.status.phase, :message => pod.status.message, :reason => pod.status.reason, - :container_node => nil, + :container_node_name => pod.spec.nodeName, :container_definitions => [], :container_replicator => nil, :build_pod_name => pod.metadata.try(:annotations).try("openshift.io/build.name".to_sym) ) - unless pod.spec.nodeName.nil? - new_result[:container_node] = @data_index.fetch_path(path_for_entity("node"), :by_name, pod.spec.nodeName) - end - new_result[:project] = @data_index.fetch_path(path_for_entity("namespace"), :by_name, pod.metadata.namespace) # TODO, map volumes From 0e63c0654ff550e9bc39fab13457cf6fa450180d Mon Sep 17 00:00:00 2001 From: Beni Cherniavsky-Paskin Date: Tue, 20 Jun 2017 05:36:20 +0300 Subject: [PATCH 03/10] Lift additional_attributes injection into node to get_nodes() --- .../providers/kubernetes/container_manager/refresh_parser.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb index f1327b9472..8639238271 100644 --- a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb +++ b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb @@ -59,6 +59,7 @@ def get_nodes(inventory) key = path_for_entity("node") process_collection(inventory["node"], key) { |n| parse_node(n) } @data[key].each do |cn| + cn[:additional_attributes] = @data_index.fetch_path(:additional_attributes, :by_node, cn[:name]) @data_index.store_path(key, :by_name, cn[:name], cn) end end @@ -523,8 +524,6 @@ def parse_node(node) new_result[:container_conditions] = parse_conditions(node) cross_link_node(new_result) - new_result[:additional_attributes] = @data_index.fetch_path(:additional_attributes, :by_node, node.metadata.name) - new_result end From d4e48e4ab92c2b2289629332be00b15840111c72 Mon Sep 17 00:00:00 2001 From: Beni Cherniavsky-Paskin Date: Tue, 20 Jun 2017 06:00:39 +0300 Subject: [PATCH 04/10] Lift some lookups into get_services() --- .../container_manager/refresh_parser.rb | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb index 8639238271..bcdb60685e 100644 --- a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb +++ b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb @@ -68,6 +68,19 @@ def get_services(inventory) key = path_for_entity("service") process_collection(inventory["service"], key) { |s| parse_service(s) } @data[key].each do |se| + se[:container_groups] = @data_index.fetch_path( + :container_endpoints, :by_namespace_and_name, se[:namespace], se[:name], + :container_groups + ) + se[:project] = @data_index.fetch_path(path_for_entity("namespace"), :by_name, se[:namespace]) + + # TODO: this loop only uses last port config - BUG? + se[:container_service_port_configs].each do |pc| + se[:container_image_registry] = @data_index.fetch_path( + :container_image_registry, :by_host_and_port, "#{se[:portal_ip]}:#{pc[:port]}" + ) + end + @data_index.store_path(key, :by_namespace_and_name, se[:namespace], se[:name], se) end end @@ -534,10 +547,6 @@ def parse_service(service) new_result[:ems_ref] = "#{new_result[:namespace]}_#{new_result[:name]}" end - container_groups = @data_index.fetch_path( - :container_endpoints, :by_namespace_and_name, new_result[:namespace], - new_result[:name], :container_groups) - labels = parse_labels(service) new_result.merge!( # TODO: We might want to change portal_ip to clusterIP @@ -547,20 +556,13 @@ def parse_service(service) :labels => labels, :tags => map_labels('ContainerService', labels), :selector_parts => parse_selector_parts(service), - :container_groups => container_groups ) ports = service.spec.ports new_result[:container_service_port_configs] = Array(ports).collect do |port_entry| - pc = parse_service_port_config(port_entry, new_result[:ems_ref]) - new_result[:container_image_registry] = @data_index.fetch_path( - :container_image_registry, :by_host_and_port, "#{new_result[:portal_ip]}:#{pc[:port]}" - ) - pc + parse_service_port_config(port_entry, new_result[:ems_ref]) end - new_result[:project] = @data_index.fetch_path(path_for_entity("namespace"), :by_name, - service.metadata.namespace) new_result end From ba6c5dcb30d8f51c65ae289a1d62b8c059f44528 Mon Sep 17 00:00:00 2001 From: Beni Cherniavsky-Paskin Date: Tue, 20 Jun 2017 06:15:53 +0300 Subject: [PATCH 05/10] Lift some lookups into get_pods() --- .../container_manager/refresh_parser.rb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb index bcdb60685e..57b2b51297 100644 --- a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb +++ b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb @@ -103,6 +103,12 @@ def get_pods(inventory) @data[key].each do |cg| node_name = cg.delete(:container_node_name) cg[:container_node] = node_name && @data_index.fetch_path(path_for_entity("node"), :by_name, node_name) + cg[:project] = @data_index.fetch_path(path_for_entity("namespace"), :by_name, cg[:namespace]) + replicator_ref = cg.delete(:container_replicator_ref) + cg[:container_replicator] = replicator_ref && @data_index.fetch_path( + path_for_entity("replication_controller"), :by_namespace_and_name, + replicator_ref[:namespace], replicator_ref[:name] + ) @data_index.store_path(key, :by_namespace_and_name, cg[:namespace], cg[:name], cg) end @@ -580,12 +586,9 @@ def parse_pod(pod) :reason => pod.status.reason, :container_node_name => pod.spec.nodeName, :container_definitions => [], - :container_replicator => nil, :build_pod_name => pod.metadata.try(:annotations).try("openshift.io/build.name".to_sym) ) - new_result[:project] = @data_index.fetch_path(path_for_entity("namespace"), :by_name, pod.metadata.namespace) - # TODO, map volumes # TODO, podIP containers_index = {} @@ -602,6 +605,7 @@ def parse_pod(pod) ) end + new_result[:container_replicator_ref] = nil # NOTE: what we are trying to access here is the attribute: # pod.metadata.annotations.kubernetes.io/created-by # but 'annotations' may be nil. The weird attribute name is @@ -611,9 +615,10 @@ def parse_pod(pod) # NOTE: the annotation content is JSON, so it needs to be parsed createdby = JSON.parse(createdby_txt) if createdby.kind_of?(Hash) && !createdby['reference'].nil? - new_result[:container_replicator] = @data_index.fetch_path( - path_for_entity("replication_controller"), :by_namespace_and_name, - createdby['reference']['namespace'], createdby['reference']['name']) + new_result[:container_replicator_ref] = { + :namespace => createdby['reference']['namespace'], + :name => createdby['reference']['name'] + } end end From 60a08788388770a7762fc9527160e6e60a794542 Mon Sep 17 00:00:00 2001 From: Beni Cherniavsky-Paskin Date: Thu, 22 Jun 2017 01:11:10 +0300 Subject: [PATCH 06/10] Lift project lookup into get_resource_quotas(), get_limit_ranges(), get_replication_controllers() --- .../container_manager/refresh_parser.rb | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb index 57b2b51297..3f29467839 100644 --- a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb +++ b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb @@ -92,6 +92,7 @@ def get_replication_controllers(inventory) parse_replication_controllers(rc) end @data[key].each do |rc| + rc[:project] = @data_index.fetch_path(path_for_entity("namespace"), :by_name, rc[:namespace]) @data_index.store_path(key, :by_namespace_and_name, rc[:namespace], rc[:name], rc) end @@ -149,11 +150,19 @@ def get_persistent_volume_claims(inventory) end def get_resource_quotas(inventory) - process_collection(inventory["resource_quota"], path_for_entity("resource_quota")) { |n| parse_quota(n) } + key = path_for_entity("resource_quota") + process_collection(inventory["resource_quota"], key) { |n| parse_quota(n) } + @data[key].each do |q| + q[:project] = @data_index.fetch_path(path_for_entity("namespace"), :by_name, q.delete(:namespace)) + end end def get_limit_ranges(inventory) - process_collection(inventory["limit_range"], path_for_entity("limit_range")) { |n| parse_range(n) } + key = path_for_entity("limit_range") + process_collection(inventory["limit_range"], key) { |n| parse_range(n) } + @data[key].each do |r| + r[:project] = @data_index.fetch_path(path_for_entity("namespace"), :by_name, r.delete(:namespace)) + end end def get_component_statuses(inventory) @@ -710,12 +719,7 @@ def parse_persistent_volume_claim(claim) end def parse_quota(resource_quota) - new_result = parse_base_item(resource_quota).except(:namespace) - new_result[:project] = @data_index.fetch_path( - path_for_entity("namespace"), - :by_name, - resource_quota.metadata.namespace - ) + new_result = parse_base_item(resource_quota) new_result[:container_quota_items] = parse_quota_items resource_quota new_result end @@ -746,12 +750,7 @@ def parse_quota_items(resource_quota) end def parse_range(limit_range) - new_result = parse_base_item(limit_range).except(:namespace) - new_result[:project] = @data_index.fetch_path( - path_for_entity("namespace"), - :by_name, - limit_range.metadata.namespace - ) + new_result = parse_base_item(limit_range) new_result[:container_limit_items] = parse_range_items limit_range new_result end @@ -813,9 +812,6 @@ def parse_replication_controllers(container_replicator) :tags => map_labels('ContainerReplicator', labels), :selector_parts => parse_selector_parts(container_replicator) ) - - new_result[:project] = @data_index.fetch_path(path_for_entity("namespace"), :by_name, - container_replicator.metadata.namespace) new_result end From 39ab5944c094482d253396fbcc76cb446907c139 Mon Sep 17 00:00:00 2001 From: Beni Cherniavsky-Paskin Date: Wed, 28 Jun 2017 13:05:43 +0300 Subject: [PATCH 07/10] Lift PVC lookup into get_persistent_volumes() --- .../container_manager/refresh_parser.rb | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb index 3f29467839..bf97d198dc 100644 --- a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb +++ b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb @@ -137,6 +137,11 @@ def get_persistent_volumes(inventory) key = path_for_entity("persistent_volume") process_collection(inventory["persistent_volume"], key) { |n| parse_persistent_volume(n) } @data[key].each do |pv| + pvc_ref = pv.delete(:persistent_volume_claim_ref) + pv[:persistent_volume_claim] = pvc_ref && @data_index.fetch_path( + path_for_entity("persistent_volume_claim"), + :by_namespace_and_name, pvc_ref[:namespace], pvc_ref[:name] + ) @data_index.store_path(key, :by_name, pv[:name], pv) end end @@ -669,21 +674,21 @@ def parse_persistent_volume(persistent_volume) new_result = parse_base_item(persistent_volume) new_result.merge!(parse_volume_source(persistent_volume.spec)) new_result.merge!( - :type => 'PersistentVolume', - :capacity => parse_resource_list(persistent_volume.spec.capacity.to_h), - :access_modes => persistent_volume.spec.accessModes.join(','), - :reclaim_policy => persistent_volume.spec.persistentVolumeReclaimPolicy, - :status_phase => persistent_volume.status.phase, - :status_message => persistent_volume.status.message, - :status_reason => persistent_volume.status.reason, - :persistent_volume_claim => nil + :type => 'PersistentVolume', + :capacity => parse_resource_list(persistent_volume.spec.capacity.to_h), + :access_modes => persistent_volume.spec.accessModes.join(','), + :reclaim_policy => persistent_volume.spec.persistentVolumeReclaimPolicy, + :status_phase => persistent_volume.status.phase, + :status_message => persistent_volume.status.message, + :status_reason => persistent_volume.status.reason, + :persistent_volume_claim_ref => nil, ) unless persistent_volume.spec.claimRef.nil? - new_result[:persistent_volume_claim] = @data_index.fetch_path(path_for_entity("persistent_volume_claim"), - :by_namespace_and_name, - persistent_volume.spec.claimRef.namespace, - persistent_volume.spec.claimRef.name) + new_result[:persistent_volume_claim_ref] = { + :namespace => persistent_volume.spec.claimRef.namespace, + :name => persistent_volume.spec.claimRef.name, + } end new_result From d79deee4d2a58e59d7b78b1ac46ef5919fe783d4 Mon Sep 17 00:00:00 2001 From: Beni Cherniavsky-Paskin Date: Wed, 28 Jun 2017 13:53:20 +0300 Subject: [PATCH 08/10] Lift pods lookup into get_endpoints() --- .../container_manager/refresh_parser.rb | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb index bf97d198dc..4105905f50 100644 --- a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb +++ b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb @@ -119,6 +119,16 @@ def get_endpoints(inventory) process_collection(inventory["endpoint"], :container_endpoints) { |n| parse_endpoint(n) } @data[:container_endpoints].each do |ep| + ep[:container_groups] = [] + ep.delete(:container_groups_refs).each do |ref| + next if ref.nil? + cg = @data_index.fetch_path( + path_for_entity("pod"), + :by_namespace_and_name, ref[:namespace], ref[:name] + ) + ep[:container_groups] << cg unless cg.nil? + end + @data_index.store_path(:container_endpoints, :by_namespace_and_name, ep[:namespace], ep[:name], ep) end @@ -647,16 +657,15 @@ def parse_pod(pod) def parse_endpoint(entity) new_result = parse_base_item(entity) - new_result[:container_groups] = [] + new_result[:container_groups_refs] = [] (entity.subsets || []).each do |subset| (subset.addresses || []).each do |address| - next if address.targetRef.try(:kind) != 'Pod' - cg = @data_index.fetch_path( - path_for_entity("pod"), :by_namespace_and_name, - address.targetRef.namespace, address.targetRef.name - ) - new_result[:container_groups] << cg unless cg.nil? + next if address.targetRef.nil? || address.targetRef.kind != 'Pod' + new_result[:container_groups_refs] << { + :namespace => address.targetRef.namespace, + :name => address.targetRef.name, + } end end From a25009f62da4e8094509a725152a747b8575cbfd Mon Sep 17 00:00:00 2001 From: Beni Cherniavsky-Paskin Date: Sun, 25 Jun 2017 19:31:54 +0300 Subject: [PATCH 09/10] Update refresh_parser_spec --- .../container_manager/refresh_parser_spec.rb | 213 ++++++++---------- 1 file changed, 100 insertions(+), 113 deletions(-) diff --git a/spec/models/manageiq/providers/kubernetes/container_manager/refresh_parser_spec.rb b/spec/models/manageiq/providers/kubernetes/container_manager/refresh_parser_spec.rb index 3b57d797cd..1c2482ad7b 100644 --- a/spec/models/manageiq/providers/kubernetes/container_manager/refresh_parser_spec.rb +++ b/spec/models/manageiq/providers/kubernetes/container_manager/refresh_parser_spec.rb @@ -480,7 +480,7 @@ :ems_ref => 'af3d1a10-44c0-11e5-b186-0aaeec44370e', :ems_created_on => '2015-08-17T09:16:46Z', :resource_version => '165339', - :project => nil, + :namespace => 'test-namespace', :container_quota_items => [ { :resource => "cpu", @@ -508,7 +508,7 @@ :ems_ref => 'af3d1a10-44c0-11e5-b186-0aaeec44370e', :ems_created_on => '2015-08-17T09:16:46Z', :resource_version => '165339', - :project => nil, + :namespace => 'test-namespace', :container_quota_items => []) end @@ -530,7 +530,7 @@ :ems_ref => 'af3d1a10-44c0-11e5-b186-0aaeec44370e', :ems_created_on => '2015-08-17T09:16:46Z', :resource_version => '165339', - :project => nil, + :namespace => 'test-namespace', :container_quota_items => [ { :resource => "cpu", @@ -565,7 +565,7 @@ :ems_ref => 'af3d1a10-44c0-11e5-b186-0aaeec44370e', :ems_created_on => '2015-08-17T09:16:46Z', :resource_version => '2', - :project => nil, + :namespace => 'test-namespace', :container_limit_items => [ { :item_type => "Container", @@ -606,7 +606,7 @@ :ems_ref => 'af3d1a10-44c0-11e5-b186-0aaeec44370e', :ems_created_on => '2015-08-17T09:16:46Z', :resource_version => '2', - :project => nil, + :namespace => 'test-namespace', :container_limit_items => [] } ranges.each do |range| @@ -805,7 +805,6 @@ :namespace => nil, :resource_version => '369104', :type => 'ManageIQ::Providers::Kubernetes::ContainerManager::ContainerNode', - :additional_attributes => nil }) end @@ -859,7 +858,6 @@ :namespace => nil, :resource_version => '3691041', :type => 'ManageIQ::Providers::Kubernetes::ContainerManager::ContainerNode', - :additional_attributes => nil }) end @@ -905,33 +903,52 @@ :namespace => nil, :resource_version => '369104', :type => 'ManageIQ::Providers::Kubernetes::ContainerManager::ContainerNode', - :additional_attributes => nil }) end - it "handles node with single custom attribute" do - parser.get_additional_attributes( - "additional_attributes" => { "node/test-node/key" => "val" } + end + + describe "get_nodes" do + let(:test_node) do + RecursiveOpenStruct.new( + :metadata => { + :name => 'test-node', + :uid => 'f0c1fe7e-9c09-11e5-bb22-28d2447dcefe', + :resourceVersion => '369104', + :creationTimestamp => '2016-01-01T11:10:21Z' + }, + :spec => { + :providerID => 'aws:///zone/aws-id' + }, + :status => { + :capacity => {} + } ) + end + let(:test_node1) do + RecursiveOpenStruct.new( + :metadata => { + :name => 'test-node1', + :uid => 'f0c1fe7e-9c09-11e5-bb22-28d2447dcefe', + :resourceVersion => '369104', + :creationTimestamp => '2016-01-01T11:10:21Z' + }, + :spec => { + :providerID => 'aws:///zone/aws-id' + }, + :status => { + :capacity => {} + } + ) + end - expect( - parser.send( - :parse_node, - RecursiveOpenStruct.new( - :metadata => { - :name => 'test-node', - :uid => 'f0c1fe7e-9c09-11e5-bb22-28d2447dcefe', - :resourceVersion => '369104', - :creationTimestamp => '2016-01-01T11:10:21Z' - }, - :spec => { - :providerID => 'aws:///zone/aws-id' - }, - :status => { - :capacity => {} - } - ), - ) - ).to eq( + it "handles node with single custom attribute" do + inventory = { + "additional_attributes" => { "node/test-node/key" => "val" }, + "node" => [test_node] + } + parser.get_additional_attributes(inventory) + parser.get_nodes(inventory) + expect(parser.instance_variable_get(:@data)[:container_nodes]).to eq([{ :name => 'test-node', :ems_ref => 'f0c1fe7e-9c09-11e5-bb22-28d2447dcefe', :ems_created_on => '2016-01-01T11:10:21Z', @@ -956,33 +973,18 @@ :resource_version => '369104', :type => 'ManageIQ::Providers::Kubernetes::ContainerManager::ContainerNode', :additional_attributes => [{ :name => "key", :value => "val", :section => "additional_attributes" }] - ) + }]) end + it "handles node with multiple custom attributes" do - parser.get_additional_attributes( + inventory = { "additional_attributes" => { "node/test-node/key1" => "val1", - "node/test-node/key2" => "val2"} - ) - - expect( - parser.send( - :parse_node, - RecursiveOpenStruct.new( - :metadata => { - :name => 'test-node', - :uid => 'f0c1fe7e-9c09-11e5-bb22-28d2447dcefe', - :resourceVersion => '369104', - :creationTimestamp => '2016-01-01T11:10:21Z' - }, - :spec => { - :providerID => 'aws:///zone/aws-id' - }, - :status => { - :capacity => {} - } - ), - ) - ).to eq( + "node/test-node/key2" => "val2"}, + "node" => [test_node] + } + parser.get_additional_attributes(inventory) + parser.get_nodes(inventory) + expect(parser.instance_variable_get(:@data)[:container_nodes]).to eq([{ :name => 'test-node', :ems_ref => 'f0c1fe7e-9c09-11e5-bb22-28d2447dcefe', :ems_created_on => '2016-01-01T11:10:21Z', @@ -1008,33 +1010,18 @@ :type => 'ManageIQ::Providers::Kubernetes::ContainerManager::ContainerNode', :additional_attributes => [{ :name => "key1", :value => "val1", :section => "additional_attributes" }, { :name => "key2", :value => "val2", :section => "additional_attributes" }] - ) + }]) end + it "ignores custom attributes of a different node" do - parser.get_additional_attributes( + inventory = { "additional_attributes" => { "node/test-node1/key1" => "val1", - "node/test-node2/key2" => "val2"} - ) - - expect( - parser.send( - :parse_node, - RecursiveOpenStruct.new( - :metadata => { - :name => 'test-node1', - :uid => 'f0c1fe7e-9c09-11e5-bb22-28d2447dcefe', - :resourceVersion => '369104', - :creationTimestamp => '2016-01-01T11:10:21Z' - }, - :spec => { - :providerID => 'aws:///zone/aws-id' - }, - :status => { - :capacity => {} - } - ), - ) - ).to eq( + "node/test-node2/key2" => "val2"}, + "node" => [test_node1] + } + parser.get_additional_attributes(inventory) + parser.get_nodes(inventory) + expect(parser.instance_variable_get(:@data)[:container_nodes]).to eq([{ :name => 'test-node1', :ems_ref => 'f0c1fe7e-9c09-11e5-bb22-28d2447dcefe', :ems_created_on => '2016-01-01T11:10:21Z', @@ -1059,7 +1046,7 @@ :resource_version => '369104', :type => 'ManageIQ::Providers::Kubernetes::ContainerManager::ContainerNode', :additional_attributes => [{ :name => "key1", :value => "val1", :section => "additional_attributes" }] - ) + }]) end end @@ -1125,40 +1112,40 @@ ) )).to eq( { - :name => 'test-volume', - :ems_ref => '66213621-80a1-11e5-b907-28d2447dcefe', - :ems_created_on => '2015-12-06T11:10:21Z', - :namespace => nil, - :resource_version => '448015', - :type => 'PersistentVolume', - :status_phase => 'Available', - :access_modes => 'ReadWriteOnce', - :capacity => {:storage => 10.gigabytes}, - :claim_name => nil, - :common_fs_type => nil, - :common_partition => nil, - :common_path => '/tmp/data01', - :common_read_only => nil, - :common_secret => nil, - :common_volume_id => nil, - :empty_dir_medium_type => nil, - :gce_pd_name => nil, - :git_repository => nil, - :git_revision => nil, - :glusterfs_endpoint_name => nil, - :iscsi_iqn => nil, - :iscsi_lun => nil, - :iscsi_target_portal => nil, - :nfs_server => nil, - :persistent_volume_claim => nil, - :rbd_ceph_monitors => '', - :rbd_image => nil, - :rbd_keyring => nil, - :rbd_pool => nil, - :rbd_rados_user => nil, - :reclaim_policy => nil, - :status_message => nil, - :status_reason => nil + :name => 'test-volume', + :ems_ref => '66213621-80a1-11e5-b907-28d2447dcefe', + :ems_created_on => '2015-12-06T11:10:21Z', + :namespace => nil, + :resource_version => '448015', + :type => 'PersistentVolume', + :status_phase => 'Available', + :access_modes => 'ReadWriteOnce', + :capacity => {:storage => 10.gigabytes}, + :claim_name => nil, + :common_fs_type => nil, + :common_partition => nil, + :common_path => '/tmp/data01', + :common_read_only => nil, + :common_secret => nil, + :common_volume_id => nil, + :empty_dir_medium_type => nil, + :gce_pd_name => nil, + :git_repository => nil, + :git_revision => nil, + :glusterfs_endpoint_name => nil, + :iscsi_iqn => nil, + :iscsi_lun => nil, + :iscsi_target_portal => nil, + :nfs_server => nil, + :persistent_volume_claim_ref => nil, + :rbd_ceph_monitors => '', + :rbd_image => nil, + :rbd_keyring => nil, + :rbd_pool => nil, + :rbd_rados_user => nil, + :reclaim_policy => nil, + :status_message => nil, + :status_reason => nil }) end end From 7486371a4f440692f117caffb321ebb59991d096 Mon Sep 17 00:00:00 2001 From: Beni Cherniavsky-Paskin Date: Sun, 25 Jun 2017 19:37:51 +0300 Subject: [PATCH 10/10] Omit most fields from get_nodes test, they're covered in parse_node tests The purpose of this test is additional_attributes. Also included name for sanity. --- .../container_manager/refresh_parser_spec.rb | 104 +++++------------- 1 file changed, 25 insertions(+), 79 deletions(-) diff --git a/spec/models/manageiq/providers/kubernetes/container_manager/refresh_parser_spec.rb b/spec/models/manageiq/providers/kubernetes/container_manager/refresh_parser_spec.rb index 1c2482ad7b..cff0aff2e3 100644 --- a/spec/models/manageiq/providers/kubernetes/container_manager/refresh_parser_spec.rb +++ b/spec/models/manageiq/providers/kubernetes/container_manager/refresh_parser_spec.rb @@ -948,32 +948,14 @@ } parser.get_additional_attributes(inventory) parser.get_nodes(inventory) - expect(parser.instance_variable_get(:@data)[:container_nodes]).to eq([{ - :name => 'test-node', - :ems_ref => 'f0c1fe7e-9c09-11e5-bb22-28d2447dcefe', - :ems_created_on => '2016-01-01T11:10:21Z', - :container_conditions => [], - :identity_infra => 'aws:///zone/aws-id', - :labels => [], - :tags => [], - :lives_on_id => nil, - :lives_on_type => nil, - :max_container_groups => nil, - :computer_system => { - :hardware => { - :cpu_total_cores => nil, - :memory_mb => nil - }, - :operating_system => { - :distribution => nil, - :kernel_version => nil - } - }, - :namespace => nil, - :resource_version => '369104', - :type => 'ManageIQ::Providers::Kubernetes::ContainerManager::ContainerNode', - :additional_attributes => [{ :name => "key", :value => "val", :section => "additional_attributes" }] - }]) + expect(parser.instance_variable_get(:@data)[:container_nodes]).to match( + [ + a_hash_including( + :name => 'test-node', + :additional_attributes => [{ :name => "key", :value => "val", :section => "additional_attributes" }] + ) + ] + ) end it "handles node with multiple custom attributes" do @@ -984,33 +966,15 @@ } parser.get_additional_attributes(inventory) parser.get_nodes(inventory) - expect(parser.instance_variable_get(:@data)[:container_nodes]).to eq([{ - :name => 'test-node', - :ems_ref => 'f0c1fe7e-9c09-11e5-bb22-28d2447dcefe', - :ems_created_on => '2016-01-01T11:10:21Z', - :container_conditions => [], - :identity_infra => 'aws:///zone/aws-id', - :labels => [], - :tags => [], - :lives_on_id => nil, - :lives_on_type => nil, - :max_container_groups => nil, - :computer_system => { - :hardware => { - :cpu_total_cores => nil, - :memory_mb => nil - }, - :operating_system => { - :distribution => nil, - :kernel_version => nil - } - }, - :namespace => nil, - :resource_version => '369104', - :type => 'ManageIQ::Providers::Kubernetes::ContainerManager::ContainerNode', - :additional_attributes => [{ :name => "key1", :value => "val1", :section => "additional_attributes" }, - { :name => "key2", :value => "val2", :section => "additional_attributes" }] - }]) + expect(parser.instance_variable_get(:@data)[:container_nodes]).to match( + [ + a_hash_including( + :name => 'test-node', + :additional_attributes => [{ :name => "key1", :value => "val1", :section => "additional_attributes" }, + { :name => "key2", :value => "val2", :section => "additional_attributes" }] + ) + ] + ) end it "ignores custom attributes of a different node" do @@ -1021,32 +985,14 @@ } parser.get_additional_attributes(inventory) parser.get_nodes(inventory) - expect(parser.instance_variable_get(:@data)[:container_nodes]).to eq([{ - :name => 'test-node1', - :ems_ref => 'f0c1fe7e-9c09-11e5-bb22-28d2447dcefe', - :ems_created_on => '2016-01-01T11:10:21Z', - :container_conditions => [], - :identity_infra => 'aws:///zone/aws-id', - :labels => [], - :tags => [], - :lives_on_id => nil, - :lives_on_type => nil, - :max_container_groups => nil, - :computer_system => { - :hardware => { - :cpu_total_cores => nil, - :memory_mb => nil - }, - :operating_system => { - :distribution => nil, - :kernel_version => nil - } - }, - :namespace => nil, - :resource_version => '369104', - :type => 'ManageIQ::Providers::Kubernetes::ContainerManager::ContainerNode', - :additional_attributes => [{ :name => "key1", :value => "val1", :section => "additional_attributes" }] - }]) + expect(parser.instance_variable_get(:@data)[:container_nodes]).to match( + [ + a_hash_including( + :name => 'test-node1', + :additional_attributes => [{ :name => "key1", :value => "val1", :section => "additional_attributes" }] + ) + ] + ) end end