Skip to content

Commit

Permalink
Replace fog-kubevirt with kubeclient
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Dec 12, 2024
1 parent adbde01 commit 09051bb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 42 deletions.
47 changes: 30 additions & 17 deletions app/models/manageiq/providers/kubevirt/inventory/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,40 @@ def initialize_for_targeted_refresh
name = @target.name
@nodes = {}

@manager.with_provider_connection(:namespace => @target.location) do |connection|
if @target.template?
@templates = [connection.template(name)]
else
@vms = [connection.vm(name)]
begin
@vm_instances = [connection.vm_instance(name)]
rescue
# target refresh of a vm might fail if it has no vm instance
_log.debug("The is no running vm resource for '#{name}'")
end
if @target.template?
@templates = [openshift_template_client.get_template(name, @target.location)]
else
@vms = [kubevirt_client.get_virtual_machine(name, @target.location)]
begin
@vm_instances = [kubevirt_client.get_virtual_machine_instance(name, @target.location)]
rescue Kubeclient::ResourceNotFoundError
# target refresh of a vm might fail if it has no vm instance
_log.debug("The is no running vm resource for '#{name}'")
end
end
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(api_group = nil)
api_path, api_version = api_group&.split("/")

options = {:service => "kubernetes"}
options.merge!(:path => "/apis/#{api_path}", :version => api_version) if api_path

@manager.parent_manager.connect(options)
end

def kubevirt_client
@kubevirt_client ||= kube_client("kubevirt.io/v1")
end

def openshift_template_client
@openshift_template_client ||= kube_client("template.openshift.io/v1")
end
end
59 changes: 35 additions & 24 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

node_info = object.status.nodeInfo

# 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
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,13 @@ 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)
spec = object.spec.template.spec
domain = spec.domain

vm_object = process_domain(object.metadata.namespace, domain.resources&.requests&.memory, domain.cpu, 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 +118,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

# Get the identifier of the virtual machine from the owner reference:
unless object.owner_name.nil?
owner_references = object.metadata.ownerReferences&.first
if owner_references&.name
# 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)
process_status(vm_object, object.ip_address, object.node_name)
vm_object = process_domain(object.metadata.namespace, object.spec.domain.memory&.guest, object.spec.domain.cpu&.cores, uid, name)

process_status(vm_object, object.status.interfaces.first&.ipAddress, object.status.nodeName)

vm_object.host = host_collection.lazy_find(object.node_name, :ref => :by_name)
vm_object.host = host_collection.lazy_find(object.status.nodeName, :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,25 +200,25 @@ 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)
process_os(template_object, object.metadata.labels, object.metadata.annotations)
end

def vm_from_objects(objects)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ def parse
private

def get_object_ids(objects)
objects.map { |o| o.uid }.uniq
objects.map { |o| o.metadata.uid }.uniq
end
end

0 comments on commit 09051bb

Please sign in to comment.