From e2ddbf60dbcdfea3b0e3218f0f360a081e96e6f7 Mon Sep 17 00:00:00 2001 From: Ari Zellner Date: Mon, 22 May 2017 14:57:50 +0300 Subject: [PATCH] collect container defninition limits --- .../container_manager/refresh_parser.rb | 41 +++++++++++++------ .../container_manager/refresh_parser_spec.rb | 32 +++++++++++++++ 2 files changed, 60 insertions(+), 13 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 da2495240c..7fca25a87b 100644 --- a/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb +++ b/app/models/manageiq/providers/kubernetes/container_manager/refresh_parser.rb @@ -1017,22 +1017,27 @@ def parse_conditions(entity) def parse_container_spec(container_spec, pod_id) new_result = { - :ems_ref => "#{pod_id}_#{container_spec.name}_#{container_spec.image}", - :name => container_spec.name, - :image => container_spec.image, - :image_pull_policy => container_spec.imagePullPolicy, - :command => container_spec.command ? Shellwords.join(container_spec.command) : nil, - :memory => container_spec.memory, + :ems_ref => "#{pod_id}_#{container_spec.name}_#{container_spec.image}", + :name => container_spec.name, + :image => container_spec.image, + :image_pull_policy => container_spec.imagePullPolicy, + :command => container_spec.command ? Shellwords.join(container_spec.command) : nil, + :memory => container_spec.memory, # https://github.com/GoogleCloudPlatform/kubernetes/blob/0b801a91b15591e2e6e156cf714bfb866807bf30/pkg/api/v1beta3/types.go#L815 - :cpu_cores => container_spec.cpu.to_f / 1000, - :capabilities_add => container_spec.securityContext.try(:capabilities).try(:add).to_a.join(','), - :capabilities_drop => container_spec.securityContext.try(:capabilities).try(:drop).to_a.join(','), - :privileged => container_spec.securityContext.try(:privileged), - :run_as_user => container_spec.securityContext.try(:runAsUser), - :run_as_non_root => container_spec.securityContext.try(:runAsNonRoot), - :security_context => parse_security_context(container_spec.securityContext) + :cpu_cores => container_spec.cpu.to_f / 1000, + :capabilities_add => container_spec.securityContext.try(:capabilities).try(:add).to_a.join(','), + :capabilities_drop => container_spec.securityContext.try(:capabilities).try(:drop).to_a.join(','), + :privileged => container_spec.securityContext.try(:privileged), + :run_as_user => container_spec.securityContext.try(:runAsUser), + :run_as_non_root => container_spec.securityContext.try(:runAsNonRoot), + :security_context => parse_security_context(container_spec.securityContext), + :limit_cpu_cores => parse_quantity(container_spec.try(:resources).try(:limits).try(:cpu)), + :limit_memory_bytes => parse_quantity(container_spec.try(:resources).try(:limits).try(:memory)), + :request_cpu_cores => parse_quantity(container_spec.try(:resources).try(:requests).try(:cpu)), + :request_memory_bytes => parse_quantity(container_spec.try(:resources).try(:requests).try(:memory)) } ports = container_spec.ports + new_result[:container_port_configs] = Array(ports).collect do |port_entry| parse_container_port_config(port_entry, pod_id, container_spec.name) end @@ -1044,6 +1049,16 @@ def parse_container_spec(container_spec, pod_id) new_result end + def parse_quantity(resource) # parse a string with a suffix into a int\float + return nil if resource.nil? + + begin + resource.iec_60027_2_to_i + rescue + resource.decimal_si_to_f + end + end + def parse_container_status(container, pod_id) h = { :type => 'ManageIQ::Providers::Kubernetes::ContainerManager::Container', 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 b1a4e6249a..f9f900bfec 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 @@ -1227,5 +1227,37 @@ ) end end + + describe "parse_quantity" do + let(:container_spec) do + array_recursive_ostruct( + :name => "mongodb", + :image => "centos/mongodb-32-centos7@sha256:02685168dd84c9119f8ab635078eec8697442fba93a7b342095e03b31aa8c5dd", + :ports => [{:containerPort => 27_017, :protocol => "TCP"}], + :resources => { + :limits => { + :cpu => "3500m", + :memory => "512Mi" + }, + :requests => { + #:cpu => nil + :memory => "1.2e6" + } + }, + :volumeMounts => [] + ) + end + + let(:pod_id) { "95b9aa14-7186-11e7-8ac6-001a4a162683" } + + it "handles parsing of quantities in container spec limits" do + expect(parser.parse_container_spec(container_spec, pod_id)).to include( + :limit_cpu_cores => 3.5, + :limit_memory_bytes => 536_870_912, + :request_cpu_cores => nil, + :request_memory_bytes => 1_200_000.0 + ) + end + end end end