@@ -39,11 +39,12 @@ import (
3939)
4040
4141type LibVirt struct {
42- virt * libvirt.Libvirt
43- client client.Client
44- migrationJobs map [string ]context.CancelFunc
45- migrationLock sync.Mutex
46- version string
42+ virt * libvirt.Libvirt
43+ client client.Client
44+ migrationJobs map [string ]context.CancelFunc
45+ migrationLock sync.Mutex
46+ version string
47+ hypervisorVersion string
4748
4849 // Event channels for domains by their libvirt event id.
4950 domEventChs map [libvirt.DomainEventID ]<- chan any
@@ -81,14 +82,25 @@ func NewLibVirt(k client.Client) *LibVirt {
8182 make (map [string ]context.CancelFunc ),
8283 sync.Mutex {},
8384 "N/A" ,
84- make (map [libvirt.DomainEventID ]<- chan any ), sync.Mutex {},
85- make (map [libvirt.DomainEventID ]map [string ]func (context.Context , any )), sync.Mutex {},
85+ "N/A" ,
86+ make (map [libvirt.DomainEventID ]<- chan any ),
87+ sync.Mutex {},
88+ make (map [libvirt.DomainEventID ]map [string ]func (context.Context , any )),
89+ sync.Mutex {},
8690 capabilities .NewClient (),
8791 domcapabilities .NewClient (),
8892 dominfo .NewClient (),
8993 }
9094}
9195
96+ // formatLibvirtVersion converts a libvirt version integer to a semver string.
97+ // Libvirt versions are encoded as major*1000000 + minor*1000 + release.
98+ // For example, version 8001002 becomes "8.1.2".
99+ func formatLibvirtVersion (version uint64 ) string {
100+ major , minor , release := version / 1000000 , (version / 1000 )% 1000 , version % 1000
101+ return fmt .Sprintf ("%d.%d.%d" , major , minor , release )
102+ }
103+
92104func (l * LibVirt ) Connect () error {
93105 // Check if already connected
94106 if l .virt .IsConnected () {
@@ -104,12 +116,18 @@ func (l *LibVirt) Connect() error {
104116 return err
105117 }
106118
107- // Update the version
119+ // Update the libvirt library version
108120 if version , err := l .virt .ConnectGetLibVersion (); err != nil {
109121 logger .Log .Error (err , "unable to fetch libvirt version" )
110122 } else {
111- major , minor , release := version / 1000000 , (version / 1000 )% 1000 , version % 1000
112- l .version = fmt .Sprintf ("%d.%d.%d" , major , minor , release )
123+ l .version = formatLibvirtVersion (version )
124+ }
125+
126+ // Update the hypervisor version
127+ if hvVersion , err := l .virt .ConnectGetVersion (); err != nil {
128+ logger .Log .Error (err , "unable to fetch hypervisor version" )
129+ } else {
130+ l .hypervisorVersion = formatLibvirtVersion (hvVersion )
113131 }
114132
115133 l .WatchDomainChanges (
@@ -222,7 +240,6 @@ func (l *LibVirt) WatchDomainChanges(
222240 handlerId string ,
223241 handler func (context.Context , any ),
224242) {
225-
226243 // Register the handler so that it is called when an event with the provided
227244 // eventId is received.
228245 l .domEventChangeHandlersLock .Lock ()
@@ -268,10 +285,11 @@ func (l *LibVirt) Process(hv v1.Hypervisor) (v1.Hypervisor, error) {
268285 return hv , nil
269286}
270287
271- // Add the libvirt version to the hypervisor instance.
288+ // Add the libvirt and hypervisor versions to the hypervisor instance.
272289func (l * LibVirt ) addVersion (old v1.Hypervisor ) (v1.Hypervisor , error ) {
273290 newHv := * old .DeepCopy ()
274291 newHv .Status .LibVirtVersion = l .version
292+ newHv .Status .HypervisorVersion = l .hypervisorVersion
275293 return newHv , nil
276294}
277295
0 commit comments