Files
ilker f74859d138 hpcod phase 1: multi-provider discovery
First code for the HPC-on-demand portal. The portal is meant to front more
than Proxmox - OpenStack is expected next - so the provider abstraction comes
first and Proxmox is simply its first implementation. internal/model holds the
vocabulary everything above reasons in; adding a provider means writing one
package and registering it.

The layered capability detection from the design is implemented and enforced.
A PCI class code cannot identify InfiniBand: a ConnectX-4 Lx reports class
0x0200 because that variant is Ethernet-only, while a ConnectX-6 reports
0x0207 in IB mode and 0x0200 in Ethernet mode. Every capability claim
therefore records how it was made - class, curated vendor:device map, or live
agent - and Capability.Trusted refuses to schedule on a fabric until an agent
confirms the link. Unconfirmed devices are still reported, since that gap is
what an operator has to close. Passthrough viability comes from IOMMU group
membership rather than device presence: sibling functions of one card are
fine, an unrelated device in the group is not.

Verified against a live Proxmox site, where it correctly classifies the
Mellanox cards as roce rather than infiniband and withholds them from the
schedulable set, while offering the Quadro K620 that does sit alone in its
IOMMU group.

Two bugs found and fixed while running it for real:

- Query strings were appended to url.URL.Path, which is the decoded path, so
  String() escaped the "?" and /cluster/resources?type=vm silently stopped
  filtering. Guests were ignored and a node running 22 VMs reported all cores
  free. Regression test added.
- Physical cores are now the schedulable total by default rather than being
  ignored, with a per-site cpu_overcommit for hosts where oversubscription is
  acceptable. An HPC tenant given oversubscribed cores just runs slowly.

Secrets stay out of the catalogue: any setting may be written env:NAME. The
Proxmox token needs PVEAuditor only.
2026-09-01 11:53:35 +02:00

119 lines
3.3 KiB
Go

// Package proxmox implements provider.Provider for Proxmox VE.
//
// Discovery is read-only and uses an API token, which should be bound to a
// role with audit permissions only. The control plane never needs write access
// to a site during phase 1.
package proxmox
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
type client struct {
base *url.URL
token string // "USER@REALM!TOKENID=UUID"
http *http.Client
}
func newClient(endpoint, token string, insecureTLS bool, timeout time.Duration) (*client, error) {
u, err := url.Parse(strings.TrimRight(endpoint, "/"))
if err != nil {
return nil, fmt.Errorf("bad endpoint %q: %w", endpoint, err)
}
if u.Scheme == "" || u.Host == "" {
return nil, fmt.Errorf("endpoint %q must be a full URL, e.g. https://host:8006", endpoint)
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureTLS}, //nolint:gosec // opt-in per site
Proxy: http.ProxyFromEnvironment,
}
return &client{
base: u,
token: token,
http: &http.Client{Transport: tr, Timeout: timeout},
}, nil
}
// get fetches /api2/json<path> and decodes the "data" envelope into out.
//
// path may carry a query string. It has to be split off before assignment,
// because url.URL.Path is the *decoded* path: leaving "?type=vm" in it makes
// String() escape the "?" and the filter silently stops applying - which
// showed up as a node reporting all of its cores free while guests were
// running on it.
func (c *client) get(ctx context.Context, path string, out any) error {
rawQuery := ""
if i := strings.IndexByte(path, '?'); i >= 0 {
path, rawQuery = path[:i], path[i+1:]
}
u := *c.base
u.Path = strings.TrimRight(u.Path, "/") + "/api2/json" + path
u.RawQuery = rawQuery
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "PVEAPIToken="+c.token)
req.Header.Set("Accept", "application/json")
resp, err := c.http.Do(req)
if err != nil {
return fmt.Errorf("GET %s: %w", path, err)
}
defer func() { _ = resp.Body.Close() }()
body, err := io.ReadAll(io.LimitReader(resp.Body, 32<<20))
if err != nil {
return fmt.Errorf("GET %s: reading body: %w", path, err)
}
if resp.StatusCode != http.StatusOK {
msg := strings.TrimSpace(string(body))
if len(msg) > 300 {
msg = msg[:300] + "..."
}
return fmt.Errorf("GET %s: %s: %s", path, resp.Status, msg)
}
var env struct {
Data json.RawMessage `json:"data"`
}
if err := json.Unmarshal(body, &env); err != nil {
return fmt.Errorf("GET %s: decoding envelope: %w", path, err)
}
if len(env.Data) == 0 || string(env.Data) == "null" {
return nil
}
if err := json.Unmarshal(env.Data, out); err != nil {
return fmt.Errorf("GET %s: decoding data: %w", path, err)
}
return nil
}
// flexInt tolerates Proxmox returning numbers as either JSON numbers or
// strings depending on the endpoint and version.
type flexInt int64
func (f *flexInt) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), `"`)
if s == "" || s == "null" {
*f = 0
return nil
}
var v float64
if err := json.Unmarshal([]byte(s), &v); err != nil {
return nil // unparsable values are treated as zero, not fatal
}
*f = flexInt(v)
return nil
}