Per-host connection pool over SSH with the libvirt unix socket forwarded through the session, lazy dial and redial after disconnect, context cancellation for go-libvirt's context-less RPCs, handshake deadline and SSH keepalives. Error classification onto the provider taxonomy. Tested with a fake dialer and an in-process SSH server that forwards unix sockets. Step 3 of docs/plans/2026-09-04-1404-kvm-provider.md. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
66 lines
2.2 KiB
Go
66 lines
2.2 KiB
Go
package kvm
|
|
|
|
import (
|
|
"errors"
|
|
|
|
libvirt "github.com/digitalocean/go-libvirt"
|
|
"github.com/go-logr/logr"
|
|
|
|
"gitea.home.hrajfrisbee.cz/kacerr/egress-proxies-operator/internal/provider"
|
|
)
|
|
|
|
// classify maps a libvirt error onto the provider taxonomy. Missing domain
|
|
// or volume → NotFound; anything the host will keep rejecting (bad XML,
|
|
// unsupported config, denied access, missing pool) → Permanent; every other
|
|
// libvirt code, plus SSH/transport failures and cancellation, → Transient,
|
|
// because retrying is safer than latching Failed on an error nobody taught
|
|
// this function to recognise. libvirt exposes no quota signal, so nothing
|
|
// maps to QuotaExceeded.
|
|
func classify(err error) error {
|
|
var lerr libvirt.Error
|
|
if !errors.As(err, &lerr) {
|
|
return provider.ErrTransient
|
|
}
|
|
switch libvirt.ErrorNumber(lerr.Code) {
|
|
case libvirt.ErrNoDomain, libvirt.ErrNoStorageVol:
|
|
return provider.ErrNotFound
|
|
case libvirt.ErrNoStoragePool,
|
|
libvirt.ErrXMLError, libvirt.ErrXMLDetail, libvirt.ErrXMLInvalidSchema,
|
|
libvirt.ErrConfigUnsupported, libvirt.ErrNoSupport,
|
|
libvirt.ErrOperationUnsupported, libvirt.ErrArgumentUnsupported,
|
|
libvirt.ErrInvalidArg,
|
|
libvirt.ErrAuthFailed, libvirt.ErrAccessDenied, libvirt.ErrOperationDenied:
|
|
return provider.ErrPermanent
|
|
default:
|
|
return provider.ErrTransient
|
|
}
|
|
}
|
|
|
|
// isLibvirtError reports whether err came back from the libvirt daemon (as
|
|
// opposed to the SSH or RPC transport), which is what decides whether the
|
|
// cached connection is still trustworthy.
|
|
func isLibvirtError(err error) bool {
|
|
var lerr libvirt.Error
|
|
return errors.As(err, &lerr)
|
|
}
|
|
|
|
func hasCode(err error, code libvirt.ErrorNumber) bool {
|
|
var lerr libvirt.Error
|
|
return errors.As(err, &lerr) && libvirt.ErrorNumber(lerr.Code) == code
|
|
}
|
|
|
|
// logAPIError records the raw libvirt code and message at V(1); classify
|
|
// collapses them onto the coarser taxonomy, so this is the only place the
|
|
// original detail survives.
|
|
func logAPIError(log logr.Logger, op string, err error) {
|
|
if !log.V(1).Enabled() {
|
|
return
|
|
}
|
|
kv := []any{"op", op, "error", err.Error()}
|
|
var lerr libvirt.Error
|
|
if errors.As(err, &lerr) {
|
|
kv = append(kv, "libvirtCode", lerr.Code, "libvirtMessage", lerr.Message)
|
|
}
|
|
log.V(1).Info("libvirt call failed", kv...)
|
|
}
|