Files
ilkerandClaude Opus 5.5 090719fc74
ci / derle (push) Successful in 30s
hpcod: clusters across the nodes of a Proxmox cluster
Discovery already saw every node of a Proxmox cluster and placement could
put a VM on any of them, but the template, the snippets and a simple SDN
zone only worked on PVE_NODE, so a cluster could land where it could not
be built.

- Setup asks which nodes clusters may use (PVE_NODES; a multi-select in
  the TUI when Proxmox has more than one). Discovery offers only those.
- Heads are placed on PVE_NODE only (head_node), where the template is, so
  no shared storage is needed; diskless compute nodes spread.
- PVE_SDN_ZONE_TYPE (auto|simple|vxlan): more than one node makes the zone
  vxlan, every node a peer, MTU 1450. A zone of the wrong type is remade
  only while no cluster has a vnet in it. Guest fabric NICs take the
  bridge MTU through virtio (mtu = 1).
- setup keeps nodes and head_node of its own site in sites.yaml, leaving
  the rest of the file as the operator wrote it.
- verify checks the nodes are online and the zone matches.

Run on the simulated Proxmox with the zone forced to vxlan: setup remade
the zone, a two-node selftest passed, both compute NICs came up at 1450,
and a DF ping of 1422 bytes passed between them while 1423 did not. Two
real Proxmox nodes have not been tested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 19:51:35 +01:00

186 lines
5.3 KiB
Go

