Skip to content

Commit a322ba5

Browse files
Wait for libvirt connection in hypervisor controller
1 parent 21b8e95 commit a322ba5

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

internal/controller/hypervisor_controller.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ type HypervisorReconciler struct {
5858

5959
// Channel that can be used to trigger reconcile events.
6060
reconcileCh chan event.GenericEvent
61+
62+
// An interval that determines how long to wait between connection attempts
63+
// to libvirt. This is used in the Start method when trying to connect to
64+
// libvirt, and can be set to a lower value for testing purposes.
65+
libvirtConnectInterval time.Duration
6166
}
6267

6368
const (
@@ -326,11 +331,20 @@ func (r *HypervisorReconciler) Start(ctx context.Context) error {
326331
log := logger.FromContext(ctx, "controller", "hypervisor")
327332
log.Info("starting libvirt event subscription")
328333

329-
// Ensure we're connected to libvirt.
330-
if err := r.Libvirt.Connect(); err != nil {
331-
log.Error(err, "unable to connect to libvirt")
332-
return err
334+
// Block until we're connected to libvirt.
335+
for {
336+
timeToSleep := r.libvirtConnectInterval
337+
if timeToSleep == 0 {
338+
timeToSleep = 5 * time.Second // default value
339+
}
340+
if err := r.Libvirt.Connect(); err != nil {
341+
log.Error(err, "unable to connect to libvirt, retrying in 5 seconds")
342+
time.Sleep(timeToSleep)
343+
continue
344+
}
345+
break
333346
}
347+
log.Info("connected to libvirt")
334348

335349
// Run a ticker which reconciles the hypervisor resource every minute.
336350
// This ensures that we periodically reconcile the hypervisor even

0 commit comments

Comments
 (0)