Skip to content

Commit

Permalink
WIP replace fog-kubevirt with kubeclient
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Dec 10, 2024
1 parent adbde01 commit fe57918
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
22 changes: 16 additions & 6 deletions app/models/manageiq/providers/kubevirt/inventory/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ def initialize_for_targeted_refresh
end

def initialize_for_full_refresh
@manager.with_provider_connection do |connection|
@nodes = connection.nodes
@vms = connection.vms
@vm_instances = connection.vm_instances
@templates = connection.templates
end
@nodes = kube_client.get_nodes
@vms = kubevirt_client.get_virtual_machines
@vm_instances = kubevirt_client.get_virtual_machine_instances
@templates = openshift_template_client.get_templates
end

def kube_client
@kube_client ||= @manager.parent_manager.connect(:service => "kubernetes")
end

def kubevirt_client
@kubevirt_client ||= @manager.parent_manager.connect(:service => "kubernetes", :path => "/apis/kubevirt.io", :version => "v1")
end

def openshift_template_client
@openshift_template_client ||= @manager.parent_manager.connect(:service => "kubernetes", :path => "/apis/template.openshift.io", :version => "v1")
end
end
51 changes: 30 additions & 21 deletions app/models/manageiq/providers/kubevirt/inventory/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,31 @@ def process_nodes(objects)

def process_node(object)
# Get the basic information:
uid = object.uid
name = object.name
uid = object.metadata.uid
name = object.metadata.name
addresses = object.status.addresses.index_by(&:type)
hostname = addresses["Hostname"]&.address
ipaddress = addresses["InternalIP"]&.address

# Add the inventory object for the host:
host_object = host_collection.find_or_build(uid)
host_object.connection_state = 'connected'
host_object.ems_cluster = cluster_collection.lazy_find(CLUSTER_ID)
host_object.ems_ref = uid
host_object.hostname = object.hostname
host_object.ipaddress = object.ip_address
host_object.hostname = hostname
host_object.ipaddress = ipaddress
host_object.name = name
host_object.uid_ems = uid

# Add the inventory object for the operating system details:
os_object = os_collection.find_or_build(host_object)
os_object.name = object.hostname
os_object.product_name = object.os_image
os_object.product_type = object.operating_system
os_object.version = object.kernel_version

node_info = object.status.nodeInfo

os_object.name = hostname
os_object.product_name = node_info&.osImage
os_object.product_type = node_info&.operatingSystem
os_object.version = node_info&.kernelVersion

# Find the storage:
storage_object = storage_collection.lazy_find(STORAGE_ID)
Expand All @@ -92,10 +98,11 @@ def process_vms(objects)

def process_vm(object)
# Process the domain:
vm_object = process_domain(object.namespace, object.memory, object.cpu_cores, object.uid, object.name)
domain = object.spec.template.spec.domain
vm_object = process_domain(object.metadata.namespace, domain.memory.guest, domain.cpu.guest, object.metadata.uid, object.metadata.name)

# Add the inventory object for the OperatingSystem
process_os(vm_object, object.labels, object.annotations)
process_os(vm_object, object.metadata.labels, object.metadata.annotations)

# The power status is initially off, it will be set to on later if the virtual machine instance exists:
vm_object.raw_power_state = 'Succeeded'
Expand All @@ -109,23 +116,25 @@ def process_vm_instances(objects)

def process_vm_instance(object)
# Get the basic information:
uid = object.uid
name = object.name
uid = object.metadata.uid
name = object.metadata.name

owner_references = object.metadata.ownerReferences.first
# Get the identifier of the virtual machine from the owner reference:
unless object.owner_name.nil?
unless owner_references&.name.nil?
# seems like valid use case for now
uid = object.owner_uid
name = object.owner_name
uid = owner_references.uid
name = owner_references.name
end

# Process the domain:
vm_object = process_domain(object.namespace, object.memory, object.cpu_cores, uid, name)
domain = object.spec.domain
vm_object = process_domain(object.metadata.namespace, domain.memory.guest, domain.cpu.cores, uid, name)
process_status(vm_object, object.ip_address, object.node_name)

vm_object.host = host_collection.lazy_find(object.node_name, :ref => :by_name)

vm_object.raw_power_state = object.status
vm_object.raw_power_state = object.status.phase
end

def process_domain(namespace, memory, cores, uid, name)
Expand Down Expand Up @@ -189,22 +198,22 @@ def process_templates(objects)

def process_template(object)
# Get the basic information:
uid = object.uid
uid = object.metadata.uid
vm = vm_from_objects(object.objects)
return if vm.nil?

# Add the inventory object for the template:
template_object = template_collection.find_or_build(uid)
template_object.connection_state = 'connected'
template_object.ems_ref = uid
template_object.name = object.name
template_object.name = object.metadata.name
template_object.raw_power_state = 'never'
template_object.template = true
template_object.uid_ems = uid
template_object.location = object.namespace
template_object.location = object.metadata.namespace

# Add the inventory object for the hardware:
process_hardware(template_object, object.parameters, object.labels, vm.dig(:spec, :template, :spec, :domain))
process_hardware(template_object, object.parameters, object.metadata.labels, vm.dig(:spec, :template, :spec, :domain))

# Add the inventory object for the OperatingSystem
process_os(template_object, object.labels, object.annotations)
Expand Down

0 comments on commit fe57918

Please sign in to comment.