Skip to content

Commit

Permalink
Merge pull request #42 from zeari/unify_container_definition
Browse files Browse the repository at this point in the history
unify container and container definition
  • Loading branch information
Fryguy authored Jul 26, 2017
2 parents 05baa39 + b1bacea commit 00df730
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,6 @@ def initialize_inventory_collections(ems, _options)
)
initialize_container_conditions_collection(ems.container_groups)
initialize_custom_attributes_collections(ems.container_groups, %w(labels node_selectors))
@inv_collections[:container_definitions] =
::ManagerRefresh::InventoryCollection.new(
:model_class => ContainerDefinition,
:parent => ems,
:builder_params => {:ems_id => ems.id},
:association => :container_definitions,
# parser sets :ems_ref => "#{pod_id}_#{container_def.name}_#{container_def.image}"
:delete_method => :disconnect_inv,
)
@inv_collections[:container_volumes] =
::ManagerRefresh::InventoryCollection.new(
:model_class => ContainerVolume,
Expand Down Expand Up @@ -143,7 +134,7 @@ def initialize_inventory_collections(ems, _options)
:parent => ems,
:association => :container_env_vars,
# TODO: old save matches on all :name, :value, :field_path - does this matter?
:manager_ref => [:container_definition, :name],
:manager_ref => [:container, :name],
)
@inv_collections[:security_contexts] =
::ManagerRefresh::InventoryCollection.new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,11 @@ def get_pods_graph(inv)
:name => h.delete(:build_pod_name))

custom_attrs = h.extract!(:labels, :node_selector_parts)
children = h.extract!(:container_definitions, :containers, :container_conditions, :container_volumes)
children = h.extract!(:containers, :container_conditions, :container_volumes)

container_group = collection.build(h)

