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

KubeVirt Events Capture #256

Merged
merged 4 commits into from
Nov 15, 2024
Merged

KubeVirt Events Capture #256

merged 4 commits into from
Nov 15, 2024

Conversation

nasark
Copy link
Member

@nasark nasark commented Nov 7, 2024

@nasark
Copy link
Member Author

nasark commented Nov 7, 2024

WIP need to:

  • Test a bit more and refine parser and filtered events
  • OSV changes

@agrare event catching logic etc works so feel free to review in the meantime

class ManageIQ::Providers::Kubevirt::InfraManager::KubernetesEventMonitor < ManageIQ::Providers::Kubernetes::ContainerManager::KubernetesEventMonitor
def inventory
# :service is required to handle also the case where @ems is Openshift
@inventory ||= @ems.parent_manager.connect(:service => ManageIQ::Providers::Kubernetes::ContainerManager.ems_type)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using k8s client since fog-kubevirt doesn't have an events API to my knowledge

def extract_event_data(event)
event_data = {
:timestamp => event.object.lastTimestamp,
:kind => event.object.involvedObject.kind,
:name => event.object.involvedObject.name,
:namespace => event.object.involvedObject.namespace,
:reason => event.object.reason,
:message => event.object.message,
:uid => event.object.involvedObject.uid,
:event_uid => event.object.metadata.uid,
}

unless event.object.involvedObject.fieldPath.nil?
event_data[:fieldpath] = event.object.involvedObject.fieldPath
end

event_type_prefix = event_data[:kind].upcase

# Handle event data for specific entities
if event_data[:kind] == "VirtualMachine" || event_data[:kind] == "VirtualMachineKind"
event_data[:vm_name] = event_data[:name]
end

event_data[:event_type] = "#{event_type_prefix}_#{event_data[:reason].upcase}"

event_data
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think all of this should live in the EventParser, the purpose of that class is to take the "native" event object and normalize it to our ems_event table

Comment on lines 2 to 7
ENABLED_EVENTS = {
'VirtualMachine' => %w(Created Started Migrated SuccessfulCreate SuccessfulDelete ShuttingDown Killing),
'VirtualMachineInstance' => %w(Created Started Migrated SuccessfulCreate SuccessfulDelete ShuttingDown Killing)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR but as a follow-up we'll want to create event handler objects in manageiq-content for these so that customers can hook into these events for policy

Example: https://github.com/ManageIQ/manageiq-content/tree/master/content/automate/ManageIQ/System/Event/EmsEvent/Kubernetes.class

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:timestamp => event.object.lastTimestamp,
:message => event.object.message,
:container_namespace => event.object.involvedObject.namespace,
:full_data => event.to_h,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing to double check here, event is a RecursiveOpenStruct can you confirm that event.to_h converts the nested values to simple hash also and not ROS objects?

Copy link
Member Author

@nasark nasark Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it does

(byebug) pp event_hash[:full_data]
{:type=>"MODIFIED",
 :object=>
  {:kind=>"Event",
   :apiVersion=>"v1",
   :metadata=>
    {:name=>"centos-stream9-scarlet-duck-44.1807437ad1a2db12",
     :namespace=>"miq",
     :uid=>"cf6dc3a5-3223-4d86-b2cf-531fef74d25e",
     :resourceVersion=>"32920483",
     :creationTimestamp=>"2024-11-12T15:52:56Z",
     :managedFields=>
      [{:manager=>"virt-handler",
        :operation=>"Update",
        :apiVersion=>"v1",
        :time=>"2024-11-12T16:16:50Z",
        :fieldsType=>"FieldsV1",
        :fieldsV1=>{:"f:count"=>{}, :"f:firstTimestamp"=>{}, :"f:involvedObject"=>{}, :"f:lastTimestamp"=>{}, :"f:message"=>{}, :"f:reason"=>{}, :"f:reportingComponent"=>{}, :"f:reportingInstance"=>{}, :"f:source"=>{:"f:component"=>{}, :"f:host"=>{}}, :"f:type"=>{}}}]},
   :involvedObject=>{:kind=>"VirtualMachineInstance", :namespace=>"miq", :name=>"centos-stream9-scarlet-duck-44", :uid=>"b80fa3c9-c7c2-4c5c-9201-12ae7b6da1c8", :apiVersion=>"kubevirt.io/v1", :resourceVersion=>"32904613"},
   :reason=>"Migrated",
   :message=>"EvictionStrategy is set but vmi is not migratable; cannot migrate VMI: PVC centos-stream9-scarlet-duck-44 is not shared, live migration requires that all PVCs must be shared (using ReadWriteMany access mode)",
   :source=>{:component=>"virt-handler", :host=>"<worker>"},
   :firstTimestamp=>"2024-11-12T15:52:56Z",
   :lastTimestamp=>"2024-11-12T16:16:50Z",
   :count=>11,
   :type=>"Warning",
   :eventTime=>nil,
   :reportingComponent=>"virt-handler",
   :reportingInstance=>"<worker>"}}

@@ -0,0 +1,25 @@
class ManageIQ::Providers::Kubevirt::InfraManager::EventParser
def self.event_to_hash(event, ems_id = nil, source)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added source as a parameter to differentiate between Kubevirt and OSV. Since OSV will be using this same parser class

end

def queue_event(event)
event_hash = ManageIQ::Providers::Kubevirt::InfraManager::EventParser.event_to_hash(event, @cfg[:ems_id], @ems.emstype.upcase)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nasark I think we should do @ems.class::EventParser here so that if we were to make any Openshift specific changes we could do it in an Openshift::InfraManager::EventParser subclass

@agrare
Copy link
Member

agrare commented Nov 15, 2024

@nasark It'd be good to add some spec tests for the EventParser so take an example payload for each of the events and show what they resulting parsed hash looks like

@agrare agrare merged commit 6bee2e6 into ManageIQ:master Nov 15, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants