Skip to content

Commit

Permalink
Merge pull request #22 from zeari/get_anon_limits
Browse files Browse the repository at this point in the history
collect container definition limits
  • Loading branch information
Mooli Tayer authored Jul 30, 2017
2 parents 0f62a51 + e2ddbf6 commit 0c4e816
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0c4e816

Please sign in to comment.