Files
ilkerandClaude Opus 5.5 85f04096dd openstack: heads boot from a Cinder volume
A cloud's flavors seldom have a 120 GB root disk; on DevStack only the
largest does, so a 2-vCPU head was given 8 vCPUs and 16 GiB. With
OS_HEAD_VOLUME (auto: where the catalog lists Cinder) the head's disk is
a 120 GB volume made from the head image, and the head takes the
smallest flavor with its vCPUs and memory. OS_VOLUME_TYPE picks the
type. Setup and verify check Cinder, the type and the volume quota; the
provider reports a site problem when the quota cannot take another head
(head_volume in sites.yaml). model.HeadDiskGB is the one place the 120
lives.

The volume is a Terraform resource, not Nova's image-to-volume boot:
Nova waits 185 s, and Cinder on LVM took 4m21s to copy the image, so the
head went to ERROR and the volume was left in the project outside the
state. Terraform now waits for it (30m) and deletes it.

Run on DevStack rebuilt with Cinder: the head took m1.medium, booted from
its volume, saw 119 GB at /; selftest passed twice (with Cinder's image
cache and without), and each destroy removed the volume.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 21:27:36 +01:00

172 lines
6.4 KiB
Go

// Package provision turns a recorded cluster into infrastructure.
//
// Rendering the variables and running the tool live here rather than in
// the command, because the portal has to do exactly the same thing. Two
// copies of "what this cluster is" would eventually build two different
// clusters from one record.
package provision
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/ilkermanap/hpcod/internal/model"
"github.com/ilkermanap/hpcod/internal/store"
)
// Contract is every variable TFVars writes, and so every variable a
// backend's environment has to declare (started is passed on the command
// line, by the runner, rather than written).
//
// These describe the cluster and nothing else. How a backend realises them -
// Proxmox VMIDs, hostpci slots, an SDN vnet - is the backend environment's
// business and stays in its own files, next to the site's values. A name
// here that only one backend understands is a leak: the next backend would
// have to accept it and ignore it, or fail on it.
//
// cluster_id, tenant_id, tenant_name who the cluster is
// mgmt_ip_cidr the head's address on the management network
// head, compute sizes; compute.count is the node count
// head_host, compute_hosts where placement put each guest: a
// backend host name, compute_hosts
// positional by node index
// devices per compute node index (1-based,
// as a string), the native ids of the
// devices placement gave it
// apps applications resolved at allocation
// control_plane_cert the portal's certificate (runner only)
// started power the compute nodes on (runner only)
var Contract = []string{
"cluster_id", "tenant_id", "tenant_name", "mgmt_ip_cidr",
"head", "compute", "head_host", "compute_hosts", "devices", "apps",
"control_plane_cert", "started",
}
// TFVars writes the cluster's half of the contract, taking every value that
// describes the cluster from the record rather than from flags.
func TFVars(c store.Cluster, vms []store.VM, apps []store.ClusterApp, tenantName string, mgmtCIDR string) (string, error) {
var head *store.VM
var compute []store.VM
for i := range vms {
switch vms[i].Role {
case "head":
head = &vms[i]
case "compute":
compute = append(compute, vms[i])
}
}
if head == nil {
return "", fmt.Errorf("cluster %d has no head node recorded", c.SerialID)
}
if len(compute) == 0 {
return "", fmt.Errorf("cluster %d has no compute nodes recorded", c.SerialID)
}
sort.Slice(compute, func(i, j int) bool { return compute[i].NodeIndex < compute[j].NodeIndex })
// The module indexes compute nodes 1..n positionally, so a record whose
// indexes skip a number would silently shift a GPU onto the wrong node.
for i, vm := range compute {
if vm.NodeIndex != i+1 {
return "", fmt.Errorf("compute nodes are indexed %d where %d was expected; the record and the module would disagree about which node is which",
vm.NodeIndex, i+1)
}
}
if tenantName == "" {
tenantName = fmt.Sprintf("tenant-%d", c.TenantID)
}
hosts := make([]string, len(compute))
devices := map[string][]string{}
for i, vm := range compute {
hosts[i] = vm.HostNode
for _, d := range vm.Devices {
k := fmt.Sprintf("%d", vm.NodeIndex)
devices[k] = append(devices[k], d.NativeID)
}
}
var b strings.Builder
fmt.Fprintf(&b, "# Generated by hpcod provision from cluster record %d (%s).\n",
c.SerialID, c.AccountingName)
fmt.Fprintf(&b, "# Do not edit: every value here is the planner's decision, and a hand\n")
fmt.Fprintf(&b, "# edit is a cluster built differently from the one that was recorded.\n\n")
// Only what the record determines. node_name, the gateway, the storage
// and the credentials describe the site, not this cluster, and belong
// in the site's own variables - defining them in two files is a silent
// question about which one wins.
fmt.Fprintf(&b, "cluster_id = %d\n", c.ClusterID)
fmt.Fprintf(&b, "tenant_id = %d\n", c.TenantID)
fmt.Fprintf(&b, "tenant_name = %s\n\n", quote(tenantName))
fmt.Fprintf(&b, "mgmt_ip_cidr = %s\n\n", quote(mgmtCIDR))
fmt.Fprintf(&b, "head = { cores = %d, memory_gb = %d, disk_gb = %d }\n", head.Cores, head.MemoryGB, model.HeadDiskGB)
fmt.Fprintf(&b, "compute = { count = %d, cores = %d, memory_gb = %d }\n\n",
len(compute), compute[0].Cores, compute[0].MemoryGB)
fmt.Fprintf(&b, "head_host = %s\n", quote(head.HostNode))
fmt.Fprintf(&b, "compute_hosts = [%s]\n", quoteList(hosts))
if len(devices) == 0 {
fmt.Fprintf(&b, "devices = {}\n")
} else {
fmt.Fprintf(&b, "devices = {\n")
for _, k := range sortedKeys(devices) {
fmt.Fprintf(&b, " %s = [%s]\n", quote(k), quoteList(devices[k]))
}
fmt.Fprintf(&b, "}\n")
}
// The applications, already resolved against the catalogue when the
// cluster was allocated. Emitted even when empty, and that is the
// point: before this the variable was never written at all, so the
// module's own `apps = []` default was what every cluster got, whatever
// the tenant had ticked. An empty list now means the record says
// empty, rather than meaning nobody asked the question.
if len(apps) == 0 {
fmt.Fprintf(&b, "\napps = []\n")
} else {
fmt.Fprintf(&b, "\napps = [\n")
for _, a := range apps {
// The hashes go too, empty or not. A head that is handed no
// hash can say it could not check; one that is handed none
// because the variable was dropped looks identical and is not.
fmt.Fprintf(&b,
" { name = %s, version = %s, image = %s, digest = %s, sif_sha256 = %s, module = %s, binaries = [%s] },\n",
quote(a.Name), quote(a.Version), quote(a.Image),
quote(a.Digest), quote(a.SIFSHA256), quote(a.Module),
quoteList(a.Binaries))
}
fmt.Fprintf(&b, "]\n")
}
return b.String(), nil
}
// quote renders a Terraform string. json.Marshal escapes exactly what HCL
// needs for a quoted literal, and refuses nothing a hostname can contain.
func quote(s string) string {
b, _ := json.Marshal(s)
return string(b)
}
func quoteList(xs []string) string {
out := make([]string, len(xs))
for i, x := range xs {
out[i] = quote(x)
}
return strings.Join(out, ", ")
}
func sortedKeys(m map[string][]string) []string {
out := make([]string, 0, len(m))
for k := range m {
out = append(out, k)
}
sort.Strings(out)
return out
}