Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persister: InventoryCollection building through add_collection() #265

Merged
merged 3 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def target_collection_inv_to_persister(ems, inventory, options = Config::Options
end

def persister_inv_to_persister(persister, inventory, options)
persister.add_collection(@tag_mapper.tags_to_resolve_collection)
persister.add_collection_directly(@tag_mapper.tags_to_resolve_collection)
# TODO(lsmola) expose persister and use that and use that instead of @inv_collections
@inv_collections = persister.collections

Expand Down
12 changes: 12 additions & 0 deletions app/models/manageiq/providers/kubernetes/inventory/persister.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
class ManageIQ::Providers::Kubernetes::Inventory::Persister < ManagerRefresh::Inventory::Persister
require_nested :ContainerManager
require_nested :TargetCollection

def add_collection_directly(collection)
@collections[collection.name] = collection
end

# ManagerRefresh::InventoryCollection.inventory_object_attributes
# are not defined
def make_builder_settings(extra_settings = {})
opts = super
opts[:auto_inventory_attributes] = false
opts
end
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
class ManageIQ::Providers::Kubernetes::Inventory::Persister::ContainerManager < ManageIQ::Providers::Kubernetes::Inventory::Persister
include ManageIQ::Providers::Kubernetes::ContainerManager::InventoryCollections
include ManageIQ::Providers::Kubernetes::Inventory::Persister::Definitions::ContainerCollections

def initialize_inventory_collections
initialize_container_inventory_collections
end

protected

def targeted?
false
end

def strategy
nil
end

def shared_options
{
:strategy => strategy,
:targeted => targeted?,
:parent => manager.presence
}
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
module ManageIQ::Providers::Kubernetes::Inventory::Persister::Definitions::ContainerCollections
extend ActiveSupport::Concern

def initialize_container_inventory_collections
%i(containers
container_builds
container_build_pods
container_env_vars
container_groups
container_image_registries
container_images
container_limits
container_limit_items
container_nodes
container_port_configs
container_projects
container_quotas
container_quota_scopes
container_quota_items
container_volumes
container_replicators
container_routes
container_services
container_service_port_configs
container_templates
container_template_parameters
computer_systems
computer_system_hardwares
computer_system_operating_systems
persistent_volumes
persistent_volume_claims
security_contexts).each do |name|

add_collection(container, name)
end

initialize_container_conditions

initialize_custom_attributes

initialize_taggings
end

protected

def initialize_container_conditions
%i(container_groups
container_nodes).each do |name|
add_container_conditions(manager, name)
end
end

def initialize_custom_attributes
%i(container_nodes
container_projects).each do |name|
add_custom_attributes(name, %w(labels additional_attributes))
end

%i(container_groups).each do |name|
add_custom_attributes(name, %w(labels node_selectors))
end

%i(container_replicators
container_services).each do |name|
add_custom_attributes(name, %w(labels selectors))
end

%i(container_builds
container_build_pods
container_routes
container_templates).each do |name|
add_custom_attributes(name, %w(labels))
end
end

def initialize_taggings
%i(container_builds
container_groups
container_nodes
container_projects
container_replicators
container_routes
container_services
container_templates).each do |name|

add_taggings(name)
end
end

# ContainerCondition is polymorphic child of ContainerNode & ContainerGroup.
# @param manager [ExtManagementSystem]
# @param association [Symbol]
def add_container_conditions(manager, association)
relation = manager.public_send(association)
query = ContainerCondition.where(
:container_entity_type => relation.model.base_class.name,
:container_entity_id => relation, # nested SELECT. TODO: compare to a JOIN.
)

add_collection(container,
[:container_conditions_for, relation.model.base_class.name],
{},
{:auto_inventory_attributes => false}) do |builder|

builder.add_properties(
:model_class => ContainerCondition,
:association => nil,
:name => "container_conditions_for_#{association}".to_sym,
:arel => query,
:manager_ref => %i(container_entity name),
)
end
end

# CustomAttribute is polymorphic child of many models
# @param parent [Symbol]
# @param sections [Array<String>]
def add_custom_attributes(parent, sections)
parent_collection = @collections[parent]

type = parent_collection.model_class.base_class.name
relation = parent_collection.full_collection_for_comparison

sections.each do |section|
query = ::CustomAttribute.where(
:resource_type => type,
:resource_id => relation,
:section => section.to_s
)

add_collection(container, [:custom_attributes_for, type, section.to_s], {}, { :auto_inventory_attributes => false }) do |builder|
builder.add_properties(
:model_class => ::CustomAttribute,
:association => nil,
:name => "custom_attributes_for_#{parent_collection.name}_#{section}".to_sym,
:arel => query,
:manager_ref => %i(resource section name),
:parent_inventory_collections => [parent_collection.name],
)
end
end
end

# @param parent_name [Symbol]
def add_taggings(parent_name)
parent_collection = @collections[parent_name]
type = parent_collection.model_class.base_class.name
relation = parent_collection.full_collection_for_comparison

query = Tagging.where(
:taggable_type => type,
:taggable_id => relation,
).joins(:tag).merge(Tag.controlled_by_mapping)

add_collection(container, [:taggings_for, type], {}, {:auto_inventory_attributes => false}) do |builder|
builder.add_properties(
:model_class => ::Tagging,
:association => nil,
:name => "taggings_for_#{parent_collection.name}".to_sym,
:arel => query,
:manager_ref => %i(taggable tag),
:parent_inventory_collections => [parent_collection.name],
)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
class ManageIQ::Providers::Kubernetes::Inventory::Persister::TargetCollection < ManageIQ::Providers::Kubernetes::Inventory::Persister::ContainerManager
def targeted
def targeted?
false # TODO(lsmola) get ready for true, which means a proper targeted refresh. That will require more effort.
end

def shared_options
settings_options = options[:inventory_collections].try(:to_hash) || {}

settings_options.merge(
:targeted => targeted,
{
:targeted => targeted?,
:complete => false, # For now, we want to a only create and update elements using watches data, delete events could
# probably set finished_at and deleted_on dates, as an update based disconnect_inv.
:strategy => :local_db_find_missing_references, # By default no IC will be saved
)
:parent => manager.presence
}
end
end