package setup
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
)
// fakeSDN is a Proxmox cluster's SDN, as far as ensureZone sees it.
type fakeSDN struct {
mu sync.Mutex
nodes map[string]string // name -> status
ips map[string]string
zones map[string]map[string]string
vnets map[string]string // vnet -> zone
applied int
calls []string
}
func (f *fakeSDN) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f.mu.Lock()
defer f.mu.Unlock()
path := strings.TrimPrefix(r.URL.Path, "/api2/json")
_ = r.ParseForm()
f.calls = append(f.calls, r.Method+" "+path)
data := func(v any) { _ = json.NewEncoder(w).Encode(map[string]any{"data": v}) }
switch {
case path == "/nodes":
var out []map[string]string
for n, st := range f.nodes {
out = append(out, map[string]string{"node": n, "status": st})
}
data(out)
case path == "/cluster/status":
var out []map[string]string
for n, ip := range f.ips {
out = append(out, map[string]string{"type": "node", "name": n, "ip": ip})
}
data(out)
case path == "/cluster/sdn/zones" && r.Method == http.MethodGet:
var out []map[string]string
for id, z := range f.zones {
m := map[string]string{"zone": id}
for k, v := range z {
m[k] = v
}
out = append(out, m)
}
data(out)
case path == "/cluster/sdn/zones" && r.Method == http.MethodPost:
z := map[string]string{}
for k := range r.PostForm {
if k != "zone" {
z[k] = r.PostFormValue(k)
}
}
f.zones[r.PostFormValue("zone")] = z
data(nil)
case strings.HasPrefix(path, "/cluster/sdn/zones/"):
id := strings.TrimPrefix(path, "/cluster/sdn/zones/")
switch r.Method {
case http.MethodDelete:
delete(f.zones, id)
case http.MethodPut:
for k := range r.PostForm {
f.zones[id][k] = r.PostFormValue(k)
}
}
data(nil)
case path == "/cluster/sdn/vnets":
var out []map[string]string
for v, z := range f.vnets {
out = append(out, map[string]string{"vnet": v, "zone": z})
}
data(out)
case path == "/cluster/sdn" && r.Method == http.MethodPut:
f.applied++
data(nil)
default:
http.NotFound(w, r)
}
}
func sdnSite(t *testing.T, f *fakeSDN, nodes string) (*PVE, Site) {
srv := httptest.NewServer(f)
t.Cleanup(srv.Close)
s := goodSite()
s["PVE_NODE"], s["PVE_NODES"], s["PVE_SDN_ZONE"], s["PVE_SDN_ZONE_TYPE"] = "pve1", nodes, "hpcod", "auto"
return PVEWithToken(srv.URL, true, "root@pam!hpcod-tf=x"), s
}
func threeNodes() *fakeSDN {
return &fakeSDN{
nodes: map[string]string{"pve1": "online", "pve2": "online", "pve3": "online"},
ips: map[string]string{"pve1": "10.0.0.11", "pve2": "10.0.0.12", "pve3": "10.0.0.13"},
zones: map[string]map[string]string{},
vnets: map[string]string{},
}
}
func TestZoneIsSimpleOnOneNode(t *testing.T) {
f := threeNodes()
p, s := sdnSite(t, f, "")
if err := ensureZone(t.Context(), p, s, t.Logf); err != nil {
t.Fatal(err)
}
if z := f.zones["hpcod"]; z["type"] != "simple" || f.applied != 1 {
t.Errorf("zone = %v, applied %d times; want simple, applied once", z, f.applied)
}
}
func TestZoneIsVxlanAcrossNodesWithEveryNodeAPeer(t *testing.T) {
f := threeNodes()
p, s := sdnSite(t, f, "pve1,pve3")
if err := ensureZone(t.Context(), p, s, t.Logf); err != nil {
t.Fatal(err)
}
z := f.zones["hpcod"]
if z["type"] != "vxlan" || z["peers"] != "10.0.0.11,10.0.0.13" || z["nodes"] != "pve1,pve3" || z["mtu"] != "1450" {
t.Errorf("zone = %v", z)
}
// Run again: nothing to do, nothing applied.
f.applied = 0
if err := ensureZone(t.Context(), p, s, t.Logf); err != nil || f.applied != 0 {
t.Errorf("second run: err=%v applied=%d", err, f.applied)
}
// A node added: peers change in place, the zone is not recreated.
s["PVE_NODES"] = "pve1,pve2,pve3"
f.calls = nil
if err := ensureZone(t.Context(), p, s, t.Logf); err != nil {
t.Fatal(err)
}
if z := f.zones["hpcod"]; z["peers"] != "10.0.0.11,10.0.0.12,10.0.0.13" {
t.Errorf("peers = %s after adding pve2", z["peers"])
}
for _, c := range f.calls {
if strings.HasPrefix(c, "DELETE") {
t.Errorf("zone was deleted to add a peer: %s", c)
}
}
}
func TestSimpleZoneIsRemadeAsVxlanOnlyWhenEmpty(t *testing.T) {
f := threeNodes()
f.zones["hpcod"] = map[string]string{"type": "simple", "ipam": "pve"}
f.vnets["c001"] = "hpcod"
p, s := sdnSite(t, f, "pve1,pve2")
err := ensureZone(t.Context(), p, s, t.Logf)
if err == nil || !strings.Contains(err.Error(), "c001") {
t.Fatalf("err = %v, want a refusal naming c001", err)
}
if f.zones["hpcod"]["type"] != "simple" {
t.Fatal("the zone was changed under a live cluster")
}
delete(f.vnets, "c001")
if err := ensureZone(t.Context(), p, s, t.Logf); err != nil {
t.Fatal(err)
}
if z := f.zones["hpcod"]; z["type"] != "vxlan" || z["peers"] != "10.0.0.11,10.0.0.12" {
t.Errorf("zone = %v, want vxlan with both peers", z)
}
}
func TestAnOfflineOrMissingNodeStopsSetup(t *testing.T) {
f := threeNodes()
f.nodes["pve2"] = "offline"
p, s := sdnSite(t, f, "pve1,pve2")
if err := ensureZone(t.Context(), p, s, t.Logf); err == nil || !strings.Contains(err.Error(), "pve2 is offline") {
t.Errorf("err = %v, want pve2 is offline", err)
}
s["PVE_NODES"] = "pve1,pve9"
if err := ensureZone(t.Context(), p, s, t.Logf); err == nil || !strings.Contains(err.Error(), "pve9") {
t.Errorf("err = %v, want one naming pve9", err)
}
if len(f.zones) != 0 {
t.Errorf("a zone was made anyway: %v", f.zones)
}
}