61 lines
2.0 KiB
Go
61 lines
2.0 KiB
Go
package kvm2
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/digitalocean/go-libvirt"
|
|
|
|
"gitea.home.hrajfrisbee.cz/kacerr/egress-proxies-operator/internal/provider"
|
|
)
|
|
|
|
// ListByTag is spec entry 12: every domain on every configured host, in
|
|
// config order, filtered client-side on the kvm2 mark. A host that cannot
|
|
// be reached or listed fails the whole call (fail-whole); a single domain
|
|
// that cannot be read is skipped, unless ctx ended, which fails the call
|
|
// rather than pass off an empty view as the truth.
|
|
func (p *Provider) ListByTag(ctx context.Context) ([]provider.Instance, error) {
|
|
log := p.logger(ctx)
|
|
var out []provider.Instance
|
|
for _, host := range p.cfg.Hosts {
|
|
hlog := log.WithValues("host", host.Name)
|
|
be, err := p.hosts.get(ctx, host)
|
|
if err != nil {
|
|
logAPIError(hlog, "list", err)
|
|
return nil, p.wrapErr("list", "", err)
|
|
}
|
|
doms, _, err := be.ConnectListAllDomains(ctx, 1, libvirt.ConnectListDomainsActive|libvirt.ConnectListDomainsInactive)
|
|
if err != nil {
|
|
err = fmt.Errorf("listing domains on host %s: %w", host.Name, err)
|
|
logAPIError(hlog, "list", err)
|
|
return nil, p.wrapErr("list", "", err)
|
|
}
|
|
for _, dom := range doms {
|
|
dlog := hlog.WithValues("name", dom.Name)
|
|
meta, err := readMetadata(ctx, be, dom)
|
|
if err != nil {
|
|
if ctx.Err() != nil {
|
|
return nil, p.wrapErr("list", "", err)
|
|
}
|
|
dlog.V(1).Info("skipping domain with unreadable metadata", "error", err.Error())
|
|
continue
|
|
}
|
|
if !meta.managed() {
|
|
continue
|
|
}
|
|
inst, err := inspect(ctx, dlog, be, host.Name, dom, meta)
|
|
if err != nil {
|
|
if ctx.Err() != nil {
|
|
return nil, p.wrapErr("list", "", err)
|
|
}
|
|
dlog.V(1).Info("skipping domain that could not be inspected", "error", err.Error())
|
|
continue
|
|
}
|
|
dlog.V(2).Info("kvm2 listed instance", "id", inst.ID, "state", inst.State, "uid", inst.UID, "createdAt", inst.CreatedAt)
|
|
out = append(out, inst)
|
|
}
|
|
}
|
|
log.V(1).Info("kvm2 domains listed", "hosts", len(p.cfg.Hosts), "managed", len(out))
|
|
return out, nil
|
|
}
|