get_container_definitions_graph(container_group, children[:container_definitions])
get_containers_graph(container_group, children[:containers])
get_container_conditions_graph(container_group, children[:container_conditions])
get_custom_attributes_graph(container_group,
:labels => custom_attrs[:labels],
Expand All @@ -420,33 +420,18 @@ def get_container_conditions_graph(parent, hashes)
end
end

def get_container_definitions_graph(parent, hashes)
collection = @inv_collections[:container_definitions]
hashes.each do |h|
h[:container_group] = parent
children = h.extract!(:container_port_configs, :container_env_vars, :security_context, :container)

container_definition = collection.build(h)

get_container_port_configs_graph(container_definition, children[:container_port_configs])
get_container_env_vars_graph(container_definition, children[:container_env_vars])
get_container_security_context_graph(container_definition, children[:security_context]) if children[:security_context]
get_containers_graph(container_definition, children[:container]) if children[:container]
end
end

def get_container_port_configs_graph(parent, hashes)
collection = @inv_collections[:container_port_configs]
hashes.each do |h|
h[:container_definition] = parent
h[:container] = parent
collection.build(h)
end
end

def get_container_env_vars_graph(parent, hashes)
collection = @inv_collections[:container_env_vars]
hashes.each do |h|
h[:container_definition] = parent
h[:container] = parent
collection.build(h)
end
end
Expand All @@ -457,13 +442,20 @@ def get_container_security_context_graph(parent, h)
collection.build(h)
end

def get_containers_graph(parent, h)
def get_containers_graph(parent, hashes)
collection = @inv_collections[:containers]

h[:container_definition] = parent
h[:container_image] = lazy_find_image(h[:container_image])
hashes.each do |h|
h[:container_group] = parent
h[:container_image] = lazy_find_image(h[:container_image])
children = h.extract!(:container_port_configs, :container_env_vars, :security_context)

collection.build(h)
container = collection.build(h)

get_container_port_configs_graph(container, children[:container_port_configs])
get_container_env_vars_graph(container, children[:container_env_vars])
get_container_security_context_graph(container, children[:security_context]) if children[:security_context]
end
end

# TODO: how would this work with partial refresh?
Expand Down Expand Up @@ -729,34 +721,34 @@ def parse_pod(pod)
new_result = parse_base_item(pod)

new_result.merge!(
:type => 'ManageIQ::Providers::Kubernetes::ContainerManager::ContainerGroup',
:restart_policy => pod.spec.restartPolicy,
:dns_policy => pod.spec.dnsPolicy,
:ipaddress => pod.status.podIP,
:phase => pod.status.phase,
:message => pod.status.message,
:reason => pod.status.reason,
:container_node_name => pod.spec.nodeName,
:container_definitions => [],
:build_pod_name => pod.metadata.try(:annotations).try("openshift.io/build.name".to_sym)
:type => 'ManageIQ::Providers::Kubernetes::ContainerManager::ContainerGroup',
:restart_policy => pod.spec.restartPolicy,
:dns_policy => pod.spec.dnsPolicy,
:ipaddress => pod.status.podIP,
:phase => pod.status.phase,
:message => pod.status.message,
:reason => pod.status.reason,
:container_node_name => pod.spec.nodeName,
:containers => [],
:build_pod_name => pod.metadata.try(:annotations).try("openshift.io/build.name".to_sym)
)

# TODO, map volumes
# TODO, podIP
containers_index = {}
containers = pod.spec.containers
containers.each do |container_spec|
containers_index[container_spec.name] = parse_container_spec(container_spec, pod.metadata.uid)
new_result[:containers] << containers_index[container_spec.name]
end

unless pod.status.nil? || pod.status.containerStatuses.nil?
pod.status.containerStatuses.each do |cn|
containers_index[cn.name] = parse_container(cn, pod.metadata.uid)
containers_index[cn.name] ||= {}
containers_index[cn.name].merge!(parse_container_status(cn, pod.metadata.uid))
end
end

new_result[:container_definitions] = containers.collect do |container_def|
parse_container_definition(container_def, pod.metadata.uid).merge(
:container => containers_index[container_def.name]
)
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
Expand Down Expand Up @@ -1018,40 +1010,38 @@ def parse_conditions(entity)
end
end

def parse_container_definition(container_def, pod_id)
def parse_container_spec(container_spec, pod_id)
new_result = {
:ems_ref => "#{pod_id}_#{container_def.name}_#{container_def.image}",
:name => container_def.name,
:image => container_def.image,
:image_pull_policy => container_def.imagePullPolicy,
:command => container_def.command ? Shellwords.join(container_def.command) : nil,
:memory => container_def.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_def.cpu.to_f / 1000,
:capabilities_add => container_def.securityContext.try(:capabilities).try(:add).to_a.join(','),
:capabilities_drop => container_def.securityContext.try(:capabilities).try(:drop).to_a.join(','),
:privileged => container_def.securityContext.try(:privileged),
:run_as_user => container_def.securityContext.try(:runAsUser),
:run_as_non_root => container_def.securityContext.try(:runAsNonRoot),
:security_context => parse_security_context(container_def.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)
}
ports = container_def.ports
ports = container_spec.ports
new_result[:container_port_configs] = Array(ports).collect do |port_entry|
parse_container_port_config(port_entry, pod_id, container_def.name)
parse_container_port_config(port_entry, pod_id, container_spec.name)
end
env = container_def.env
env = container_spec.env
new_result[:container_env_vars] = Array(env).collect do |env_var|
parse_container_env_var(env_var)
end

new_result
end

def parse_container(container, pod_id)
def parse_container_status(container, pod_id)
h = {
:type => 'ManageIQ::Providers::Kubernetes::ContainerManager::Container',
:ems_ref => "#{pod_id}_#{container.name}_#{container.image}",
:name => container.name,
:restart_count => container.restartCount,
:backing_ref => container.containerID,
:container_image => parse_container_image(container.image, container.imageID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def full_refresh_test
assert_authentication
assert_table_counts
assert_specific_container
assert_specific_container_definition
assert_specific_container_group
assert_specific_container_node
assert_specific_container_service
Expand All @@ -69,7 +68,6 @@ def assert_table_counts
expect(ContainerService.count).to eq(5)
expect(ContainerPortConfig.count).to eq(2)
expect(ContainerEnvVar.count).to eq(3)
expect(ContainerDefinition.count).to eq(3)
expect(ContainerReplicator.count).to eq(2)
expect(ContainerProject.count).to eq(1)
expect(ContainerQuota.count).to eq(2)
Expand Down Expand Up @@ -116,7 +114,7 @@ def assert_specific_container
end

expect(@container.container_image.name).to eq("kubernetes/heapster")
expect(@container.container_definition.command).to eq("/heapster --source\\=kubernetes:https://kubernetes "\
expect(@container.command).to eq("/heapster --source\\=kubernetes:https://kubernetes "\
"--sink\\=influxdb:http://monitoring-influxdb:80")

@container2 = Container.find_by(:name => "influxdb")
Expand All @@ -133,20 +131,15 @@ def assert_specific_container
:name => "monitoring-influx-grafana-controller-22icy"
)

# Check relation to provider, container definition and container image
# Check relation to provider and container image
expect(@container2.container_image.name).to eq("kubernetes/heapster_influxdb")
expect(@container2.container_definition).not_to be_nil
expect(@container2.ext_management_system).to eq(@ems)

expect(@container.container_node).to have_attributes(
:name => "10.35.0.169"
)
end

def assert_specific_container_definition
expect(ContainerDefinition.find_by(:name => "heapster").ext_management_system).to eq(@ems)
end

def assert_specific_container_group
@containergroup = ContainerGroup.find_by(:name => "monitoring-heapster-controller-4j5zu")
expect(@containergroup).to have_attributes(
Expand Down Expand Up @@ -424,7 +417,6 @@ def tag_in_category_with_description(category, description)
it "saves the objects in the DB" do
expect(ContainerGroup.count).to eq(10)
expect(Container.count).to eq(10)
expect(ContainerDefinition.count).to eq(10)
expect(ContainerService.count).to eq(11)
expect(ContainerQuota.count).to eq(3)
expect(ContainerQuotaItem.count).to eq(15)
Expand Down Expand Up @@ -452,8 +444,6 @@ def tag_in_category_with_description(category, description)
expect(ContainerGroup.where(:deleted_on => nil).count).to eq(7)
expect(Container.count).to eq(10)
expect(Container.where(:deleted_on => nil).count).to eq(7)
expect(ContainerDefinition.count).to eq(10)
expect(ContainerDefinition.where(:deleted_on => nil).count).to eq(7)
end

it "removes the deleted objects from the DB" do
Expand Down Expand Up @@ -485,19 +475,19 @@ def tag_in_category_with_description(category, description)
assert_disconnected(pod)
expect(pod.container_project).to be_nil
expect(pod.containers.count).to eq(1)
expect(pod.container_definitions.count).to eq(1)
expect(pod.containers.count).to eq(1)
end

container_def0 = ContainerDefinition.find_by(:name => "my-container", :container_group => pod0)
container_def1 = ContainerDefinition.find_by(:name => "my-container", :container_group => pod1)
container0 = Container.find_by(:name => "my-container", :container_group => pod0)
container1 = Container.find_by(:name => "my-container", :container_group => pod1)

[container_def0, container_def1].each do |container_def|
assert_disconnected(container_def)
expect(container_def.container).not_to be_nil
[container0, container1].each do |container|
assert_disconnected(container)
expect(container).not_to be_nil
end

container0 = Container.find_by(:name => "my-container", :container_definition => container_def0)
container1 = Container.find_by(:name => "my-container", :container_definition => container_def1)
container0 = Container.find_by(:name => "my-container")
container1 = Container.find_by(:name => "my-container")

[container0, container1].each do |container|
assert_disconnected(container)
Expand Down

0 comments on commit 00df730

Please sign in to comment.