A libvirt site is one host, reached as the provisioner over SSH with the host key checked against the one setup pins: this is root on a hypervisor, so a key that is missing or different refuses the connection rather than trusting it on sight. libvirt's socket is reached through the SSH connection (stream-local forwarding), so nothing is installed on the host, and the context's deadline is put on the TCP connection because go-libvirt's calls take none. It reports what the Proxmox provider does, by the same rules: cores and memory with the shared oversubscription policy (moved to the provider package), guests' cores and memory counted while running or paused, PCI devices classified, offerable only when alone in their IOMMU group (the rule moved to the capability package) and owned per slot by any guest whose definition names them, stopped or not. The host is its head node. Tests parse node devices and domains as libvirt writes them, one fixture taken from a real host. Against the test host: 12 cores, 47 GiB, the CPU model and the pool read correctly; a missing pinned key and a wrong one are both refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
294 lines
8.9 KiB
Go
294 lines
8.9 KiB
Go
package proxmox
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/ilkermanap/hpcod/internal/capability"
|
|
"github.com/ilkermanap/hpcod/internal/model"
|
|
"github.com/ilkermanap/hpcod/internal/provider"
|
|
)
|
|
|
|
func init() { provider.Register("proxmox", newProvider) }
|
|
|
|
// Provider discovers one Proxmox cluster.
|
|
type Provider struct {
|
|
id string
|
|
name string
|
|
cli *client
|
|
skipPCI bool
|
|
// nodes, when set, are the only hypervisors this site offers; the rest
|
|
// of the Proxmox cluster is someone else's. headNode is where heads go.
|
|
nodes map[string]bool
|
|
headNode string
|
|
// Oversubscription policy. Off by default: an HPC tenant handed
|
|
// oversubscribed cores simply runs slowly, and one handed memory the
|
|
// host cannot actually produce is killed, so a site has to opt in.
|
|
//
|
|
// The switch is separate from the ratios so that turning the policy off
|
|
// does not mean editing every number back to 1.0 and losing what they
|
|
// were tuned to.
|
|
oversubscribe bool
|
|
cpuOvercommit float64
|
|
memOvercommit float64
|
|
}
|
|
|
|
func newProvider(cfg provider.SiteConfig) (provider.Provider, error) {
|
|
endpoint, err := cfg.Setting("endpoint")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
token, err := cfg.Setting("token")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
timeout, err := time.ParseDuration(cfg.SettingOr("timeout", "20s"))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("site %q: bad timeout: %w", cfg.ID, err)
|
|
}
|
|
cli, err := newClient(endpoint, token, cfg.BoolSetting("insecure_tls", false), timeout)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("site %q: %w", cfg.ID, err)
|
|
}
|
|
name := cfg.Name
|
|
if name == "" {
|
|
name = cfg.ID
|
|
}
|
|
cpuOver, memOver, oversub, err := oversubscription(cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var nodes map[string]bool
|
|
for _, n := range strings.FieldsFunc(cfg.SettingOr("nodes", ""), func(r rune) bool { return r == ',' || r == ' ' }) {
|
|
if nodes == nil {
|
|
nodes = map[string]bool{}
|
|
}
|
|
nodes[n] = true
|
|
}
|
|
headNode := cfg.SettingOr("head_node", "")
|
|
if headNode != "" && nodes != nil && !nodes[headNode] {
|
|
return nil, fmt.Errorf("site %q: head_node %s is not one of its nodes", cfg.ID, headNode)
|
|
}
|
|
return &Provider{
|
|
id: cfg.ID, name: name, cli: cli,
|
|
nodes: nodes, headNode: headNode,
|
|
skipPCI: cfg.BoolSetting("skip_pci", false),
|
|
oversubscribe: oversub,
|
|
cpuOvercommit: cpuOver,
|
|
memOvercommit: memOver,
|
|
}, nil
|
|
}
|
|
|
|
// oversubscription is the policy every provider shares; see provider.Oversubscription.
|
|
func oversubscription(cfg provider.SiteConfig) (cpu, mem float64, on bool, err error) {
|
|
return provider.Oversubscription(cfg)
|
|
}
|
|
|
|
func (p *Provider) ID() string { return p.id }
|
|
func (p *Provider) Kind() string { return "proxmox" }
|
|
|
|
// Discover reads the cluster's inventory. An unreachable site is reported as
|
|
// data - Reachable=false with Error set - not as a returned error, because the
|
|
// control plane needs to record that it tried and record when.
|
|
func (p *Provider) Discover(ctx context.Context) (model.Site, error) {
|
|
site := model.Site{
|
|
ID: p.id, Name: p.name, Kind: p.Kind(), LastSeen: time.Now().UTC(),
|
|
HeadNode: p.headNode,
|
|
}
|
|
|
|
var nodes []struct {
|
|
Node string `json:"node"`
|
|
Status string `json:"status"`
|
|
MaxCPU flexInt `json:"maxcpu"`
|
|
MaxMem flexInt `json:"maxmem"`
|
|
Mem flexInt `json:"mem"`
|
|
}
|
|
if err := p.cli.get(ctx, "/nodes", &nodes); err != nil {
|
|
site.Error = err.Error()
|
|
return site, nil
|
|
}
|
|
site.Reachable = true
|
|
site.Healthy = p.quorate(ctx)
|
|
|
|
// Guest allocations, so we report free capacity rather than raw totals.
|
|
allocCores, allocMem := p.allocations(ctx)
|
|
|
|
// Which passthrough devices are already spoken for. Read once for the
|
|
// whole site: it is one sweep of the guest configs, and doing it per
|
|
// node would repeat that sweep for every hypervisor.
|
|
var owners map[string]string
|
|
if !p.skipPCI {
|
|
owners = p.deviceOwners(ctx)
|
|
}
|
|
|
|
for _, n := range nodes {
|
|
if p.nodes != nil && !p.nodes[n.Node] {
|
|
continue // a node of the Proxmox cluster this site does not use
|
|
}
|
|
// Cores and MemBytes are what may be scheduled; the Physical fields
|
|
// are what the hardware is. Reporting only the first would hide the
|
|
// policy inside a number, and an operator looking at "168 cores"
|
|
// has no way to tell a large machine from a heavily oversubscribed
|
|
// small one.
|
|
node := model.Node{
|
|
Name: n.Node,
|
|
Online: n.Status == "online",
|
|
PhysicalCores: int(n.MaxCPU),
|
|
PhysicalMemBytes: int64(n.MaxMem),
|
|
Cores: int(float64(n.MaxCPU) * p.cpuOvercommit),
|
|
MemBytes: int64(float64(n.MaxMem) * p.memOvercommit),
|
|
AllocCores: allocCores[n.Node],
|
|
AllocMemBytes: allocMem[n.Node],
|
|
}
|
|
if node.Online {
|
|
p.enrichNode(ctx, &node, owners)
|
|
}
|
|
site.Nodes = append(site.Nodes, node)
|
|
}
|
|
sort.Slice(site.Nodes, func(i, j int) bool { return site.Nodes[i].Name < site.Nodes[j].Name })
|
|
return site, nil
|
|
}
|
|
|
|
func (p *Provider) quorate(ctx context.Context) bool {
|
|
var st []struct {
|
|
Type string `json:"type"`
|
|
Quorate flexInt `json:"quorate"`
|
|
}
|
|
if err := p.cli.get(ctx, "/cluster/status", &st); err != nil {
|
|
return false
|
|
}
|
|
for _, s := range st {
|
|
if s.Type == "cluster" {
|
|
return s.Quorate == 1
|
|
}
|
|
}
|
|
// A standalone node reports no "cluster" entry. It is not un-healthy;
|
|
// there is simply nothing to be quorate about.
|
|
return true
|
|
}
|
|
|
|
// allocations sums CPU and memory already committed to guests, per node.
|
|
// Discovering total capacity without this would let placement oversubscribe a
|
|
// busy host.
|
|
func (p *Provider) allocations(ctx context.Context) (map[string]int, map[string]int64) {
|
|
cores := map[string]int{}
|
|
mem := map[string]int64{}
|
|
|
|
var res []struct {
|
|
Type string `json:"type"`
|
|
Node string `json:"node"`
|
|
Status string `json:"status"`
|
|
MaxCPU flexInt `json:"maxcpu"`
|
|
MaxMem flexInt `json:"maxmem"`
|
|
}
|
|
if err := p.cli.get(ctx, "/cluster/resources?type=vm", &res); err != nil {
|
|
return cores, mem
|
|
}
|
|
for _, r := range res {
|
|
if r.Node == "" || r.Status != "running" {
|
|
continue
|
|
}
|
|
cores[r.Node] += int(r.MaxCPU)
|
|
mem[r.Node] += int64(r.MaxMem)
|
|
}
|
|
return cores, mem
|
|
}
|
|
|
|
func (p *Provider) enrichNode(ctx context.Context, node *model.Node, owners map[string]string) {
|
|
var status struct {
|
|
CPUInfo struct {
|
|
Model string `json:"model"`
|
|
CPUs flexInt `json:"cpus"`
|
|
} `json:"cpuinfo"`
|
|
}
|
|
if err := p.cli.get(ctx, "/nodes/"+node.Name+"/status", &status); err == nil {
|
|
node.CPUModel = strings.TrimSpace(status.CPUInfo.Model)
|
|
if node.Cores == 0 {
|
|
node.Cores = int(status.CPUInfo.CPUs)
|
|
}
|
|
}
|
|
|
|
var stores []struct {
|
|
Storage string `json:"storage"`
|
|
Type string `json:"type"`
|
|
Total flexInt `json:"total"`
|
|
Avail flexInt `json:"avail"`
|
|
Shared flexInt `json:"shared"`
|
|
Active flexInt `json:"active"`
|
|
}
|
|
if err := p.cli.get(ctx, "/nodes/"+node.Name+"/storage", &stores); err == nil {
|
|
for _, s := range stores {
|
|
if s.Active != 1 {
|
|
continue
|
|
}
|
|
node.Storages = append(node.Storages, model.Storage{
|
|
Name: s.Storage,
|
|
Type: s.Type,
|
|
TotalBytes: int64(s.Total),
|
|
FreeBytes: int64(s.Avail),
|
|
Shared: s.Shared == 1,
|
|
})
|
|
}
|
|
}
|
|
|
|
if !p.skipPCI {
|
|
node.Devices = p.devices(ctx, node.Name, owners)
|
|
}
|
|
}
|
|
|
|
// devices reads the node's PCI inventory and classifies it.
|
|
//
|
|
// Passthrough viability comes from IOMMU group membership: a device whose
|
|
// group also holds unrelated functions cannot be handed to a guest cleanly, so
|
|
// it is discovered but not offerable. Functions of the same physical card
|
|
// (an NVIDIA GPU and its HDA audio function, for instance) do not disqualify it.
|
|
func (p *Provider) devices(ctx context.Context, node string, owners map[string]string) []model.Device {
|
|
var pci []struct {
|
|
ID string `json:"id"`
|
|
Class string `json:"class"`
|
|
Vendor string `json:"vendor"`
|
|
Device string `json:"device"`
|
|
VendorName string `json:"vendor_name"`
|
|
DeviceName string `json:"device_name"`
|
|
IOMMUGroup flexInt `json:"iommugroup"`
|
|
}
|
|
if err := p.cli.get(ctx, "/nodes/"+node+"/hardware/pci", &pci); err != nil {
|
|
return nil
|
|
}
|
|
|
|
// Group membership, and the slot each function belongs to.
|
|
byGroup := map[int][]string{}
|
|
for _, d := range pci {
|
|
g := int(d.IOMMUGroup)
|
|
byGroup[g] = append(byGroup[g], d.ID)
|
|
}
|
|
|
|
var out []model.Device
|
|
for _, d := range pci {
|
|
dev, ok := capability.Classify(capability.Input{
|
|
NativeID: d.ID,
|
|
Class: d.Class,
|
|
Vendor: d.Vendor,
|
|
Device: d.Device,
|
|
VendorName: d.VendorName,
|
|
DeviceName: d.DeviceName,
|
|
IsolationGroup: fmt.Sprintf("%d", int(d.IOMMUGroup)),
|
|
})
|
|
if !ok {
|
|
continue
|
|
}
|
|
dev.Passthrough = groupIsolated(byGroup[int(d.IOMMUGroup)], d.ID)
|
|
// Per slot, not per function: a guest holding 0000:01:00.0 has the
|
|
// whole card, so its other functions are not free either.
|
|
dev.AllocatedTo = owners[node+"/"+normalizeSlot(d.ID)]
|
|
out = append(out, dev)
|
|
}
|
|
sort.Slice(out, func(i, j int) bool { return out[i].NativeID < out[j].NativeID })
|
|
return out
|
|
}
|
|
|
|
func groupIsolated(group []string, id string) bool { return capability.GroupIsolated(group, id) }
|