140 lines
4.6 KiB
Go
140 lines
4.6 KiB
Go
// Package lab holds the persisted record of a lab and the names of its LXD
|
|
// objects.
|
|
package lab
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"net/netip"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// State is the lifecycle state of a lab record.
|
|
type State string
|
|
|
|
const (
|
|
Provisioning State = "provisioning"
|
|
Ready State = "ready"
|
|
Deleting State = "deleting"
|
|
Deleted State = "deleted"
|
|
Failed State = "failed"
|
|
)
|
|
|
|
// FailureReason says why a lab failed. None depends on the lab user.
|
|
type FailureReason string
|
|
|
|
const (
|
|
FailureProvisioning FailureReason = "provisioning_failed"
|
|
FailureTimedOut FailureReason = "provisioning_timed_out"
|
|
FailureInterrupted FailureReason = "interrupted"
|
|
)
|
|
|
|
// LXD markers. Every instance and bridge that labd creates carries the lab ID
|
|
// in ConfigLab and a name with its prefix; reconciliation deletes only
|
|
// objects that have both, so an object that an operator created by hand is
|
|
// never touched.
|
|
const (
|
|
ConfigLab = "user.lincodex.lab"
|
|
ConfigNode = "user.lincodex.node"
|
|
ConfigExpiresAt = "user.lincodex.expires-at"
|
|
InstancePrefix = "lcxl-"
|
|
// BridgePrefix is short because a Linux interface name, and so an LXD
|
|
// bridge name, has at most 15 characters.
|
|
BridgePrefix = "lxl"
|
|
|
|
shortIDBytes = 5
|
|
// MaxBridges bounds the links of a lab by the two-digit bridge suffix.
|
|
MaxBridges = 100
|
|
)
|
|
|
|
// Node is a node of a lab with its LXD instance and management address.
|
|
type Node struct {
|
|
Name string `json:"name"`
|
|
Kind string `json:"kind"`
|
|
Instance string `json:"instance"`
|
|
ManagementAddress netip.Addr `json:"management_address"`
|
|
ManagementPort uint16 `json:"management_port"`
|
|
}
|
|
|
|
// Lab is the durable record of one lab. labd writes it before it creates any
|
|
// LXD object and removes it only after every object is gone, so a restart
|
|
// always knows what to delete.
|
|
type Lab struct {
|
|
ID uuid.UUID `json:"id"`
|
|
TemplateID string `json:"template_id"`
|
|
TemplateVersion int `json:"template_version"`
|
|
// Seed is the variant seed in hex. The state file is readable only by
|
|
// the labd user.
|
|
Seed string `json:"seed"`
|
|
Fingerprint string `json:"fingerprint"`
|
|
State State `json:"state"`
|
|
FailureReason FailureReason `json:"failure_reason,omitempty"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
ReadyAt time.Time `json:"ready_at,omitzero"`
|
|
ExpiresAt time.Time `json:"expires_at"`
|
|
// DeletedAt is set once every instance and bridge of the lab is gone.
|
|
// Until then the lab holds its memory and addresses.
|
|
DeletedAt time.Time `json:"deleted_at,omitzero"`
|
|
MemoryBytes int64 `json:"memory_bytes"`
|
|
Nodes []Node `json:"nodes"`
|
|
Bridges []string `json:"bridges"`
|
|
}
|
|
|
|
// HoldsResources reports whether LXD objects of the lab may exist.
|
|
func (l *Lab) HoldsResources() bool { return l.DeletedAt.IsZero() }
|
|
|
|
// NeedsTeardown reports whether the lab ended but its objects may remain.
|
|
func (l *Lab) NeedsTeardown() bool {
|
|
return l.HoldsResources() && (l.State == Deleting || l.State == Failed)
|
|
}
|
|
|
|
// Active reports whether the lab is provisioning or ready.
|
|
func (l *Lab) Active() bool { return l.State == Provisioning || l.State == Ready }
|
|
|
|
// Node returns the node with the given template name.
|
|
func (l *Lab) Node(name string) (Node, bool) {
|
|
for _, node := range l.Nodes {
|
|
if node.Name == name {
|
|
return node, true
|
|
}
|
|
}
|
|
return Node{}, false
|
|
}
|
|
|
|
// Clone returns a deep copy that a caller may keep without the lock.
|
|
func (l *Lab) Clone() Lab {
|
|
clone := *l
|
|
clone.Nodes = slices.Clone(l.Nodes)
|
|
clone.Bridges = slices.Clone(l.Bridges)
|
|
return clone
|
|
}
|
|
|
|
// ShortID is the part of the lab ID used in LXD object names.
|
|
func ShortID(id uuid.UUID) string { return hex.EncodeToString(id[:shortIDBytes]) }
|
|
|
|
// InstanceName is the LXD instance name of a node.
|
|
func InstanceName(id uuid.UUID, node string) string {
|
|
return InstancePrefix + ShortID(id) + "-" + node
|
|
}
|
|
|
|
// BridgeName is the LXD network name of the link with the given index.
|
|
func BridgeName(id uuid.UUID, index int) string {
|
|
return fmt.Sprintf("%s%s%02d", BridgePrefix, ShortID(id), index)
|
|
}
|
|
|
|
// OwnedInstance reports whether an instance name and config mark it as a lab
|
|
// node created by labd.
|
|
func OwnedInstance(name string, config map[string]string) bool {
|
|
return strings.HasPrefix(name, InstancePrefix) && config[ConfigLab] != ""
|
|
}
|
|
|
|
// OwnedBridge reports whether a network name and config mark it as a lab
|
|
// link bridge created by labd.
|
|
func OwnedBridge(name string, config map[string]string) bool {
|
|
return strings.HasPrefix(name, BridgePrefix) && config[ConfigLab] != ""
|
|
}
|