From 1a49ec4253ca9b68836c862b0ea658c998d7b6b7 Mon Sep 17 00:00:00 2001 From: Alexander Jung Date: Wed, 20 Nov 2024 15:51:35 +0100 Subject: [PATCH] fix(qemu): Allow listing over instances which cannot be queried over QMP This commit amortizes the errors from connection sequences via QMP into well-defined errors. These errors can then be cross-validated in the listing method which would otherwise fast-fail. By omitting any issues related via QMP, we are able to properly return a full list of machines, regardless of whether they have a QMP socket. Signed-off-by: Alexander Jung --- machine/qemu/v1alpha1.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/machine/qemu/v1alpha1.go b/machine/qemu/v1alpha1.go index bdb570b09..79ec47906 100644 --- a/machine/qemu/v1alpha1.go +++ b/machine/qemu/v1alpha1.go @@ -48,6 +48,11 @@ type machineV1alpha1Service struct { eopts []exec.ExecOption } +var ( + ErrCouldNotAttachQMPClient = errors.New("could not attach QMP client") + ErrCouldNotQueryMachineViaQMP = errors.New("could not query machine via QMP") +) + // NewMachineV1alpha1Service implements kraftkit.sh/machine/platform.NewStrategyConstructor func NewMachineV1alpha1Service(ctx context.Context, opts ...any) (machinev1alpha1.MachineService, error) { service := machineV1alpha1Service{} @@ -892,7 +897,7 @@ func (service *machineV1alpha1Service) Get(ctx context.Context, machine *machine exitCode = 1 return machine, nil } else if err != nil { - return machine, fmt.Errorf("could not attach to QMP client: %v", err) + return machine, errors.Join(ErrCouldNotAttachQMPClient, err) } defer qmpClient.Close() @@ -903,7 +908,7 @@ func (service *machineV1alpha1Service) Get(ctx context.Context, machine *machine // We cannot amend the status at this point, even if the process is // alive, since it is not an indicator of the state of the VM, only of the // VMM. So we return what we already know via LookupMachineConfig. - return machine, fmt.Errorf("could not query machine status via QMP: %v", err) + return machine, errors.Join(ErrCouldNotQueryMachineViaQMP, err) } // Map the QMP status to supported machine states @@ -952,7 +957,9 @@ func (service *machineV1alpha1Service) List(ctx context.Context, machines *machi // Iterate through each machine and grab the latest status for _, machine := range cached { machine, err := service.Get(ctx, &machine) - if err != nil { + // It's fine to list machines that are not running, so we ignore the error + // as long as it is not related to the QMP client. + if err != nil && !errors.Is(err, ErrCouldNotAttachQMPClient) && !errors.Is(err, ErrCouldNotQueryMachineViaQMP) { machines.Items = cached return machines, err }