Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2c53ad6213 | |||
| c3819dde12 | |||
| fb85898569 | |||
| c10779873b | |||
| ea00158fa5 | |||
| ae6eb5a27b | |||
| 19542dd8c9 | |||
| 436641782c | |||
| 075d2f6459 | |||
| e92b18197c | |||
| d379d19deb | |||
| 60b0357eb6 |
@@ -1 +1 @@
|
||||
{ "phase": "P0b", "stage": "verify", "milestone": "v0.9", "phase_role": "execution", "updated_at": "2026-08-05T03:25:00Z", "milestone_complete": false, "verify": { "build": "pass", "go_test": "18/18", "bats": "20/20", "gofmt": "clean", "verify_reqs": "90 consistent" } }
|
||||
{ "phase": "P06", "stage": "verify", "milestone": "v0.9", "phase_role": "execution", "updated_at": "2026-08-05T04:30:00Z", "milestone_complete": false, "verify": { "build": "pass", "go_test": "22/22", "gofmt": "clean", "verify_reqs": "pending" } }
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
// Package emitter defines the Layer-4 emitter interface (REQ-074,
|
||||
// I-B-002): the bridge between the declarative *jobspec.WorkloadSpec
|
||||
// and the server-side files. An Emitter renders a *WorkloadSpec into a
|
||||
// slice of File artifacts that the SSH-push transport SCPs to peers.
|
||||
//
|
||||
// Emitters are registered per workload kind + runtime (e.g.
|
||||
// "service:wasm", "job:process", "daemonset:wasm"). The Registry looks
|
||||
// up the right emitter by "<kind>:<runtime>" and delegates. Unknown
|
||||
// combinations return an error so the caller can fail fast before any
|
||||
// file is written.
|
||||
//
|
||||
// P0c only ships the interface, the Registry, a stub SystemdEmitter
|
||||
// (process runtime), and the File/Node value types. The full emitter
|
||||
// implementations (systemd lifecycle hooks, Traefik, Syncthing,
|
||||
// sockets) land in later phases (P02 Traefik, P04 lifecycle, P08
|
||||
// sockets, P09 Syncthing, v0.10-P03 secrets).
|
||||
package emitter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
// File is a single rendered artifact destined for a peer. The SSH-push
|
||||
// transport writes Content to Path atomically (write-to-tmp + rename)
|
||||
// with the given Mode (an octal string like "0644").
|
||||
type File struct {
|
||||
Path string
|
||||
Content string
|
||||
Mode string
|
||||
}
|
||||
|
||||
// Node is the minimal peer description an emitter needs to render
|
||||
// node-specific paths. It carries the hostname, the runtimes available
|
||||
// on the node (so emitters can branch), and the node tags (used by
|
||||
// DaemonSet matching and affinity in P05).
|
||||
type Node struct {
|
||||
Hostname string
|
||||
Runtime []string
|
||||
Tags []string
|
||||
}
|
||||
|
||||
// Emitter renders a *jobspec.WorkloadSpec for a given Node into a slice
|
||||
// of File artifacts. Implementations are registered with a Registry
|
||||
// keyed by "<kind>:<runtime>".
|
||||
type Emitter interface {
|
||||
Render(spec *jobspec.WorkloadSpec, node *Node) ([]File, error)
|
||||
}
|
||||
|
||||
// Registry holds emitters keyed by "<kind>:<runtime>" (e.g.
|
||||
// "service:process", "job:wasm"). The zero-value Registry is not
|
||||
// usable; construct one with NewRegistry.
|
||||
type Registry struct {
|
||||
emitters map[string]Emitter
|
||||
}
|
||||
|
||||
// NewRegistry returns an empty Registry ready for Register calls.
|
||||
func NewRegistry() *Registry {
|
||||
return &Registry{emitters: make(map[string]Emitter)}
|
||||
}
|
||||
|
||||
// Register registers an Emitter under the given key. The key is
|
||||
// "<kind>:<runtime>" (e.g. "job:process"). Registering twice under the
|
||||
// same key overwrites the prior registration (last-wins) to keep the
|
||||
// surface simple; callers are responsible for not double-registering.
|
||||
func (r *Registry) Register(key string, e Emitter) {
|
||||
r.emitters[key] = e
|
||||
}
|
||||
|
||||
// Render looks up the emitter for "<kind>:<runtime>" in the registry and
|
||||
// delegates to it. The kind is lowercased so the canonical spec kinds
|
||||
// (Job, Service, DaemonSet) map to the lowercase registry keys
|
||||
// ("job:process", "service:wasm", "daemonset:process"). Returns an error
|
||||
// if the spec is nil, the spec is missing its Kind, the runtime is
|
||||
// missing, or no emitter is registered for the combination.
|
||||
func (r *Registry) Render(spec *jobspec.WorkloadSpec, node *Node) ([]File, error) {
|
||||
if spec == nil {
|
||||
return nil, fmt.Errorf("emitter: spec is nil")
|
||||
}
|
||||
if strings.TrimSpace(spec.Kind) == "" {
|
||||
return nil, fmt.Errorf("emitter: spec kind is empty")
|
||||
}
|
||||
if spec.Runtime == nil {
|
||||
return nil, fmt.Errorf("emitter: spec runtime is nil")
|
||||
}
|
||||
key := strings.ToLower(spec.Kind) + ":" + spec.Runtime.OneOf
|
||||
e, ok := r.emitters[key]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("emitter: no emitter registered for %q (kind:runtime)", key)
|
||||
}
|
||||
return e.Render(spec, node)
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package emitter
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
// mockEmitter is a test-only Emitter that returns a fixed File slice
|
||||
// (or an error) so the Registry tests do not depend on the
|
||||
// SystemdEmitter. Implements Emitter via value receiver.
|
||||
type mockEmitter struct {
|
||||
files []File
|
||||
err error
|
||||
}
|
||||
|
||||
func (m mockEmitter) Render(spec *jobspec.WorkloadSpec, node *Node) ([]File, error) {
|
||||
if m.err != nil {
|
||||
return nil, m.err
|
||||
}
|
||||
out := make([]File, len(m.files))
|
||||
copy(out, m.files)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func TestRegistry_RegisterAndRender(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
want := []File{{Path: "/tmp/a", Content: "alpha", Mode: "0644"}}
|
||||
r.Register("job:process", mockEmitter{files: want})
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "demo",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/true"},
|
||||
}
|
||||
node := &Node{Hostname: "node-1", Runtime: []string{"process"}}
|
||||
got, err := r.Render(spec, node)
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("got %d files, want 1", len(got))
|
||||
}
|
||||
if got[0] != want[0] {
|
||||
t.Errorf("file = %+v, want %+v", got[0], want[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistry_UnknownKindRuntime(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "wasm"},
|
||||
}
|
||||
_, err := r.Render(spec, &Node{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for unknown kind:runtime, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "no emitter registered") {
|
||||
t.Errorf("error = %q, want 'no emitter registered'", err.Error())
|
||||
}
|
||||
if !strings.Contains(err.Error(), "service:wasm") {
|
||||
t.Errorf("error = %q, want it to mention 'service:wasm'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistry_MultipleEmittersCorrectSelected(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
jobFiles := []File{{Path: "/tmp/job", Content: "job", Mode: "0644"}}
|
||||
svcFiles := []File{{Path: "/tmp/svc", Content: "svc", Mode: "0644"}}
|
||||
dsFiles := []File{{Path: "/tmp/ds", Content: "ds", Mode: "0644"}}
|
||||
r.Register("job:process", mockEmitter{files: jobFiles})
|
||||
r.Register("service:process", mockEmitter{files: svcFiles})
|
||||
r.Register("daemonset:process", mockEmitter{files: dsFiles})
|
||||
|
||||
cases := []struct {
|
||||
kind string
|
||||
runtime string
|
||||
wantPath string
|
||||
}{
|
||||
{"Job", "process", "/tmp/job"},
|
||||
{"Service", "process", "/tmp/svc"},
|
||||
{"DaemonSet", "process", "/tmp/ds"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.kind+":"+tc.runtime, func(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: tc.kind,
|
||||
Name: "x",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: tc.runtime, Command: "/bin/x"},
|
||||
}
|
||||
got, err := r.Render(spec, &Node{Hostname: "n"})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("got %d files, want 1", len(got))
|
||||
}
|
||||
if got[0].Path != tc.wantPath {
|
||||
t.Errorf("path = %q, want %q", got[0].Path, tc.wantPath)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistry_NilSpec(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
_, err := r.Render(nil, &Node{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nil spec")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "spec is nil") {
|
||||
t.Errorf("error = %q, want 'spec is nil'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistry_EmptyKind(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
spec := &jobspec.WorkloadSpec{Runtime: &jobspec.RuntimeBlock{OneOf: "process"}}
|
||||
_, err := r.Render(spec, &Node{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty kind")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "kind is empty") {
|
||||
t.Errorf("error = %q, want 'kind is empty'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistry_NilRuntime(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
spec := &jobspec.WorkloadSpec{Kind: "Job", Name: "x"}
|
||||
_, err := r.Render(spec, &Node{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nil runtime")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "runtime is nil") {
|
||||
t.Errorf("error = %q, want 'runtime is nil'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistry_EmitterErrorPropagates(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
wantErr := errors.New("boom")
|
||||
r.Register("job:process", mockEmitter{err: wantErr})
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "x",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/x"},
|
||||
}
|
||||
_, err := r.Render(spec, &Node{})
|
||||
if !errors.Is(err, wantErr) {
|
||||
t.Errorf("err = %v, want %v", err, wantErr)
|
||||
}
|
||||
}
|
||||
|
||||
// Compile-time assertion that mockEmitter and SystemdEmitter implement
|
||||
// Emitter.
|
||||
var (
|
||||
_ Emitter = mockEmitter{}
|
||||
_ Emitter = SystemdEmitter{}
|
||||
)
|
||||
@@ -0,0 +1,128 @@
|
||||
package emitter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
// SocketEmitter renders the systemd directives that implement the
|
||||
// R-007 socket-plumbing contract: workloads bind to
|
||||
// /run/orca/alloc-<id>/port-<name>.sock unless overridden via
|
||||
// service.bind = "127.0.0.1" (the only documented opt-in).
|
||||
//
|
||||
// The systemd side of the contract uses two directives:
|
||||
//
|
||||
// - RuntimeDirectory=orca/alloc-<alloc-id> — systemd creates
|
||||
// /run/orca/alloc-<alloc-id>/ owned by the service user (orca:orca)
|
||||
// with mode 0750. The directory is removed when the unit stops
|
||||
// (RuntimeDirectory= semantics). P08 emits one RuntimeDirectory=
|
||||
// line per port so each port's socket directory is created; the
|
||||
// alloc-id placeholder is spec.Name (the real alloc-id is assigned
|
||||
// by the scheduler at submit time — see allocIDFor).
|
||||
//
|
||||
// - ExecStartPre= — only when service.bind is "127.0.0.1" (the TCP
|
||||
// opt-in). In that case the workload binds a TCP port directly
|
||||
// (no socket), and the ExecStartPre is a placeholder that records
|
||||
// the bind (the actual bind happens in the process; the directive
|
||||
// is a no-op marker so operators can see the bind mode in the unit
|
||||
// file). When service.bind is empty (the default), the workload
|
||||
// binds the socket and no ExecStartPre is emitted for sockets.
|
||||
//
|
||||
// The socket path format is /run/orca/alloc-<alloc-id>/port-<port-name>.sock
|
||||
// where alloc-id is a PLACEHOLDER (spec.Name) — the real alloc-id is
|
||||
// assigned at submit time by the scheduler. The placeholder is
|
||||
// documented in the rendered unit via a comment so operators reading
|
||||
// the unit file understand the substitution.
|
||||
//
|
||||
// P08 is a PLAN/plumbing layer — the actual socket activation (socket
|
||||
// unit files, systemd socket-activation passing the pre-bound socket
|
||||
// fd to the process) lands in v0.10. P08 just renders the
|
||||
// RuntimeDirectory= lines and the optional TCP-bind ExecStartPre so
|
||||
// the directory exists at runtime.
|
||||
type SocketEmitter struct{}
|
||||
|
||||
// runtimeDirectoryRoot is the systemd RuntimeDirectory path root.
|
||||
// systemd joins this with the RuntimeDirectory= value to create
|
||||
// /run/orca/alloc-<id>. The leading slash is implicit in systemd
|
||||
// (RuntimeDirectory= is relative to /run).
|
||||
const runtimeDirectoryRoot = "orca"
|
||||
|
||||
// SocketPath returns the R-007 socket path for a port on the given
|
||||
// alloc-id. The alloc-id is the placeholder spec.Name when the real
|
||||
// alloc-id is not yet known (the scheduler assigns the real alloc-id
|
||||
// at submit time).
|
||||
func SocketPath(allocID, portName string) string {
|
||||
return fmt.Sprintf("/run/orca/alloc-%s/port-%s.sock", allocID, portName)
|
||||
}
|
||||
|
||||
// RenderSocketLines renders the systemd directives that implement
|
||||
// the R-007 socket plumbing for the given spec. The lines are returned
|
||||
// WITHOUT a trailing newline so the caller (the systemd emitter) can
|
||||
// append them to the [Service] block with consistent formatting.
|
||||
//
|
||||
// The returned lines are:
|
||||
//
|
||||
// - one RuntimeDirectory= line per port (so each port's socket
|
||||
// directory is created by systemd at unit start).
|
||||
// - a comment documenting the alloc-id placeholder.
|
||||
// - when service.bind is "127.0.0.1", an ExecStartPre= marker that
|
||||
// records the TCP opt-in (the actual bind is in the process).
|
||||
//
|
||||
// Returns an empty slice when the spec has no ports (no socket
|
||||
// plumbing needed — e.g. a Job or a port-less DaemonSet).
|
||||
func (SocketEmitter) RenderSocketLines(spec *jobspec.WorkloadSpec) []string {
|
||||
if spec == nil || len(spec.Ports) == 0 {
|
||||
return nil
|
||||
}
|
||||
allocID := allocIDForSocket(spec)
|
||||
var lines []string
|
||||
// One RuntimeDirectory= per port. systemd dedupes identical
|
||||
// values, but we emit one per port so the unit file is
|
||||
// self-documenting (each port maps to a directory entry).
|
||||
for _, p := range spec.Ports {
|
||||
lines = append(lines, fmt.Sprintf("RuntimeDirectory=%s/alloc-%s", runtimeDirectoryRoot, allocID))
|
||||
// Document the socket path this directory serves. systemd
|
||||
// ignores comment lines (lines starting with '#').
|
||||
lines = append(lines, fmt.Sprintf("# socket: %s", SocketPath(allocID, p.Name)))
|
||||
}
|
||||
// TCP opt-in: when service.bind is 127.0.0.1, the workload binds
|
||||
// a TCP port directly instead of the socket. We emit an
|
||||
// ExecStartPre marker so the bind mode is visible in the unit
|
||||
// file. The actual bind is in the process; the marker is a
|
||||
// no-op (echo to journald).
|
||||
if spec.Service != nil && strings.TrimSpace(spec.Service.Bind) != "" {
|
||||
if isTCPOptIn(spec.Service.Bind) {
|
||||
for _, p := range spec.Ports {
|
||||
lines = append(lines, fmt.Sprintf("ExecStartPre=/bin/echo orca: bind %s port %s (tcp, R-007 opt-in)", spec.Service.Bind, p.Name))
|
||||
}
|
||||
}
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
// allocIDForSocket returns the alloc-id placeholder for the spec. The
|
||||
// real alloc-id is assigned by the scheduler at submit time; P08 uses
|
||||
// spec.Name as a deterministic placeholder so the rendered unit is
|
||||
// stable across re-renders. This mirrors the Traefik emitter's
|
||||
// allocIDFor (which uses the node hostname for the Traefik
|
||||
// dynamic-config server URL); the systemd unit is per-alloc, so
|
||||
// spec.Name is the right placeholder here.
|
||||
func allocIDForSocket(spec *jobspec.WorkloadSpec) string {
|
||||
if spec == nil || strings.TrimSpace(spec.Name) == "" {
|
||||
return "<allocID>"
|
||||
}
|
||||
return spec.Name
|
||||
}
|
||||
|
||||
// isTCPOptIn returns true when the bind value is the documented
|
||||
// 127.0.0.1 TCP opt-in (R-007). Other valid IPs (::1, etc.) are also
|
||||
// TCP opt-ins (any non-empty bind opts out of the socket default); we
|
||||
// only emit the marker for 127.0.0.1 because that is the only
|
||||
// documented opt-in per the PRD — other IPs are accepted by the
|
||||
// schema validator but are operator-specific and we do not
|
||||
// second-guess them.
|
||||
func isTCPOptIn(bind string) bool {
|
||||
return strings.TrimSpace(bind) == "127.0.0.1"
|
||||
}
|
||||
@@ -0,0 +1,265 @@
|
||||
package emitter
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
func TestSocketEmitter_RenderSocketLines_NoPorts(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "backup",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/rsync"},
|
||||
}
|
||||
lines := (SocketEmitter{}).RenderSocketLines(spec)
|
||||
if len(lines) != 0 {
|
||||
t.Errorf("got %d lines, want 0 for no ports: %v", len(lines), lines)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSocketEmitter_RenderSocketLines_NilSpec(t *testing.T) {
|
||||
lines := (SocketEmitter{}).RenderSocketLines(nil)
|
||||
if lines != nil {
|
||||
t.Errorf("nil spec should return nil, got %v", lines)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSocketEmitter_RenderSocketLines_SinglePort(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
lines := (SocketEmitter{}).RenderSocketLines(spec)
|
||||
// Expect: RuntimeDirectory + comment. No TCP bind (default socket).
|
||||
wantRT := "RuntimeDirectory=orca/alloc-web"
|
||||
if !contains(lines, wantRT) {
|
||||
t.Errorf("lines %v missing %q", lines, wantRT)
|
||||
}
|
||||
wantSock := "# socket: /run/orca/alloc-web/port-http.sock"
|
||||
if !contains(lines, wantSock) {
|
||||
t.Errorf("lines %v missing %q", lines, wantSock)
|
||||
}
|
||||
for _, l := range lines {
|
||||
if strings.HasPrefix(l, "ExecStartPre=") {
|
||||
t.Errorf("socket bind should not emit ExecStartPre (no TCP opt-in): %s", l)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSocketEmitter_RenderSocketLines_MultiplePorts(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "api",
|
||||
Ports: []jobspec.PortSpec{
|
||||
{Name: "http", Port: 8080},
|
||||
{Name: "grpc", Port: 9090},
|
||||
},
|
||||
}
|
||||
lines := (SocketEmitter{}).RenderSocketLines(spec)
|
||||
// Two RuntimeDirectory lines (one per port).
|
||||
count := 0
|
||||
for _, l := range lines {
|
||||
if l == "RuntimeDirectory=orca/alloc-api" {
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count != 2 {
|
||||
t.Errorf("RuntimeDirectory count = %d, want 2 (one per port)", count)
|
||||
}
|
||||
if !contains(lines, "# socket: /run/orca/alloc-api/port-http.sock") {
|
||||
t.Errorf("missing http socket comment")
|
||||
}
|
||||
if !contains(lines, "# socket: /run/orca/alloc-api/port-grpc.sock") {
|
||||
t.Errorf("missing grpc socket comment")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSocketEmitter_RenderSocketLines_TCPBind127(t *testing.T) {
|
||||
// service.bind = 127.0.0.1 → TCP opt-in → ExecStartPre marker per port.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
Service: &jobspec.ServiceBlock{Bind: "127.0.0.1"},
|
||||
}
|
||||
lines := (SocketEmitter{}).RenderSocketLines(spec)
|
||||
found := false
|
||||
for _, l := range lines {
|
||||
if strings.HasPrefix(l, "ExecStartPre=/bin/echo orca: bind 127.0.0.1 port http (tcp, R-007 opt-in)") {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("missing TCP bind ExecStartPre marker; lines: %v", lines)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSocketEmitter_RenderSocketLines_TCPBindIPv6(t *testing.T) {
|
||||
// Non-127.0.0.1 bind is accepted by schema but not the documented
|
||||
// opt-in; the marker is only emitted for 127.0.0.1. The
|
||||
// RuntimeDirectory lines are still emitted (the directory exists
|
||||
// regardless of bind mode — sockets or TCP).
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
Service: &jobspec.ServiceBlock{Bind: "::1"},
|
||||
}
|
||||
lines := (SocketEmitter{}).RenderSocketLines(spec)
|
||||
for _, l := range lines {
|
||||
if strings.HasPrefix(l, "ExecStartPre=") {
|
||||
t.Errorf("::1 bind should NOT emit TCP marker (only 127.0.0.1 is documented opt-in): %s", l)
|
||||
}
|
||||
}
|
||||
if !contains(lines, "RuntimeDirectory=orca/alloc-web") {
|
||||
t.Errorf("RuntimeDirectory should still be emitted for ::1 bind")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSocketEmitter_RenderSocketLines_EmptyBindSocket(t *testing.T) {
|
||||
// Empty bind → default socket → no TCP marker, but RuntimeDirectory emitted.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
Service: &jobspec.ServiceBlock{Bind: ""},
|
||||
}
|
||||
lines := (SocketEmitter{}).RenderSocketLines(spec)
|
||||
for _, l := range lines {
|
||||
if strings.HasPrefix(l, "ExecStartPre=") {
|
||||
t.Errorf("empty bind should NOT emit TCP marker: %s", l)
|
||||
}
|
||||
}
|
||||
if !contains(lines, "RuntimeDirectory=orca/alloc-web") {
|
||||
t.Errorf("RuntimeDirectory missing for empty bind")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSocketEmitter_RenderSocketLines_NilService(t *testing.T) {
|
||||
// No service block → default socket → no TCP marker, but RuntimeDirectory emitted.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
lines := (SocketEmitter{}).RenderSocketLines(spec)
|
||||
for _, l := range lines {
|
||||
if strings.HasPrefix(l, "ExecStartPre=") {
|
||||
t.Errorf("nil service should NOT emit TCP marker: %s", l)
|
||||
}
|
||||
}
|
||||
if !contains(lines, "RuntimeDirectory=orca/alloc-web") {
|
||||
t.Errorf("RuntimeDirectory missing for nil service")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSocketEmitter_SocketPath(t *testing.T) {
|
||||
got := SocketPath("alloc-123", "http")
|
||||
want := "/run/orca/alloc-alloc-123/port-http.sock"
|
||||
if got != want {
|
||||
t.Errorf("SocketPath = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSocketEmitter_AllocIDPlaceholderNilSpec(t *testing.T) {
|
||||
if got := allocIDForSocket(nil); got != "<allocID>" {
|
||||
t.Errorf("allocIDForSocket(nil) = %q, want <allocID>", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSocketEmitter_AllocIDPlaceholderEmptyName(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{Name: " "}
|
||||
if got := allocIDForSocket(spec); got != "<allocID>" {
|
||||
t.Errorf("allocIDForSocket(empty name) = %q, want <allocID>", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSocketEmitter_AllocIDPlaceholderNamedSpec(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{Name: "web"}
|
||||
if got := allocIDForSocket(spec); got != "web" {
|
||||
t.Errorf("allocIDForSocket(web) = %q, want web", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSocketEmitter_SocketPathPlaceholder(t *testing.T) {
|
||||
got := SocketPath("<allocID>", "grpc")
|
||||
want := "/run/orca/alloc-<allocID>/port-grpc.sock"
|
||||
if got != want {
|
||||
t.Errorf("SocketPath = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_IntegratesSocketLines(t *testing.T) {
|
||||
// End-to-end: the systemd unit for a Service with ports contains
|
||||
// the RuntimeDirectory line emitted by the SocketEmitter.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/httpd"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
files, err := SystemdEmitter{}.Render(spec, &Node{})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
c := files[0].Content
|
||||
if !strings.Contains(c, "RuntimeDirectory=orca/alloc-web\n") {
|
||||
t.Errorf("unit missing RuntimeDirectory line\n%s", c)
|
||||
}
|
||||
if !strings.Contains(c, "# socket: /run/orca/alloc-web/port-http.sock\n") {
|
||||
t.Errorf("unit missing socket path comment\n%s", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_IntegratesSocketLinesTCPBind(t *testing.T) {
|
||||
// When service.bind = 127.0.0.1, the unit contains the ExecStartPre
|
||||
// TCP-bind marker.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/httpd"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
Service: &jobspec.ServiceBlock{Bind: "127.0.0.1"},
|
||||
}
|
||||
files, err := SystemdEmitter{}.Render(spec, &Node{})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
c := files[0].Content
|
||||
if !strings.Contains(c, "ExecStartPre=/bin/echo orca: bind 127.0.0.1 port http (tcp, R-007 opt-in)\n") {
|
||||
t.Errorf("unit missing TCP bind ExecStartPre marker\n%s", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_NoSocketLinesForPortlessSpec(t *testing.T) {
|
||||
// A Job with no ports → no RuntimeDirectory line in the unit.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "backup",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/rsync"},
|
||||
}
|
||||
files, err := SystemdEmitter{}.Render(spec, &Node{})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
c := files[0].Content
|
||||
if strings.Contains(c, "RuntimeDirectory=") {
|
||||
t.Errorf("portless spec should not emit RuntimeDirectory\n%s", c)
|
||||
}
|
||||
if strings.Contains(c, "# socket:") {
|
||||
t.Errorf("portless spec should not emit socket comment\n%s", c)
|
||||
}
|
||||
}
|
||||
|
||||
// contains reports whether the slice contains the string s.
|
||||
func contains(lines []string, s string) bool {
|
||||
for _, l := range lines {
|
||||
if l == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
package emitter
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
// SystemdEmitter is the Emitter implementation for the "process"
|
||||
// runtime. It renders the systemd unit file for the workload,
|
||||
// including the lifecycle hooks (P04) and the R-007 socket plumbing
|
||||
// (P08).
|
||||
//
|
||||
// Lifecycle hooks map to systemd semantics (PRD §10.1):
|
||||
//
|
||||
// - lifecycle.pre_stop → ExecStop= (the command run on stop; systemd
|
||||
// runs ExecStop, then kills the main process after the deadline).
|
||||
// - lifecycle.post_start → ExecStartPost= (runs after the main
|
||||
// process starts).
|
||||
//
|
||||
// systemd has no ExecStartPre equivalent for a "pre_start" hook; the
|
||||
// spec does not define pre_start (only pre_stop and post_start per
|
||||
// PRD §10.1), so no mapping is needed.
|
||||
//
|
||||
// The unit name carries the `orca-v1-` prefix per the dual-write
|
||||
// window (REQ-090) so the v0.9 SSH-push path does not collide with the
|
||||
// v0.8 daemon's `orca-<job>.service` units during the migration
|
||||
// window.
|
||||
//
|
||||
// Later phases extend this emitter:
|
||||
//
|
||||
// - v0.10-P03: secrets via EnvironmentFile= + LoadCredential=
|
||||
type SystemdEmitter struct{}
|
||||
|
||||
// unitNamePrefix is the v0.9 SSH-push unit-name prefix. The v0.8
|
||||
// daemon uses `orca-<job>.service`; the v0.9 path uses
|
||||
// `orca-v1-<spec.Name>.service` so the two never overlap (REQ-090,
|
||||
// I-C-006). The prefix is load-bearing — do not change it without
|
||||
// updating the dual-write window contract.
|
||||
const unitNamePrefix = "orca-v1-"
|
||||
|
||||
// Render renders the systemd unit file for a process-runtime workload.
|
||||
//
|
||||
// When the spec has no Tasks (the single-process case, the historical
|
||||
// shape), the unit name is
|
||||
// /etc/systemd/system/<unitNamePrefix><spec.Name>.service and the
|
||||
// content is a [Service] block with ExecStart, optional ExecStartPost
|
||||
// (lifecycle.post_start), optional ExecStop (lifecycle.pre_stop), and
|
||||
// the R-007 socket-plumbing lines (RuntimeDirectory=, optional
|
||||
// TCP-bind ExecStartPre). Mode is 0644.
|
||||
//
|
||||
// When the spec has a task group (P06, spec.Tasks non-empty), the
|
||||
// alloc is multi-process and Render emits one systemd unit per task
|
||||
// (`orca-v1-alloc-<alloc-id>-<task-name>.service`) plus a single
|
||||
// grouping target unit (`orca-v1-alloc-<alloc-id>.target`) that
|
||||
// starts/stops all tasks together. Each per-task unit carries
|
||||
// `PartOf=orca-v1-alloc-<alloc-id>.target` and is
|
||||
// `WantedBy=multi-user.target` so the task starts at boot. Tasks
|
||||
// that omit their own runtime inherit the top-level spec.Runtime as
|
||||
// the per-group default.
|
||||
//
|
||||
// The rendered shape (single-process) is:
|
||||
//
|
||||
// [Service]
|
||||
// ExecStart=<runtime command>
|
||||
// ExecStartPost=<post_start command 1>
|
||||
// ExecStartPost=<post_start command 2>
|
||||
// ExecStop=<pre_stop command 1>
|
||||
// ExecStop=<pre_stop command 2>
|
||||
// RuntimeDirectory=orca/alloc-<alloc-id>
|
||||
// # socket: /run/orca/alloc-<alloc-id>/port-<name>.sock
|
||||
// ExecStartPre=/bin/echo orca: bind 127.0.0.1 port <name> (tcp, R-007 opt-in)
|
||||
//
|
||||
// Returns an error if the spec is nil, the spec is missing its name,
|
||||
// the runtime block is nil, or the runtime command is empty (a
|
||||
// workload with no command has nothing to ExecStart). For task groups,
|
||||
// returns an error if any task has no resolvable runtime command.
|
||||
func (SystemdEmitter) Render(spec *jobspec.WorkloadSpec, node *Node) ([]File, error) {
|
||||
if spec == nil {
|
||||
return nil, errors.New("emitter/systemd: spec is nil")
|
||||
}
|
||||
if strings.TrimSpace(spec.Name) == "" {
|
||||
return nil, errors.New("emitter/systemd: spec name is empty")
|
||||
}
|
||||
if len(spec.Tasks) > 0 {
|
||||
return renderTaskGroup(spec, node)
|
||||
}
|
||||
if spec.Runtime == nil {
|
||||
return nil, errors.New("emitter/systemd: runtime block is nil")
|
||||
}
|
||||
if strings.TrimSpace(spec.Runtime.Command) == "" {
|
||||
return nil, errors.New("emitter/systemd: runtime command is empty")
|
||||
}
|
||||
path := fmt.Sprintf("/etc/systemd/system/%s%s.service", unitNamePrefix, spec.Name)
|
||||
content := renderSystemdUnit(spec)
|
||||
return []File{{Path: path, Content: content, Mode: "0644"}}, nil
|
||||
}
|
||||
|
||||
// renderTaskGroup renders one systemd unit per task plus the grouping
|
||||
// target unit. Each task's runtime falls back to the top-level
|
||||
// spec.Runtime when the task omits its own. Tasks with no resolvable
|
||||
// command (no task.Command, no task.Runtime.Command, no top-level
|
||||
// Runtime) return an error.
|
||||
func renderTaskGroup(spec *jobspec.WorkloadSpec, node *Node) ([]File, error) {
|
||||
allocID := spec.Name
|
||||
targetUnit := fmt.Sprintf("%salloc-%s.target", unitNamePrefix, allocID)
|
||||
targetPath := fmt.Sprintf("/etc/systemd/system/%s", targetUnit)
|
||||
var files []File
|
||||
for _, task := range spec.Tasks {
|
||||
rt := taskRuntime(spec, &task)
|
||||
if rt == nil {
|
||||
return nil, fmt.Errorf("emitter/systemd: task %q has no runtime (set tasks[].runtime or top-level runtime)", task.Name)
|
||||
}
|
||||
cmd := taskCommand(spec, &task, rt)
|
||||
if strings.TrimSpace(cmd) == "" {
|
||||
return nil, fmt.Errorf("emitter/systemd: task %q command is empty", task.Name)
|
||||
}
|
||||
unitName := fmt.Sprintf("%salloc-%s-%s.service", unitNamePrefix, allocID, task.Name)
|
||||
path := fmt.Sprintf("/etc/systemd/system/%s", unitName)
|
||||
content := renderTaskUnit(spec, &task, rt, cmd, targetUnit)
|
||||
files = append(files, File{Path: path, Content: content, Mode: "0644"})
|
||||
}
|
||||
files = append(files, File{
|
||||
Path: targetPath,
|
||||
Content: renderTargetUnit(targetUnit, spec, allocID),
|
||||
Mode: "0644",
|
||||
})
|
||||
return files, nil
|
||||
}
|
||||
|
||||
// taskRuntime returns the effective runtime for a task: the task's own
|
||||
// runtime when set, otherwise the top-level spec.Runtime (the per-group
|
||||
// default). Returns nil when neither is set.
|
||||
func taskRuntime(spec *jobspec.WorkloadSpec, task *jobspec.TaskGroupTask) *jobspec.RuntimeBlock {
|
||||
if task.Runtime != nil {
|
||||
return task.Runtime
|
||||
}
|
||||
return spec.Runtime
|
||||
}
|
||||
|
||||
// taskCommand returns the ExecStart command for a task. A task-level
|
||||
// Command takes precedence; otherwise the task's runtime command is
|
||||
// used; otherwise the top-level runtime command is used. Returns an
|
||||
// empty string when none is set.
|
||||
func taskCommand(spec *jobspec.WorkloadSpec, task *jobspec.TaskGroupTask, rt *jobspec.RuntimeBlock) string {
|
||||
if strings.TrimSpace(task.Command) != "" {
|
||||
return task.Command
|
||||
}
|
||||
if rt != nil && strings.TrimSpace(rt.Command) != "" {
|
||||
return rt.Command
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// renderTaskUnit renders a single per-task systemd [Unit]+[Service]
|
||||
// block. The unit is `PartOf=` the alloc target and
|
||||
// `WantedBy=multi-user.target` so it starts at boot and stops with the
|
||||
// group. The [Service] block carries the task's ExecStart and the
|
||||
// socket-plumbing lines derived from the spec's ports.
|
||||
func renderTaskUnit(spec *jobspec.WorkloadSpec, task *jobspec.TaskGroupTask, rt *jobspec.RuntimeBlock, cmd, targetUnit string) string {
|
||||
var b strings.Builder
|
||||
b.WriteString("[Unit]\n")
|
||||
b.WriteString(fmt.Sprintf("Description=orca alloc task %s\n", task.Name))
|
||||
b.WriteString(fmt.Sprintf("PartOf=%s\n", targetUnit))
|
||||
b.WriteString("\n[Service]\n")
|
||||
b.WriteString(fmt.Sprintf("ExecStart=%s\n", cmd))
|
||||
for _, line := range (SocketEmitter{}).RenderSocketLines(spec) {
|
||||
b.WriteString(line)
|
||||
b.WriteString("\n")
|
||||
}
|
||||
b.WriteString("\n[Install]\n")
|
||||
b.WriteString("WantedBy=multi-user.target\n")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// renderTargetUnit renders the grouping target unit
|
||||
// (`orca-v1-alloc-<alloc-id>.target`) that starts/stops all tasks
|
||||
// together. The [Unit] block lists every per-task unit under Wants=
|
||||
// so `systemctl start <target>` brings them all up, and
|
||||
// `systemctl stop <target>` tears them down (PartOf= propagates stop).
|
||||
func renderTargetUnit(targetUnit string, spec *jobspec.WorkloadSpec, allocID string) string {
|
||||
var b strings.Builder
|
||||
b.WriteString("[Unit]\n")
|
||||
b.WriteString(fmt.Sprintf("Description=orca alloc %s task group\n", allocID))
|
||||
for _, task := range spec.Tasks {
|
||||
b.WriteString(fmt.Sprintf("Wants=%salloc-%s-%s.service\n", unitNamePrefix, allocID, task.Name))
|
||||
}
|
||||
b.WriteString("\n[Install]\n")
|
||||
b.WriteString("WantedBy=multi-user.target\n")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// renderSystemdUnit renders the full [Service] block for the spec,
|
||||
// including ExecStart, lifecycle hooks (ExecStartPost, ExecStop), and
|
||||
// the R-007 socket-plumbing lines (RuntimeDirectory=, optional
|
||||
// TCP-bind ExecStartPre). The output is a single string with a
|
||||
// trailing newline per line.
|
||||
func renderSystemdUnit(spec *jobspec.WorkloadSpec) string {
|
||||
var b strings.Builder
|
||||
b.WriteString("[Service]\n")
|
||||
b.WriteString(fmt.Sprintf("ExecStart=%s\n", spec.Runtime.Command))
|
||||
// Lifecycle: post_start → ExecStartPost (runs after start).
|
||||
for _, cmd := range lifecyclePostStart(spec) {
|
||||
b.WriteString(fmt.Sprintf("ExecStartPost=%s\n", cmd))
|
||||
}
|
||||
// Lifecycle: pre_stop → ExecStop (runs before the process is killed).
|
||||
for _, cmd := range lifecyclePreStop(spec) {
|
||||
b.WriteString(fmt.Sprintf("ExecStop=%s\n", cmd))
|
||||
}
|
||||
// R-007 socket plumbing: RuntimeDirectory= per port + optional
|
||||
// TCP-bind ExecStartPre.
|
||||
for _, line := range (SocketEmitter{}).RenderSocketLines(spec) {
|
||||
b.WriteString(line)
|
||||
b.WriteString("\n")
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// lifecyclePostStart returns the post_start lifecycle commands for
|
||||
// the spec, or nil when the spec has no lifecycle block or no
|
||||
// post_start commands.
|
||||
func lifecyclePostStart(spec *jobspec.WorkloadSpec) []string {
|
||||
if spec.Lifecycle == nil {
|
||||
return nil
|
||||
}
|
||||
return spec.Lifecycle.PostStart
|
||||
}
|
||||
|
||||
// lifecyclePreStop returns the pre_stop lifecycle commands for the
|
||||
// spec, or nil when the spec has no lifecycle block or no pre_stop
|
||||
// commands.
|
||||
func lifecyclePreStop(spec *jobspec.WorkloadSpec) []string {
|
||||
if spec.Lifecycle == nil {
|
||||
return nil
|
||||
}
|
||||
return spec.Lifecycle.PreStop
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package emitter
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
func TestSystemdEmitter_LifecyclePostStart(t *testing.T) {
|
||||
// lifecycle.post_start → ExecStartPost (one line per command).
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/usr/local/bin/httpd -f"},
|
||||
Lifecycle: &jobspec.LifecycleBlock{
|
||||
PostStart: []string{"/usr/bin/sleep 1", "/usr/bin/curl localhost/healthz"},
|
||||
},
|
||||
}
|
||||
files, err := SystemdEmitter{}.Render(spec, &Node{Hostname: "n1"})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
c := files[0].Content
|
||||
if !strings.Contains(c, "ExecStartPost=/usr/bin/sleep 1\n") {
|
||||
t.Errorf("missing ExecStartPost for sleep 1\n%s", c)
|
||||
}
|
||||
if !strings.Contains(c, "ExecStartPost=/usr/bin/curl localhost/healthz\n") {
|
||||
t.Errorf("missing ExecStartPost for curl\n%s", c)
|
||||
}
|
||||
// ExecStart must still be present.
|
||||
if !strings.Contains(c, "ExecStart=/usr/local/bin/httpd -f\n") {
|
||||
t.Errorf("missing ExecStart\n%s", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_LifecyclePreStop(t *testing.T) {
|
||||
// lifecycle.pre_stop → ExecStop (one line per command).
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/usr/local/bin/httpd -f"},
|
||||
Lifecycle: &jobspec.LifecycleBlock{
|
||||
PreStop: []string{"/usr/local/bin/httpd -graceful", "/usr/bin/sleep 5"},
|
||||
},
|
||||
}
|
||||
files, err := SystemdEmitter{}.Render(spec, &Node{Hostname: "n1"})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
c := files[0].Content
|
||||
if !strings.Contains(c, "ExecStop=/usr/local/bin/httpd -graceful\n") {
|
||||
t.Errorf("missing ExecStop for graceful\n%s", c)
|
||||
}
|
||||
if !strings.Contains(c, "ExecStop=/usr/bin/sleep 5\n") {
|
||||
t.Errorf("missing ExecStop for sleep 5\n%s", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_LifecycleBoth(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/httpd"},
|
||||
Lifecycle: &jobspec.LifecycleBlock{
|
||||
PostStart: []string{"/bin/after-start"},
|
||||
PreStop: []string{"/bin/before-stop"},
|
||||
},
|
||||
}
|
||||
files, err := SystemdEmitter{}.Render(spec, &Node{})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
c := files[0].Content
|
||||
// ExecStartPost must appear before ExecStop (post_start runs after
|
||||
// start; pre_stop runs before stop — the order in the unit file
|
||||
// reflects the lifecycle order).
|
||||
startIdx := strings.Index(c, "ExecStart=")
|
||||
postIdx := strings.Index(c, "ExecStartPost=")
|
||||
stopIdx := strings.Index(c, "ExecStop=")
|
||||
if startIdx < 0 || postIdx < 0 || stopIdx < 0 {
|
||||
t.Fatalf("missing one of ExecStart/ExecStartPost/ExecStop\n%s", c)
|
||||
}
|
||||
if !(startIdx < postIdx && postIdx < stopIdx) {
|
||||
t.Errorf("expected order ExecStart < ExecStartPost < ExecStop\n%s", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_LifecycleNilOmitsDirectives(t *testing.T) {
|
||||
// No lifecycle block → no ExecStartPost / ExecStop lines.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "backup",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/rsync"},
|
||||
}
|
||||
files, err := SystemdEmitter{}.Render(spec, &Node{})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
c := files[0].Content
|
||||
if strings.Contains(c, "ExecStartPost=") {
|
||||
t.Errorf("ExecStartPost should be omitted when no lifecycle\n%s", c)
|
||||
}
|
||||
if strings.Contains(c, "ExecStop=") {
|
||||
t.Errorf("ExecStop should be omitted when no lifecycle\n%s", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_LifecycleEmptyListsOmitted(t *testing.T) {
|
||||
// Lifecycle block present but empty lists → no ExecStartPost / ExecStop.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "x",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/x"},
|
||||
Lifecycle: &jobspec.LifecycleBlock{},
|
||||
}
|
||||
files, err := SystemdEmitter{}.Render(spec, &Node{})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
c := files[0].Content
|
||||
if strings.Contains(c, "ExecStartPost=") {
|
||||
t.Errorf("ExecStartPost should be omitted for empty PostStart\n%s", c)
|
||||
}
|
||||
if strings.Contains(c, "ExecStop=") {
|
||||
t.Errorf("ExecStop should be omitted for empty PreStop\n%s", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_LifecycleOnlyPostStart(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "x",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/x"},
|
||||
Lifecycle: &jobspec.LifecycleBlock{
|
||||
PostStart: []string{"/bin/notify-up"},
|
||||
},
|
||||
}
|
||||
files, err := SystemdEmitter{}.Render(spec, &Node{})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
c := files[0].Content
|
||||
if !strings.Contains(c, "ExecStartPost=/bin/notify-up\n") {
|
||||
t.Errorf("missing ExecStartPost\n%s", c)
|
||||
}
|
||||
if strings.Contains(c, "ExecStop=") {
|
||||
t.Errorf("ExecStop should be omitted when only PostStart set\n%s", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_LifecycleOnlyPreStop(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "x",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/x"},
|
||||
Lifecycle: &jobspec.LifecycleBlock{
|
||||
PreStop: []string{"/bin/notify-down"},
|
||||
},
|
||||
}
|
||||
files, err := SystemdEmitter{}.Render(spec, &Node{})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
c := files[0].Content
|
||||
if !strings.Contains(c, "ExecStop=/bin/notify-down\n") {
|
||||
t.Errorf("missing ExecStop\n%s", c)
|
||||
}
|
||||
if strings.Contains(c, "ExecStartPost=") {
|
||||
t.Errorf("ExecStartPost should be omitted when only PreStop set\n%s", c)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
package emitter
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
func TestSystemdEmitter_RenderJob(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "backup",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/usr/bin/rsync -a /src /dst"},
|
||||
}
|
||||
node := &Node{Hostname: "node-1", Runtime: []string{"process"}}
|
||||
files, err := SystemdEmitter{}.Render(spec, node)
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if len(files) != 1 {
|
||||
t.Fatalf("got %d files, want 1", len(files))
|
||||
}
|
||||
f := files[0]
|
||||
wantPath := "/etc/systemd/system/orca-v1-backup.service"
|
||||
if f.Path != wantPath {
|
||||
t.Errorf("Path = %q, want %q", f.Path, wantPath)
|
||||
}
|
||||
wantContent := "[Service]\nExecStart=/usr/bin/rsync -a /src /dst\n"
|
||||
if f.Content != wantContent {
|
||||
t.Errorf("Content = %q, want %q", f.Content, wantContent)
|
||||
}
|
||||
if f.Mode != "0644" {
|
||||
t.Errorf("Mode = %q, want 0644", f.Mode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_RenderService(t *testing.T) {
|
||||
// The full service emitter (Traefik route + health checks) lands in
|
||||
// P02; here we only prove the systemd side renders for a Service
|
||||
// kind with a process runtime.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/usr/local/bin/httpd -f"},
|
||||
}
|
||||
node := &Node{Hostname: "node-1", Runtime: []string{"process"}}
|
||||
files, err := SystemdEmitter{}.Render(spec, node)
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if len(files) != 1 {
|
||||
t.Fatalf("got %d files, want 1", len(files))
|
||||
}
|
||||
if files[0].Path != "/etc/systemd/system/orca-v1-web.service" {
|
||||
t.Errorf("Path = %q, want /etc/systemd/system/orca-v1-web.service", files[0].Path)
|
||||
}
|
||||
if !strings.Contains(files[0].Content, "ExecStart=/usr/local/bin/httpd -f") {
|
||||
t.Errorf("Content = %q, want it to contain the ExecStart line", files[0].Content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_EmptyCommandError(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "x",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: ""},
|
||||
}
|
||||
_, err := SystemdEmitter{}.Render(spec, &Node{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty command, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "command is empty") {
|
||||
t.Errorf("error = %q, want 'command is empty'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_WhitespaceCommandError(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "x",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: " "},
|
||||
}
|
||||
_, err := SystemdEmitter{}.Render(spec, &Node{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for whitespace-only command, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "command is empty") {
|
||||
t.Errorf("error = %q, want 'command is empty'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_NilSpec(t *testing.T) {
|
||||
v := SystemdEmitter{}
|
||||
if _, err := v.Render(nil, &Node{}); err == nil {
|
||||
t.Fatal("expected error for nil spec")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_EmptyName(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: " ",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/x"},
|
||||
}
|
||||
_, err := SystemdEmitter{}.Render(spec, &Node{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty name")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "name is empty") {
|
||||
t.Errorf("error = %q, want 'name is empty'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_NilRuntime(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{Kind: "Job", Name: "x"}
|
||||
_, err := SystemdEmitter{}.Render(spec, &Node{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nil runtime")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "runtime block is nil") {
|
||||
t.Errorf("error = %q, want 'runtime block is nil'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_UnitNamePrefix(t *testing.T) {
|
||||
// The orca-v1- prefix is load-bearing for the dual-write window
|
||||
// (REQ-090, I-C-006): the v0.8 daemon writes `orca-<job>.service`
|
||||
// and the v0.9 SSH-push path writes `orca-v1-<spec.Name>.service`,
|
||||
// so the two never collide. This test guards against accidental
|
||||
// removal of the prefix.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "dual-write-safety",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/true"},
|
||||
}
|
||||
files, err := SystemdEmitter{}.Render(spec, &Node{Hostname: "n"})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if !strings.HasPrefix(files[0].Path, "/etc/systemd/system/orca-v1-") {
|
||||
t.Errorf("Path = %q, want it to start with /etc/systemd/system/orca-v1- (REQ-090)", files[0].Path)
|
||||
}
|
||||
if !strings.HasSuffix(files[0].Path, ".service") {
|
||||
t.Errorf("Path = %q, want it to end with .service", files[0].Path)
|
||||
}
|
||||
// Explicitly assert the full expected unit name to lock the contract.
|
||||
want := "/etc/systemd/system/orca-v1-dual-write-safety.service"
|
||||
if files[0].Path != want {
|
||||
t.Errorf("Path = %q, want %q", files[0].Path, want)
|
||||
}
|
||||
// Sanity: the prefix is exactly "orca-v1-", not "orca-v0" or "orca".
|
||||
if unitNamePrefix != "orca-v1-" {
|
||||
t.Errorf("unitNamePrefix = %q, want orca-v1-", unitNamePrefix)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_TaskGroupTwoTasks(t *testing.T) {
|
||||
// P06: a task group with two tasks renders one unit per task plus
|
||||
// a grouping target unit. Each per-task unit is
|
||||
// `orca-v1-alloc-<alloc-id>-<task-name>.service`, carries
|
||||
// `PartOf=orca-v1-alloc-<alloc-id>.target`, and is
|
||||
// `WantedBy=multi-user.target`. The target unit lists every
|
||||
// per-task unit under Wants=.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Tasks: []jobspec.TaskGroupTask{
|
||||
{
|
||||
Name: "app",
|
||||
Command: "/usr/bin/httpd -f",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
},
|
||||
{
|
||||
Name: "sidecar",
|
||||
Command: "/bin/wasm-runner sidecar.wasm",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "wasm"},
|
||||
},
|
||||
},
|
||||
}
|
||||
files, err := SystemdEmitter{}.Render(spec, &Node{Hostname: "n1"})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
// 2 per-task units + 1 target unit.
|
||||
if len(files) != 3 {
|
||||
t.Fatalf("got %d files, want 3 (2 per-task units + 1 target)", len(files))
|
||||
}
|
||||
wantApp := "/etc/systemd/system/orca-v1-alloc-web-app.service"
|
||||
wantSide := "/etc/systemd/system/orca-v1-alloc-web-sidecar.service"
|
||||
wantTarget := "/etc/systemd/system/orca-v1-alloc-web.target"
|
||||
paths := make(map[string]*File, len(files))
|
||||
for i := range files {
|
||||
paths[files[i].Path] = &files[i]
|
||||
}
|
||||
if _, ok := paths[wantApp]; !ok {
|
||||
t.Errorf("missing per-task unit %q; got paths %v", wantApp, filePaths(files))
|
||||
}
|
||||
if _, ok := paths[wantSide]; !ok {
|
||||
t.Errorf("missing per-task unit %q; got paths %v", wantSide, filePaths(files))
|
||||
}
|
||||
if _, ok := paths[wantTarget]; !ok {
|
||||
t.Errorf("missing target unit %q; got paths %v", wantTarget, filePaths(files))
|
||||
}
|
||||
if _, ok := paths[wantTarget]; !ok {
|
||||
t.Errorf("missing target unit %q; got paths %v", wantTarget, filePaths(files))
|
||||
}
|
||||
// Verify PartOf relations and ExecStart on per-task units.
|
||||
app := paths[wantApp]
|
||||
if !strings.Contains(app.Content, "PartOf=orca-v1-alloc-web.target") {
|
||||
t.Errorf("app unit missing PartOf=orca-v1-alloc-web.target\n%s", app.Content)
|
||||
}
|
||||
if !strings.Contains(app.Content, "ExecStart=/usr/bin/httpd -f") {
|
||||
t.Errorf("app unit missing ExecStart=/usr/bin/httpd -f\n%s", app.Content)
|
||||
}
|
||||
if !strings.Contains(app.Content, "WantedBy=multi-user.target") {
|
||||
t.Errorf("app unit missing WantedBy=multi-user.target\n%s", app.Content)
|
||||
}
|
||||
side := paths[wantSide]
|
||||
if !strings.Contains(side.Content, "PartOf=orca-v1-alloc-web.target") {
|
||||
t.Errorf("sidecar unit missing PartOf=orca-v1-alloc-web.target\n%s", side.Content)
|
||||
}
|
||||
if !strings.Contains(side.Content, "ExecStart=/bin/wasm-runner sidecar.wasm") {
|
||||
t.Errorf("sidecar unit missing ExecStart\n%s", side.Content)
|
||||
}
|
||||
// Verify the target unit Wants= both per-task units.
|
||||
target := paths[wantTarget]
|
||||
if !strings.Contains(target.Content, "Wants=orca-v1-alloc-web-app.service") {
|
||||
t.Errorf("target missing Wants=...app.service\n%s", target.Content)
|
||||
}
|
||||
if !strings.Contains(target.Content, "Wants=orca-v1-alloc-web-sidecar.service") {
|
||||
t.Errorf("target missing Wants=...sidecar.service\n%s", target.Content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_TaskGroupInheritsTopLevelRuntime(t *testing.T) {
|
||||
// P06: a task that omits its own runtime inherits the top-level
|
||||
// spec.Runtime as the per-group default. The per-task unit's
|
||||
// ExecStart must come from the top-level runtime command when
|
||||
// the task has no own command and no own runtime.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/default"},
|
||||
Tasks: []jobspec.TaskGroupTask{
|
||||
{Name: "app"},
|
||||
{Name: "sidecar", Command: "/bin/override"},
|
||||
},
|
||||
}
|
||||
files, err := SystemdEmitter{}.Render(spec, &Node{})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if len(files) != 3 {
|
||||
t.Fatalf("got %d files, want 3", len(files))
|
||||
}
|
||||
appContent := findUnitContent(files, "/etc/systemd/system/orca-v1-alloc-web-app.service")
|
||||
if appContent == "" {
|
||||
t.Fatalf("missing app unit; paths %v", filePaths(files))
|
||||
}
|
||||
if !strings.Contains(appContent, "ExecStart=/bin/default") {
|
||||
t.Errorf("app unit should inherit top-level command /bin/default\n%s", appContent)
|
||||
}
|
||||
sideContent := findUnitContent(files, "/etc/systemd/system/orca-v1-alloc-web-sidecar.service")
|
||||
if sideContent == "" {
|
||||
t.Fatalf("missing sidecar unit; paths %v", filePaths(files))
|
||||
}
|
||||
if !strings.Contains(sideContent, "ExecStart=/bin/override") {
|
||||
t.Errorf("sidecar unit should use its own command /bin/override\n%s", sideContent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_TaskGroupNoCommandError(t *testing.T) {
|
||||
// P06: a task with no resolvable command (no task.Command, no
|
||||
// task.Runtime, no top-level Runtime) is an error.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Tasks: []jobspec.TaskGroupTask{{Name: "app"}},
|
||||
}
|
||||
_, err := SystemdEmitter{}.Render(spec, &Node{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for task with no runtime, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "no runtime") {
|
||||
t.Errorf("error = %q, want 'no runtime'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_TaskGroupEmptyCommandError(t *testing.T) {
|
||||
// P06: a task whose resolved runtime command is empty/whitespace
|
||||
// is an error (mirrors the single-process rule).
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: " "},
|
||||
Tasks: []jobspec.TaskGroupTask{{Name: "app"}},
|
||||
}
|
||||
_, err := SystemdEmitter{}.Render(spec, &Node{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty command, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "command is empty") {
|
||||
t.Errorf("error = %q, want 'command is empty'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdEmitter_NoTasksBackwardCompat(t *testing.T) {
|
||||
// Backward compat: a spec with no Tasks renders exactly one unit
|
||||
// (the historical single-process shape).
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "backup",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/rsync"},
|
||||
}
|
||||
files, err := SystemdEmitter{}.Render(spec, &Node{})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if len(files) != 1 {
|
||||
t.Fatalf("got %d files, want 1 (backward compat)", len(files))
|
||||
}
|
||||
if files[0].Path != "/etc/systemd/system/orca-v1-backup.service" {
|
||||
t.Errorf("Path = %q, want /etc/systemd/system/orca-v1-backup.service", files[0].Path)
|
||||
}
|
||||
}
|
||||
|
||||
func filePaths(files []File) []string {
|
||||
out := make([]string, len(files))
|
||||
for i, f := range files {
|
||||
out[i] = f.Path
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func findUnitContent(files []File, path string) string {
|
||||
for _, f := range files {
|
||||
if f.Path == path {
|
||||
return f.Content
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
package emitter
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
// TraefikEmitter is the Layer-4 emitter for the Traefik dynamic-config
|
||||
// file (REQ-077). It renders /etc/traefik/dynamic/orca-<spec.Name>.yaml
|
||||
// — a single Traefik dynamic-config file describing the routers,
|
||||
// services (servers = the R-007 socket paths), TLS config pointing at
|
||||
// the step-ca root CA, and the service health check.
|
||||
//
|
||||
// Registered on the emitter.Registry under the service-kind keys:
|
||||
//
|
||||
// - service:process
|
||||
// - service:podman
|
||||
// - service:wasm
|
||||
//
|
||||
// RegisterTraefik wires all three; callers can also call Register
|
||||
// directly with TraefikEmitter{} for a single runtime.
|
||||
//
|
||||
// Atomic reload (gate C-10): the Traefik dynamic-config file is written
|
||||
// atomically via the SSH-push transport (sshpush.WriteFileIdempotent
|
||||
// performs temp-file + fsync + rename, and WriteTraefikDynamic wraps
|
||||
// it with an explicit tmp+mv so fsnotify sees a single rename event).
|
||||
// Traefik watches the dynamic dir with fsnotify; the rename triggers a
|
||||
// reload. On a malformed config Traefik logs an error and holds the
|
||||
// last-good config (documented Traefik behavior; the C-10 test
|
||||
// verifies the tmp+rename sequence so a half-written file is never
|
||||
// observed by Traefik). Drain is rendered by setting the backend
|
||||
// server's weight to 0 (or removing it) — see RenderDrain.
|
||||
//
|
||||
// The orca-v1- prefix is NOT applied to Traefik dynamic-config paths
|
||||
// (the prefix is only for systemd unit names; the Traefik file is named
|
||||
// orca-<spec.Name>.yaml and is the single source of truth for the
|
||||
// service route — there is no dual-write window for Traefik configs).
|
||||
type TraefikEmitter struct{}
|
||||
|
||||
// traefikDynamicDir is the canonical Traefik dynamic-config directory
|
||||
// (R-006). The emitter writes one file per service at
|
||||
// /etc/traefik/dynamic/orca-<spec.Name>.yaml.
|
||||
const traefikDynamicDir = "/etc/traefik/dynamic"
|
||||
|
||||
// traefikRouterTLSCertResolver is the Traefik cert-resolver name that
|
||||
// the orca step-ca integration configures on the Traefik static config
|
||||
// (P10 / v0.10 wires the step-ca root into this resolver). The
|
||||
// dynamic-config file references it by name.
|
||||
const traefikRouterTLSCertResolver = "orca"
|
||||
|
||||
// defaultTrustDomain is the SPIFFE trust domain used in the rendered
|
||||
// TLS stanza when the spec does not carry an explicit trust domain.
|
||||
// The step-ca provisioner (P10) overrides this at render time via the
|
||||
// node argument; for P02 the emitter renders the placeholder.
|
||||
const defaultTrustDomain = "cluster.orca.local"
|
||||
|
||||
// Render renders the Traefik dynamic-config YAML for a Service
|
||||
// workload. The output is a single File whose Path is
|
||||
// /etc/traefik/dynamic/orca-<spec.Name>.yaml, Content is the rendered
|
||||
// YAML, and Mode is 0644.
|
||||
//
|
||||
// Returns an error if the spec is nil, the name is empty, the spec has
|
||||
// no ports (a Service with no ports has no backends to route to), or a
|
||||
// service.bind value (when present) is not a valid IP address (R-007).
|
||||
func (TraefikEmitter) Render(spec *jobspec.WorkloadSpec, node *Node) ([]File, error) {
|
||||
if spec == nil {
|
||||
return nil, errors.New("emitter/traefik: spec is nil")
|
||||
}
|
||||
if strings.TrimSpace(spec.Name) == "" {
|
||||
return nil, errors.New("emitter/traefik: spec name is empty")
|
||||
}
|
||||
if len(spec.Ports) == 0 {
|
||||
return nil, errors.New("emitter/traefik: service has no ports (no backends to route to)")
|
||||
}
|
||||
if spec.Service != nil {
|
||||
if b := strings.TrimSpace(spec.Service.Bind); b != "" && net.ParseIP(b) == nil {
|
||||
return nil, fmt.Errorf("emitter/traefik: service.bind %q is not a valid IP (R-007)", b)
|
||||
}
|
||||
}
|
||||
content, err := renderTraefikYAML(spec, node)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
path := fmt.Sprintf("%s/orca-%s.yaml", traefikDynamicDir, spec.Name)
|
||||
return []File{{Path: path, Content: content, Mode: "0644"}}, nil
|
||||
}
|
||||
|
||||
// RenderDrain renders a Traefik dynamic-config that drains the service
|
||||
// by setting every backend server's weight to 0 (I-B-005 drain). The
|
||||
// path matches the live config so the atomic rename overwrites the
|
||||
// routing config with the drained config (Traefik reloads and stops
|
||||
// sending traffic). The caller writes the result via
|
||||
// WriteTraefikDynamic for the C-10 atomicity protocol.
|
||||
func (e TraefikEmitter) RenderDrain(spec *jobspec.WorkloadSpec, node *Node) ([]File, error) {
|
||||
if spec == nil {
|
||||
return nil, errors.New("emitter/traefik: spec is nil")
|
||||
}
|
||||
if strings.TrimSpace(spec.Name) == "" {
|
||||
return nil, errors.New("emitter/traefik: spec name is empty")
|
||||
}
|
||||
if len(spec.Ports) == 0 {
|
||||
return nil, errors.New("emitter/traefik: service has no ports (no backends to drain)")
|
||||
}
|
||||
content, err := renderTraefikYAMLDrain(spec, node)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
path := fmt.Sprintf("%s/orca-%s.yaml", traefikDynamicDir, spec.Name)
|
||||
return []File{{Path: path, Content: content, Mode: "0644"}}, nil
|
||||
}
|
||||
|
||||
// RegisterTraefik registers the TraefikEmitter on the given Registry
|
||||
// under the three service-kind runtime keys (service:process,
|
||||
// service:podman, service:wasm). The emitter is the same instance for
|
||||
// all three runtimes — the rendered Traefik config is runtime-agnostic
|
||||
// (the backend server URL is the R-007 socket path, which the runtime
|
||||
// layer binds regardless of process/wasm/podman).
|
||||
func RegisterTraefik(reg *Registry) {
|
||||
e := TraefikEmitter{}
|
||||
reg.Register("service:process", e)
|
||||
reg.Register("service:podman", e)
|
||||
reg.Register("service:wasm", e)
|
||||
}
|
||||
|
||||
// renderTraefikYAML renders the Traefik dynamic-config YAML for the
|
||||
// given spec + node. The shape (verified by the Traefik docs) is:
|
||||
//
|
||||
// http:
|
||||
// routers:
|
||||
// orca-<name>:
|
||||
// rule: PathPrefix("/<name>")
|
||||
// service: orca-<name>
|
||||
// tls:
|
||||
// certResolver: orca
|
||||
// domains:
|
||||
// - main: "<trust-domain>"
|
||||
// services:
|
||||
// orca-<name>:
|
||||
// loadBalancer:
|
||||
// servers:
|
||||
// - url: "unix:///run/orca/alloc-<allocID>/port-<portName>.sock"
|
||||
// healthCheck:
|
||||
// path: /healthz
|
||||
// interval: <interval>
|
||||
// timeout: <timeout>
|
||||
//
|
||||
// The alloc-id placeholder is "<allocID>" pending the P08 socket
|
||||
// layer; Traefik will reject the URL until a real alloc-id is
|
||||
// substituted. For P02 the emitter renders the placeholder so the
|
||||
// C-10 atomicity protocol is testable end-to-end; the socket layer
|
||||
// (P08) replaces the placeholder with the live alloc-id.
|
||||
func renderTraefikYAML(spec *jobspec.WorkloadSpec, node *Node) (string, error) {
|
||||
return renderTraefikYAMLWeighted(spec, node, false)
|
||||
}
|
||||
|
||||
// renderTraefikYAMLDrain renders the drained Traefik dynamic-config
|
||||
// (every backend server has weight: 0). The shape mirrors the live
|
||||
// config so the rename overwrites the live route with the drain.
|
||||
func renderTraefikYAMLDrain(spec *jobspec.WorkloadSpec, node *Node) (string, error) {
|
||||
return renderTraefikYAMLWeighted(spec, node, true)
|
||||
}
|
||||
|
||||
// renderTraefikYAMLWeighted renders the Traefik dynamic-config YAML.
|
||||
// When drain is true, every server entry is emitted with `weight: 0`
|
||||
// (I-B-005). When drain is false, no weight is emitted (Traefik
|
||||
// defaults to 1 — equal weighting across servers).
|
||||
func renderTraefikYAMLWeighted(spec *jobspec.WorkloadSpec, node *Node, drain bool) (string, error) {
|
||||
var b strings.Builder
|
||||
routerName := "orca-" + spec.Name
|
||||
serviceName := "orca-" + spec.Name
|
||||
rule := fmt.Sprintf("PathPrefix(\"/%s\")", spec.Name)
|
||||
trustDomain := defaultTrustDomain
|
||||
|
||||
b.WriteString("http:\n")
|
||||
b.WriteString(" routers:\n")
|
||||
b.WriteString(fmt.Sprintf(" %s:\n", routerName))
|
||||
b.WriteString(fmt.Sprintf(" rule: %s\n", rule))
|
||||
b.WriteString(fmt.Sprintf(" service: %s\n", serviceName))
|
||||
b.WriteString(" tls:\n")
|
||||
b.WriteString(fmt.Sprintf(" certResolver: %s\n", traefikRouterTLSCertResolver))
|
||||
b.WriteString(" domains:\n")
|
||||
b.WriteString(fmt.Sprintf(" - main: %q\n", trustDomain))
|
||||
b.WriteString(" services:\n")
|
||||
b.WriteString(fmt.Sprintf(" %s:\n", serviceName))
|
||||
b.WriteString(" loadBalancer:\n")
|
||||
b.WriteString(" servers:\n")
|
||||
allocID := allocIDFor(node)
|
||||
for _, p := range spec.Ports {
|
||||
sock := fmt.Sprintf("unix:///run/orca/alloc-%s/port-%s.sock", allocID, p.Name)
|
||||
b.WriteString(" - url: ")
|
||||
b.WriteString(fmt.Sprintf("%q\n", sock))
|
||||
if drain {
|
||||
b.WriteString(" weight: 0\n")
|
||||
}
|
||||
}
|
||||
if spec.Health != nil {
|
||||
b.WriteString(" healthCheck:\n")
|
||||
path := "/healthz"
|
||||
b.WriteString(fmt.Sprintf(" path: %s\n", path))
|
||||
if spec.Health.Interval != "" {
|
||||
b.WriteString(fmt.Sprintf(" interval: %s\n", spec.Health.Interval))
|
||||
}
|
||||
if spec.Health.Timeout != "" {
|
||||
b.WriteString(fmt.Sprintf(" timeout: %s\n", spec.Health.Timeout))
|
||||
}
|
||||
}
|
||||
return b.String(), nil
|
||||
}
|
||||
|
||||
// allocIDFor returns the alloc-id placeholder for the node. P08 will
|
||||
// substitute the live alloc-id from the socket layer; for P02 we use a
|
||||
// deterministic placeholder derived from the node hostname so the
|
||||
// rendered config is stable across re-renders (the C-10 idempotency
|
||||
// check depends on a stable hash). When the node is nil or has no
|
||||
// hostname, the literal placeholder "<allocID>" is emitted.
|
||||
func allocIDFor(node *Node) string {
|
||||
if node == nil || strings.TrimSpace(node.Hostname) == "" {
|
||||
return "<allocID>"
|
||||
}
|
||||
return node.Hostname
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package emitter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// AtomicWriter is the SSH-push transport surface that
|
||||
// WriteTraefikDynamic uses to write the Traefik dynamic-config file
|
||||
// atomically. It is the subset of *sshpush.Transport that the
|
||||
// atomicity protocol depends on. Tests substitute a mock to assert
|
||||
// the tmp+rename sequence (gate C-10) without a real SSH server.
|
||||
//
|
||||
// *sshpush.Transport satisfies this interface (the compile-time
|
||||
// assertion lives in internal/sshpush to avoid an import cycle — the
|
||||
// sshpush package imports emitter for fan-out, so this package cannot
|
||||
// import sshpush).
|
||||
type AtomicWriter interface {
|
||||
// WriteFileIdempotent writes content to peer:path atomically with
|
||||
// mode, returning written=true if the file was actually written
|
||||
// (content hash differed). Used by WriteTraefikDynamic to write
|
||||
// the .tmp sibling.
|
||||
WriteFileIdempotent(ctx context.Context, peer string, path string, content []byte, mode os.FileMode) (bool, error)
|
||||
// Exec runs a command on peer and returns its combined output.
|
||||
// Used by WriteTraefikDynamic to perform the atomic `mv -f
|
||||
// path.tmp path`.
|
||||
Exec(ctx context.Context, peer string, cmd string) ([]byte, error)
|
||||
}
|
||||
|
||||
// WriteTraefikDynamic writes a Traefik dynamic-config file atomically
|
||||
// (gate C-10: tmpfile + fsync + rename). The protocol is:
|
||||
//
|
||||
// 1. Write content to <path>.tmp via WriteFileIdempotent. The
|
||||
// underlying sshpush transport writes the tmp file in the same
|
||||
// directory as the target with mode-appended naming, fsyncs, and
|
||||
// renames — but we add an extra hop here so the *Traefik* file is
|
||||
// only ever observed at its final path after a single atomic
|
||||
// rename event that Traefik's fsnotify watcher sees.
|
||||
// 2. `mv -f <path>.tmp <path>` on the peer (atomic rename on POSIX).
|
||||
// Traefik's fsnotify watcher picks up the rename → reload.
|
||||
//
|
||||
// On a malformed config Traefik logs an error and holds the
|
||||
// last-good config (documented Traefik behavior; the C-10 test
|
||||
// verifies the tmp+rename sequence so a half-written file is never
|
||||
// observed by Traefik — the only window where Traefik can read the
|
||||
// file is after the rename, which is atomic on POSIX).
|
||||
//
|
||||
// The mode is 0644 (Traefik reads the dynamic dir as root; the lead
|
||||
// applier chmods after the rename).
|
||||
func WriteTraefikDynamic(ctx context.Context, t AtomicWriter, peer string, path string, content []byte) error {
|
||||
if t == nil {
|
||||
return fmt.Errorf("traefik: atomic writer is nil")
|
||||
}
|
||||
if path == "" {
|
||||
return fmt.Errorf("traefik: path is empty")
|
||||
}
|
||||
tmpPath := path + ".tmp"
|
||||
if _, err := t.WriteFileIdempotent(ctx, peer, tmpPath, content, 0o644); err != nil {
|
||||
return fmt.Errorf("traefik: write tmp %s: %w", tmpPath, err)
|
||||
}
|
||||
// Atomic rename on POSIX. `mv -f` overwrites an existing target
|
||||
// without prompting. The rename is atomic; Traefik's fsnotify
|
||||
// watcher observes a single IN_MOVED_TO event.
|
||||
renameCmd := fmt.Sprintf("mv -f %s %s", shellQuoteLocal(tmpPath), shellQuoteLocal(path))
|
||||
if _, err := t.Exec(ctx, peer, renameCmd); err != nil {
|
||||
return fmt.Errorf("traefik: rename %s -> %s: %w", tmpPath, path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// shellQuoteLocal single-quotes a path for safe shell interpolation on
|
||||
// the peer. It escapes embedded single-quotes via the standard '\”
|
||||
// idiom (close the single-quoted string, escape the literal single
|
||||
// quote, reopen the single-quoted string). This is a local
|
||||
// re-implementation (the sshpush package has its own) so the emitter
|
||||
// layer does not depend on the transport package's private helpers —
|
||||
// the AtomicWriter interface keeps the boundary clean for testing.
|
||||
func shellQuoteLocal(s string) string {
|
||||
var b []byte
|
||||
b = append(b, '\'')
|
||||
for i := 0; i < len(s); i++ {
|
||||
c := s[i]
|
||||
if c == '\'' {
|
||||
// close quote, escape the literal single-quote, reopen.
|
||||
b = append(b, '\'', '\\', '\'', '\'')
|
||||
continue
|
||||
}
|
||||
b = append(b, c)
|
||||
}
|
||||
b = append(b, '\'')
|
||||
return string(b)
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package emitter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// mockAtomicWriter is a test-only AtomicWriter that records calls so
|
||||
// the C-10 atomicity protocol (tmp + rename) can be asserted.
|
||||
type mockAtomicWriter struct {
|
||||
written []writeCall
|
||||
execed []execCall
|
||||
writeErr error
|
||||
writeWrote bool
|
||||
execErr error
|
||||
}
|
||||
|
||||
type writeCall struct {
|
||||
peer string
|
||||
path string
|
||||
mode os.FileMode
|
||||
bytes []byte
|
||||
}
|
||||
|
||||
type execCall struct {
|
||||
peer string
|
||||
cmd string
|
||||
}
|
||||
|
||||
func (m *mockAtomicWriter) WriteFileIdempotent(ctx context.Context, peer string, path string, content []byte, mode os.FileMode) (bool, error) {
|
||||
m.written = append(m.written, writeCall{peer: peer, path: path, mode: mode, bytes: append([]byte(nil), content...)})
|
||||
if m.writeErr != nil {
|
||||
return false, m.writeErr
|
||||
}
|
||||
return m.writeWrote, nil
|
||||
}
|
||||
|
||||
func (m *mockAtomicWriter) Exec(ctx context.Context, peer string, cmd string) ([]byte, error) {
|
||||
m.execed = append(m.execed, execCall{peer: peer, cmd: cmd})
|
||||
if m.execErr != nil {
|
||||
return nil, m.execErr
|
||||
}
|
||||
return []byte("ok"), nil
|
||||
}
|
||||
|
||||
func TestWriteTraefikDynamic_TmpThenRename(t *testing.T) {
|
||||
// Gate C-10: the Traefik dynamic-config write must be a tmp +
|
||||
// rename sequence so Traefik's fsnotify watcher never observes a
|
||||
// half-written file.
|
||||
mock := &mockAtomicWriter{writeWrote: true}
|
||||
path := "/etc/traefik/dynamic/orca-web.yaml"
|
||||
peer := "node-1:22"
|
||||
content := []byte("http:\n routers: {}\n")
|
||||
|
||||
if err := WriteTraefikDynamic(context.Background(), mock, peer, path, content); err != nil {
|
||||
t.Fatalf("WriteTraefikDynamic: %v", err)
|
||||
}
|
||||
|
||||
if len(mock.written) != 1 {
|
||||
t.Fatalf("WriteFileIdempotent calls = %d, want 1", len(mock.written))
|
||||
}
|
||||
w := mock.written[0]
|
||||
if w.peer != peer {
|
||||
t.Errorf("write peer = %q, want %q", w.peer, peer)
|
||||
}
|
||||
// The tmp path is the target path + ".tmp".
|
||||
if w.path != path+".tmp" {
|
||||
t.Errorf("write path = %q, want %q (.tmp suffix is the C-10 atomicity protocol)", w.path, path+".tmp")
|
||||
}
|
||||
if string(w.bytes) != string(content) {
|
||||
t.Errorf("write content = %q, want %q", string(w.bytes), string(content))
|
||||
}
|
||||
if w.mode != 0o644 {
|
||||
t.Errorf("write mode = %o, want 0644", w.mode)
|
||||
}
|
||||
|
||||
if len(mock.execed) != 1 {
|
||||
t.Fatalf("Exec calls = %d, want 1 (the rename)", len(mock.execed))
|
||||
}
|
||||
e := mock.execed[0]
|
||||
if e.peer != peer {
|
||||
t.Errorf("exec peer = %q, want %q", e.peer, peer)
|
||||
}
|
||||
// The rename command must `mv -f` the .tmp file to the final path.
|
||||
if !strings.Contains(e.cmd, "mv -f") {
|
||||
t.Errorf("exec cmd = %q, want it to contain 'mv -f' (atomic rename)", e.cmd)
|
||||
}
|
||||
if !strings.Contains(e.cmd, path+".tmp") {
|
||||
t.Errorf("exec cmd = %q, want it to contain the .tmp path as source", e.cmd)
|
||||
}
|
||||
if !strings.Contains(e.cmd, path) {
|
||||
t.Errorf("exec cmd = %q, want it to contain the final path as destination", e.cmd)
|
||||
}
|
||||
// Sanity: the source must come before the destination in the
|
||||
// mv command.
|
||||
srcIdx := strings.Index(e.cmd, path+".tmp")
|
||||
dstIdx := strings.Index(e.cmd, "'"+path+"'")
|
||||
if srcIdx < 0 || dstIdx < 0 || srcIdx > dstIdx {
|
||||
t.Errorf("exec cmd %q: source .tmp must come before destination %s", e.cmd, path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteTraefikDynamic_WriteTmpError(t *testing.T) {
|
||||
mock := &mockAtomicWriter{writeErr: errors.New("disk full")}
|
||||
err := WriteTraefikDynamic(context.Background(), mock, "p", "/etc/traefik/dynamic/orca-x.yaml", []byte("x"))
|
||||
if err == nil {
|
||||
t.Fatal("expected error from WriteFileIdempotent, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "write tmp") {
|
||||
t.Errorf("error = %q, want 'write tmp'", err.Error())
|
||||
}
|
||||
if !strings.Contains(err.Error(), "disk full") {
|
||||
t.Errorf("error = %q, want underlying 'disk full'", err.Error())
|
||||
}
|
||||
if len(mock.execed) != 0 {
|
||||
t.Errorf("on tmp write failure, no rename should happen; execed = %v", mock.execed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteTraefikDynamic_RenameError(t *testing.T) {
|
||||
mock := &mockAtomicWriter{writeWrote: true, execErr: errors.New("permission denied")}
|
||||
err := WriteTraefikDynamic(context.Background(), mock, "p", "/etc/traefik/dynamic/orca-x.yaml", []byte("x"))
|
||||
if err == nil {
|
||||
t.Fatal("expected error from rename, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "rename") {
|
||||
t.Errorf("error = %q, want 'rename'", err.Error())
|
||||
}
|
||||
if !strings.Contains(err.Error(), "permission denied") {
|
||||
t.Errorf("error = %q, want underlying 'permission denied'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteTraefikDynamic_NilWriter(t *testing.T) {
|
||||
err := WriteTraefikDynamic(context.Background(), nil, "p", "/x", []byte("x"))
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nil writer")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "nil") {
|
||||
t.Errorf("error = %q, want 'nil'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteTraefikDynamic_EmptyPath(t *testing.T) {
|
||||
mock := &mockAtomicWriter{writeWrote: true}
|
||||
err := WriteTraefikDynamic(context.Background(), mock, "p", "", []byte("x"))
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty path")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "path is empty") {
|
||||
t.Errorf("error = %q, want 'path is empty'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteTraefikDynamic_SkipWhenContentMatches(t *testing.T) {
|
||||
// When the .tmp file already matches (writeWrote=false), the
|
||||
// protocol still proceeds with the rename — the idempotency
|
||||
// check is per-file, not per-protocol. The rename still happens
|
||||
// so the final path reflects the (unchanged) content.
|
||||
mock := &mockAtomicWriter{writeWrote: false}
|
||||
err := WriteTraefikDynamic(context.Background(), mock, "p", "/etc/traefik/dynamic/orca-x.yaml", []byte("x"))
|
||||
if err != nil {
|
||||
t.Fatalf("WriteTraefikDynamic: %v", err)
|
||||
}
|
||||
if len(mock.execed) != 1 {
|
||||
t.Errorf("rename should still happen on idempotent skip; execed = %v", mock.execed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShellQuoteLocal(t *testing.T) {
|
||||
cases := []struct {
|
||||
in, want string
|
||||
}{
|
||||
{"/etc/traefik/dynamic/orca-web.yaml", "'/etc/traefik/dynamic/orca-web.yaml'"},
|
||||
{"", "''"},
|
||||
{"/path with space/x", "'/path with space/x'"},
|
||||
{"a'b", "'a'\\''b'"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.in, func(t *testing.T) {
|
||||
got := shellQuoteLocal(tc.in)
|
||||
if got != tc.want {
|
||||
t.Errorf("shellQuoteLocal(%q) = %q, want %q", tc.in, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,353 @@
|
||||
package emitter
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
func TestTraefikEmitter_RenderBasic(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http", Interval: "5s", Timeout: "1s"},
|
||||
}
|
||||
node := &Node{Hostname: "node-1", Runtime: []string{"process"}}
|
||||
files, err := TraefikEmitter{}.Render(spec, node)
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if len(files) != 1 {
|
||||
t.Fatalf("got %d files, want 1", len(files))
|
||||
}
|
||||
f := files[0]
|
||||
wantPath := "/etc/traefik/dynamic/orca-web.yaml"
|
||||
if f.Path != wantPath {
|
||||
t.Errorf("Path = %q, want %q", f.Path, wantPath)
|
||||
}
|
||||
if f.Mode != "0644" {
|
||||
t.Errorf("Mode = %q, want 0644", f.Mode)
|
||||
}
|
||||
c := f.Content
|
||||
if !strings.Contains(c, "http:") {
|
||||
t.Errorf("content missing 'http:'\n%s", c)
|
||||
}
|
||||
if !strings.Contains(c, "routers:") {
|
||||
t.Errorf("content missing 'routers:'\n%s", c)
|
||||
}
|
||||
if !strings.Contains(c, "orca-web:") {
|
||||
t.Errorf("content missing 'orca-web:' router/service key\n%s", c)
|
||||
}
|
||||
if !strings.Contains(c, `rule: PathPrefix("/web")`) {
|
||||
t.Errorf("content missing PathPrefix rule\n%s", c)
|
||||
}
|
||||
if !strings.Contains(c, "services:") {
|
||||
t.Errorf("content missing 'services:'\n%s", c)
|
||||
}
|
||||
if !strings.Contains(c, "loadBalancer:") {
|
||||
t.Errorf("content missing 'loadBalancer:'\n%s", c)
|
||||
}
|
||||
if !strings.Contains(c, "unix:///run/orca/alloc-node-1/port-http.sock") {
|
||||
t.Errorf("content missing socket server URL\n%s", c)
|
||||
}
|
||||
if !strings.Contains(c, "certResolver: orca") {
|
||||
t.Errorf("content missing 'certResolver: orca'\n%s", c)
|
||||
}
|
||||
if !strings.Contains(c, "domains:") {
|
||||
t.Errorf("content missing TLS domains\n%s", c)
|
||||
}
|
||||
if !strings.Contains(c, "healthCheck:") {
|
||||
t.Errorf("content missing 'healthCheck:'\n%s", c)
|
||||
}
|
||||
if !strings.Contains(c, "interval: 5s") {
|
||||
t.Errorf("content missing 'interval: 5s'\n%s", c)
|
||||
}
|
||||
if !strings.Contains(c, "timeout: 1s") {
|
||||
t.Errorf("content missing 'timeout: 1s'\n%s", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTraefikEmitter_RenderMultiplePorts(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "api",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Ports: []jobspec.PortSpec{
|
||||
{Name: "http", Port: 8080},
|
||||
{Name: "grpc", Port: 9090},
|
||||
},
|
||||
}
|
||||
node := &Node{Hostname: "n1"}
|
||||
files, err := TraefikEmitter{}.Render(spec, node)
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
c := files[0].Content
|
||||
if !strings.Contains(c, "port-http.sock") {
|
||||
t.Errorf("missing http socket: %s", c)
|
||||
}
|
||||
if !strings.Contains(c, "port-grpc.sock") {
|
||||
t.Errorf("missing grpc socket: %s", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTraefikEmitter_RenderDrain(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
node := &Node{Hostname: "n1"}
|
||||
files, err := TraefikEmitter{}.RenderDrain(spec, node)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderDrain: %v", err)
|
||||
}
|
||||
if len(files) != 1 {
|
||||
t.Fatalf("got %d files, want 1", len(files))
|
||||
}
|
||||
c := files[0].Content
|
||||
if !strings.Contains(c, "weight: 0") {
|
||||
t.Errorf("drain config missing 'weight: 0'\n%s", c)
|
||||
}
|
||||
if !strings.Contains(c, "unix:///run/orca/alloc-n1/port-http.sock") {
|
||||
t.Errorf("drain config missing socket URL\n%s", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTraefikEmitter_RenderLiveHasNoWeightZero(t *testing.T) {
|
||||
// Sanity: the live (non-drain) render must NOT emit `weight: 0`.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
node := &Node{Hostname: "n1"}
|
||||
files, err := TraefikEmitter{}.Render(spec, node)
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if strings.Contains(files[0].Content, "weight: 0") {
|
||||
t.Errorf("live config should not contain 'weight: 0'\n%s", files[0].Content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTraefikEmitter_RenderNoHealthOmitsHealthCheck(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
node := &Node{Hostname: "n1"}
|
||||
files, err := TraefikEmitter{}.Render(spec, node)
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if strings.Contains(files[0].Content, "healthCheck:") {
|
||||
t.Errorf("config without Health should omit 'healthCheck:'\n%s", files[0].Content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTraefikEmitter_NilSpec(t *testing.T) {
|
||||
_, err := TraefikEmitter{}.Render(nil, &Node{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nil spec")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTraefikEmitter_EmptyName(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: " ",
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
_, err := TraefikEmitter{}.Render(spec, &Node{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTraefikEmitter_NoPorts(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
}
|
||||
_, err := TraefikEmitter{}.Render(spec, &Node{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing ports")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "no ports") {
|
||||
t.Errorf("error = %q, want 'no ports'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTraefikEmitter_NoPortsDrain(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
}
|
||||
_, err := TraefikEmitter{}.RenderDrain(spec, &Node{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing ports on drain")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTraefikEmitter_InvalidBind(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
Service: &jobspec.ServiceBlock{Bind: "not-an-ip"},
|
||||
}
|
||||
_, err := TraefikEmitter{}.Render(spec, &Node{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid service.bind")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "valid IP") {
|
||||
t.Errorf("error = %q, want 'valid IP'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTraefikEmitter_ValidBindLoopback(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
Service: &jobspec.ServiceBlock{Bind: "127.0.0.1"},
|
||||
}
|
||||
_, err := TraefikEmitter{}.Render(spec, &Node{})
|
||||
if err != nil {
|
||||
t.Fatalf("127.0.0.1 should be accepted, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTraefikEmitter_NilNodeAllocPlaceholder(t *testing.T) {
|
||||
// With a nil node, the alloc-id placeholder is the literal
|
||||
// "<allocID>" sentinel so the rendered config is still valid YAML
|
||||
// (the P08 socket layer substitutes the real alloc-id).
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
files, err := TraefikEmitter{}.Render(spec, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if !strings.Contains(files[0].Content, "alloc-<allocID>") {
|
||||
t.Errorf("nil node should render alloc-<allocID> placeholder\n%s", files[0].Content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTraefikEmitter_EmptyHostnameAllocPlaceholder(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
files, err := TraefikEmitter{}.Render(spec, &Node{Hostname: " "})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if !strings.Contains(files[0].Content, "alloc-<allocID>") {
|
||||
t.Errorf("empty hostname should render alloc-<allocID> placeholder\n%s", files[0].Content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTraefikEmitter_PathNotOrcaV1Prefixed(t *testing.T) {
|
||||
// REQ-090: the orca-v1- prefix is only for systemd units; Traefik
|
||||
// dynamic-config paths are named orca-<spec.Name>.yaml (single
|
||||
// source of truth — no dual-write window for Traefik configs).
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
files, err := TraefikEmitter{}.Render(spec, &Node{Hostname: "n1"})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if strings.Contains(files[0].Path, "orca-v1-") {
|
||||
t.Errorf("Path %q should NOT contain the orca-v1- prefix (systemd-only)", files[0].Path)
|
||||
}
|
||||
if !strings.HasPrefix(files[0].Path, "/etc/traefik/dynamic/orca-") {
|
||||
t.Errorf("Path %q should start with /etc/traefik/dynamic/orca-", files[0].Path)
|
||||
}
|
||||
if !strings.HasSuffix(files[0].Path, ".yaml") {
|
||||
t.Errorf("Path %q should end with .yaml", files[0].Path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTraefikEmitter_RenderYAMLHasRoutersServicesTLS(t *testing.T) {
|
||||
// Aggregate structural assertion: the rendered YAML has the four
|
||||
// top-level Traefik concepts (routers, services, tls, servers).
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
}
|
||||
files, err := TraefikEmitter{}.Render(spec, &Node{Hostname: "n1"})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
c := files[0].Content
|
||||
for _, want := range []string{"routers:", "services:", "tls:", "servers:", "url:"} {
|
||||
if !strings.Contains(c, want) {
|
||||
t.Errorf("rendered YAML missing %q\n%s", want, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterTraefik_AllServiceRuntimes(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
RegisterTraefik(r)
|
||||
spec := func(runtime string) *jobspec.WorkloadSpec {
|
||||
return &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: runtime},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
}
|
||||
for _, runtime := range []string{"process", "podman", "wasm"} {
|
||||
t.Run(runtime, func(t *testing.T) {
|
||||
files, err := r.Render(spec(runtime), &Node{Hostname: "n1"})
|
||||
if err != nil {
|
||||
t.Fatalf("Render(service:%s): %v", runtime, err)
|
||||
}
|
||||
if len(files) != 1 {
|
||||
t.Fatalf("got %d files, want 1", len(files))
|
||||
}
|
||||
if !strings.Contains(files[0].Path, "/etc/traefik/dynamic/orca-web.yaml") {
|
||||
t.Errorf("Path = %q", files[0].Path)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterTraefik_OverwritesExisting(t *testing.T) {
|
||||
// RegisterTraefik should overwrite any prior registration (the
|
||||
// Registry documents last-wins).
|
||||
r := NewRegistry()
|
||||
r.Register("service:process", mockEmitter{files: []File{{Path: "/old"}}})
|
||||
RegisterTraefik(r)
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
files, err := r.Render(spec, &Node{Hostname: "n1"})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if files[0].Path == "/old" {
|
||||
t.Errorf("RegisterTraefik did not overwrite the prior registration")
|
||||
}
|
||||
}
|
||||
|
||||
// Compile-time assertion that TraefikEmitter implements Emitter.
|
||||
var _ Emitter = TraefikEmitter{}
|
||||
@@ -0,0 +1,237 @@
|
||||
package emitter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
// UpdatePlan is the computed update sequence for a Service (P03). It is
|
||||
// a PLAN, not an execution — the transactional execution lands in
|
||||
// v0.10-P10. Each step describes a discrete action the executor takes:
|
||||
// start a set of allocs (Action="start"), wait for them to become
|
||||
// healthy (WaitForHealthy=true), or cutover from old to new
|
||||
// (Action="cutover" for blue-green). The Allocs field carries
|
||||
// placeholder alloc names of the form "<spec.Name>-<index>" where
|
||||
// index is 1-based (the scheduler assigns the real alloc-id at submit
|
||||
// time; P03 uses spec.Name as a placeholder per the socket layer
|
||||
// contract — see SocketEmitter).
|
||||
type UpdatePlan struct {
|
||||
Steps []UpdateStep
|
||||
}
|
||||
|
||||
// UpdateStep is a single step in an UpdatePlan. Action is one of
|
||||
// "start", "wait", "cutover", "promote". Allocs is the list of
|
||||
// placeholder alloc names the step applies to. WaitForHealthy is true
|
||||
// when the executor must wait for the allocs in this step to pass
|
||||
// their health check before proceeding to the next step (driven by
|
||||
// min_healthy_time / healthy_deadline on the spec, which the executor
|
||||
// — not the plan — enforces).
|
||||
type UpdateStep struct {
|
||||
Action string
|
||||
Allocs []string
|
||||
WaitForHealthy bool
|
||||
}
|
||||
|
||||
// maxParallelFor returns the effective max_parallel for the spec,
|
||||
// defaulting to 1 when unset (0) and clamping to count (the validator
|
||||
// already rejects out-of-range values; this is a defensive clamp for
|
||||
// direct callers that bypass the validator).
|
||||
func maxParallelFor(spec *jobspec.WorkloadSpec) int {
|
||||
if spec.Update == nil {
|
||||
return 1
|
||||
}
|
||||
if spec.Update.MaxParallel < 1 {
|
||||
return 1
|
||||
}
|
||||
if spec.Count > 0 && spec.Update.MaxParallel > spec.Count {
|
||||
return spec.Count
|
||||
}
|
||||
return spec.Update.MaxParallel
|
||||
}
|
||||
|
||||
// allocName returns the placeholder alloc name for index i (1-based).
|
||||
// The real alloc-id is assigned by the scheduler at submit time; P03
|
||||
// uses spec.Name as the placeholder per the socket-layer contract.
|
||||
func allocName(spec *jobspec.WorkloadSpec, i int) string {
|
||||
return fmt.Sprintf("%s-%d", spec.Name, i)
|
||||
}
|
||||
|
||||
// allAllocs returns the placeholder alloc names for the full count of
|
||||
// the spec (1..count).
|
||||
func allAllocs(spec *jobspec.WorkloadSpec) []string {
|
||||
out := make([]string, 0, spec.Count)
|
||||
for i := 1; i <= spec.Count; i++ {
|
||||
out = append(out, allocName(spec, i))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// canaryCount returns the integer canary count for the spec. The
|
||||
// canary field accepts an integer count or a percentage ("<n>%"). For
|
||||
// a percentage, the count is ceil(count * n / 100) with a minimum of 1
|
||||
// when n > 0 (a 10% canary of a 3-replica service is 1 alloc, not 0).
|
||||
// When the canary field is empty, the default is 1 (a single canary
|
||||
// alloc — the smallest meaningful canary).
|
||||
func canaryCount(spec *jobspec.WorkloadSpec) int {
|
||||
if spec.Update == nil {
|
||||
return 1
|
||||
}
|
||||
c := strings.TrimSpace(spec.Update.Canary)
|
||||
if c == "" {
|
||||
return 1
|
||||
}
|
||||
if strings.HasSuffix(c, "%") {
|
||||
n, err := strconv.Atoi(strings.TrimSpace(strings.TrimSuffix(c, "%")))
|
||||
if err != nil || n <= 0 {
|
||||
return 1
|
||||
}
|
||||
allocs := spec.Count * n / 100
|
||||
if allocs < 1 {
|
||||
allocs = 1
|
||||
}
|
||||
return allocs
|
||||
}
|
||||
n, err := strconv.Atoi(c)
|
||||
if err != nil || n < 1 {
|
||||
return 1
|
||||
}
|
||||
if spec.Count > 0 && n > spec.Count {
|
||||
return spec.Count
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// RenderUpdatePlan computes the rolling/canary/blue-green update
|
||||
// sequence for a Service spec. Returns an *UpdatePlan describing the
|
||||
// steps; the actual transactional execution lands in v0.10-P10.
|
||||
//
|
||||
// The three strategies:
|
||||
//
|
||||
// - rolling: allocs are started in batches of max_parallel. Each
|
||||
// batch waits for healthy before the next batch starts. This is
|
||||
// the simplest strategy and the default for stateless services.
|
||||
//
|
||||
// - canary: a single canary alloc (or N per the canary field) is
|
||||
// started first and waits for healthy. After the canary is
|
||||
// healthy, the plan emits a "promote" step (manual or auto per
|
||||
// auto_promote); the remaining allocs are then started in
|
||||
// max_parallel batches.
|
||||
//
|
||||
// - blue-green: all new allocs are started in parallel (a single
|
||||
// "start" step with the full count). After they are healthy, a
|
||||
// "cutover" step swaps traffic from the old allocs to the new
|
||||
// ones. The old allocs are then stopped (the stop is implicit in
|
||||
// the cutover step for the plan; v0.10-P10 makes it explicit).
|
||||
//
|
||||
// Returns an error if the spec is nil, the update block is nil, or
|
||||
// the strategy is unknown (the validator should have caught these,
|
||||
// but RenderUpdatePlan is defensive — emitters are called from
|
||||
// render paths that may bypass the schema validator).
|
||||
func RenderUpdatePlan(spec *jobspec.WorkloadSpec) (*UpdatePlan, error) {
|
||||
if spec == nil {
|
||||
return nil, fmt.Errorf("emitter/update: spec is nil")
|
||||
}
|
||||
if spec.Update == nil {
|
||||
return nil, fmt.Errorf("emitter/update: update block is nil")
|
||||
}
|
||||
if spec.Count < 1 {
|
||||
return nil, fmt.Errorf("emitter/update: count must be ≥ 1, got %d", spec.Count)
|
||||
}
|
||||
switch spec.Update.Strategy {
|
||||
case "rolling":
|
||||
return renderRollingPlan(spec), nil
|
||||
case "canary":
|
||||
return renderCanaryPlan(spec), nil
|
||||
case "blue-green":
|
||||
return renderBlueGreenPlan(spec), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("emitter/update: unknown strategy %q (want rolling, canary, or blue-green)", spec.Update.Strategy)
|
||||
}
|
||||
}
|
||||
|
||||
// renderRollingPlan emits the rolling-update plan: allocs in batches
|
||||
// of max_parallel, each batch waiting for healthy before the next.
|
||||
func renderRollingPlan(spec *jobspec.WorkloadSpec) *UpdatePlan {
|
||||
plan := &UpdatePlan{}
|
||||
batch := maxParallelFor(spec)
|
||||
allocs := allAllocs(spec)
|
||||
for i := 0; i < len(allocs); i += batch {
|
||||
end := i + batch
|
||||
if end > len(allocs) {
|
||||
end = len(allocs)
|
||||
}
|
||||
plan.Steps = append(plan.Steps, UpdateStep{
|
||||
Action: "start",
|
||||
Allocs: allocs[i:end],
|
||||
WaitForHealthy: true,
|
||||
})
|
||||
}
|
||||
return plan
|
||||
}
|
||||
|
||||
// renderCanaryPlan emits the canary-update plan: a canary batch first
|
||||
// (size per the canary field, default 1), a "promote" step, then the
|
||||
// remaining allocs in max_parallel batches.
|
||||
func renderCanaryPlan(spec *jobspec.WorkloadSpec) *UpdatePlan {
|
||||
plan := &UpdatePlan{}
|
||||
allocs := allAllocs(spec)
|
||||
canary := canaryCount(spec)
|
||||
if canary > len(allocs) {
|
||||
canary = len(allocs)
|
||||
}
|
||||
if canary < 1 {
|
||||
canary = 1
|
||||
}
|
||||
// Step 1: start the canary alloc(s) and wait for healthy.
|
||||
plan.Steps = append(plan.Steps, UpdateStep{
|
||||
Action: "start",
|
||||
Allocs: allocs[:canary],
|
||||
WaitForHealthy: true,
|
||||
})
|
||||
// Step 2: promote (manual or auto per auto_promote).
|
||||
plan.Steps = append(plan.Steps, UpdateStep{
|
||||
Action: "promote",
|
||||
Allocs: allocs[:canary],
|
||||
})
|
||||
// Step 3+: remaining allocs in max_parallel batches.
|
||||
batch := maxParallelFor(spec)
|
||||
remaining := allocs[canary:]
|
||||
for i := 0; i < len(remaining); i += batch {
|
||||
end := i + batch
|
||||
if end > len(remaining) {
|
||||
end = len(remaining)
|
||||
}
|
||||
plan.Steps = append(plan.Steps, UpdateStep{
|
||||
Action: "start",
|
||||
Allocs: remaining[i:end],
|
||||
WaitForHealthy: true,
|
||||
})
|
||||
}
|
||||
return plan
|
||||
}
|
||||
|
||||
// renderBlueGreenPlan emits the blue-green update plan: all new allocs
|
||||
// start in parallel, wait for healthy, then cutover (swap traffic).
|
||||
func renderBlueGreenPlan(spec *jobspec.WorkloadSpec) *UpdatePlan {
|
||||
plan := &UpdatePlan{}
|
||||
allocs := allAllocs(spec)
|
||||
// Step 1: start ALL new allocs in parallel (blue-green does not
|
||||
// batch — the new fleet stands up alongside the old).
|
||||
plan.Steps = append(plan.Steps, UpdateStep{
|
||||
Action: "start",
|
||||
Allocs: allocs,
|
||||
WaitForHealthy: true,
|
||||
})
|
||||
// Step 2: cutover — swap traffic from old to new. The old allocs
|
||||
// are stopped implicitly as part of the cutover (v0.10-P10 makes
|
||||
// the stop explicit in the transactional plane).
|
||||
plan.Steps = append(plan.Steps, UpdateStep{
|
||||
Action: "cutover",
|
||||
Allocs: allocs,
|
||||
WaitForHealthy: false,
|
||||
})
|
||||
return plan
|
||||
}
|
||||
@@ -0,0 +1,396 @@
|
||||
package emitter
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
func TestRenderUpdatePlan_RollingBatches(t *testing.T) {
|
||||
// count=4, max_parallel=2 → 2 batches of 2.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "rolling",
|
||||
MaxParallel: 2,
|
||||
},
|
||||
}
|
||||
plan, err := RenderUpdatePlan(spec)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderUpdatePlan: %v", err)
|
||||
}
|
||||
if len(plan.Steps) != 2 {
|
||||
t.Fatalf("got %d steps, want 2", len(plan.Steps))
|
||||
}
|
||||
for i, s := range plan.Steps {
|
||||
if s.Action != "start" {
|
||||
t.Errorf("step %d action = %q, want start", i, s.Action)
|
||||
}
|
||||
if !s.WaitForHealthy {
|
||||
t.Errorf("step %d WaitForHealthy = false, want true", i)
|
||||
}
|
||||
if len(s.Allocs) != 2 {
|
||||
t.Errorf("step %d allocs = %d, want 2", i, len(s.Allocs))
|
||||
}
|
||||
}
|
||||
if plan.Steps[0].Allocs[0] != "web-1" || plan.Steps[0].Allocs[1] != "web-2" {
|
||||
t.Errorf("step 0 allocs = %v, want [web-1 web-2]", plan.Steps[0].Allocs)
|
||||
}
|
||||
if plan.Steps[1].Allocs[0] != "web-3" || plan.Steps[1].Allocs[1] != "web-4" {
|
||||
t.Errorf("step 1 allocs = %v, want [web-3 web-4]", plan.Steps[1].Allocs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderUpdatePlan_RollingUnevenBatches(t *testing.T) {
|
||||
// count=5, max_parallel=2 → 3 batches: 2, 2, 1.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 5,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "rolling",
|
||||
MaxParallel: 2,
|
||||
},
|
||||
}
|
||||
plan, err := RenderUpdatePlan(spec)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderUpdatePlan: %v", err)
|
||||
}
|
||||
if len(plan.Steps) != 3 {
|
||||
t.Fatalf("got %d steps, want 3", len(plan.Steps))
|
||||
}
|
||||
if len(plan.Steps[2].Allocs) != 1 {
|
||||
t.Errorf("step 2 allocs = %d, want 1 (remainder)", len(plan.Steps[2].Allocs))
|
||||
}
|
||||
if plan.Steps[2].Allocs[0] != "web-5" {
|
||||
t.Errorf("step 2 allocs = %v, want [web-5]", plan.Steps[2].Allocs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderUpdatePlan_RollingMaxParallelUnset(t *testing.T) {
|
||||
// max_parallel unset (0) → default 1 → 4 batches of 1.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "rolling",
|
||||
},
|
||||
}
|
||||
plan, err := RenderUpdatePlan(spec)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderUpdatePlan: %v", err)
|
||||
}
|
||||
if len(plan.Steps) != 4 {
|
||||
t.Fatalf("got %d steps, want 4 (one per alloc, batch=1)", len(plan.Steps))
|
||||
}
|
||||
for _, s := range plan.Steps {
|
||||
if len(s.Allocs) != 1 {
|
||||
t.Errorf("allocs = %d, want 1", len(s.Allocs))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderUpdatePlan_CanaryDefaultOne(t *testing.T) {
|
||||
// canary unset → default 1 canary alloc, then 3 in batches of 2.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
MaxParallel: 2,
|
||||
},
|
||||
}
|
||||
plan, err := RenderUpdatePlan(spec)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderUpdatePlan: %v", err)
|
||||
}
|
||||
// Step 0: canary (1 alloc). Step 1: promote. Steps 2..: remaining.
|
||||
if plan.Steps[0].Action != "start" || len(plan.Steps[0].Allocs) != 1 {
|
||||
t.Errorf("step 0 = %+v, want canary start with 1 alloc", plan.Steps[0])
|
||||
}
|
||||
if plan.Steps[0].Allocs[0] != "web-1" {
|
||||
t.Errorf("canary alloc = %q, want web-1", plan.Steps[0].Allocs[0])
|
||||
}
|
||||
if !plan.Steps[0].WaitForHealthy {
|
||||
t.Error("canary step should wait for healthy")
|
||||
}
|
||||
if plan.Steps[1].Action != "promote" {
|
||||
t.Errorf("step 1 action = %q, want promote", plan.Steps[1].Action)
|
||||
}
|
||||
// Remaining: web-2, web-3, web-4 in batches of 2 → [web-2,web-3], [web-4].
|
||||
if len(plan.Steps) != 4 {
|
||||
t.Fatalf("got %d steps, want 4 (canary + promote + 2 batches)", len(plan.Steps))
|
||||
}
|
||||
if len(plan.Steps[2].Allocs) != 2 || plan.Steps[2].Allocs[0] != "web-2" {
|
||||
t.Errorf("step 2 = %v, want [web-2 web-3]", plan.Steps[2].Allocs)
|
||||
}
|
||||
if len(plan.Steps[3].Allocs) != 1 || plan.Steps[3].Allocs[0] != "web-4" {
|
||||
t.Errorf("step 3 = %v, want [web-4]", plan.Steps[3].Allocs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderUpdatePlan_CanaryPercent(t *testing.T) {
|
||||
// count=10, canary=20% → 2 canary allocs, then 8 in batches of 3.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 10,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
MaxParallel: 3,
|
||||
Canary: "20%",
|
||||
},
|
||||
}
|
||||
plan, err := RenderUpdatePlan(spec)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderUpdatePlan: %v", err)
|
||||
}
|
||||
if len(plan.Steps[0].Allocs) != 2 {
|
||||
t.Errorf("canary step allocs = %d, want 2 (20%% of 10)", len(plan.Steps[0].Allocs))
|
||||
}
|
||||
// Remaining 8 in batches of 3 → ceil(8/3)=3 batches.
|
||||
// Steps: canary, promote, batch(3), batch(3), batch(2) = 5 steps.
|
||||
if len(plan.Steps) != 5 {
|
||||
t.Fatalf("got %d steps, want 5", len(plan.Steps))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderUpdatePlan_CanaryIntegerCount(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
Canary: "2",
|
||||
},
|
||||
}
|
||||
plan, err := RenderUpdatePlan(spec)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderUpdatePlan: %v", err)
|
||||
}
|
||||
if len(plan.Steps[0].Allocs) != 2 {
|
||||
t.Errorf("canary step allocs = %d, want 2", len(plan.Steps[0].Allocs))
|
||||
}
|
||||
if plan.Steps[0].Allocs[0] != "web-1" || plan.Steps[0].Allocs[1] != "web-2" {
|
||||
t.Errorf("canary allocs = %v, want [web-1 web-2]", plan.Steps[0].Allocs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderUpdatePlan_CanaryFullCount(t *testing.T) {
|
||||
// canary == count → no remaining allocs after canary.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 3,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
Canary: "3",
|
||||
},
|
||||
}
|
||||
plan, err := RenderUpdatePlan(spec)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderUpdatePlan: %v", err)
|
||||
}
|
||||
// Steps: canary start (3), promote. No remaining batches.
|
||||
if len(plan.Steps) != 2 {
|
||||
t.Fatalf("got %d steps, want 2 (canary + promote, no remainder)", len(plan.Steps))
|
||||
}
|
||||
if plan.Steps[1].Action != "promote" {
|
||||
t.Errorf("step 1 action = %q, want promote", plan.Steps[1].Action)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderUpdatePlan_BlueGreen(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "blue-green",
|
||||
},
|
||||
}
|
||||
plan, err := RenderUpdatePlan(spec)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderUpdatePlan: %v", err)
|
||||
}
|
||||
// Step 0: start all 4 in parallel. Step 1: cutover.
|
||||
if len(plan.Steps) != 2 {
|
||||
t.Fatalf("got %d steps, want 2", len(plan.Steps))
|
||||
}
|
||||
if plan.Steps[0].Action != "start" {
|
||||
t.Errorf("step 0 action = %q, want start", plan.Steps[0].Action)
|
||||
}
|
||||
if len(plan.Steps[0].Allocs) != 4 {
|
||||
t.Errorf("step 0 allocs = %d, want 4 (all new in parallel)", len(plan.Steps[0].Allocs))
|
||||
}
|
||||
if !plan.Steps[0].WaitForHealthy {
|
||||
t.Error("blue-green start step should wait for healthy")
|
||||
}
|
||||
if plan.Steps[1].Action != "cutover" {
|
||||
t.Errorf("step 1 action = %q, want cutover", plan.Steps[1].Action)
|
||||
}
|
||||
if plan.Steps[1].WaitForHealthy {
|
||||
t.Error("cutover step should NOT wait for healthy (already healthy)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderUpdatePlan_BlueGreenAllocs(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "api",
|
||||
Count: 3,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "blue-green",
|
||||
},
|
||||
}
|
||||
plan, err := RenderUpdatePlan(spec)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderUpdatePlan: %v", err)
|
||||
}
|
||||
want := []string{"api-1", "api-2", "api-3"}
|
||||
if len(plan.Steps[0].Allocs) != 3 {
|
||||
t.Errorf("allocs = %v, want %v", plan.Steps[0].Allocs, want)
|
||||
}
|
||||
for i, a := range want {
|
||||
if plan.Steps[0].Allocs[i] != a {
|
||||
t.Errorf("alloc[%d] = %q, want %q", i, plan.Steps[0].Allocs[i], a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderUpdatePlan_NilSpec(t *testing.T) {
|
||||
_, err := RenderUpdatePlan(nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nil spec")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "spec is nil") {
|
||||
t.Errorf("error = %q, want 'spec is nil'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderUpdatePlan_NilUpdate(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{Kind: "Service", Name: "web", Count: 1}
|
||||
_, err := RenderUpdatePlan(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nil update block")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "update block is nil") {
|
||||
t.Errorf("error = %q, want 'update block is nil'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderUpdatePlan_CountZero(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 0,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "rolling",
|
||||
},
|
||||
}
|
||||
_, err := RenderUpdatePlan(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for count 0")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "count must be") {
|
||||
t.Errorf("error = %q, want 'count must be'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderUpdatePlan_UnknownStrategy(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 2,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "recreate",
|
||||
},
|
||||
}
|
||||
_, err := RenderUpdatePlan(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for unknown strategy")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "unknown strategy") {
|
||||
t.Errorf("error = %q, want 'unknown strategy'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderUpdatePlan_AllStrategies(t *testing.T) {
|
||||
for _, strat := range []string{"rolling", "canary", "blue-green"} {
|
||||
t.Run(strat, func(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 3,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: strat,
|
||||
MaxParallel: 1,
|
||||
},
|
||||
}
|
||||
plan, err := RenderUpdatePlan(spec)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderUpdatePlan(%s): %v", strat, err)
|
||||
}
|
||||
if len(plan.Steps) == 0 {
|
||||
t.Errorf("strategy %s produced 0 steps", strat)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderUpdatePlan_PromoteStepCarriesCanaryAllocs(t *testing.T) {
|
||||
// The promote step lists the canary allocs so the executor knows
|
||||
// which allocs are being promoted from canary to stable.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
Canary: "2",
|
||||
},
|
||||
}
|
||||
plan, err := RenderUpdatePlan(spec)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderUpdatePlan: %v", err)
|
||||
}
|
||||
promote := plan.Steps[1]
|
||||
if promote.Action != "promote" {
|
||||
t.Fatalf("step 1 action = %q, want promote", promote.Action)
|
||||
}
|
||||
if len(promote.Allocs) != 2 {
|
||||
t.Errorf("promote allocs = %d, want 2 (the canary allocs)", len(promote.Allocs))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderUpdatePlan_MaxParallelClampedToCount(t *testing.T) {
|
||||
// max_parallel > count is clamped to count (defensive; validator
|
||||
// rejects this but the emitter is defensive against direct
|
||||
// callers).
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 2,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "rolling",
|
||||
MaxParallel: 99,
|
||||
},
|
||||
}
|
||||
plan, err := RenderUpdatePlan(spec)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderUpdatePlan: %v", err)
|
||||
}
|
||||
// Clamped to 2 → single batch of 2.
|
||||
if len(plan.Steps) != 1 {
|
||||
t.Errorf("got %d steps, want 1 (clamped)", len(plan.Steps))
|
||||
}
|
||||
if len(plan.Steps[0].Allocs) != 2 {
|
||||
t.Errorf("step 0 allocs = %d, want 2", len(plan.Steps[0].Allocs))
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,77 @@ type WorkloadSpec struct {
|
||||
Secrets []string
|
||||
Volumes []VolumeSpec
|
||||
Body string
|
||||
|
||||
// Kind-specific blocks consumed by the P0c schema validators
|
||||
// (internal/spec/schema). P02 populates Restart, Update, Service,
|
||||
// Health, Constraints, Affinity, Lifecycle from the Markdown
|
||||
// frontmatter (the rest are still populated by later phases).
|
||||
|
||||
// Restart is the restart policy block. Required for Service and
|
||||
// DaemonSet; optional for Job (defaults to never/on-failure).
|
||||
// P02 populates it from the `restart:` frontmatter block.
|
||||
Restart *RestartBlock
|
||||
|
||||
// Schedule is the schedule block. For Job it carries an optional
|
||||
// cron string; for DaemonSet it carries the placement mode
|
||||
// (every-node/matching/mandatory). Populated by P05 (scheduler
|
||||
// skeleton) and the DaemonSet phase.
|
||||
Schedule *ScheduleBlock
|
||||
|
||||
// Update is the rolling/canary update stanza. Required for
|
||||
// Service. P02 populates it from the `update:` frontmatter block;
|
||||
// the rolling/canary semantics land in P03.
|
||||
Update *UpdateBlock
|
||||
|
||||
// Service is the service block (Traefik route definition). For
|
||||
// Service kind it is implied; Job and DaemonSet do not carry a
|
||||
// Traefik route by default (D-175). P02 populates it from the
|
||||
// `service:` frontmatter block.
|
||||
Service *ServiceBlock
|
||||
|
||||
// Health is the health-check block. Required for Service (Traefik
|
||||
// routing depends on it). P02 populates it from the `health:`
|
||||
// frontmatter block (R-012).
|
||||
Health *HealthBlock
|
||||
|
||||
// Constraints is the CEL expression list for placement. P02
|
||||
// populates it from the `constraints:` frontmatter array; P05
|
||||
// consumes it for the CLI-side scheduler (REQ-083).
|
||||
Constraints []string
|
||||
|
||||
// Affinity is the affinity rule list for placement. P02 populates
|
||||
// it from the `affinity:` frontmatter array; P05 consumes it.
|
||||
Affinity []AffinityRule
|
||||
|
||||
// Lifecycle is the lifecycle hook block (pre_stop, post_start).
|
||||
// P02 populates it from the `lifecycle:` frontmatter block; P04
|
||||
// wires it into the systemd unit (ExecStop / ExecStartPost).
|
||||
Lifecycle *LifecycleBlock
|
||||
|
||||
// Timeout is an optional execution timeout (duration string) for
|
||||
// Job. Populated by P04.
|
||||
Timeout string
|
||||
|
||||
// Tasks is the task-group list for multi-process services (P06,
|
||||
// PRD §9.1). When non-empty, the alloc runs one systemd unit per
|
||||
// task (`orca-v1-alloc-<alloc-id>-<task-name>.service`) all
|
||||
// grouped under a single `<alloc-id>.target`. When nil/empty,
|
||||
// the alloc is a single-process alloc driven by the top-level
|
||||
// Runtime block (backward compat). Tasks that omit their own
|
||||
// runtime inherit the top-level Runtime as the per-group default.
|
||||
Tasks []TaskGroupTask
|
||||
}
|
||||
|
||||
// TaskGroupTask is a single task within a task group (P06, PRD §9.1).
|
||||
// Each task has its own runtime (a wasm task + a process sidecar is
|
||||
// allowed), its own command, and an optional env overlay. When
|
||||
// Runtime is nil, the task inherits the top-level
|
||||
// WorkloadSpec.Runtime (the per-group default).
|
||||
type TaskGroupTask struct {
|
||||
Name string
|
||||
Runtime *RuntimeBlock
|
||||
Env map[string]string
|
||||
Command string
|
||||
}
|
||||
|
||||
// RuntimeBlock is a minimal runtime abstraction surface populated by the
|
||||
@@ -35,6 +106,76 @@ type RuntimeBlock struct {
|
||||
Command string
|
||||
}
|
||||
|
||||
// RestartBlock is the restart policy block. Mode is one of never,
|
||||
// on-failure, service (REQ-074 schema validators). P02 populates it from
|
||||
// the frontmatter `restart:` block.
|
||||
type RestartBlock struct {
|
||||
Mode string
|
||||
MaxRetries int
|
||||
Delay string
|
||||
}
|
||||
|
||||
// ScheduleBlock is the scheduling block. For Job, Cron is an optional
|
||||
// cron expression. For DaemonSet, Mode is one of every-node, matching,
|
||||
// mandatory (REQ-074). Populated by P05 and the DaemonSet phase.
|
||||
type ScheduleBlock struct {
|
||||
Mode string
|
||||
Cron string
|
||||
}
|
||||
|
||||
// UpdateBlock is the rolling/canary update stanza. Required for Service.
|
||||
// P02 populates it from the frontmatter `update:` block; the
|
||||
// rolling/canary/blue-green semantics land in P03.
|
||||
type UpdateBlock struct {
|
||||
Strategy string
|
||||
MaxSurge int
|
||||
MaxParallel int
|
||||
MinHealthyTime string
|
||||
HealthyDeadline string
|
||||
Canary string
|
||||
AutoPromote bool
|
||||
}
|
||||
|
||||
// ServiceBlock is the Traefik route definition. For Service it is
|
||||
// implied (Traefik route YES); Job and DaemonSet do not carry one by
|
||||
// default (D-175). P02 populates it from the frontmatter `service:`
|
||||
// block.
|
||||
type ServiceBlock struct {
|
||||
Host string
|
||||
RouteID string
|
||||
Name string
|
||||
Port int
|
||||
Bind string
|
||||
}
|
||||
|
||||
// HealthBlock is the health-check block. P02 populates it from the
|
||||
// frontmatter `health:` block (R-012). The Traefik emitter (REQ-077)
|
||||
// renders it as the service's health-check stanza; ServiceValidator
|
||||
// requires it for Traefik routing.
|
||||
type HealthBlock struct {
|
||||
CheckType string
|
||||
Interval string
|
||||
Timeout string
|
||||
UnhealthyThreshold int
|
||||
}
|
||||
|
||||
// AffinityRule is a single affinity entry: target CEL expression +
|
||||
// integer weight. P02 populates it from the `affinity:` frontmatter
|
||||
// array; P05 consumes it for the CLI-side scheduler (REQ-083).
|
||||
type AffinityRule struct {
|
||||
Target string
|
||||
Weight int
|
||||
}
|
||||
|
||||
// LifecycleBlock is the lifecycle hook block. PreStop and PostStart
|
||||
// are command lists run before stop / after start. P02 populates it
|
||||
// from the `lifecycle:` frontmatter block; P04 wires it into the
|
||||
// systemd unit (ExecStop / ExecStartPost).
|
||||
type LifecycleBlock struct {
|
||||
PreStop []string
|
||||
PostStart []string
|
||||
}
|
||||
|
||||
// PortSpec is a minimal port binding entry. HostIP is optional.
|
||||
type PortSpec struct {
|
||||
Name string
|
||||
@@ -257,10 +398,25 @@ func parseFrontmatterBlock(block string) (*WorkloadSpec, error) {
|
||||
secEnv
|
||||
secSecrets
|
||||
secVolumes
|
||||
secRestart
|
||||
secUpdate
|
||||
secService
|
||||
secHealth
|
||||
secLifecycle
|
||||
secAffinity
|
||||
secConstraints
|
||||
secTasks
|
||||
secTaskEnv
|
||||
secTaskRuntime
|
||||
)
|
||||
cur := secNone
|
||||
var curPort *PortSpec
|
||||
var curVol *VolumeSpec
|
||||
var curAffinity *AffinityRule
|
||||
var lifecycleCur string
|
||||
var curTask *TaskGroupTask
|
||||
var taskIndent int
|
||||
var taskFieldIndent int
|
||||
|
||||
flushPort := func() {
|
||||
if curPort != nil {
|
||||
@@ -274,6 +430,35 @@ func parseFrontmatterBlock(block string) (*WorkloadSpec, error) {
|
||||
curVol = nil
|
||||
}
|
||||
}
|
||||
flushAffinity := func() {
|
||||
if curAffinity != nil {
|
||||
spec.Affinity = append(spec.Affinity, *curAffinity)
|
||||
curAffinity = nil
|
||||
}
|
||||
}
|
||||
flushTask := func() {
|
||||
if curTask != nil {
|
||||
spec.Tasks = append(spec.Tasks, *curTask)
|
||||
curTask = nil
|
||||
}
|
||||
}
|
||||
// taskSubBlock returns the sub-section to switch to when the
|
||||
// given `key: value` line opens a nested block under a task
|
||||
// (`env:` → secTaskEnv, `runtime:` → secTaskRuntime). Returns
|
||||
// secTasks for non-block keys (no switch).
|
||||
taskSubBlock := func(kvLine string) section {
|
||||
key, _, ok := splitKV(kvLine)
|
||||
if !ok {
|
||||
return secTasks
|
||||
}
|
||||
switch key {
|
||||
case "env":
|
||||
return secTaskEnv
|
||||
case "runtime":
|
||||
return secTaskRuntime
|
||||
}
|
||||
return secTasks
|
||||
}
|
||||
|
||||
for lineNo, raw := range lines {
|
||||
line := stripComment(raw)
|
||||
@@ -287,6 +472,8 @@ func parseFrontmatterBlock(block string) (*WorkloadSpec, error) {
|
||||
// Flush any pending nested entry before switching sections.
|
||||
flushPort()
|
||||
flushVol()
|
||||
flushAffinity()
|
||||
flushTask()
|
||||
cur = secNone
|
||||
|
||||
key, val, ok := splitKV(trimmed)
|
||||
@@ -330,8 +517,46 @@ func parseFrontmatterBlock(block string) (*WorkloadSpec, error) {
|
||||
}
|
||||
case "volumes":
|
||||
cur = secVolumes
|
||||
case "restart":
|
||||
spec.Restart = &RestartBlock{}
|
||||
cur = secRestart
|
||||
case "update":
|
||||
spec.Update = &UpdateBlock{}
|
||||
cur = secUpdate
|
||||
case "service":
|
||||
spec.Service = &ServiceBlock{}
|
||||
cur = secService
|
||||
case "health":
|
||||
spec.Health = &HealthBlock{}
|
||||
cur = secHealth
|
||||
case "lifecycle":
|
||||
spec.Lifecycle = &LifecycleBlock{}
|
||||
cur = secLifecycle
|
||||
case "constraints":
|
||||
if strings.TrimSpace(val) != "" {
|
||||
arr, err := parseStringArray(val)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse markdown: line %d: constraints: %w", lineNo+1, err)
|
||||
}
|
||||
spec.Constraints = append(spec.Constraints, arr...)
|
||||
cur = secNone
|
||||
} else {
|
||||
cur = secConstraints
|
||||
}
|
||||
case "affinity":
|
||||
if strings.TrimSpace(val) != "" {
|
||||
// Inline form not supported for affinity objects;
|
||||
// require the block form. Ignore inline values.
|
||||
cur = secNone
|
||||
} else {
|
||||
cur = secAffinity
|
||||
}
|
||||
case "tasks":
|
||||
cur = secTasks
|
||||
taskIndent = -1
|
||||
taskFieldIndent = -1
|
||||
default:
|
||||
// Unknown top-level keys are ignored (forward-compat).
|
||||
// Unknown top-level key are ignored (forward-compat).
|
||||
cur = secNone
|
||||
}
|
||||
continue
|
||||
@@ -402,10 +627,267 @@ func parseFrontmatterBlock(block string) (*WorkloadSpec, error) {
|
||||
} else if curVol != nil {
|
||||
applyVolumeKV(curVol, trimmed)
|
||||
}
|
||||
case secRestart:
|
||||
if spec.Restart == nil {
|
||||
spec.Restart = &RestartBlock{}
|
||||
}
|
||||
key, val, ok := splitKV(trimmed)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
switch key {
|
||||
case "mode":
|
||||
spec.Restart.Mode = unquote(val)
|
||||
case "attempts", "max_retries":
|
||||
if n, err := strconv.Atoi(strings.TrimSpace(unquote(val))); err == nil {
|
||||
spec.Restart.MaxRetries = n
|
||||
}
|
||||
case "delay":
|
||||
spec.Restart.Delay = unquote(val)
|
||||
}
|
||||
case secUpdate:
|
||||
if spec.Update == nil {
|
||||
spec.Update = &UpdateBlock{}
|
||||
}
|
||||
key, val, ok := splitKV(trimmed)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
switch key {
|
||||
case "strategy":
|
||||
spec.Update.Strategy = unquote(val)
|
||||
case "max_parallel":
|
||||
if n, err := strconv.Atoi(strings.TrimSpace(unquote(val))); err == nil {
|
||||
spec.Update.MaxParallel = n
|
||||
}
|
||||
case "max_surge":
|
||||
if n, err := strconv.Atoi(strings.TrimSpace(unquote(val))); err == nil {
|
||||
spec.Update.MaxSurge = n
|
||||
}
|
||||
case "min_healthy_time":
|
||||
spec.Update.MinHealthyTime = unquote(val)
|
||||
case "healthy_deadline":
|
||||
spec.Update.HealthyDeadline = unquote(val)
|
||||
case "canary":
|
||||
spec.Update.Canary = unquote(val)
|
||||
case "auto_promote":
|
||||
spec.Update.AutoPromote = parseBool(val)
|
||||
}
|
||||
case secService:
|
||||
if spec.Service == nil {
|
||||
spec.Service = &ServiceBlock{}
|
||||
}
|
||||
key, val, ok := splitKV(trimmed)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
switch key {
|
||||
case "name":
|
||||
spec.Service.Name = unquote(val)
|
||||
case "port":
|
||||
if n, err := strconv.Atoi(strings.TrimSpace(unquote(val))); err == nil {
|
||||
spec.Service.Port = n
|
||||
}
|
||||
case "bind":
|
||||
spec.Service.Bind = unquote(val)
|
||||
case "host":
|
||||
spec.Service.Host = unquote(val)
|
||||
case "route_id":
|
||||
spec.Service.RouteID = unquote(val)
|
||||
}
|
||||
case secHealth:
|
||||
if spec.Health == nil {
|
||||
spec.Health = &HealthBlock{}
|
||||
}
|
||||
key, val, ok := splitKV(trimmed)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
switch key {
|
||||
case "check_type":
|
||||
spec.Health.CheckType = unquote(val)
|
||||
case "interval":
|
||||
spec.Health.Interval = unquote(val)
|
||||
case "timeout":
|
||||
spec.Health.Timeout = unquote(val)
|
||||
case "unhealthy_threshold":
|
||||
if n, err := strconv.Atoi(strings.TrimSpace(unquote(val))); err == nil {
|
||||
spec.Health.UnhealthyThreshold = n
|
||||
}
|
||||
}
|
||||
case secLifecycle:
|
||||
if spec.Lifecycle == nil {
|
||||
spec.Lifecycle = &LifecycleBlock{}
|
||||
}
|
||||
// pre_stop / post_start are string arrays. The block form
|
||||
// is:
|
||||
// lifecycle:
|
||||
// pre_stop:
|
||||
// - cmd1
|
||||
// - cmd2
|
||||
// post_start:
|
||||
// - cmd3
|
||||
// We track which sub-list we are appending to via a local
|
||||
// cursor that is reset on every top-level section change.
|
||||
key, val, ok := splitKV(trimmed)
|
||||
if !ok {
|
||||
// Could be a list item under pre_stop/post_start.
|
||||
if strings.HasPrefix(trimmed, "- ") || trimmed == "-" {
|
||||
item := strings.TrimSpace(strings.TrimPrefix(trimmed, "-"))
|
||||
if item != "" && lifecycleCur != "" {
|
||||
appendLifecycleCmd(spec.Lifecycle, lifecycleCur, unquote(item))
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
switch key {
|
||||
case "pre_stop", "post_start":
|
||||
lifecycleCur = key
|
||||
if strings.TrimSpace(val) != "" {
|
||||
// Inline list form: `pre_stop: [cmd1, cmd2]`.
|
||||
arr, err := parseStringArray(val)
|
||||
if err == nil {
|
||||
for _, s := range arr {
|
||||
appendLifecycleCmd(spec.Lifecycle, key, s)
|
||||
}
|
||||
}
|
||||
lifecycleCur = ""
|
||||
}
|
||||
default:
|
||||
lifecycleCur = ""
|
||||
}
|
||||
case secAffinity:
|
||||
if strings.HasPrefix(trimmed, "- ") || trimmed == "-" {
|
||||
flushAffinity()
|
||||
r := AffinityRule{}
|
||||
curAffinity = &r
|
||||
rest := strings.TrimSpace(strings.TrimPrefix(trimmed, "-"))
|
||||
if rest != "" {
|
||||
applyAffinityKV(curAffinity, rest)
|
||||
}
|
||||
} else if curAffinity != nil {
|
||||
applyAffinityKV(curAffinity, trimmed)
|
||||
}
|
||||
case secConstraints:
|
||||
if strings.HasPrefix(trimmed, "- ") || trimmed == "-" {
|
||||
item := strings.TrimSpace(strings.TrimPrefix(trimmed, "-"))
|
||||
if item != "" {
|
||||
spec.Constraints = append(spec.Constraints, unquote(item))
|
||||
}
|
||||
}
|
||||
case secTasks:
|
||||
// Tasks is a list of task objects. A `- ` at the list
|
||||
// indent opens a new task; deeper-indented lines belong
|
||||
// to the current task's fields (name, command) or
|
||||
// nested sub-blocks (runtime, env).
|
||||
if strings.HasPrefix(trimmed, "- ") || trimmed == "-" {
|
||||
if taskIndent < 0 {
|
||||
taskIndent = indent
|
||||
taskFieldIndent = indent + 2
|
||||
}
|
||||
if indent == taskIndent {
|
||||
flushTask()
|
||||
t := TaskGroupTask{}
|
||||
curTask = &t
|
||||
rest := strings.TrimSpace(strings.TrimPrefix(trimmed, "-"))
|
||||
if rest != "" {
|
||||
if applyTaskKV(curTask, rest) {
|
||||
cur = taskSubBlock(rest)
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
if curTask != nil {
|
||||
if applyTaskKV(curTask, trimmed) {
|
||||
cur = taskSubBlock(trimmed)
|
||||
}
|
||||
}
|
||||
case secTaskEnv:
|
||||
if curTask == nil {
|
||||
cur = secTasks
|
||||
continue
|
||||
}
|
||||
// Pop back to the task field level when the indent
|
||||
// returns to taskFieldIndent (the next sibling
|
||||
// field or a new `- ` list item). The line is then
|
||||
// reprocessed as a task field.
|
||||
if taskFieldIndent > 0 && indent <= taskFieldIndent {
|
||||
cur = secTasks
|
||||
if indent == taskIndent && (strings.HasPrefix(trimmed, "- ") || trimmed == "-") {
|
||||
flushTask()
|
||||
t := TaskGroupTask{}
|
||||
curTask = &t
|
||||
rest := strings.TrimSpace(strings.TrimPrefix(trimmed, "-"))
|
||||
if rest != "" {
|
||||
if applyTaskKV(curTask, rest) {
|
||||
cur = taskSubBlock(rest)
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
if applyTaskKV(curTask, trimmed) {
|
||||
cur = taskSubBlock(trimmed)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if curTask.Env == nil {
|
||||
curTask.Env = map[string]string{}
|
||||
}
|
||||
key, val, ok := splitKV(trimmed)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if val == "" {
|
||||
curTask.Env[key] = ""
|
||||
} else if strings.HasPrefix(val, "{") && strings.HasSuffix(val, "}") {
|
||||
curTask.Env[key] = val
|
||||
} else {
|
||||
curTask.Env[key] = unquote(val)
|
||||
}
|
||||
case secTaskRuntime:
|
||||
if curTask == nil || curTask.Runtime == nil {
|
||||
cur = secTasks
|
||||
continue
|
||||
}
|
||||
// Pop back to the task field level (see secTaskEnv).
|
||||
if taskFieldIndent > 0 && indent <= taskFieldIndent {
|
||||
cur = secTasks
|
||||
if indent == taskIndent && (strings.HasPrefix(trimmed, "- ") || trimmed == "-") {
|
||||
flushTask()
|
||||
t := TaskGroupTask{}
|
||||
curTask = &t
|
||||
rest := strings.TrimSpace(strings.TrimPrefix(trimmed, "-"))
|
||||
if rest != "" {
|
||||
if applyTaskKV(curTask, rest) {
|
||||
cur = taskSubBlock(rest)
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
if applyTaskKV(curTask, trimmed) {
|
||||
cur = taskSubBlock(trimmed)
|
||||
}
|
||||
continue
|
||||
}
|
||||
key, val, ok := splitKV(trimmed)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
switch key {
|
||||
case "one_of":
|
||||
curTask.Runtime.OneOf = unquote(val)
|
||||
case "image":
|
||||
curTask.Runtime.Image = unquote(val)
|
||||
case "command":
|
||||
curTask.Runtime.Command = unquote(val)
|
||||
}
|
||||
}
|
||||
}
|
||||
flushPort()
|
||||
flushVol()
|
||||
flushAffinity()
|
||||
flushTask()
|
||||
return spec, nil
|
||||
}
|
||||
|
||||
@@ -456,6 +938,75 @@ func applyVolumeKV(v *VolumeSpec, s string) {
|
||||
}
|
||||
}
|
||||
|
||||
// applyAffinityKV applies a `key: value` pair to an AffinityRule entry.
|
||||
func applyAffinityKV(r *AffinityRule, s string) {
|
||||
key, val, ok := splitKV(s)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
switch key {
|
||||
case "target":
|
||||
r.Target = unquote(val)
|
||||
case "weight":
|
||||
if n, err := strconv.Atoi(strings.TrimSpace(unquote(val))); err == nil {
|
||||
r.Weight = n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// applyTaskKV applies a `key: value` pair to the current TaskGroupTask.
|
||||
// The returned bool reports whether the key opened a nested sub-block
|
||||
// (`env` or `runtime`); when true the caller switches the parser
|
||||
// section to the corresponding sub-block handler.
|
||||
func applyTaskKV(t *TaskGroupTask, s string) (openedSubBlock bool) {
|
||||
key, val, ok := splitKV(s)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
switch key {
|
||||
case "name":
|
||||
t.Name = unquote(val)
|
||||
case "command":
|
||||
t.Command = unquote(val)
|
||||
case "env":
|
||||
if t.Env == nil {
|
||||
t.Env = map[string]string{}
|
||||
}
|
||||
return true
|
||||
case "runtime":
|
||||
if t.Runtime == nil {
|
||||
t.Runtime = &RuntimeBlock{}
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// appendLifecycleCmd appends a command to the named lifecycle hook list
|
||||
// (pre_stop or post_start) on the given LifecycleBlock.
|
||||
func appendLifecycleCmd(lb *LifecycleBlock, name, cmd string) {
|
||||
if lb == nil || cmd == "" {
|
||||
return
|
||||
}
|
||||
switch name {
|
||||
case "pre_stop":
|
||||
lb.PreStop = append(lb.PreStop, cmd)
|
||||
case "post_start":
|
||||
lb.PostStart = append(lb.PostStart, cmd)
|
||||
}
|
||||
}
|
||||
|
||||
// parseBool parses a YAML-ish boolean value (true/yes/on/1 → true). The
|
||||
// comparison is case-insensitive. Empty and unrecognized values return
|
||||
// false (forward-compatible with future strict-mode validation).
|
||||
func parseBool(s string) bool {
|
||||
switch strings.ToLower(strings.TrimSpace(unquote(s))) {
|
||||
case "true", "yes", "on", "1":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// validateWorkload enforces required fields and kind validity (R-012).
|
||||
func validateWorkload(spec *WorkloadSpec) error {
|
||||
if spec.Kind == "" {
|
||||
|
||||
@@ -355,3 +355,498 @@ func TestParseMarkdown_UnknownKeyIgnored(t *testing.T) {
|
||||
t.Fatalf("ParseMarkdown should ignore unknown keys: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMarkdown_RestartBlock(t *testing.T) {
|
||||
input := "---\n" +
|
||||
"kind: Service\n" +
|
||||
"name: web\n" +
|
||||
"restart:\n" +
|
||||
" mode: service\n" +
|
||||
" attempts: 5\n" +
|
||||
" delay: 3s\n" +
|
||||
"---\nbody\n"
|
||||
spec, err := ParseMarkdown([]byte(input))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMarkdown: %v", err)
|
||||
}
|
||||
if spec.Restart == nil {
|
||||
t.Fatal("Restart is nil")
|
||||
}
|
||||
if spec.Restart.Mode != "service" {
|
||||
t.Errorf("Restart.Mode = %q, want service", spec.Restart.Mode)
|
||||
}
|
||||
if spec.Restart.MaxRetries != 5 {
|
||||
t.Errorf("Restart.MaxRetries = %d, want 5", spec.Restart.MaxRetries)
|
||||
}
|
||||
if spec.Restart.Delay != "3s" {
|
||||
t.Errorf("Restart.Delay = %q, want 3s", spec.Restart.Delay)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMarkdown_RestartBlockMaxRetriesAlias(t *testing.T) {
|
||||
// max_retries is the canonical key; attempts is an accepted alias.
|
||||
input := "---\n" +
|
||||
"kind: Service\n" +
|
||||
"name: web\n" +
|
||||
"restart:\n" +
|
||||
" mode: on-failure\n" +
|
||||
" max_retries: 3\n" +
|
||||
" delay: 1s\n" +
|
||||
"---\nbody\n"
|
||||
spec, err := ParseMarkdown([]byte(input))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMarkdown: %v", err)
|
||||
}
|
||||
if spec.Restart == nil || spec.Restart.MaxRetries != 3 {
|
||||
t.Fatalf("Restart.MaxRetries = %d, want 3 (max_retries alias)", spec.Restart.MaxRetries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMarkdown_UpdateBlock(t *testing.T) {
|
||||
input := "---\n" +
|
||||
"kind: Service\n" +
|
||||
"name: web\n" +
|
||||
"update:\n" +
|
||||
" strategy: canary\n" +
|
||||
" max_parallel: 2\n" +
|
||||
" min_healthy_time: 30s\n" +
|
||||
" healthy_deadline: 5m\n" +
|
||||
" canary: 10%\n" +
|
||||
" auto_promote: true\n" +
|
||||
"---\nbody\n"
|
||||
spec, err := ParseMarkdown([]byte(input))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMarkdown: %v", err)
|
||||
}
|
||||
if spec.Update == nil {
|
||||
t.Fatal("Update is nil")
|
||||
}
|
||||
if spec.Update.Strategy != "canary" {
|
||||
t.Errorf("Update.Strategy = %q, want canary", spec.Update.Strategy)
|
||||
}
|
||||
if spec.Update.MaxParallel != 2 {
|
||||
t.Errorf("Update.MaxParallel = %d, want 2", spec.Update.MaxParallel)
|
||||
}
|
||||
if spec.Update.MinHealthyTime != "30s" {
|
||||
t.Errorf("Update.MinHealthyTime = %q, want 30s", spec.Update.MinHealthyTime)
|
||||
}
|
||||
if spec.Update.HealthyDeadline != "5m" {
|
||||
t.Errorf("Update.HealthyDeadline = %q, want 5m", spec.Update.HealthyDeadline)
|
||||
}
|
||||
if spec.Update.Canary != "10%" {
|
||||
t.Errorf("Update.Canary = %q, want 10%%", spec.Update.Canary)
|
||||
}
|
||||
if !spec.Update.AutoPromote {
|
||||
t.Errorf("Update.AutoPromote = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMarkdown_ServiceBlock(t *testing.T) {
|
||||
input := "---\n" +
|
||||
"kind: Service\n" +
|
||||
"name: web\n" +
|
||||
"service:\n" +
|
||||
" name: web\n" +
|
||||
" port: 8080\n" +
|
||||
" bind: 127.0.0.1\n" +
|
||||
"---\nbody\n"
|
||||
spec, err := ParseMarkdown([]byte(input))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMarkdown: %v", err)
|
||||
}
|
||||
if spec.Service == nil {
|
||||
t.Fatal("Service is nil")
|
||||
}
|
||||
if spec.Service.Name != "web" {
|
||||
t.Errorf("Service.Name = %q, want web", spec.Service.Name)
|
||||
}
|
||||
if spec.Service.Port != 8080 {
|
||||
t.Errorf("Service.Port = %d, want 8080", spec.Service.Port)
|
||||
}
|
||||
if spec.Service.Bind != "127.0.0.1" {
|
||||
t.Errorf("Service.Bind = %q, want 127.0.0.1", spec.Service.Bind)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMarkdown_HealthBlock(t *testing.T) {
|
||||
input := "---\n" +
|
||||
"kind: Service\n" +
|
||||
"name: web\n" +
|
||||
"health:\n" +
|
||||
" check_type: http\n" +
|
||||
" interval: 10s\n" +
|
||||
" timeout: 2s\n" +
|
||||
" unhealthy_threshold: 3\n" +
|
||||
"---\nbody\n"
|
||||
spec, err := ParseMarkdown([]byte(input))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMarkdown: %v", err)
|
||||
}
|
||||
if spec.Health == nil {
|
||||
t.Fatal("Health is nil")
|
||||
}
|
||||
if spec.Health.CheckType != "http" {
|
||||
t.Errorf("Health.CheckType = %q, want http", spec.Health.CheckType)
|
||||
}
|
||||
if spec.Health.Interval != "10s" {
|
||||
t.Errorf("Health.Interval = %q, want 10s", spec.Health.Interval)
|
||||
}
|
||||
if spec.Health.Timeout != "2s" {
|
||||
t.Errorf("Health.Timeout = %q, want 2s", spec.Health.Timeout)
|
||||
}
|
||||
if spec.Health.UnhealthyThreshold != 3 {
|
||||
t.Errorf("Health.UnhealthyThreshold = %d, want 3", spec.Health.UnhealthyThreshold)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMarkdown_ConstraintsInlineArray(t *testing.T) {
|
||||
// Inline flow-array form: the parser does NOT unescape YAML
|
||||
// escapes (consistent with the secrets inline parser). Use
|
||||
// single-quoted scalars inside the flow array so the CEL strings
|
||||
// are preserved verbatim.
|
||||
input := "---\nkind: Service\nname: web\nconstraints: ['node.role == \"web\"', 'region == \"us\"']\n---\nbody\n"
|
||||
spec, err := ParseMarkdown([]byte(input))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMarkdown: %v", err)
|
||||
}
|
||||
if len(spec.Constraints) != 2 {
|
||||
t.Fatalf("Constraints = %d, want 2", len(spec.Constraints))
|
||||
}
|
||||
if spec.Constraints[0] != `node.role == "web"` {
|
||||
t.Errorf("Constraints[0] = %q", spec.Constraints[0])
|
||||
}
|
||||
if spec.Constraints[1] != `region == "us"` {
|
||||
t.Errorf("Constraints[1] = %q", spec.Constraints[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMarkdown_ConstraintsBlockArray(t *testing.T) {
|
||||
input := "---\n" +
|
||||
"kind: Service\n" +
|
||||
"name: web\n" +
|
||||
"constraints:\n" +
|
||||
" - node.role == \"web\"\n" +
|
||||
" - region == \"us\"\n" +
|
||||
"---\nbody\n"
|
||||
spec, err := ParseMarkdown([]byte(input))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMarkdown: %v", err)
|
||||
}
|
||||
if len(spec.Constraints) != 2 {
|
||||
t.Fatalf("Constraints = %d, want 2", len(spec.Constraints))
|
||||
}
|
||||
if spec.Constraints[0] != `node.role == "web"` {
|
||||
t.Errorf("Constraints[0] = %q", spec.Constraints[0])
|
||||
}
|
||||
if spec.Constraints[1] != `region == "us"` {
|
||||
t.Errorf("Constraints[1] = %q", spec.Constraints[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMarkdown_AffinityBlock(t *testing.T) {
|
||||
input := "---\n" +
|
||||
"kind: Service\n" +
|
||||
"name: web\n" +
|
||||
"affinity:\n" +
|
||||
" - target: node.role == \"web\"\n" +
|
||||
" weight: 100\n" +
|
||||
" - target: region == \"us\"\n" +
|
||||
" weight: 50\n" +
|
||||
"---\nbody\n"
|
||||
spec, err := ParseMarkdown([]byte(input))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMarkdown: %v", err)
|
||||
}
|
||||
if len(spec.Affinity) != 2 {
|
||||
t.Fatalf("Affinity = %d, want 2", len(spec.Affinity))
|
||||
}
|
||||
if spec.Affinity[0].Target != `node.role == "web"` {
|
||||
t.Errorf("Affinity[0].Target = %q", spec.Affinity[0].Target)
|
||||
}
|
||||
if spec.Affinity[0].Weight != 100 {
|
||||
t.Errorf("Affinity[0].Weight = %d, want 100", spec.Affinity[0].Weight)
|
||||
}
|
||||
if spec.Affinity[1].Target != `region == "us"` {
|
||||
t.Errorf("Affinity[1].Target = %q", spec.Affinity[1].Target)
|
||||
}
|
||||
if spec.Affinity[1].Weight != 50 {
|
||||
t.Errorf("Affinity[1].Weight = %d, want 50", spec.Affinity[1].Weight)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMarkdown_LifecycleBlock(t *testing.T) {
|
||||
input := "---\n" +
|
||||
"kind: Service\n" +
|
||||
"name: web\n" +
|
||||
"lifecycle:\n" +
|
||||
" pre_stop:\n" +
|
||||
" - /bin/sh -c 'sleep 5'\n" +
|
||||
" - /usr/local/bin/drain.sh\n" +
|
||||
" post_start:\n" +
|
||||
" - /usr/local/bin/warm-cache.sh\n" +
|
||||
"---\nbody\n"
|
||||
spec, err := ParseMarkdown([]byte(input))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMarkdown: %v", err)
|
||||
}
|
||||
if spec.Lifecycle == nil {
|
||||
t.Fatal("Lifecycle is nil")
|
||||
}
|
||||
if len(spec.Lifecycle.PreStop) != 2 {
|
||||
t.Fatalf("PreStop = %d, want 2", len(spec.Lifecycle.PreStop))
|
||||
}
|
||||
if spec.Lifecycle.PreStop[0] != "/bin/sh -c 'sleep 5'" {
|
||||
t.Errorf("PreStop[0] = %q", spec.Lifecycle.PreStop[0])
|
||||
}
|
||||
if spec.Lifecycle.PreStop[1] != "/usr/local/bin/drain.sh" {
|
||||
t.Errorf("PreStop[1] = %q", spec.Lifecycle.PreStop[1])
|
||||
}
|
||||
if len(spec.Lifecycle.PostStart) != 1 {
|
||||
t.Fatalf("PostStart = %d, want 1", len(spec.Lifecycle.PostStart))
|
||||
}
|
||||
if spec.Lifecycle.PostStart[0] != "/usr/local/bin/warm-cache.sh" {
|
||||
t.Errorf("PostStart[0] = %q", spec.Lifecycle.PostStart[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMarkdown_LifecycleInlineArray(t *testing.T) {
|
||||
input := "---\n" +
|
||||
"kind: Service\n" +
|
||||
"name: web\n" +
|
||||
"lifecycle:\n" +
|
||||
" pre_stop: [\"/bin/true\"]\n" +
|
||||
" post_start: [\"/bin/warmup\", \"/bin/check\"]\n" +
|
||||
"---\nbody\n"
|
||||
spec, err := ParseMarkdown([]byte(input))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMarkdown: %v", err)
|
||||
}
|
||||
if spec.Lifecycle == nil {
|
||||
t.Fatal("Lifecycle is nil")
|
||||
}
|
||||
if len(spec.Lifecycle.PreStop) != 1 || spec.Lifecycle.PreStop[0] != "/bin/true" {
|
||||
t.Errorf("PreStop = %v, want [/bin/true]", spec.Lifecycle.PreStop)
|
||||
}
|
||||
if len(spec.Lifecycle.PostStart) != 2 {
|
||||
t.Fatalf("PostStart = %v, want 2 entries", spec.Lifecycle.PostStart)
|
||||
}
|
||||
if spec.Lifecycle.PostStart[0] != "/bin/warmup" || spec.Lifecycle.PostStart[1] != "/bin/check" {
|
||||
t.Errorf("PostStart = %v, want [/bin/warmup /bin/check]", spec.Lifecycle.PostStart)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMarkdown_FullServiceSpec(t *testing.T) {
|
||||
// A complete Service spec exercising every P02-parsed block together.
|
||||
input := "---\n" +
|
||||
"kind: Service\n" +
|
||||
"name: web\n" +
|
||||
"count: 3\n" +
|
||||
"runtime:\n" +
|
||||
" one_of: process\n" +
|
||||
" command: /usr/bin/httpd\n" +
|
||||
"ports:\n" +
|
||||
" - name: http\n" +
|
||||
" port: 8080\n" +
|
||||
"restart:\n" +
|
||||
" mode: service\n" +
|
||||
" attempts: 5\n" +
|
||||
" delay: 2s\n" +
|
||||
"update:\n" +
|
||||
" strategy: rolling\n" +
|
||||
" max_parallel: 1\n" +
|
||||
" auto_promote: false\n" +
|
||||
"service:\n" +
|
||||
" name: web\n" +
|
||||
" port: 8080\n" +
|
||||
"health:\n" +
|
||||
" check_type: http\n" +
|
||||
" interval: 5s\n" +
|
||||
" timeout: 1s\n" +
|
||||
" unhealthy_threshold: 2\n" +
|
||||
"constraints:\n" +
|
||||
" - node.role == \"web\"\n" +
|
||||
"affinity:\n" +
|
||||
" - target: zone == \"a\"\n" +
|
||||
" weight: 80\n" +
|
||||
"lifecycle:\n" +
|
||||
" post_start:\n" +
|
||||
" - /bin/ready.sh\n" +
|
||||
"---\n# body\n"
|
||||
spec, err := ParseMarkdown([]byte(input))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMarkdown: %v", err)
|
||||
}
|
||||
if spec.Restart == nil || spec.Restart.Mode != "service" {
|
||||
t.Errorf("Restart not parsed: %+v", spec.Restart)
|
||||
}
|
||||
if spec.Update == nil || spec.Update.Strategy != "rolling" {
|
||||
t.Errorf("Update not parsed: %+v", spec.Update)
|
||||
}
|
||||
if spec.Service == nil || spec.Service.Port != 8080 {
|
||||
t.Errorf("Service not parsed: %+v", spec.Service)
|
||||
}
|
||||
if spec.Health == nil || spec.Health.CheckType != "http" {
|
||||
t.Errorf("Health not parsed: %+v", spec.Health)
|
||||
}
|
||||
if len(spec.Constraints) != 1 {
|
||||
t.Errorf("Constraints = %v", spec.Constraints)
|
||||
}
|
||||
if len(spec.Affinity) != 1 || spec.Affinity[0].Weight != 80 {
|
||||
t.Errorf("Affinity = %v", spec.Affinity)
|
||||
}
|
||||
if spec.Lifecycle == nil || len(spec.Lifecycle.PostStart) != 1 {
|
||||
t.Errorf("Lifecycle not parsed: %+v", spec.Lifecycle)
|
||||
}
|
||||
if spec.Body != "# body\n" {
|
||||
t.Errorf("Body = %q, want %q (R-015)", spec.Body, "# body\n")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMarkdown_TasksBlock(t *testing.T) {
|
||||
// P06: a task group with two tasks, each carrying its own runtime
|
||||
// and command. The parser must populate spec.Tasks with two
|
||||
// entries preserving name, runtime (one_of/image/command), and
|
||||
// the task-level command.
|
||||
input := "---\n" +
|
||||
"kind: Service\n" +
|
||||
"name: web\n" +
|
||||
"tasks:\n" +
|
||||
" - name: app\n" +
|
||||
" runtime:\n" +
|
||||
" one_of: process\n" +
|
||||
" image: docker.io/nginx:latest\n" +
|
||||
" command: /usr/bin/httpd -f\n" +
|
||||
" command: /usr/bin/httpd -f\n" +
|
||||
" - name: sidecar\n" +
|
||||
" runtime:\n" +
|
||||
" one_of: wasm\n" +
|
||||
" command: /bin/wasm-runner sidecar.wasm\n" +
|
||||
" command: /bin/wasm-runner sidecar.wasm\n" +
|
||||
"---\nbody\n"
|
||||
spec, err := ParseMarkdown([]byte(input))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMarkdown: %v", err)
|
||||
}
|
||||
if len(spec.Tasks) != 2 {
|
||||
t.Fatalf("Tasks = %d, want 2", len(spec.Tasks))
|
||||
}
|
||||
app := spec.Tasks[0]
|
||||
if app.Name != "app" {
|
||||
t.Errorf("Tasks[0].Name = %q, want app", app.Name)
|
||||
}
|
||||
if app.Runtime == nil {
|
||||
t.Fatal("Tasks[0].Runtime is nil")
|
||||
}
|
||||
if app.Runtime.OneOf != "process" {
|
||||
t.Errorf("Tasks[0].Runtime.OneOf = %q, want process", app.Runtime.OneOf)
|
||||
}
|
||||
if app.Runtime.Image != "docker.io/nginx:latest" {
|
||||
t.Errorf("Tasks[0].Runtime.Image = %q", app.Runtime.Image)
|
||||
}
|
||||
if app.Runtime.Command != "/usr/bin/httpd -f" {
|
||||
t.Errorf("Tasks[0].Runtime.Command = %q", app.Runtime.Command)
|
||||
}
|
||||
if app.Command != "/usr/bin/httpd -f" {
|
||||
t.Errorf("Tasks[0].Command = %q", app.Command)
|
||||
}
|
||||
side := spec.Tasks[1]
|
||||
if side.Name != "sidecar" {
|
||||
t.Errorf("Tasks[1].Name = %q, want sidecar", side.Name)
|
||||
}
|
||||
if side.Runtime == nil || side.Runtime.OneOf != "wasm" {
|
||||
t.Errorf("Tasks[1].Runtime = %+v, want one_of=wasm", side.Runtime)
|
||||
}
|
||||
if side.Command != "/bin/wasm-runner sidecar.wasm" {
|
||||
t.Errorf("Tasks[1].Command = %q", side.Command)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMarkdown_TasksBlockWithEnv(t *testing.T) {
|
||||
// P06: a task group task carrying an env overlay.
|
||||
input := "---\n" +
|
||||
"kind: Service\n" +
|
||||
"name: web\n" +
|
||||
"tasks:\n" +
|
||||
" - name: app\n" +
|
||||
" command: /usr/bin/httpd\n" +
|
||||
" env:\n" +
|
||||
" LOG_LEVEL: debug\n" +
|
||||
" REGION: us\n" +
|
||||
"---\nbody\n"
|
||||
spec, err := ParseMarkdown([]byte(input))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMarkdown: %v", err)
|
||||
}
|
||||
if len(spec.Tasks) != 1 {
|
||||
t.Fatalf("Tasks = %d, want 1", len(spec.Tasks))
|
||||
}
|
||||
task := spec.Tasks[0]
|
||||
if task.Env == nil {
|
||||
t.Fatal("Tasks[0].Env is nil")
|
||||
}
|
||||
if got := task.Env["LOG_LEVEL"]; got != "debug" {
|
||||
t.Errorf("Env[LOG_LEVEL] = %q, want debug", got)
|
||||
}
|
||||
if got := task.Env["REGION"]; got != "us" {
|
||||
t.Errorf("Env[REGION] = %q, want us", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMarkdown_TasksBlockInheritsTopLevelRuntime(t *testing.T) {
|
||||
// P06: when a task omits its own runtime, the top-level runtime
|
||||
// is the per-group default. The parser must NOT create a task
|
||||
// runtime when the task block lacks a `runtime:` sub-block; the
|
||||
// emitter/validator resolve the default from spec.Runtime.
|
||||
input := "---\n" +
|
||||
"kind: Service\n" +
|
||||
"name: web\n" +
|
||||
"runtime:\n" +
|
||||
" one_of: process\n" +
|
||||
" command: /bin/default\n" +
|
||||
"tasks:\n" +
|
||||
" - name: app\n" +
|
||||
" command: /bin/app\n" +
|
||||
" - name: sidecar\n" +
|
||||
" command: /bin/sidecar\n" +
|
||||
"---\nbody\n"
|
||||
spec, err := ParseMarkdown([]byte(input))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMarkdown: %v", err)
|
||||
}
|
||||
if spec.Runtime == nil || spec.Runtime.OneOf != "process" {
|
||||
t.Fatalf("top-level runtime not parsed: %+v", spec.Runtime)
|
||||
}
|
||||
if len(spec.Tasks) != 2 {
|
||||
t.Fatalf("Tasks = %d, want 2", len(spec.Tasks))
|
||||
}
|
||||
for i, task := range spec.Tasks {
|
||||
if task.Runtime != nil {
|
||||
t.Errorf("Tasks[%d].Runtime should be nil (inherit top-level), got %+v", i, task.Runtime)
|
||||
}
|
||||
}
|
||||
if spec.Tasks[0].Name != "app" || spec.Tasks[1].Name != "sidecar" {
|
||||
t.Errorf("task names = %q, %q", spec.Tasks[0].Name, spec.Tasks[1].Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMarkdown_NoTasksBackwardCompat(t *testing.T) {
|
||||
// Backward compat: a spec with no `tasks:` block parses as a
|
||||
// single-process alloc; spec.Tasks must be empty/nil.
|
||||
input := "---\n" +
|
||||
"kind: Job\n" +
|
||||
"name: backup\n" +
|
||||
"runtime:\n" +
|
||||
" one_of: process\n" +
|
||||
" command: /bin/rsync\n" +
|
||||
"---\nbody\n"
|
||||
spec, err := ParseMarkdown([]byte(input))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMarkdown: %v", err)
|
||||
}
|
||||
if len(spec.Tasks) != 0 {
|
||||
t.Fatalf("Tasks = %d, want 0 (backward compat)", len(spec.Tasks))
|
||||
}
|
||||
if spec.Runtime == nil || spec.Runtime.Command != "/bin/rsync" {
|
||||
t.Errorf("Runtime = %+v, want command=/bin/rsync", spec.Runtime)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,536 @@
|
||||
// Package scheduler — cel.go implements a minimal CEL-subset evaluator
|
||||
// for the CLI-side scheduler constraint expressions (REQ-083, P05).
|
||||
//
|
||||
// The full CEL specification (google.golang.org/genproto/...
|
||||
// googleapis/api/expr/v1alpha1) is intentionally NOT a dependency of
|
||||
// this module (see go.mod): adding it for a single callsite would pull
|
||||
// in a large transitive graph and contradict the "stdlib + minimal
|
||||
// deps" guardrail. Instead this file implements a hand-rolled
|
||||
// recursive-descent evaluator for the subset the PRD exercises:
|
||||
//
|
||||
// - attribute access on a `node.<name>` object (hostname, kind,
|
||||
// cpus, memory, tags, runtimes)
|
||||
// - string and integer literals (double-quoted)
|
||||
// - comparison operators: == != >= <= > <
|
||||
// - membership: <expr> in <expr>, <expr> not in <expr>
|
||||
// - boolean composition: and, or, not (parenthesised)
|
||||
//
|
||||
// Anything outside this subset returns an error rather than a silent
|
||||
// wrong answer; that is the documented limitation. The grammar is
|
||||
// small enough to be unambiguous with a top-down precedence-climbing
|
||||
// parser.
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// EvaluateConstraint evaluates a single CEL-subset expression against
|
||||
// the supplied NodeInfo. Returns (matched, err). An expression that
|
||||
// references an unknown attribute, uses an unsupported operator, or
|
||||
// fails to parse yields an error. Schedule treats a constraint
|
||||
// evaluation error as a non-fit (the node is silently skipped) rather
|
||||
// than a hard fail because operators routinely write exploratory
|
||||
// constraints against attributes the local cluster does not expose.
|
||||
func EvaluateConstraint(expr string, node NodeInfo) (bool, error) {
|
||||
p := newParser(strings.TrimSpace(expr), node)
|
||||
if p.len() == 0 {
|
||||
return false, fmt.Errorf("cel: empty expression")
|
||||
}
|
||||
v, err := p.parseExpr()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if p.tok.kind != tokEOF {
|
||||
return false, fmt.Errorf("cel: trailing input near %q", p.tok.text)
|
||||
}
|
||||
b, ok := v.(bool)
|
||||
if !ok {
|
||||
return false, fmt.Errorf("cel: expression did not evaluate to bool (got %T)", v)
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// EvaluateAll returns true iff every constraint evaluates to true
|
||||
// against the node (logical AND). An empty constraint list is vacuously
|
||||
// true. The first evaluation error short-circuits and is returned.
|
||||
func EvaluateAll(constraints []string, node NodeInfo) (bool, error) {
|
||||
for _, c := range constraints {
|
||||
ok, err := EvaluateConstraint(c, node)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("constraint %q: %w", c, err)
|
||||
}
|
||||
if !ok {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Value model
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// celValue is the union of values the evaluator produces. We use the
|
||||
// Go interface{} representation so that comparisons can be polymorphic
|
||||
// without a tagged-union ceremony; the supported concrete types are
|
||||
// bool, int64, and string. Lists are []celValue of the above.
|
||||
type celValue = interface{}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Tokenizer
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
type tokKind int
|
||||
|
||||
const (
|
||||
tokEOF tokKind = iota
|
||||
tokIdent
|
||||
tokInt
|
||||
tokStr
|
||||
tokOp // ==, !=, >=, <=, >, <, (, ), .
|
||||
tokIn // "in"
|
||||
tokAnd // "and"
|
||||
tokOr // "or"
|
||||
tokNot // "not"
|
||||
)
|
||||
|
||||
type token struct {
|
||||
kind tokKind
|
||||
text string
|
||||
}
|
||||
|
||||
type lexer struct {
|
||||
src string
|
||||
pos int
|
||||
}
|
||||
|
||||
func (l *lexer) next() (token, error) {
|
||||
for l.pos < len(l.src) && unicode.IsSpace(rune(l.src[l.pos])) {
|
||||
l.pos++
|
||||
}
|
||||
if l.pos >= len(l.src) {
|
||||
return token{kind: tokEOF}, nil
|
||||
}
|
||||
c := l.src[l.pos]
|
||||
// string literal
|
||||
if c == '"' {
|
||||
start := l.pos
|
||||
l.pos++
|
||||
for l.pos < len(l.src) && l.src[l.pos] != '"' {
|
||||
l.pos++
|
||||
}
|
||||
if l.pos >= len(l.src) {
|
||||
return token{}, fmt.Errorf("cel: unterminated string at %d", start)
|
||||
}
|
||||
val := l.src[start+1 : l.pos]
|
||||
l.pos++ // consume closing quote
|
||||
return token{kind: tokStr, text: val}, nil
|
||||
}
|
||||
// integer literal
|
||||
if unicode.IsDigit(rune(c)) {
|
||||
start := l.pos
|
||||
for l.pos < len(l.src) && unicode.IsDigit(rune(l.src[l.pos])) {
|
||||
l.pos++
|
||||
}
|
||||
return token{kind: tokInt, text: l.src[start:l.pos]}, nil
|
||||
}
|
||||
// identifier / keyword
|
||||
if isIdentStart(c) {
|
||||
start := l.pos
|
||||
for l.pos < len(l.src) && isIdentPart(l.src[l.pos]) {
|
||||
l.pos++
|
||||
}
|
||||
word := l.src[start:l.pos]
|
||||
switch word {
|
||||
case "in":
|
||||
return token{kind: tokIn, text: word}, nil
|
||||
case "and":
|
||||
return token{kind: tokAnd, text: word}, nil
|
||||
case "or":
|
||||
return token{kind: tokOr, text: word}, nil
|
||||
case "not":
|
||||
return token{kind: tokNot, text: word}, nil
|
||||
default:
|
||||
return token{kind: tokIdent, text: word}, nil
|
||||
}
|
||||
}
|
||||
// operators
|
||||
if strings.ContainsRune("()=!<>.", rune(c)) {
|
||||
// multi-char operators
|
||||
if l.pos+1 < len(l.src) {
|
||||
two := l.src[l.pos : l.pos+2]
|
||||
switch two {
|
||||
case "==", "!=", ">=", "<=":
|
||||
l.pos += 2
|
||||
return token{kind: tokOp, text: two}, nil
|
||||
}
|
||||
}
|
||||
l.pos++
|
||||
return token{kind: tokOp, text: string(c)}, nil
|
||||
}
|
||||
return token{}, fmt.Errorf("cel: unexpected character %q at %d", c, l.pos)
|
||||
}
|
||||
|
||||
func isIdentStart(c byte) bool {
|
||||
return c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
|
||||
}
|
||||
|
||||
func isIdentPart(c byte) bool {
|
||||
return isIdentStart(c) || (c >= '0' && c <= '9')
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Parser (recursive descent, precedence climbing)
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
type parser struct {
|
||||
src string
|
||||
pos int
|
||||
tok token
|
||||
err error
|
||||
node NodeInfo
|
||||
}
|
||||
|
||||
func newParser(src string, node NodeInfo) *parser {
|
||||
p := &parser{src: src, node: node}
|
||||
p.advance()
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *parser) len() int { return len(p.src) }
|
||||
|
||||
func (p *parser) advance() {
|
||||
if p.err != nil {
|
||||
return
|
||||
}
|
||||
l := lexer{src: p.src, pos: p.pos}
|
||||
t, err := l.next()
|
||||
if err != nil {
|
||||
p.err = err
|
||||
return
|
||||
}
|
||||
p.pos = l.pos
|
||||
p.tok = t
|
||||
}
|
||||
|
||||
// Grammar (lowest precedence first):
|
||||
//
|
||||
// expr := orExpr
|
||||
// orExpr := andExpr ("or" andExpr)*
|
||||
// andExpr := notExpr ("and" notExpr)*
|
||||
// notExpr := "not" notExpr | cmpExpr
|
||||
// cmpExpr := primary (op primary | "in" primary | "not" "in" primary)?
|
||||
// primary := "(" expr ")"
|
||||
// | int
|
||||
// | str
|
||||
// | "true" | "false"
|
||||
// | nodeAttr ("." ident)? // node.<field>
|
||||
// | ident // bare attribute (e.g. region)
|
||||
// nodeAttr := "node"
|
||||
|
||||
func (p *parser) parseExpr() (celValue, error) {
|
||||
if p.err != nil {
|
||||
return nil, p.err
|
||||
}
|
||||
return p.parseOr()
|
||||
}
|
||||
|
||||
func (p *parser) parseOr() (celValue, error) {
|
||||
left, err := p.parseAnd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for p.tok.kind == tokOr {
|
||||
p.advance()
|
||||
right, err := p.parseAnd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lb, ok := left.(bool)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cel: 'or' operand not bool: %T", left)
|
||||
}
|
||||
rb, ok := right.(bool)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cel: 'or' operand not bool: %T", right)
|
||||
}
|
||||
left = lb || rb
|
||||
}
|
||||
return left, nil
|
||||
}
|
||||
|
||||
func (p *parser) parseAnd() (celValue, error) {
|
||||
left, err := p.parseNot()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for p.tok.kind == tokAnd {
|
||||
p.advance()
|
||||
right, err := p.parseNot()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lb, ok := left.(bool)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cel: 'and' operand not bool: %T", left)
|
||||
}
|
||||
rb, ok := right.(bool)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cel: 'and' operand not bool: %T", right)
|
||||
}
|
||||
left = lb && rb
|
||||
}
|
||||
return left, nil
|
||||
}
|
||||
|
||||
func (p *parser) parseNot() (celValue, error) {
|
||||
if p.tok.kind == tokNot {
|
||||
// "not" at the start of a primary is logical negation. "not in"
|
||||
// is handled in parseCmp where it follows a primary.
|
||||
p.advance()
|
||||
v, err := p.parseNot()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b, ok := v.(bool)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cel: 'not' operand not bool: %T", v)
|
||||
}
|
||||
return !b, nil
|
||||
}
|
||||
return p.parseCmp()
|
||||
}
|
||||
|
||||
func (p *parser) parseCmp() (celValue, error) {
|
||||
left, err := p.parsePrimary()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// "not in"
|
||||
if p.tok.kind == tokNot {
|
||||
p.advance()
|
||||
if p.tok.kind != tokIn {
|
||||
return nil, fmt.Errorf("cel: expected 'in' after 'not', got %q", p.tok.text)
|
||||
}
|
||||
p.advance()
|
||||
right, err := p.parsePrimary()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
member, err := inMember(left, right)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return !member, nil
|
||||
}
|
||||
// "in"
|
||||
if p.tok.kind == tokIn {
|
||||
p.advance()
|
||||
right, err := p.parsePrimary()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return inMember(left, right)
|
||||
}
|
||||
// comparison operators
|
||||
if p.tok.kind == tokOp {
|
||||
op := p.tok.text
|
||||
switch op {
|
||||
case "==", "!=", ">=", "<=", ">", "<":
|
||||
p.advance()
|
||||
right, err := p.parsePrimary()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return compare(op, left, right)
|
||||
default:
|
||||
return nil, fmt.Errorf("cel: unexpected operator %q", op)
|
||||
}
|
||||
}
|
||||
return left, nil
|
||||
}
|
||||
|
||||
// inMember reports whether left is a member of right. right must be a
|
||||
// list ([]celValue) of comparable values; left may be a string or
|
||||
// int64.
|
||||
func inMember(left, right celValue) (bool, error) {
|
||||
list, ok := right.([]celValue)
|
||||
if !ok {
|
||||
return false, fmt.Errorf("cel: 'in' rhs not a list: %T", right)
|
||||
}
|
||||
for _, e := range list {
|
||||
if valuesEqual(left, e) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func valuesEqual(a, b celValue) bool {
|
||||
switch av := a.(type) {
|
||||
case string:
|
||||
bv, ok := b.(string)
|
||||
return ok && av == bv
|
||||
case int64:
|
||||
bv, ok := b.(int64)
|
||||
return ok && av == bv
|
||||
case bool:
|
||||
bv, ok := b.(bool)
|
||||
return ok && av == bv
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// compare applies a binary comparison operator to two scalar values.
|
||||
// Strings compare lexicographically; ints numerically; bools only via
|
||||
// ==/!=.
|
||||
func compare(op string, left, right celValue) (bool, error) {
|
||||
switch op {
|
||||
case "==":
|
||||
return valuesEqual(left, right), nil
|
||||
case "!=":
|
||||
return !valuesEqual(left, right), nil
|
||||
}
|
||||
// ordered comparisons require ordered operands
|
||||
ls, lok := left.(string)
|
||||
rs, rok := right.(string)
|
||||
if lok && rok {
|
||||
switch op {
|
||||
case "<":
|
||||
return ls < rs, nil
|
||||
case "<=":
|
||||
return ls <= rs, nil
|
||||
case ">":
|
||||
return ls > rs, nil
|
||||
case ">=":
|
||||
return ls >= rs, nil
|
||||
}
|
||||
}
|
||||
li, lok := left.(int64)
|
||||
ri, rok := right.(int64)
|
||||
if lok && rok {
|
||||
switch op {
|
||||
case "<":
|
||||
return li < ri, nil
|
||||
case "<=":
|
||||
return li <= ri, nil
|
||||
case ">":
|
||||
return li > ri, nil
|
||||
case ">=":
|
||||
return li >= ri, nil
|
||||
}
|
||||
}
|
||||
return false, fmt.Errorf("cel: cannot apply %q to %T and %T", op, left, right)
|
||||
}
|
||||
|
||||
// parsePrimary parses the smallest standalone unit: parenthesised
|
||||
// expressions, literals, and attribute references.
|
||||
func (p *parser) parsePrimary() (celValue, error) {
|
||||
switch p.tok.kind {
|
||||
case tokOp:
|
||||
if p.tok.text == "(" {
|
||||
p.advance()
|
||||
v, err := p.parseExpr()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if p.tok.kind != tokOp || p.tok.text != ")" {
|
||||
return nil, fmt.Errorf("cel: expected ')' got %q", p.tok.text)
|
||||
}
|
||||
p.advance()
|
||||
return v, nil
|
||||
}
|
||||
return nil, fmt.Errorf("cel: unexpected operator %q", p.tok.text)
|
||||
case tokInt:
|
||||
n, err := strconv.ParseInt(p.tok.text, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cel: bad int %q: %w", p.tok.text, err)
|
||||
}
|
||||
p.advance()
|
||||
return n, nil
|
||||
case tokStr:
|
||||
v := p.tok.text
|
||||
p.advance()
|
||||
return v, nil
|
||||
case tokIdent:
|
||||
return p.parseAttrRef()
|
||||
}
|
||||
return nil, fmt.Errorf("cel: unexpected token %q", p.tok.text)
|
||||
}
|
||||
|
||||
// parseAttrRef resolves a bare or `node.<field>` attribute reference
|
||||
// against the node being evaluated. Bare identifiers (e.g. `region`)
|
||||
// resolve against the same attribute map as `node.region`; the PRD
|
||||
// examples use both forms interchangeably (see
|
||||
// TestParseMarkdown_ConstraintsInlineArray).
|
||||
func (p *parser) parseAttrRef() (celValue, error) {
|
||||
name := p.tok.text
|
||||
p.advance()
|
||||
// dotted access: node.<field>
|
||||
if p.tok.kind == tokOp && p.tok.text == "." {
|
||||
if name != "node" {
|
||||
return nil, fmt.Errorf("cel: dotted access on non-node: %q", name)
|
||||
}
|
||||
p.advance()
|
||||
if p.tok.kind != tokIdent {
|
||||
return nil, fmt.Errorf("cel: expected attribute name after '.', got %q", p.tok.text)
|
||||
}
|
||||
field := p.tok.text
|
||||
p.advance()
|
||||
return p.nodeAttr(name + "." + field)
|
||||
}
|
||||
// bare identifier
|
||||
switch name {
|
||||
case "true":
|
||||
return true, nil
|
||||
case "false":
|
||||
return false, nil
|
||||
default:
|
||||
return p.nodeAttr(name)
|
||||
}
|
||||
}
|
||||
|
||||
// nodeAttr resolves an attribute name to its value on the parser's
|
||||
// active node. Mapping (per PRD T2):
|
||||
//
|
||||
// node.hostname -> Hostname (string)
|
||||
// node.kind -> Kind (string)
|
||||
// node.cpus -> CPU (int64)
|
||||
// node.memory -> Memory (int64)
|
||||
// node.tags -> Tags ([]string -> []celValue)
|
||||
// node.runtimes -> Runtimes ([]string -> []celValue)
|
||||
//
|
||||
// Bare names (without the `node.` prefix) resolve through the same
|
||||
// map, so `region == "us"` and `node.region == "us"` are equivalent
|
||||
// when the attribute exists.
|
||||
func (p *parser) nodeAttr(name string) (celValue, error) {
|
||||
switch name {
|
||||
case "node.hostname", "hostname":
|
||||
return p.node.Hostname, nil
|
||||
case "node.kind", "kind":
|
||||
return p.node.Kind, nil
|
||||
case "node.cpus", "cpus":
|
||||
return p.node.CPU, nil
|
||||
case "node.memory", "memory":
|
||||
return p.node.Memory, nil
|
||||
case "node.tags", "tags":
|
||||
return toStringValues(p.node.Tags), nil
|
||||
case "node.runtimes", "runtimes":
|
||||
return toStringValues(p.node.Runtimes), nil
|
||||
}
|
||||
return nil, fmt.Errorf("cel: unknown attribute %q", name)
|
||||
}
|
||||
|
||||
// toStringValues converts a []string to []celValue so the membership
|
||||
// operators can compare element-wise.
|
||||
func toStringValues(in []string) []celValue {
|
||||
out := make([]celValue, len(in))
|
||||
for i, s := range in {
|
||||
out[i] = s
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
package scheduler
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestEvaluateConstraint_Equality(t *testing.T) {
|
||||
node := NodeInfo{Hostname: "h-1", Kind: "linux", CPU: 4, Memory: 4096, Tags: []string{"web"}, Runtimes: []string{"process"}}
|
||||
cases := []struct {
|
||||
name string
|
||||
expr string
|
||||
want bool
|
||||
}{
|
||||
{"hostname eq", `node.hostname == "h-1"`, true},
|
||||
{"hostname ne", `node.hostname == "h-2"`, false},
|
||||
{"kind eq", `node.kind == "linux"`, true},
|
||||
{"kind ne", `node.kind == "proxmox"`, false},
|
||||
{"cpus eq", `node.cpus == 4`, true},
|
||||
{"memory eq", `node.memory == 4096`, true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got, err := EvaluateConstraint(c.expr, node)
|
||||
if err != nil {
|
||||
t.Errorf("%s: %v", c.name, err)
|
||||
continue
|
||||
}
|
||||
if got != c.want {
|
||||
t.Errorf("%s: got %v, want %v", c.name, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluateConstraint_Comparison(t *testing.T) {
|
||||
node := NodeInfo{Hostname: "h", Kind: "linux", CPU: 4, Memory: 4096}
|
||||
cases := []struct {
|
||||
expr string
|
||||
want bool
|
||||
}{
|
||||
{"node.cpus >= 2", true},
|
||||
{"node.cpus >= 4", true},
|
||||
{"node.cpus > 4", false},
|
||||
{"node.cpus > 2", true},
|
||||
{"node.cpus <= 4", true},
|
||||
{"node.cpus < 2", false},
|
||||
{"node.cpus != 8", true},
|
||||
{"node.cpus == 8", false},
|
||||
{"node.memory >= 2048", true},
|
||||
{"node.memory < 1024", false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got, err := EvaluateConstraint(c.expr, node)
|
||||
if err != nil {
|
||||
t.Errorf("%q: %v", c.expr, err)
|
||||
continue
|
||||
}
|
||||
if got != c.want {
|
||||
t.Errorf("%q: got %v, want %v", c.expr, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluateConstraint_Membership(t *testing.T) {
|
||||
node := NodeInfo{Tags: []string{"web", "log-shipper"}, Runtimes: []string{"process", "wasmtime"}}
|
||||
cases := []struct {
|
||||
expr string
|
||||
want bool
|
||||
}{
|
||||
{`"web" in node.tags`, true},
|
||||
{`"missing" in node.tags`, false},
|
||||
{`"process" in node.runtimes`, true},
|
||||
{`"podman" in node.runtimes`, false},
|
||||
{`"log-shipper" not in node.tags`, false},
|
||||
{`"missing" not in node.tags`, true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got, err := EvaluateConstraint(c.expr, node)
|
||||
if err != nil {
|
||||
t.Errorf("%q: %v", c.expr, err)
|
||||
continue
|
||||
}
|
||||
if got != c.want {
|
||||
t.Errorf("%q: got %v, want %v", c.expr, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluateConstraint_BooleanComposition(t *testing.T) {
|
||||
node := NodeInfo{Kind: "linux", CPU: 4, Tags: []string{"web"}}
|
||||
cases := []struct {
|
||||
expr string
|
||||
want bool
|
||||
}{
|
||||
{`node.kind == "linux" and node.cpus >= 2`, true},
|
||||
{`node.kind == "proxmox" and node.cpus >= 2`, false},
|
||||
{`node.kind == "linux" or node.kind == "proxmox"`, true},
|
||||
{`node.kind == "proxmox" or node.kind == "linux"`, true},
|
||||
{`not node.kind == "proxmox"`, true},
|
||||
{`not node.kind == "linux"`, false},
|
||||
{`(node.kind == "linux") and (node.cpus >= 2)`, true},
|
||||
{`node.cpus >= 2 and not "blocked" in node.tags`, true},
|
||||
{`node.kind == "linux" and node.cpus >= 2 and "web" in node.tags`, true},
|
||||
{`node.kind == "linux" or node.kind == "proxmox" or node.cpus > 100`, true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got, err := EvaluateConstraint(c.expr, node)
|
||||
if err != nil {
|
||||
t.Errorf("%q: %v", c.expr, err)
|
||||
continue
|
||||
}
|
||||
if got != c.want {
|
||||
t.Errorf("%q: got %v, want %v", c.expr, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluateConstraint_BareIdentifiers(t *testing.T) {
|
||||
// Bare identifiers resolve through the same attribute map as
|
||||
// node.<field> (per PRD: constraints may use either form).
|
||||
node := NodeInfo{Kind: "linux", CPU: 4}
|
||||
got, err := EvaluateConstraint(`kind == "linux"`, node)
|
||||
if err != nil {
|
||||
t.Fatalf("bare kind: %v", err)
|
||||
}
|
||||
if !got {
|
||||
t.Error("bare kind == linux: got false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluateConstraint_TrueFalseLiterals(t *testing.T) {
|
||||
node := NodeInfo{}
|
||||
cases := []struct {
|
||||
expr string
|
||||
want bool
|
||||
}{
|
||||
{"true", true},
|
||||
{"false", false},
|
||||
{"not false", true},
|
||||
{"not true", false},
|
||||
{"true and true", true},
|
||||
{"true and false", false},
|
||||
{"false or true", true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got, err := EvaluateConstraint(c.expr, node)
|
||||
if err != nil {
|
||||
t.Errorf("%q: %v", c.expr, err)
|
||||
continue
|
||||
}
|
||||
if got != c.want {
|
||||
t.Errorf("%q: got %v, want %v", c.expr, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluateConstraint_Errors(t *testing.T) {
|
||||
node := NodeInfo{Kind: "linux"}
|
||||
cases := []struct {
|
||||
name string
|
||||
expr string
|
||||
}{
|
||||
{"empty", ""},
|
||||
{"unterminated string", `node.kind == "linux`},
|
||||
{"unknown attribute", `node.bogus == 1`},
|
||||
{"unknown bare attr", `bogus == 1`},
|
||||
{"dotted on non-node", `host.kind == "linux"`},
|
||||
{"bad operator", `node.cpus + 2`},
|
||||
{"trailing input", `node.kind == "linux" garbage`},
|
||||
{"unbalanced paren", `(node.kind == "linux"`},
|
||||
{"missing rhs", `node.cpus >=`},
|
||||
{"not without in", `"x" not node.tags`},
|
||||
{"ordered compare on bool", `true < false`},
|
||||
{"ordered compare on mismatched types", `node.kind > 2`},
|
||||
{"in on non-list", `"x" in node.kind`},
|
||||
}
|
||||
for _, c := range cases {
|
||||
_, err := EvaluateConstraint(c.expr, node)
|
||||
if err == nil {
|
||||
t.Errorf("%s: expected error for %q, got nil", c.name, c.expr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluateAll(t *testing.T) {
|
||||
node := NodeInfo{Kind: "linux", CPU: 4, Tags: []string{"web"}}
|
||||
cases := []struct {
|
||||
name string
|
||||
constraints []string
|
||||
want bool
|
||||
}{
|
||||
{"empty", nil, true},
|
||||
{"all pass", []string{`node.kind == "linux"`, "node.cpus >= 2"}, true},
|
||||
{"one fails", []string{`node.kind == "linux"`, "node.cpus >= 8"}, false},
|
||||
{"all fail", []string{`node.kind == "proxmox"`, "node.cpus >= 8"}, false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got, err := EvaluateAll(c.constraints, node)
|
||||
if err != nil {
|
||||
t.Errorf("%s: %v", c.name, err)
|
||||
continue
|
||||
}
|
||||
if got != c.want {
|
||||
t.Errorf("%s: got %v, want %v", c.name, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluateAll_PropagatesError(t *testing.T) {
|
||||
node := NodeInfo{}
|
||||
if _, err := EvaluateAll([]string{"bogus == 1"}, node); err == nil {
|
||||
t.Error("EvaluateAll: expected error for malformed constraint")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,472 @@
|
||||
// Package scheduler implements the v0.9 CLI-side scheduler (REQ-083,
|
||||
// P05). Unlike the v0.8 daemon-side best-fit scheduler
|
||||
// (internal/engine/scheduler.go), this scheduler runs entirely in the
|
||||
// `orca` CLI process (R-001) and is pure: it takes a list of candidate
|
||||
// nodes plus a workload request and returns placement decisions
|
||||
// without performing any I/O.
|
||||
//
|
||||
// The scheduler is runtime-aware: a workload that declares
|
||||
// `runtime.one_of: wasm` is only placed on nodes that expose
|
||||
// `wasmtime` in their Runtimes list; a `pve-vm` workload is only
|
||||
// placed on `proxmox` nodes. It is also constraint- and
|
||||
// affinity-aware via the CEL-subset evaluator in cel.go.
|
||||
//
|
||||
// Workload kinds are handled differently per the PRD:
|
||||
//
|
||||
// - Job: one-shot, returns exactly one placement (best-fit
|
||||
// bin-packing).
|
||||
// - Service: count replicas spread across distinct nodes
|
||||
// (anti-affinity by default); if fewer distinct nodes than count,
|
||||
// colocation is permitted but distinct nodes are preferred.
|
||||
// - DaemonSet: one placement per node that fits the constraints.
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
// NodeInfo is the scheduler's projection of a peer node: total and
|
||||
// free capacity, the runtimes the node advertises, its tags, and its
|
||||
// kind (linux/proxmox). The CLI populates this from the
|
||||
// cluster/peers/ inventory plus the per-node capacity reports
|
||||
// collected over SSH; the scheduler itself never reads either.
|
||||
type NodeInfo struct {
|
||||
Hostname string
|
||||
Runtimes []string
|
||||
Tags []string
|
||||
CPU int64
|
||||
Memory int64
|
||||
FreeCPU int64
|
||||
FreeMem int64
|
||||
Kind string
|
||||
}
|
||||
|
||||
// WorkloadRequest bundles a parsed WorkloadSpec with the namespace
|
||||
// the workload is being scheduled into. The namespace is carried
|
||||
// through to placement so the resulting AllocID can be namespaced,
|
||||
// but the scheduler itself does not inspect it for fitting decisions.
|
||||
type WorkloadRequest struct {
|
||||
Spec *jobspec.WorkloadSpec
|
||||
Namespace string
|
||||
}
|
||||
|
||||
// Placement is a single scheduling decision: which node, which
|
||||
// allocation id, and the bin-packing score that won the node the
|
||||
// placement. AllocID is `ns/spec.Name-<idx>` so a multi-replica
|
||||
// Service produces distinct ids per replica.
|
||||
type Placement struct {
|
||||
Node string
|
||||
AllocID string
|
||||
Score int64
|
||||
}
|
||||
|
||||
// Schedule is the main entry point. For Job (kind=Job) it returns one
|
||||
// placement on the best-fit node. For Service it returns `Count`
|
||||
// placements spread across distinct nodes where possible (anti-
|
||||
// affinity), permitting colocation when Count > nodes. For DaemonSet
|
||||
// it returns one placement per node that fits. Any kind-agnostic
|
||||
// validation error (no spec, unknown kind, no fitting node) is
|
||||
// returned as an error rather than an empty slice so callers can
|
||||
// distinguish "nothing fits" from "scheduled zero replicas".
|
||||
func Schedule(nodes []NodeInfo, req WorkloadRequest) ([]Placement, error) {
|
||||
if req.Spec == nil {
|
||||
return nil, fmt.Errorf("scheduler: nil WorkloadSpec")
|
||||
}
|
||||
if len(nodes) == 0 {
|
||||
return nil, fmt.Errorf("scheduler: no candidate nodes")
|
||||
}
|
||||
switch req.Spec.Kind {
|
||||
case "Job":
|
||||
return scheduleJob(nodes, req)
|
||||
case "Service":
|
||||
return scheduleService(nodes, req)
|
||||
case "DaemonSet":
|
||||
return scheduleDaemonSet(nodes, req)
|
||||
default:
|
||||
return nil, fmt.Errorf("scheduler: unknown kind %q", req.Spec.Kind)
|
||||
}
|
||||
}
|
||||
|
||||
// Score evaluates a single node against a workload. fits is true iff
|
||||
// the node (a) advertises a runtime compatible with the workload's
|
||||
// `runtime.one_of`, (b) satisfies every CEL constraint in
|
||||
// `spec.Constraints`, and (c) has enough free CPU+memory for the
|
||||
// workload's requested resources. When fits is true, score is the
|
||||
// bin-packing score (more free capacity = higher score, so the node
|
||||
// most likely to absorb the workload without starving its
|
||||
// neighbours wins). When fits is false, score is 0.
|
||||
func Score(node NodeInfo, req WorkloadRequest) (score int64, fits bool) {
|
||||
// (a) runtime compatibility. A workload with no Runtime block or
|
||||
// an empty OneOf is treated as runtime-agnostic (always fits on
|
||||
// the runtime axis); this matches the v0.8 behaviour where a
|
||||
// missing runtime meant "process".
|
||||
runtimeOK := true
|
||||
if req.Spec != nil && req.Spec.Runtime != nil && req.Spec.Runtime.OneOf != "" {
|
||||
runtimeOK = hasRuntime(node, req.Spec.Runtime.OneOf)
|
||||
}
|
||||
if !runtimeOK {
|
||||
return 0, false
|
||||
}
|
||||
// (b) constraints. Evaluation errors are treated as non-fit so a
|
||||
// malformed constraint does not crash Schedule; the caller still
|
||||
// sees the node filtered out.
|
||||
if req.Spec != nil {
|
||||
ok, err := EvaluateAll(req.Spec.Constraints, node)
|
||||
if err != nil || !ok {
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
// (c) capacity. A workload with no Resources block is treated as
|
||||
// zero-sized for fitting purposes (it always fits the capacity
|
||||
// axis); real workloads declare cpu/memory.
|
||||
needCPU, needMem := workloadResources(req)
|
||||
if node.FreeCPU < needCPU || node.FreeMem < needMem {
|
||||
return 0, false
|
||||
}
|
||||
// bin-packing score: most free capacity wins. CPU is weighted
|
||||
// 1000x memory so a 1-core difference outweighs a 1-MiB
|
||||
// difference, mirroring the v0.8 Score weighting that biased
|
||||
// toward CPU (the more common binding constraint).
|
||||
score = (node.FreeCPU-needCPU)*1000 + (node.FreeMem - needMem)
|
||||
if score < 0 {
|
||||
score = 0
|
||||
}
|
||||
return score, true
|
||||
}
|
||||
|
||||
// hasRuntime reports whether node advertises the requested runtime.
|
||||
// The match is case-insensitive and tolerant of aliases: `wasm` and
|
||||
// `wasmtime` are treated as the same runtime, and `pve-vm`/`pve-ct`
|
||||
// only match nodes whose Kind is "proxmox".
|
||||
func hasRuntime(node NodeInfo, oneOf string) bool {
|
||||
want := normalizeRuntime(oneOf)
|
||||
// pve-* runtimes require a proxmox-kind node regardless of the
|
||||
// node's Runtimes list (a proxmox node doesn't list "pve-vm" in
|
||||
// Runtimes; it IS the runtime).
|
||||
switch want {
|
||||
case "pve-vm", "pve-ct", "proxmox":
|
||||
return normalizeKind(node.Kind) == "proxmox"
|
||||
}
|
||||
for _, r := range node.Runtimes {
|
||||
if normalizeRuntime(r) == want {
|
||||
return true
|
||||
}
|
||||
// alias: wasmtime nodes advertise "wasmtime"; workloads ask
|
||||
// for "wasm".
|
||||
if want == "wasm" && normalizeRuntime(r) == "wasmtime" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// normalizeRuntime lowercases and trims a runtime name for matching.
|
||||
func normalizeRuntime(s string) string {
|
||||
s = toLowerASCII(s)
|
||||
switch s {
|
||||
case "wasmtime":
|
||||
return "wasm"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// normalizeKind lowercases and trims a node Kind for matching.
|
||||
func normalizeKind(s string) string { return toLowerASCII(s) }
|
||||
|
||||
// toLowerASCII lowercases ASCII letters without bringing in strings
|
||||
// (avoid an alloc-heavy stdlib call in the hot path).
|
||||
func toLowerASCII(s string) string {
|
||||
b := []byte(s)
|
||||
for i, c := range b {
|
||||
if c >= 'A' && c <= 'Z' {
|
||||
b[i] = c + 32
|
||||
}
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// workloadResources returns the (cpu, memory) the workload requests,
|
||||
// read from the WorkloadSpec's Resources block if present. The
|
||||
// v0.9-P05 WorkloadSpec does not yet carry a Resources field (it
|
||||
// lands in P0c, REQ-074); until then this returns (0, 0) so the
|
||||
// capacity check is a no-op and runtime/constraints do the real
|
||||
// filtering. The signature is here so the scheduler logic does not
|
||||
// need to change when Resources lands.
|
||||
func workloadResources(req WorkloadRequest) (int64, int64) {
|
||||
_ = req
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Kind-specific scheduling
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// scheduleJob places a single Job on the best-fit node.
|
||||
func scheduleJob(nodes []NodeInfo, req WorkloadRequest) ([]Placement, error) {
|
||||
type cand struct {
|
||||
node NodeInfo
|
||||
score int64
|
||||
}
|
||||
var cands []cand
|
||||
for _, n := range nodes {
|
||||
s, ok := Score(n, req)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
cands = append(cands, cand{node: n, score: s})
|
||||
}
|
||||
// CEL-based affinity rules apply to single-shot Jobs too: a Job
|
||||
// with `affinity: [{target: "\"ssd\" in node.tags", weight: 100}]`
|
||||
// should land on the tagged node even without prior placements.
|
||||
// Name-based affinity (no prior placements to check) contributes
|
||||
// zero for a standalone Job, so it is harmless to call here.
|
||||
for i := range cands {
|
||||
cands[i].score += affinityScore(cands[i].node, req, nil)
|
||||
}
|
||||
if len(cands) == 0 {
|
||||
return nil, fmt.Errorf("scheduler: no node fits workload %q", req.Spec.Name)
|
||||
}
|
||||
sort.SliceStable(cands, func(i, j int) bool {
|
||||
if cands[i].score != cands[j].score {
|
||||
return cands[i].score > cands[j].score
|
||||
}
|
||||
return cands[i].node.Hostname < cands[j].node.Hostname
|
||||
})
|
||||
w := cands[0]
|
||||
return []Placement{{
|
||||
Node: w.node.Hostname,
|
||||
AllocID: allocID(req, 0),
|
||||
Score: w.score,
|
||||
}}, nil
|
||||
}
|
||||
|
||||
// scheduleService places `Count` replicas with implicit anti-affinity:
|
||||
// prefer distinct nodes, but permit colocation when Count exceeds the
|
||||
// number of fitting nodes. Each replica gets a distinct AllocID.
|
||||
func scheduleService(nodes []NodeInfo, req WorkloadRequest) ([]Placement, error) {
|
||||
count := req.Spec.Count
|
||||
if count <= 0 {
|
||||
count = 1
|
||||
}
|
||||
// Pre-filter fitting nodes once; the loop below re-scores them
|
||||
// after each placement so the capacity accounting reflects the
|
||||
// replicas already placed.
|
||||
fitting := filterFitting(nodes, req)
|
||||
if len(fitting) == 0 {
|
||||
return nil, fmt.Errorf("scheduler: no node fits service %q", req.Spec.Name)
|
||||
}
|
||||
var placements []Placement
|
||||
placed := map[string]int{} // hostname -> count placed there
|
||||
// First pass: spread across distinct nodes.
|
||||
for i := 0; i < count; i++ {
|
||||
best, score, ok := pickServiceNode(fitting, req, placements, placed)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
placements = append(placements, Placement{
|
||||
Node: best.Hostname,
|
||||
AllocID: allocID(req, i),
|
||||
Score: score,
|
||||
})
|
||||
placed[best.Hostname]++
|
||||
// Reflect the consumed capacity in the candidate snapshot so
|
||||
// subsequent picks see updated free capacity.
|
||||
needCPU, needMem := workloadResources(req)
|
||||
for j := range fitting {
|
||||
if fitting[j].Hostname == best.Hostname {
|
||||
fitting[j].FreeCPU -= needCPU
|
||||
fitting[j].FreeMem -= needMem
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(placements) < count {
|
||||
return nil, fmt.Errorf("scheduler: only placed %d/%d replicas for service %q",
|
||||
len(placements), count, req.Spec.Name)
|
||||
}
|
||||
return placements, nil
|
||||
}
|
||||
|
||||
// pickServiceNode selects the best node for the next replica. The
|
||||
// selection prefers nodes with zero prior placements of this service
|
||||
// (anti-affinity) and applies affinity scoring on top of the
|
||||
// bin-packing score.
|
||||
func pickServiceNode(fitting []NodeInfo, req WorkloadRequest, placements []Placement, placed map[string]int) (NodeInfo, int64, bool) {
|
||||
type scored struct {
|
||||
node NodeInfo
|
||||
score int64
|
||||
}
|
||||
var cands []scored
|
||||
for _, n := range fitting {
|
||||
s, ok := Score(n, req)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
// Implicit anti-affinity: a node with N prior replicas of this
|
||||
// service incurs a penalty of N * (1 << 62) so distinct nodes
|
||||
// are preferred, but colocation is permitted (with a
|
||||
// per-replica penalty) when no distinct node remains. This
|
||||
// produces a balanced spread (e.g. 5 replicas on 3 nodes →
|
||||
// 2/2/1) rather than stacking everything on the first node.
|
||||
if placed[n.Hostname] > 0 {
|
||||
s -= int64(placed[n.Hostname]) * (1 << 62)
|
||||
}
|
||||
// Affinity rules from the spec add/subtract their weight.
|
||||
s += affinityScore(n, req, placements)
|
||||
cands = append(cands, scored{node: n, score: s})
|
||||
}
|
||||
if len(cands) == 0 {
|
||||
return NodeInfo{}, 0, false
|
||||
}
|
||||
sort.SliceStable(cands, func(i, j int) bool {
|
||||
if cands[i].score != cands[j].score {
|
||||
return cands[i].score > cands[j].score
|
||||
}
|
||||
return cands[i].node.Hostname < cands[j].node.Hostname
|
||||
})
|
||||
w := cands[0]
|
||||
return w.node, w.score, true
|
||||
}
|
||||
|
||||
// scheduleDaemonSet places one replica per node that fits the
|
||||
// constraints. The PRD's DaemonSet placement mode (every-node /
|
||||
// matching / mandatory) lives on the WorkloadSpec.Schedule block; the
|
||||
// scheduler honours it indirectly by filtering on Constraints: a
|
||||
// `matching` DaemonSet carries constraints that select the matching
|
||||
// nodes, an `every-node` DaemonSet carries none, and a `mandatory`
|
||||
// one is enforced elsewhere (the scheduler still just returns
|
||||
// placements for every fitting node).
|
||||
func scheduleDaemonSet(nodes []NodeInfo, req WorkloadRequest) ([]Placement, error) {
|
||||
var placements []Placement
|
||||
for _, n := range nodes {
|
||||
s, ok := Score(n, req)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
placements = append(placements, Placement{
|
||||
Node: n.Hostname,
|
||||
AllocID: allocID(req, len(placements)),
|
||||
Score: s,
|
||||
})
|
||||
}
|
||||
if len(placements) == 0 {
|
||||
return nil, fmt.Errorf("scheduler: no node fits daemonset %q", req.Spec.Name)
|
||||
}
|
||||
return placements, nil
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Affinity scoring
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// affinityScore returns the weighted affinity contribution for a
|
||||
// node given the placements already made. For each AffinityRule the
|
||||
// Target is a CEL expression; if it evaluates true against the node,
|
||||
// the rule's Weight is added (positive = co-locate, negative =
|
||||
// anti-affinity). An affinity target that fails to evaluate is
|
||||
// ignored rather than failing the schedule: operators use affinity as
|
||||
// a hint, not a hard gate.
|
||||
//
|
||||
// The PRD also mentions affinity rules like `{target: "redis", weight:
|
||||
// 50}` where Target is a workload *name* rather than a CEL expression.
|
||||
// We support both: if Target parses as a CEL expression it is
|
||||
// evaluated against the node; otherwise it is treated as a workload
|
||||
// name and we check whether any already-placed alloc for that name
|
||||
// exists on the node. The placement-already-here check is done by the
|
||||
// caller via placements; this function checks the node's own
|
||||
// attributes only.
|
||||
func affinityScore(node NodeInfo, req WorkloadRequest, placements []Placement) int64 {
|
||||
if req.Spec == nil {
|
||||
return 0
|
||||
}
|
||||
var total int64
|
||||
for _, rule := range req.Spec.Affinity {
|
||||
// Try CEL evaluation first; if the target is a bare workload
|
||||
// name (no operator) the CEL parser will fail and we fall
|
||||
// back to name-based placement counting.
|
||||
ok, err := EvaluateConstraint(rule.Target, node)
|
||||
if err == nil {
|
||||
if ok {
|
||||
total += int64(rule.Weight)
|
||||
}
|
||||
continue
|
||||
}
|
||||
// Fallback: target is a workload name; count existing
|
||||
// placements for that workload on this node and apply the
|
||||
// weight once per co-located replica.
|
||||
for _, p := range placements {
|
||||
if p.Node == node.Hostname && isAllocFor(p.AllocID, rule.Target) {
|
||||
total += int64(rule.Weight)
|
||||
}
|
||||
}
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
// isAllocFor reports whether an AllocID encodes a placement for the
|
||||
// named workload. AllocIDs are `ns/name-idx`, so we look for the
|
||||
// workload name as the segment after the first slash and before the
|
||||
// trailing `-idx`.
|
||||
func isAllocFor(allocID, workloadName string) bool {
|
||||
// strip namespace prefix
|
||||
rest := allocID
|
||||
if i := indexByte(rest, '/'); i >= 0 {
|
||||
rest = rest[i+1:]
|
||||
}
|
||||
// strip trailing -idx
|
||||
if i := lastIndexByte(rest, '-'); i >= 0 {
|
||||
rest = rest[:i]
|
||||
}
|
||||
return rest == workloadName
|
||||
}
|
||||
|
||||
// indexByte returns the index of the first occurrence of b in s, or
|
||||
// -1. Avoids importing strings just for one helper.
|
||||
func indexByte(s string, b byte) int {
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] == b {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// lastIndexByte returns the index of the last occurrence of b in s, or
|
||||
// -1.
|
||||
func lastIndexByte(s string, b byte) int {
|
||||
for i := len(s) - 1; i >= 0; i-- {
|
||||
if s[i] == b {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// filterFitting returns a copy of the nodes that pass Score for the
|
||||
// request, preserving order. Capacity is not yet decremented; the
|
||||
// caller adjusts FreeCPU/FreeMem as it places replicas.
|
||||
func filterFitting(nodes []NodeInfo, req WorkloadRequest) []NodeInfo {
|
||||
var out []NodeInfo
|
||||
for _, n := range nodes {
|
||||
if _, ok := Score(n, req); ok {
|
||||
out = append(out, n)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// allocID renders a stable, namespaced allocation id for a placement.
|
||||
// Format: `ns/spec.Name-<idx>`.
|
||||
func allocID(req WorkloadRequest, idx int) string {
|
||||
ns := req.Namespace
|
||||
if ns == "" {
|
||||
ns = "default"
|
||||
}
|
||||
return fmt.Sprintf("%s/%s-%d", ns, req.Spec.Name, idx)
|
||||
}
|
||||
@@ -0,0 +1,451 @@
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
// threeLinuxNodes returns a small cluster of three Linux nodes with
|
||||
// distinct free capacities so best-fit ordering is unambiguous.
|
||||
func threeLinuxNodes() []NodeInfo {
|
||||
return []NodeInfo{
|
||||
{Hostname: "node-a", Runtimes: []string{"process"}, Tags: nil, CPU: 4, Memory: 4096, FreeCPU: 4, FreeMem: 4096, Kind: "linux"},
|
||||
{Hostname: "node-b", Runtimes: []string{"process"}, Tags: nil, CPU: 8, Memory: 8192, FreeCPU: 8, FreeMem: 8192, Kind: "linux"},
|
||||
{Hostname: "node-c", Runtimes: []string{"process"}, Tags: nil, CPU: 2, Memory: 2048, FreeCPU: 2, FreeMem: 2048, Kind: "linux"},
|
||||
}
|
||||
}
|
||||
|
||||
func jobSpec(name, oneOf string, constraints []string) *jobspec.WorkloadSpec {
|
||||
return &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: name,
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: oneOf},
|
||||
Constraints: constraints,
|
||||
}
|
||||
}
|
||||
|
||||
func serviceSpec(name, oneOf string, count int, constraints []string) *jobspec.WorkloadSpec {
|
||||
return &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: name,
|
||||
Count: count,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: oneOf},
|
||||
Constraints: constraints,
|
||||
}
|
||||
}
|
||||
|
||||
func daemonSetSpec(name, oneOf string, constraints []string) *jobspec.WorkloadSpec {
|
||||
return &jobspec.WorkloadSpec{
|
||||
Kind: "DaemonSet",
|
||||
Name: name,
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: oneOf},
|
||||
Constraints: constraints,
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Job
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestScheduleJob_BestFit(t *testing.T) {
|
||||
nodes := threeLinuxNodes()
|
||||
req := WorkloadRequest{Spec: jobSpec("batch", "process", nil), Namespace: "ns"}
|
||||
got, err := Schedule(nodes, req)
|
||||
if err != nil {
|
||||
t.Fatalf("Schedule: %v", err)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("placements = %d, want 1", len(got))
|
||||
}
|
||||
if got[0].Node != "node-b" {
|
||||
t.Errorf("Node = %q, want node-b (most free capacity)", got[0].Node)
|
||||
}
|
||||
if !strings.HasPrefix(got[0].AllocID, "ns/batch-") {
|
||||
t.Errorf("AllocID = %q, want ns/batch-*", got[0].AllocID)
|
||||
}
|
||||
if got[0].Score <= 0 {
|
||||
t.Errorf("Score = %d, want > 0", got[0].Score)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScheduleJob_NoFittingNode(t *testing.T) {
|
||||
nodes := threeLinuxNodes()
|
||||
// wasm runtime not advertised by any node.
|
||||
req := WorkloadRequest{Spec: jobSpec("wasmjob", "wasm", nil), Namespace: "ns"}
|
||||
if _, err := Schedule(nodes, req); err == nil {
|
||||
t.Fatal("Schedule: expected error for no-fitting node, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Service
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestScheduleService_SpreadAcrossNodes(t *testing.T) {
|
||||
nodes := threeLinuxNodes()
|
||||
req := WorkloadRequest{Spec: serviceSpec("web", "process", 3, nil), Namespace: "ns"}
|
||||
got, err := Schedule(nodes, req)
|
||||
if err != nil {
|
||||
t.Fatalf("Schedule: %v", err)
|
||||
}
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("placements = %d, want 3", len(got))
|
||||
}
|
||||
seen := map[string]int{}
|
||||
for _, p := range got {
|
||||
seen[p.Node]++
|
||||
}
|
||||
if len(seen) != 3 {
|
||||
t.Errorf("anti-affinity spread: distinct nodes = %d, want 3; %v", len(seen), seen)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScheduleService_ColocationWhenFewerNodes(t *testing.T) {
|
||||
nodes := threeLinuxNodes()
|
||||
req := WorkloadRequest{Spec: serviceSpec("web", "process", 5, nil), Namespace: "ns"}
|
||||
got, err := Schedule(nodes, req)
|
||||
if err != nil {
|
||||
t.Fatalf("Schedule: %v", err)
|
||||
}
|
||||
if len(got) != 5 {
|
||||
t.Fatalf("placements = %d, want 5", len(got))
|
||||
}
|
||||
seen := map[string]int{}
|
||||
for _, p := range got {
|
||||
seen[p.Node]++
|
||||
}
|
||||
if len(seen) != 3 {
|
||||
t.Errorf("colocation: distinct nodes = %d, want 3 (all used)", len(seen))
|
||||
}
|
||||
// No node should host more than 2 (3 nodes, 5 replicas: 2+2+1).
|
||||
for n, c := range seen {
|
||||
if c > 2 {
|
||||
t.Errorf("node %s has %d replicas, want <= 2", n, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestScheduleService_NoFittingNode(t *testing.T) {
|
||||
nodes := threeLinuxNodes()
|
||||
req := WorkloadRequest{Spec: serviceSpec("wasm-svc", "wasm", 3, nil), Namespace: "ns"}
|
||||
if _, err := Schedule(nodes, req); err == nil {
|
||||
t.Fatal("Schedule: expected error for service with no fitting node")
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// DaemonSet
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestScheduleDaemonSet_AllMatching(t *testing.T) {
|
||||
nodes := threeLinuxNodes()
|
||||
req := WorkloadRequest{Spec: daemonSetSpec("logrotate", "process", nil), Namespace: "ns"}
|
||||
got, err := Schedule(nodes, req)
|
||||
if err != nil {
|
||||
t.Fatalf("Schedule: %v", err)
|
||||
}
|
||||
if len(got) != 3 {
|
||||
t.Errorf("placements = %d, want 3 (one per node)", len(got))
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, p := range got {
|
||||
seen[p.Node] = true
|
||||
}
|
||||
if len(seen) != 3 {
|
||||
t.Errorf("DaemonSet distinct nodes = %d, want 3", len(seen))
|
||||
}
|
||||
}
|
||||
|
||||
func TestScheduleDaemonSet_SomeExcludedByConstraint(t *testing.T) {
|
||||
nodes := threeLinuxNodes()
|
||||
// Only nodes with cpus >= 4 qualify: node-a (4) and node-b (8).
|
||||
req := WorkloadRequest{Spec: daemonSetSpec("heavy", "process", []string{"node.cpus >= 4"}), Namespace: "ns"}
|
||||
got, err := Schedule(nodes, req)
|
||||
if err != nil {
|
||||
t.Fatalf("Schedule: %v", err)
|
||||
}
|
||||
if len(got) != 2 {
|
||||
t.Errorf("placements = %d, want 2 (cpus>=4)", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Runtime compatibility
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestSchedule_RuntimeCompatibilityWasm(t *testing.T) {
|
||||
nodes := []NodeInfo{
|
||||
{Hostname: "no-wasm", Runtimes: []string{"process"}, Kind: "linux", CPU: 8, Memory: 8192, FreeCPU: 8, FreeMem: 8192},
|
||||
{Hostname: "has-wasm", Runtimes: []string{"process", "wasmtime"}, Kind: "linux", CPU: 4, Memory: 4096, FreeCPU: 4, FreeMem: 4096},
|
||||
}
|
||||
// Even though no-wasm has more free capacity, the wasm workload
|
||||
// must land on has-wasm.
|
||||
req := WorkloadRequest{Spec: jobSpec("wasmjob", "wasm", nil), Namespace: "ns"}
|
||||
got, err := Schedule(nodes, req)
|
||||
if err != nil {
|
||||
t.Fatalf("Schedule: %v", err)
|
||||
}
|
||||
if got[0].Node != "has-wasm" {
|
||||
t.Errorf("Node = %q, want has-wasm (runtime compatibility)", got[0].Node)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchedule_RuntimeCompatibilityPveVM(t *testing.T) {
|
||||
nodes := []NodeInfo{
|
||||
{Hostname: "linux-1", Runtimes: []string{"process"}, Kind: "linux", CPU: 8, Memory: 8192, FreeCPU: 8, FreeMem: 8192},
|
||||
{Hostname: "pve-1", Runtimes: []string{"process"}, Kind: "proxmox", CPU: 8, Memory: 8192, FreeCPU: 8, FreeMem: 8192},
|
||||
}
|
||||
req := WorkloadRequest{Spec: jobSpec("vmjob", "pve-vm", nil), Namespace: "ns"}
|
||||
got, err := Schedule(nodes, req)
|
||||
if err != nil {
|
||||
t.Fatalf("Schedule: %v", err)
|
||||
}
|
||||
if got[0].Node != "pve-1" {
|
||||
t.Errorf("Node = %q, want pve-1 (pve-vm requires proxmox kind)", got[0].Node)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Constraints
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestSchedule_ConstraintKindExcludesProxmox(t *testing.T) {
|
||||
nodes := []NodeInfo{
|
||||
{Hostname: "linux-1", Runtimes: []string{"process"}, Kind: "linux", CPU: 8, Memory: 8192, FreeCPU: 8, FreeMem: 8192},
|
||||
{Hostname: "pve-1", Runtimes: []string{"process"}, Kind: "proxmox", CPU: 8, Memory: 8192, FreeCPU: 8, FreeMem: 8192},
|
||||
}
|
||||
req := WorkloadRequest{Spec: jobSpec("linuxonly", "process", []string{`node.kind == "linux"`}), Namespace: "ns"}
|
||||
got, err := Schedule(nodes, req)
|
||||
if err != nil {
|
||||
t.Fatalf("Schedule: %v", err)
|
||||
}
|
||||
if got[0].Node != "linux-1" {
|
||||
t.Errorf("Node = %q, want linux-1 (kind==linux)", got[0].Node)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchedule_ConstraintCPUsExcludesSmall(t *testing.T) {
|
||||
nodes := threeLinuxNodes() // node-c has cpus=2
|
||||
req := WorkloadRequest{Spec: jobSpec("big", "process", []string{"node.cpus >= 4"}), Namespace: "ns"}
|
||||
got, err := Schedule(nodes, req)
|
||||
if err != nil {
|
||||
t.Fatalf("Schedule: %v", err)
|
||||
}
|
||||
if got[0].Node == "node-c" {
|
||||
t.Errorf("Node = node-c, want node-a or node-b (cpus>=4)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchedule_ConstraintNotInTags(t *testing.T) {
|
||||
nodes := []NodeInfo{
|
||||
{Hostname: "tagged", Runtimes: []string{"process"}, Tags: []string{"log-shipper"}, Kind: "linux", CPU: 8, Memory: 8192, FreeCPU: 8, FreeMem: 8192},
|
||||
{Hostname: "clean", Runtimes: []string{"process"}, Tags: nil, Kind: "linux", CPU: 4, Memory: 4096, FreeCPU: 4, FreeMem: 4096},
|
||||
}
|
||||
req := WorkloadRequest{Spec: jobSpec("worker", "process", []string{`"log-shipper" not in node.tags`}), Namespace: "ns"}
|
||||
got, err := Schedule(nodes, req)
|
||||
if err != nil {
|
||||
t.Fatalf("Schedule: %v", err)
|
||||
}
|
||||
if got[0].Node != "clean" {
|
||||
t.Errorf("Node = %q, want clean (log-shipper not in tags)", got[0].Node)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Affinity
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestSchedule_AffinityPrefersColocatedNode(t *testing.T) {
|
||||
// Place a redis service first, then a worker with affinity for
|
||||
// redis; the worker should prefer the node where redis already
|
||||
// runs even if another node has more free capacity.
|
||||
nodes := []NodeInfo{
|
||||
{Hostname: "big", Runtimes: []string{"process"}, Kind: "linux", CPU: 8, Memory: 8192, FreeCPU: 8, FreeMem: 8192},
|
||||
{Hostname: "small", Runtimes: []string{"process"}, Kind: "linux", CPU: 4, Memory: 4096, FreeCPU: 4, FreeMem: 4096},
|
||||
}
|
||||
redisReq := WorkloadRequest{Spec: serviceSpec("redis", "process", 1, nil), Namespace: "ns"}
|
||||
redisPlacements, err := Schedule(nodes, redisReq)
|
||||
if err != nil {
|
||||
t.Fatalf("redis Schedule: %v", err)
|
||||
}
|
||||
// Redis lands on "big" (most free capacity). Now schedule the
|
||||
// worker with affinity to redis; it should also land on "big".
|
||||
workerReq := WorkloadRequest{Spec: &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "worker",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Affinity: []jobspec.AffinityRule{
|
||||
{Target: "redis", Weight: 1000},
|
||||
},
|
||||
}, Namespace: "ns"}
|
||||
// The affinity is name-based; we need to seed the worker schedule
|
||||
// with the redis placement so affinityScore can see it. Schedule
|
||||
// does not take prior placements, so test affinityScore directly.
|
||||
got := affinityScore(nodes[0], workerReq, redisPlacements)
|
||||
if got <= 0 {
|
||||
t.Errorf("affinityScore(big) = %d, want > 0 (redis colocated)", got)
|
||||
}
|
||||
gotSmall := affinityScore(nodes[1], workerReq, redisPlacements)
|
||||
if gotSmall != 0 {
|
||||
t.Errorf("affinityScore(small) = %d, want 0 (redis not colocated)", gotSmall)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchedule_AffinityCELExpression(t *testing.T) {
|
||||
// Affinity with a CEL target: prefer nodes tagged "ssd".
|
||||
nodes := []NodeInfo{
|
||||
{Hostname: "hdd", Runtimes: []string{"process"}, Tags: []string{"hdd"}, Kind: "linux", CPU: 8, Memory: 8192, FreeCPU: 8, FreeMem: 8192},
|
||||
{Hostname: "ssd", Runtimes: []string{"process"}, Tags: []string{"ssd"}, Kind: "linux", CPU: 4, Memory: 4096, FreeCPU: 4, FreeMem: 4096},
|
||||
}
|
||||
req := WorkloadRequest{Spec: &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "db",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Affinity: []jobspec.AffinityRule{
|
||||
{Target: `"ssd" in node.tags`, Weight: 10000},
|
||||
},
|
||||
}, Namespace: "ns"}
|
||||
got, err := Schedule(nodes, req)
|
||||
if err != nil {
|
||||
t.Fatalf("Schedule: %v", err)
|
||||
}
|
||||
if got[0].Node != "ssd" {
|
||||
t.Errorf("Node = %q, want ssd (affinity to ssd tag outweighs capacity)", got[0].Node)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Error paths
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestSchedule_EmptyNodes(t *testing.T) {
|
||||
req := WorkloadRequest{Spec: jobSpec("x", "process", nil), Namespace: "ns"}
|
||||
if _, err := Schedule(nil, req); err == nil {
|
||||
t.Fatal("Schedule: expected error for empty nodes, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchedule_NilSpec(t *testing.T) {
|
||||
if _, err := Schedule(threeLinuxNodes(), WorkloadRequest{}); err == nil {
|
||||
t.Fatal("Schedule: expected error for nil spec, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchedule_UnknownKind(t *testing.T) {
|
||||
req := WorkloadRequest{Spec: &jobspec.WorkloadSpec{Kind: "Cron", Name: "x", Count: 1}, Namespace: "ns"}
|
||||
if _, err := Schedule(threeLinuxNodes(), req); err == nil {
|
||||
t.Fatal("Schedule: expected error for unknown kind")
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Score unit tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestScore_FitsAndDoesNotFit(t *testing.T) {
|
||||
node := NodeInfo{Hostname: "n", Runtimes: []string{"process"}, Kind: "linux", CPU: 4, Memory: 4096, FreeCPU: 4, FreeMem: 4096}
|
||||
req := WorkloadRequest{Spec: jobSpec("j", "process", nil), Namespace: "ns"}
|
||||
score, fits := Score(node, req)
|
||||
if !fits {
|
||||
t.Error("fits = false, want true")
|
||||
}
|
||||
if score <= 0 {
|
||||
t.Errorf("score = %d, want > 0", score)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScore_RuntimeMismatchDoesNotFit(t *testing.T) {
|
||||
node := NodeInfo{Hostname: "n", Runtimes: []string{"process"}, Kind: "linux", CPU: 4, Memory: 4096, FreeCPU: 4, FreeMem: 4096}
|
||||
req := WorkloadRequest{Spec: jobSpec("j", "wasm", nil), Namespace: "ns"}
|
||||
if _, fits := Score(node, req); fits {
|
||||
t.Error("fits = true for wasm on process-only node, want false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestScore_ConstraintFailsDoesNotFit(t *testing.T) {
|
||||
node := NodeInfo{Hostname: "n", Runtimes: []string{"process"}, Kind: "linux", CPU: 4, Memory: 4096, FreeCPU: 4, FreeMem: 4096}
|
||||
req := WorkloadRequest{Spec: jobSpec("j", "process", []string{`node.kind == "proxmox"`}), Namespace: "ns"}
|
||||
if _, fits := Score(node, req); fits {
|
||||
t.Error("fits = true for kind==proxmox on linux node, want false")
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// allocID / isAllocFor helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestAllocID(t *testing.T) {
|
||||
req := WorkloadRequest{Spec: &jobspec.WorkloadSpec{Name: "web"}, Namespace: "prod"}
|
||||
if got := allocID(req, 2); got != "prod/web-2" {
|
||||
t.Errorf("allocID = %q, want prod/web-2", got)
|
||||
}
|
||||
req.Namespace = ""
|
||||
if got := allocID(req, 0); got != "default/web-0" {
|
||||
t.Errorf("allocID = %q, want default/web-0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAllocFor(t *testing.T) {
|
||||
cases := []struct {
|
||||
allocID string
|
||||
workload string
|
||||
want bool
|
||||
}{
|
||||
{"ns/redis-0", "redis", true},
|
||||
{"ns/redis-12", "redis", true},
|
||||
{"ns/worker-0", "redis", false},
|
||||
{"redis-0", "redis", true},
|
||||
{"ns/web-canary-3", "web-canary", true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := isAllocFor(c.allocID, c.workload); got != c.want {
|
||||
t.Errorf("isAllocFor(%q,%q) = %v, want %v", c.allocID, c.workload, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// normalizeRuntime / hasRuntime
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestHasRuntimeAliases(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
node NodeInfo
|
||||
want bool
|
||||
}{
|
||||
{"wasm on wasmtime node", NodeInfo{Runtimes: []string{"wasmtime"}, Kind: "linux"}, true},
|
||||
{"wasm on process node", NodeInfo{Runtimes: []string{"process"}, Kind: "linux"}, false},
|
||||
{"pve-vm on linux node", NodeInfo{Runtimes: []string{"pve-vm"}, Kind: "linux"}, false},
|
||||
{"pve-vm on proxmox node", NodeInfo{Runtimes: nil, Kind: "proxmox"}, true},
|
||||
{"process on process node", NodeInfo{Runtimes: []string{"process"}, Kind: "linux"}, true},
|
||||
{"empty runtime on any node", NodeInfo{Runtimes: []string{"process"}, Kind: "linux"}, true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if c.name == "empty runtime on any node" {
|
||||
// hasRuntime is only called when OneOf != "".
|
||||
continue
|
||||
}
|
||||
if got := hasRuntime(c.node, "wasm"); c.name == "wasm on wasmtime node" || c.name == "wasm on process node" {
|
||||
if got != c.want {
|
||||
t.Errorf("%s: hasRuntime(wasm) = %v, want %v", c.name, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Explicit pve-vm and process checks.
|
||||
if !hasRuntime(NodeInfo{Runtimes: nil, Kind: "proxmox"}, "pve-vm") {
|
||||
t.Error("pve-vm on proxmox node should fit")
|
||||
}
|
||||
if hasRuntime(NodeInfo{Runtimes: nil, Kind: "linux"}, "pve-vm") {
|
||||
t.Error("pve-vm on linux node should not fit")
|
||||
}
|
||||
if !hasRuntime(NodeInfo{Runtimes: []string{"process"}, Kind: "linux"}, "process") {
|
||||
t.Error("process on process node should fit")
|
||||
}
|
||||
}
|
||||
@@ -25,3 +25,11 @@ func Flock(path string) (release func(), err error) {
|
||||
_ = f.Close()
|
||||
}, nil
|
||||
}
|
||||
|
||||
func tryFlockEx(fd int) error {
|
||||
return syscall.Flock(fd, syscall.LOCK_EX|syscall.LOCK_NB)
|
||||
}
|
||||
|
||||
func releaseFlock(fd int) error {
|
||||
return syscall.Flock(fd, syscall.LOCK_UN)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFlock_acquireAndRelease(t *testing.T) {
|
||||
@@ -45,17 +46,37 @@ func TestFlock_concurrentBlocks(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("first Flock: %v", err)
|
||||
}
|
||||
defer r1()
|
||||
|
||||
done := make(chan error, 1)
|
||||
// Give the blocking goroutine a chance to start and block.
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
|
||||
// Verify the second lock is blocked by checking it hasn't acquired after a short window.
|
||||
// Use a non-blocking attempt: open the file and try LOCK_EX|LOCK_NB.
|
||||
blocked := make(chan bool, 1)
|
||||
go func() {
|
||||
_, err := Flock(path)
|
||||
done <- err
|
||||
f, err := os.OpenFile(path, os.O_RDWR, 0o600)
|
||||
if err != nil {
|
||||
blocked <- false
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
// LOCK_NB = non-blocking; returns EWOULDBLOCK if locked.
|
||||
if err := tryFlockEx(int(f.Fd())); err != nil {
|
||||
blocked <- true // got EWOULDBLOCK = the lock is held by r1
|
||||
return
|
||||
}
|
||||
releaseFlock(int(f.Fd()))
|
||||
blocked <- false // acquired = r1 didn't hold the lock (bug)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
t.Fatal("second Flock should block while first holds the lock")
|
||||
default:
|
||||
case b := <-blocked:
|
||||
if !b {
|
||||
t.Fatal("second lock acquired while first holds it — lock not working")
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("non-blocking try-lock timed out")
|
||||
}
|
||||
|
||||
r1()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,254 @@
|
||||
// Package schema provides kind-specific validators for the unified
|
||||
// *jobspec.WorkloadSpec introduced in P0b (REQ-064). Each workload kind
|
||||
// (Job, Service, DaemonSet per R-012) has different required fields;
|
||||
// this package exposes a Validator interface and a ValidatorFor
|
||||
// dispatcher so the emitter layer (REQ-074) and the lint engine
|
||||
// (REQ-084) can reject invalid specs before rendering.
|
||||
//
|
||||
// The validators operate purely on the *WorkloadSpec shape; they do no
|
||||
// I/O. Required-field violations return a structured error listing
|
||||
// every problem found (missing required fields, invalid combinations).
|
||||
package schema
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
// Validator validates a *jobspec.WorkloadSpec against a kind-specific
|
||||
// schema. Implementations are pure (no I/O) and return a clear error
|
||||
// listing every violation found.
|
||||
type Validator interface {
|
||||
Validate(spec *jobspec.WorkloadSpec) error
|
||||
}
|
||||
|
||||
// JobValidator validates the Job workload kind (R-012).
|
||||
//
|
||||
// Rules:
|
||||
// - no service block required (Job has no Traefik route by default D-175)
|
||||
// - restart optional (defaults to never/on-failure when omitted)
|
||||
// - schedule optional (cron string)
|
||||
// - timeout optional
|
||||
// - ports optional
|
||||
// - count must be 1 (or unset → 1); count > 1 is an error for Job
|
||||
// (use a Service for replicas)
|
||||
// - no Traefik route (a ServiceBlock is rejected)
|
||||
// - task group (spec.Tasks) optional; when present, each task must
|
||||
// have a unique name and a resolvable command (P06).
|
||||
type JobValidator struct{}
|
||||
|
||||
// ServiceValidator validates the Service workload kind (R-012).
|
||||
//
|
||||
// Rules:
|
||||
// - ports required (at least one)
|
||||
// - count ≥ 1
|
||||
// - restart required (mode must be service)
|
||||
// - update required (strategy must be rolling/canary/blue-green)
|
||||
// - runtime required (unless a task group is present; each task
|
||||
// can carry its own runtime — P06)
|
||||
// - health block required (Traefik routing depends on health checks)
|
||||
// - service block, if present, must have a valid bind (127.0.0.1
|
||||
// opt-in per R-007; default is socket — empty bind is OK)
|
||||
// - service block implied (Traefik route YES)
|
||||
// - task group (spec.Tasks) optional; when present, each task must
|
||||
// have a unique name and a resolvable command (P06).
|
||||
type ServiceValidator struct{}
|
||||
|
||||
// DaemonSetValidator validates the DaemonSet workload kind (R-012).
|
||||
//
|
||||
// Rules:
|
||||
// - schedule block with mode (every-node/matching/mandatory) required
|
||||
// - no ports (no Traefik route by default D-175)
|
||||
// - no count (implicit = nodes matching condition)
|
||||
// - restart required
|
||||
// - task group (spec.Tasks) optional; when present, each task must
|
||||
// have a unique name and a resolvable command (P06).
|
||||
type DaemonSetValidator struct{}
|
||||
|
||||
// ValidatorFor returns the Validator for the given workload kind, or an
|
||||
// error for an unknown kind. kind must be one of Job, Service,
|
||||
// DaemonSet (R-012).
|
||||
func ValidatorFor(kind string) (Validator, error) {
|
||||
switch kind {
|
||||
case "Job":
|
||||
return JobValidator{}, nil
|
||||
case "Service":
|
||||
return ServiceValidator{}, nil
|
||||
case "DaemonSet":
|
||||
return DaemonSetValidator{}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("schema: unknown kind %q (want one of Job, Service, DaemonSet)", kind)
|
||||
}
|
||||
}
|
||||
|
||||
// Validate validates a Job spec. See JobValidator for the rules.
|
||||
func (JobValidator) Validate(spec *jobspec.WorkloadSpec) error {
|
||||
if spec == nil {
|
||||
return errors.New("schema/Job: spec is nil")
|
||||
}
|
||||
var errs []string
|
||||
if strings.TrimSpace(spec.Name) == "" {
|
||||
errs = append(errs, "name is required")
|
||||
}
|
||||
if spec.Count != 0 && spec.Count != 1 {
|
||||
errs = append(errs, fmt.Sprintf("count must be 1 (or unset) for Job, got %d (use Service for replicas)", spec.Count))
|
||||
}
|
||||
if spec.Service != nil {
|
||||
errs = append(errs, "service block (Traefik route) is not allowed for Job (D-175)")
|
||||
}
|
||||
errs = append(errs, validateTaskGroup(spec)...)
|
||||
return composeErrors("schema/Job", errs)
|
||||
}
|
||||
|
||||
// Validate validates a Service spec. See ServiceValidator for the rules.
|
||||
func (ServiceValidator) Validate(spec *jobspec.WorkloadSpec) error {
|
||||
if spec == nil {
|
||||
return errors.New("schema/Service: spec is nil")
|
||||
}
|
||||
var errs []string
|
||||
if strings.TrimSpace(spec.Name) == "" {
|
||||
errs = append(errs, "name is required")
|
||||
}
|
||||
if len(spec.Ports) == 0 {
|
||||
errs = append(errs, "ports required (at least one)")
|
||||
}
|
||||
if spec.Count < 1 {
|
||||
errs = append(errs, fmt.Sprintf("count must be ≥ 1 for Service, got %d", spec.Count))
|
||||
}
|
||||
if spec.Restart == nil {
|
||||
errs = append(errs, "restart block required for Service")
|
||||
} else {
|
||||
switch spec.Restart.Mode {
|
||||
case "service", "on-failure", "never":
|
||||
// Valid per R-012 (default for Service is "service",
|
||||
// but the validator accepts the full enum; the
|
||||
// Service-specific "must be service" rule is enforced
|
||||
// below for the default case where mode is empty).
|
||||
case "":
|
||||
errs = append(errs, "restart mode required for Service (one of service, on-failure, never; default is service)")
|
||||
default:
|
||||
errs = append(errs, fmt.Sprintf("restart mode %q invalid (want one of service, on-failure, never)", spec.Restart.Mode))
|
||||
}
|
||||
}
|
||||
if spec.Update == nil {
|
||||
errs = append(errs, "update block required for Service")
|
||||
} else {
|
||||
switch spec.Update.Strategy {
|
||||
case "rolling", "canary", "blue-green":
|
||||
case "":
|
||||
errs = append(errs, "update strategy required for Service (one of rolling, canary, blue-green)")
|
||||
default:
|
||||
errs = append(errs, fmt.Sprintf("update strategy %q invalid (want one of rolling, canary, blue-green)", spec.Update.Strategy))
|
||||
}
|
||||
}
|
||||
if spec.Runtime == nil && len(spec.Tasks) == 0 {
|
||||
errs = append(errs, "runtime block required for Service (or a task group with per-task runtimes)")
|
||||
}
|
||||
if spec.Health == nil {
|
||||
errs = append(errs, "health block required for Service (Traefik routing requires health checks)")
|
||||
}
|
||||
if spec.Service != nil {
|
||||
if err := validateServiceBind(spec.Service.Bind); err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
}
|
||||
}
|
||||
errs = append(errs, validateTaskGroup(spec)...)
|
||||
return composeErrors("schema/Service", errs)
|
||||
}
|
||||
|
||||
// validateServiceBind validates the service.bind field (R-007). Empty
|
||||
// is OK (default = socket). When set, it must be a valid IPv4/IPv6
|
||||
// address (the only opt-in to bind on a non-loopback address); the
|
||||
// loopback 127.0.0.1 is the documented opt-in. Anything that is not
|
||||
// parseable as an IP address is rejected.
|
||||
func validateServiceBind(bind string) error {
|
||||
if strings.TrimSpace(bind) == "" {
|
||||
return nil
|
||||
}
|
||||
if net.ParseIP(bind) == nil {
|
||||
return fmt.Errorf("service.bind %q is not a valid IP address (R-007: 127.0.0.1 opt-in; default is socket)", bind)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate validates a DaemonSet spec. See DaemonSetValidator for the rules.
|
||||
func (DaemonSetValidator) Validate(spec *jobspec.WorkloadSpec) error {
|
||||
if spec == nil {
|
||||
return errors.New("schema/DaemonSet: spec is nil")
|
||||
}
|
||||
var errs []string
|
||||
if strings.TrimSpace(spec.Name) == "" {
|
||||
errs = append(errs, "name is required")
|
||||
}
|
||||
if spec.Schedule == nil {
|
||||
errs = append(errs, "schedule block required for DaemonSet")
|
||||
} else {
|
||||
switch spec.Schedule.Mode {
|
||||
case "every-node", "matching", "mandatory":
|
||||
case "":
|
||||
errs = append(errs, "schedule mode required for DaemonSet (one of every-node, matching, mandatory)")
|
||||
default:
|
||||
errs = append(errs, fmt.Sprintf("schedule mode %q invalid (want one of every-node, matching, mandatory)", spec.Schedule.Mode))
|
||||
}
|
||||
}
|
||||
if len(spec.Ports) > 0 {
|
||||
errs = append(errs, "ports not allowed for DaemonSet (no Traefik route by default D-175)")
|
||||
}
|
||||
if spec.Count != 0 {
|
||||
errs = append(errs, fmt.Sprintf("count not allowed for DaemonSet (implicit = nodes matching condition), got %d", spec.Count))
|
||||
}
|
||||
if spec.Restart == nil {
|
||||
errs = append(errs, "restart block required for DaemonSet")
|
||||
}
|
||||
errs = append(errs, validateTaskGroup(spec)...)
|
||||
return composeErrors("schema/DaemonSet", errs)
|
||||
}
|
||||
|
||||
// composeErrors joins the per-field errors into a single error prefixed
|
||||
// by the validator name. Returns nil when there are no errors so the
|
||||
// caller can return the result directly.
|
||||
func composeErrors(name string, errs []string) error {
|
||||
if len(errs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%s: %s", name, strings.Join(errs, "; "))
|
||||
}
|
||||
|
||||
// validateTaskGroup validates the task-group list shared by all kinds
|
||||
// (P06, PRD §9.1). When the spec carries a task group (spec.Tasks
|
||||
// non-empty), each task must have a unique name and a resolvable
|
||||
// command (the task's own Command, the task's runtime command, or the
|
||||
// top-level runtime command as the per-group default). The top-level
|
||||
// runtime is optional when tasks is present (each task can carry its
|
||||
// own runtime). Returns nil when the spec has no task group.
|
||||
func validateTaskGroup(spec *jobspec.WorkloadSpec) []string {
|
||||
if len(spec.Tasks) == 0 {
|
||||
return nil
|
||||
}
|
||||
var errs []string
|
||||
seen := make(map[string]bool, len(spec.Tasks))
|
||||
for i, task := range spec.Tasks {
|
||||
if strings.TrimSpace(task.Name) == "" {
|
||||
errs = append(errs, fmt.Sprintf("tasks[%d]: name is required", i))
|
||||
} else if seen[task.Name] {
|
||||
errs = append(errs, fmt.Sprintf("tasks[%d]: duplicate task name %q (names must be unique within the group)", i, task.Name))
|
||||
} else {
|
||||
seen[task.Name] = true
|
||||
}
|
||||
cmd := task.Command
|
||||
if strings.TrimSpace(cmd) == "" && task.Runtime != nil {
|
||||
cmd = task.Runtime.Command
|
||||
}
|
||||
if strings.TrimSpace(cmd) == "" && spec.Runtime != nil {
|
||||
cmd = spec.Runtime.Command
|
||||
}
|
||||
if strings.TrimSpace(cmd) == "" {
|
||||
errs = append(errs, fmt.Sprintf("tasks[%d]: command is required (set tasks[].command, tasks[].runtime.command, or top-level runtime.command)", i))
|
||||
}
|
||||
}
|
||||
return errs
|
||||
}
|
||||
@@ -0,0 +1,871 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
func TestJobValidator_ValidMinimal(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{Kind: "Job", Name: "backup", Count: 1}
|
||||
v := JobValidator{}
|
||||
if err := v.Validate(spec); err != nil {
|
||||
t.Fatalf("expected nil, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJobValidator_ValidWithSchedule(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "backup",
|
||||
Count: 1,
|
||||
Schedule: &jobspec.ScheduleBlock{Cron: "0 2 * * *"},
|
||||
Timeout: "1h",
|
||||
}
|
||||
v := JobValidator{}
|
||||
if err := v.Validate(spec); err != nil {
|
||||
t.Fatalf("expected nil, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJobValidator_ValidUnsetCount(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{Kind: "Job", Name: "one-shot"}
|
||||
v := JobValidator{}
|
||||
if err := v.Validate(spec); err != nil {
|
||||
t.Fatalf("unset count should default-accept, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJobValidator_MissingName(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{Kind: "Job", Count: 1}
|
||||
err := JobValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing name, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "name is required") {
|
||||
t.Errorf("error = %q, want 'name is required'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestJobValidator_CountGreaterThanOne(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{Kind: "Job", Name: "batch", Count: 3}
|
||||
err := JobValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for count > 1, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "count must be 1") {
|
||||
t.Errorf("error = %q, want 'count must be 1'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestJobValidator_ServiceBlockRejected(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "x",
|
||||
Count: 1,
|
||||
Service: &jobspec.ServiceBlock{Host: "x.example"},
|
||||
}
|
||||
err := JobValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for service block on Job, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "service block") {
|
||||
t.Errorf("error = %q, want 'service block'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestJobValidator_NilSpec(t *testing.T) {
|
||||
v := JobValidator{}
|
||||
if err := v.Validate(nil); err == nil {
|
||||
t.Fatal("expected error for nil spec")
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_ValidFull(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 3,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/http"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling", MaxSurge: 1},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http", Interval: "5s"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
v := ServiceValidator{}
|
||||
if err := v.Validate(spec); err != nil {
|
||||
t.Fatalf("expected nil, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_MissingPorts(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 2,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
}
|
||||
err := ServiceValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing ports, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "ports required") {
|
||||
t.Errorf("error = %q, want 'ports required'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_CountZero(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 0,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
err := ServiceValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for count 0, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "count must be") {
|
||||
t.Errorf("error = %q, want 'count must be'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_MissingRestart(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
err := ServiceValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing restart, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "restart block required") {
|
||||
t.Errorf("error = %q, want 'restart block required'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_WrongRestartMode(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "always"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
err := ServiceValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid restart mode, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "restart mode") {
|
||||
t.Errorf("error = %q, want 'restart mode'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_AcceptedRestartModes(t *testing.T) {
|
||||
// R-012: restart.mode accepts service / on-failure / never for
|
||||
// Service; the default per R-012 is "service" but the validator
|
||||
// accepts the full enum (a Service that wants on-failure is
|
||||
// unusual but not invalid — only "always" and unknown modes are
|
||||
// rejected).
|
||||
for _, mode := range []string{"service", "on-failure", "never"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Restart: &jobspec.RestartBlock{Mode: mode},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
v := ServiceValidator{}
|
||||
if err := v.Validate(spec); err != nil {
|
||||
t.Errorf("mode %q should be accepted, got: %v", mode, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_MissingUpdate(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
err := ServiceValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing update, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "update block required") {
|
||||
t.Errorf("error = %q, want 'update block required'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_MissingRuntime(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
err := ServiceValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing runtime, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "runtime block required") {
|
||||
t.Errorf("error = %q, want 'runtime block required'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_NilSpec(t *testing.T) {
|
||||
v := ServiceValidator{}
|
||||
if err := v.Validate(nil); err == nil {
|
||||
t.Fatal("expected error for nil spec")
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_MissingHealth(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
err := ServiceValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing health block, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "health block required") {
|
||||
t.Errorf("error = %q, want 'health block required'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_InvalidRestartMode(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "always"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
err := ServiceValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid restart mode, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "restart mode") || !strings.Contains(err.Error(), "invalid") {
|
||||
t.Errorf("error = %q, want 'restart mode ... invalid'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_EmptyRestartMode(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Restart: &jobspec.RestartBlock{Mode: ""},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
err := ServiceValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty restart mode, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "restart mode required") {
|
||||
t.Errorf("error = %q, want 'restart mode required'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_InvalidUpdateStrategy(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "recreate"},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
err := ServiceValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid update strategy, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "update strategy") || !strings.Contains(err.Error(), "invalid") {
|
||||
t.Errorf("error = %q, want 'update strategy ... invalid'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_EmptyUpdateStrategy(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: ""},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
err := ServiceValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty update strategy, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "update strategy required") {
|
||||
t.Errorf("error = %q, want 'update strategy required'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_AcceptedUpdateStrategies(t *testing.T) {
|
||||
for _, strat := range []string{"rolling", "canary", "blue-green"} {
|
||||
t.Run(strat, func(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: strat},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
v := ServiceValidator{}
|
||||
if err := v.Validate(spec); err != nil {
|
||||
t.Errorf("strategy %q should be accepted, got: %v", strat, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_InvalidServiceBind(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
Service: &jobspec.ServiceBlock{Bind: "not-an-ip"},
|
||||
}
|
||||
err := ServiceValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid service.bind, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "service.bind") || !strings.Contains(err.Error(), "valid IP") {
|
||||
t.Errorf("error = %q, want 'service.bind ... valid IP'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_ValidServiceBindLoopback(t *testing.T) {
|
||||
// R-007: 127.0.0.1 is the documented opt-in for a non-socket bind.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
Service: &jobspec.ServiceBlock{Bind: "127.0.0.1"},
|
||||
}
|
||||
v := ServiceValidator{}
|
||||
if err := v.Validate(spec); err != nil {
|
||||
t.Fatalf("127.0.0.1 should be accepted, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_ValidServiceBindIPv6(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
Service: &jobspec.ServiceBlock{Bind: "::1"},
|
||||
}
|
||||
v := ServiceValidator{}
|
||||
if err := v.Validate(spec); err != nil {
|
||||
t.Fatalf("::1 should be accepted, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_EmptyServiceBindOK(t *testing.T) {
|
||||
// R-007: empty bind = default = socket; valid.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
Service: &jobspec.ServiceBlock{Bind: ""},
|
||||
}
|
||||
v := ServiceValidator{}
|
||||
if err := v.Validate(spec); err != nil {
|
||||
t.Fatalf("empty bind should default to socket (valid), got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceValidator_MultipleErrors(t *testing.T) {
|
||||
// Multiple violations should all surface in the composed error.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "",
|
||||
Count: 0,
|
||||
Restart: &jobspec.RestartBlock{Mode: "always"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "recreate"},
|
||||
Service: &jobspec.ServiceBlock{Bind: "not-an-ip"},
|
||||
}
|
||||
err := ServiceValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
for _, want := range []string{
|
||||
"name is required",
|
||||
"ports required",
|
||||
"count must be",
|
||||
"restart mode",
|
||||
"update strategy",
|
||||
"runtime block required",
|
||||
"health block required",
|
||||
"service.bind",
|
||||
} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("error %q missing %q", err.Error(), want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDaemonSetValidator_Valid(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "DaemonSet",
|
||||
Name: "log-shipper",
|
||||
Schedule: &jobspec.ScheduleBlock{Mode: "every-node"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
|
||||
}
|
||||
v := DaemonSetValidator{}
|
||||
if err := v.Validate(spec); err != nil {
|
||||
t.Fatalf("expected nil, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDaemonSetValidator_ValidMatchingMode(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "DaemonSet",
|
||||
Name: "x",
|
||||
Schedule: &jobspec.ScheduleBlock{Mode: "matching"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
|
||||
}
|
||||
v := DaemonSetValidator{}
|
||||
if err := v.Validate(spec); err != nil {
|
||||
t.Fatalf("matching mode should be accepted, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDaemonSetValidator_ValidMandatoryMode(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "DaemonSet",
|
||||
Name: "x",
|
||||
Schedule: &jobspec.ScheduleBlock{Mode: "mandatory"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
|
||||
}
|
||||
v := DaemonSetValidator{}
|
||||
if err := v.Validate(spec); err != nil {
|
||||
t.Fatalf("mandatory mode should be accepted, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDaemonSetValidator_MissingScheduleMode(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "DaemonSet",
|
||||
Name: "x",
|
||||
Schedule: &jobspec.ScheduleBlock{Mode: ""},
|
||||
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
|
||||
}
|
||||
err := DaemonSetValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing schedule mode, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "schedule mode required") {
|
||||
t.Errorf("error = %q, want 'schedule mode required'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDaemonSetValidator_MissingScheduleBlock(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "DaemonSet",
|
||||
Name: "x",
|
||||
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
|
||||
}
|
||||
err := DaemonSetValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing schedule block, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "schedule block required") {
|
||||
t.Errorf("error = %q, want 'schedule block required'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDaemonSetValidator_InvalidScheduleMode(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "DaemonSet",
|
||||
Name: "x",
|
||||
Schedule: &jobspec.ScheduleBlock{Mode: "always"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
|
||||
}
|
||||
err := DaemonSetValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid schedule mode, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "schedule mode") {
|
||||
t.Errorf("error = %q, want 'schedule mode'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDaemonSetValidator_HasPorts(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "DaemonSet",
|
||||
Name: "x",
|
||||
Schedule: &jobspec.ScheduleBlock{Mode: "every-node"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 80}},
|
||||
}
|
||||
err := DaemonSetValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for ports on DaemonSet, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "ports not allowed") {
|
||||
t.Errorf("error = %q, want 'ports not allowed'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDaemonSetValidator_HasCount(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "DaemonSet",
|
||||
Name: "x",
|
||||
Count: 3,
|
||||
Schedule: &jobspec.ScheduleBlock{Mode: "every-node"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
|
||||
}
|
||||
err := DaemonSetValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for count on DaemonSet, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "count not allowed") {
|
||||
t.Errorf("error = %q, want 'count not allowed'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDaemonSetValidator_MissingRestart(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "DaemonSet",
|
||||
Name: "x",
|
||||
Schedule: &jobspec.ScheduleBlock{Mode: "every-node"},
|
||||
}
|
||||
err := DaemonSetValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing restart on DaemonSet, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "restart block required") {
|
||||
t.Errorf("error = %q, want 'restart block required'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDaemonSetValidator_NilSpec(t *testing.T) {
|
||||
v := DaemonSetValidator{}
|
||||
if err := v.Validate(nil); err == nil {
|
||||
t.Fatal("expected error for nil spec")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorFor_EachKind(t *testing.T) {
|
||||
cases := []struct {
|
||||
kind string
|
||||
want string
|
||||
}{
|
||||
{"Job", "schema.JobValidator"},
|
||||
{"Service", "schema.ServiceValidator"},
|
||||
{"DaemonSet", "schema.DaemonSetValidator"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.kind, func(t *testing.T) {
|
||||
v, err := ValidatorFor(tc.kind)
|
||||
if err != nil {
|
||||
t.Fatalf("ValidatorFor(%q): %v", tc.kind, err)
|
||||
}
|
||||
got := fmtType(v)
|
||||
if got != tc.want {
|
||||
t.Errorf("ValidatorFor(%q) type = %q, want %q", tc.kind, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorFor_UnknownKind(t *testing.T) {
|
||||
_, err := ValidatorFor("CronJob")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for unknown kind, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "unknown kind") {
|
||||
t.Errorf("error = %q, want 'unknown kind'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// fmtType returns a readable type name for a validator. Uses fmt.Sprintf
|
||||
// with %T rather than reflection to keep the test surface minimal.
|
||||
func fmtType(v Validator) string {
|
||||
switch v.(type) {
|
||||
case JobValidator:
|
||||
return "schema.JobValidator"
|
||||
case ServiceValidator:
|
||||
return "schema.ServiceValidator"
|
||||
case DaemonSetValidator:
|
||||
return "schema.DaemonSetValidator"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure composeErrors returns nil for empty input (covers the
|
||||
// short-circuit branch that the validators rely on).
|
||||
func TestComposeErrors_Empty(t *testing.T) {
|
||||
if err := composeErrors("schema/X", nil); err != nil {
|
||||
t.Errorf("composeErrors(nil) = %v, want nil", err)
|
||||
}
|
||||
if err := composeErrors("schema/X", []string{}); err != nil {
|
||||
t.Errorf("composeErrors([]) = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the error type returned by composeErrors is a non-nil error
|
||||
// when violations are present (guards against accidental nil-return).
|
||||
func TestComposeErrors_NonEmpty(t *testing.T) {
|
||||
err := composeErrors("schema/X", []string{"a", "b"})
|
||||
if err == nil {
|
||||
t.Fatal("expected non-nil error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "a") || !strings.Contains(err.Error(), "b") {
|
||||
t.Errorf("error = %q, want both 'a' and 'b'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Compile-time assertion that the validators implement the interface.
|
||||
var (
|
||||
_ Validator = JobValidator{}
|
||||
_ Validator = ServiceValidator{}
|
||||
_ Validator = DaemonSetValidator{}
|
||||
)
|
||||
|
||||
func TestTaskGroup_Valid(t *testing.T) {
|
||||
// P06: a valid task group — two tasks, each with a unique name
|
||||
// and a resolvable command (own command). The top-level runtime
|
||||
// is optional when each task carries its own.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Tasks: []jobspec.TaskGroupTask{
|
||||
{Name: "app", Command: "/usr/bin/httpd"},
|
||||
{Name: "sidecar", Command: "/bin/wasm-runner sidecar.wasm"},
|
||||
},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
if err := (ServiceValidator{}).Validate(spec); err != nil {
|
||||
t.Fatalf("expected nil, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskGroup_ValidInheritsTopLevelRuntime(t *testing.T) {
|
||||
// P06: tasks without their own runtime inherit the top-level
|
||||
// runtime command. The validator accepts this as long as the
|
||||
// resolved command is non-empty.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/default"},
|
||||
Tasks: []jobspec.TaskGroupTask{
|
||||
{Name: "app"},
|
||||
{Name: "sidecar"},
|
||||
},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
if err := (ServiceValidator{}).Validate(spec); err != nil {
|
||||
t.Fatalf("expected nil, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskGroup_ValidTaskRuntimeCommand(t *testing.T) {
|
||||
// P06: a task whose command is provided via the task's own
|
||||
// runtime.command (no top-level runtime) is valid.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 1,
|
||||
Tasks: []jobspec.TaskGroupTask{
|
||||
{Name: "app", Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/usr/bin/httpd"}},
|
||||
},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
if err := (ServiceValidator{}).Validate(spec); err != nil {
|
||||
t.Fatalf("expected nil, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskGroup_MissingTaskName(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Tasks: []jobspec.TaskGroupTask{
|
||||
{Command: "/usr/bin/httpd"},
|
||||
{Name: "sidecar", Command: "/bin/wasm-runner"},
|
||||
},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
err := ServiceValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing task name, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "name is required") {
|
||||
t.Errorf("error = %q, want 'name is required'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskGroup_DuplicateTaskNames(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Tasks: []jobspec.TaskGroupTask{
|
||||
{Name: "app", Command: "/usr/bin/httpd"},
|
||||
{Name: "app", Command: "/bin/other"},
|
||||
},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
err := ServiceValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for duplicate task names, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "duplicate task name") {
|
||||
t.Errorf("error = %q, want 'duplicate task name'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskGroup_MissingCommand(t *testing.T) {
|
||||
// P06: a task with no resolvable command (no task.Command, no
|
||||
// task.Runtime, no top-level Runtime) is rejected.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Tasks: []jobspec.TaskGroupTask{
|
||||
{Name: "app"},
|
||||
},
|
||||
Restart: &jobspec.RestartBlock{Mode: "service"},
|
||||
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
|
||||
Health: &jobspec.HealthBlock{CheckType: "http"},
|
||||
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
|
||||
}
|
||||
err := ServiceValidator{}.Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing task command, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "command is required") {
|
||||
t.Errorf("error = %q, want 'command is required'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskGroup_JobAcceptsTaskGroup(t *testing.T) {
|
||||
// P06: task groups apply to all kinds, not just Service. Job
|
||||
// accepts a task group with unique names + resolvable commands.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "batch",
|
||||
Count: 1,
|
||||
Tasks: []jobspec.TaskGroupTask{
|
||||
{Name: "step1", Command: "/bin/extract"},
|
||||
{Name: "step2", Command: "/bin/transform"},
|
||||
},
|
||||
}
|
||||
if err := (JobValidator{}).Validate(spec); err != nil {
|
||||
t.Fatalf("expected nil, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskGroup_DaemonSetAcceptsTaskGroup(t *testing.T) {
|
||||
// P06: DaemonSet accepts a task group.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "DaemonSet",
|
||||
Name: "log-shipper",
|
||||
Schedule: &jobspec.ScheduleBlock{Mode: "every-node"},
|
||||
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
|
||||
Tasks: []jobspec.TaskGroupTask{
|
||||
{Name: "collector", Command: "/bin/collect"},
|
||||
{Name: "forwarder", Command: "/bin/forward"},
|
||||
},
|
||||
}
|
||||
if err := (DaemonSetValidator{}).Validate(spec); err != nil {
|
||||
t.Fatalf("expected nil, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskGroup_NoTasksBackwardCompat(t *testing.T) {
|
||||
// Backward compat: a spec with no Tasks is validated by the
|
||||
// existing kind-specific rules (no task-group check fires).
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "backup",
|
||||
Count: 1,
|
||||
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/rsync"},
|
||||
}
|
||||
if err := (JobValidator{}).Validate(spec); err != nil {
|
||||
t.Fatalf("expected nil, got %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
// UpdateValidator validates the rolling/canary/blue-green update stanza
|
||||
// (P03). The schema validator (ServiceValidator) already enforces that
|
||||
// the strategy is one of rolling/canary/blue-green and that the update
|
||||
// block is present for a Service. UpdateValidator adds the
|
||||
// field-level validation:
|
||||
//
|
||||
// - max_parallel: integer in [1, count] (defaults to 1 when unset)
|
||||
// - min_healthy_time: a valid time.Duration when set (time.ParseDuration)
|
||||
// - healthy_deadline: a valid time.Duration when set (time.ParseDuration)
|
||||
// - canary: an integer count in [0, count] OR a percentage string of
|
||||
// the form "<n>%" where n is in [0, 100] (the parser already accepts
|
||||
// both shapes; the validator accepts them too). Only meaningful
|
||||
// for the canary strategy; ignored (but still validated for shape)
|
||||
// for rolling/blue-green.
|
||||
// - auto_promote: boolean (no validation beyond the parser's
|
||||
// true/false parse; the field is always populated)
|
||||
//
|
||||
// The validator is pure (no I/O). Violations return a clear error
|
||||
// listing every problem found, mirroring the per-field style of
|
||||
// ServiceValidator.
|
||||
type UpdateValidator struct{}
|
||||
|
||||
// Validate validates the UpdateBlock on the given spec. The spec must
|
||||
// be non-nil and carry a Count (services have count ≥ 1 per
|
||||
// ServiceValidator). When spec.Update is nil the validator returns an
|
||||
// error (the update block is required for Service; this validator
|
||||
// assumes the caller has already established the spec is a Service).
|
||||
func (UpdateValidator) Validate(spec *jobspec.WorkloadSpec) error {
|
||||
if spec == nil {
|
||||
return fmt.Errorf("schema/Update: spec is nil")
|
||||
}
|
||||
if spec.Update == nil {
|
||||
return fmt.Errorf("schema/Update: update block is nil")
|
||||
}
|
||||
var errs []string
|
||||
u := spec.Update
|
||||
|
||||
switch u.Strategy {
|
||||
case "rolling", "canary", "blue-green":
|
||||
case "":
|
||||
errs = append(errs, "update strategy required (one of rolling, canary, blue-green)")
|
||||
default:
|
||||
errs = append(errs, fmt.Sprintf("update strategy %q invalid (want one of rolling, canary, blue-green)", u.Strategy))
|
||||
}
|
||||
|
||||
// max_parallel defaults to 1 when unset (0); validate the range
|
||||
// only when the user has set it explicitly.
|
||||
if u.MaxParallel != 0 {
|
||||
if u.MaxParallel < 1 {
|
||||
errs = append(errs, fmt.Sprintf("update.max_parallel must be ≥ 1, got %d", u.MaxParallel))
|
||||
}
|
||||
if spec.Count > 0 && u.MaxParallel > spec.Count {
|
||||
errs = append(errs, fmt.Sprintf("update.max_parallel %d exceeds count %d (must be 1..count)", u.MaxParallel, spec.Count))
|
||||
}
|
||||
}
|
||||
|
||||
if u.MinHealthyTime != "" {
|
||||
if _, err := time.ParseDuration(u.MinHealthyTime); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("update.min_healthy_time %q is not a valid duration: %v", u.MinHealthyTime, err))
|
||||
}
|
||||
}
|
||||
if u.HealthyDeadline != "" {
|
||||
if _, err := time.ParseDuration(u.HealthyDeadline); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("update.healthy_deadline %q is not a valid duration: %v", u.HealthyDeadline, err))
|
||||
}
|
||||
}
|
||||
|
||||
// canary accepts an integer count (0..count) or a percentage
|
||||
// ("<n>%" with n in 0..100). The field is only meaningful for the
|
||||
// canary strategy but we validate the shape regardless so a typo
|
||||
// in a rolling/blue-green stanza still surfaces.
|
||||
if u.Canary != "" {
|
||||
if err := validateCanary(u.Canary, spec.Count); err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// auto_promote is a bool; no extra validation beyond the parser.
|
||||
|
||||
return composeErrors("schema/Update", errs)
|
||||
}
|
||||
|
||||
// validateCanary validates the canary field shape: either an integer
|
||||
// count (0..count) or a percentage string "<n>%" (n in 0..100). count
|
||||
// is the spec.Count; when count is 0 (e.g. a DaemonSet or unset), the
|
||||
// integer-count upper bound is not enforced (only the percentage
|
||||
// bound is enforced, since percentage does not depend on count).
|
||||
func validateCanary(canary string, count int) error {
|
||||
c := strings.TrimSpace(canary)
|
||||
if c == "" {
|
||||
return nil
|
||||
}
|
||||
if strings.HasSuffix(c, "%") {
|
||||
nStr := strings.TrimSuffix(c, "%")
|
||||
n, err := strconv.Atoi(strings.TrimSpace(nStr))
|
||||
if err != nil {
|
||||
return fmt.Errorf("update.canary %q is not a valid percentage (want \"<n>%%\")", canary)
|
||||
}
|
||||
if n < 0 || n > 100 {
|
||||
return fmt.Errorf("update.canary percentage %d out of range (want 0..100)", n)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
n, err := strconv.Atoi(c)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update.canary %q is not a valid count or percentage (want integer or \"<n>%%\")", canary)
|
||||
}
|
||||
if n < 0 {
|
||||
return fmt.Errorf("update.canary count %d must be ≥ 0", n)
|
||||
}
|
||||
if count > 0 && n > count {
|
||||
return fmt.Errorf("update.canary count %d exceeds count %d (must be 0..count)", n, count)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,464 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
func TestUpdateValidator_ValidRolling(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "rolling",
|
||||
MaxParallel: 2,
|
||||
MinHealthyTime: "30s",
|
||||
HealthyDeadline: "5m",
|
||||
},
|
||||
}
|
||||
v := UpdateValidator{}
|
||||
if err := v.Validate(spec); err != nil {
|
||||
t.Fatalf("expected nil, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_ValidCanary(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
MaxParallel: 2,
|
||||
MinHealthyTime: "30s",
|
||||
HealthyDeadline: "5m",
|
||||
Canary: "10%",
|
||||
AutoPromote: true,
|
||||
},
|
||||
}
|
||||
if err := (UpdateValidator{}).Validate(spec); err != nil {
|
||||
t.Fatalf("expected nil, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_ValidBlueGreen(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 3,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "blue-green",
|
||||
MinHealthyTime: "1m",
|
||||
HealthyDeadline: "10m",
|
||||
},
|
||||
}
|
||||
if err := (UpdateValidator{}).Validate(spec); err != nil {
|
||||
t.Fatalf("expected nil, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_ValidCanaryIntegerCount(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
Canary: "1",
|
||||
},
|
||||
}
|
||||
if err := (UpdateValidator{}).Validate(spec); err != nil {
|
||||
t.Fatalf("integer canary count 1 should be valid, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_ValidCanaryZeroPercent(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
Canary: "0%",
|
||||
},
|
||||
}
|
||||
if err := (UpdateValidator{}).Validate(spec); err != nil {
|
||||
t.Fatalf("0%% canary should be valid, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_ValidCanaryHundredPercent(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
Canary: "100%",
|
||||
},
|
||||
}
|
||||
if err := (UpdateValidator{}).Validate(spec); err != nil {
|
||||
t.Fatalf("100%% canary should be valid, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_EmptyDurationsOK(t *testing.T) {
|
||||
// Empty min_healthy_time / healthy_deadline should be accepted
|
||||
// (they are optional; defaults are applied by the executor).
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 2,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "rolling",
|
||||
},
|
||||
}
|
||||
if err := (UpdateValidator{}).Validate(spec); err != nil {
|
||||
t.Fatalf("empty durations should be valid, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_NilSpec(t *testing.T) {
|
||||
if err := (UpdateValidator{}).Validate(nil); err == nil {
|
||||
t.Fatal("expected error for nil spec")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_NilUpdateBlock(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{Kind: "Service", Name: "web", Count: 2}
|
||||
err := (UpdateValidator{}).Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nil update block")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "update block is nil") {
|
||||
t.Errorf("error = %q, want 'update block is nil'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_InvalidStrategy(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 2,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "recreate",
|
||||
},
|
||||
}
|
||||
err := (UpdateValidator{}).Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid strategy")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "strategy") || !strings.Contains(err.Error(), "invalid") {
|
||||
t.Errorf("error = %q, want 'strategy ... invalid'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_EmptyStrategy(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 2,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "",
|
||||
},
|
||||
}
|
||||
err := (UpdateValidator{}).Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty strategy")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "strategy required") {
|
||||
t.Errorf("error = %q, want 'strategy required'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_MaxParallelZero(t *testing.T) {
|
||||
// max_parallel=0 means "unset" → default 1; accepted.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 2,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "rolling",
|
||||
MaxParallel: 0,
|
||||
},
|
||||
}
|
||||
if err := (UpdateValidator{}).Validate(spec); err != nil {
|
||||
t.Fatalf("max_parallel=0 (unset) should be valid, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_MaxParallelNegative(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 2,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "rolling",
|
||||
MaxParallel: -1,
|
||||
},
|
||||
}
|
||||
err := (UpdateValidator{}).Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for negative max_parallel")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "max_parallel") {
|
||||
t.Errorf("error = %q, want 'max_parallel'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_MaxParallelExceedsCount(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 2,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "rolling",
|
||||
MaxParallel: 5,
|
||||
},
|
||||
}
|
||||
err := (UpdateValidator{}).Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for max_parallel > count")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "exceeds count") {
|
||||
t.Errorf("error = %q, want 'exceeds count'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_MaxParallelEqualsCount(t *testing.T) {
|
||||
// max_parallel == count is the upper bound; valid.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 3,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "rolling",
|
||||
MaxParallel: 3,
|
||||
},
|
||||
}
|
||||
if err := (UpdateValidator{}).Validate(spec); err != nil {
|
||||
t.Fatalf("max_parallel==count should be valid, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_InvalidMinHealthyTime(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 2,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "rolling",
|
||||
MinHealthyTime: "not-a-duration",
|
||||
},
|
||||
}
|
||||
err := (UpdateValidator{}).Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid min_healthy_time")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "min_healthy_time") {
|
||||
t.Errorf("error = %q, want 'min_healthy_time'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_InvalidHealthyDeadline(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 2,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "rolling",
|
||||
HealthyDeadline: "nope",
|
||||
},
|
||||
}
|
||||
err := (UpdateValidator{}).Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid healthy_deadline")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "healthy_deadline") {
|
||||
t.Errorf("error = %q, want 'healthy_deadline'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_CanaryNegative(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
Canary: "-1",
|
||||
},
|
||||
}
|
||||
err := (UpdateValidator{}).Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for negative canary count")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "canary") {
|
||||
t.Errorf("error = %q, want 'canary'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_CanaryExceedsCount(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
Canary: "5",
|
||||
},
|
||||
}
|
||||
err := (UpdateValidator{}).Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for canary > count")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "exceeds count") {
|
||||
t.Errorf("error = %q, want 'exceeds count'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_CanaryPercentOver100(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
Canary: "150%",
|
||||
},
|
||||
}
|
||||
err := (UpdateValidator{}).Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for canary > 100%")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "out of range") {
|
||||
t.Errorf("error = %q, want 'out of range'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_CanaryPercentNegative(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
Canary: "-10%",
|
||||
},
|
||||
}
|
||||
err := (UpdateValidator{}).Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for negative canary percent")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "out of range") {
|
||||
t.Errorf("error = %q, want 'out of range'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_CanaryNotANumber(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
Canary: "abc",
|
||||
},
|
||||
}
|
||||
err := (UpdateValidator{}).Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for non-numeric canary")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "not a valid") {
|
||||
t.Errorf("error = %q, want 'not a valid'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_CanaryPercentNotANumber(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
Canary: "xx%",
|
||||
},
|
||||
}
|
||||
err := (UpdateValidator{}).Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for non-numeric canary percent")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "not a valid percentage") {
|
||||
t.Errorf("error = %q, want 'not a valid percentage'", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_CanaryCountZeroOK(t *testing.T) {
|
||||
// canary=0 is the lower bound; valid.
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 4,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
Canary: "0",
|
||||
},
|
||||
}
|
||||
if err := (UpdateValidator{}).Validate(spec); err != nil {
|
||||
t.Fatalf("canary=0 should be valid, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_CanaryPercentWithoutCountOK(t *testing.T) {
|
||||
// A percentage canary does not depend on count; valid even when
|
||||
// count is 0 (e.g. DaemonSet-shaped spec bypassing the Service
|
||||
// validator — defensive).
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 0,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "canary",
|
||||
Canary: "25%",
|
||||
},
|
||||
}
|
||||
if err := (UpdateValidator{}).Validate(spec); err != nil {
|
||||
t.Fatalf("percentage canary with count=0 should be valid, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValidator_MultipleErrors(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "web",
|
||||
Count: 2,
|
||||
Update: &jobspec.UpdateBlock{
|
||||
Strategy: "recreate",
|
||||
MaxParallel: 99,
|
||||
MinHealthyTime: "nope",
|
||||
HealthyDeadline: "also-nope",
|
||||
Canary: "200%",
|
||||
},
|
||||
}
|
||||
err := (UpdateValidator{}).Validate(spec)
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
for _, want := range []string{
|
||||
"strategy",
|
||||
"max_parallel",
|
||||
"min_healthy_time",
|
||||
"healthy_deadline",
|
||||
"canary",
|
||||
} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("error %q missing %q", err.Error(), want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compile-time assertion that UpdateValidator implements Validator.
|
||||
var _ Validator = UpdateValidator{}
|
||||
@@ -0,0 +1,30 @@
|
||||
// Package sshpush_test contains compile-time assertions that *Transport
|
||||
// satisfies the emitter.AtomicWriter interface (the Traefik C-10
|
||||
// atomicity protocol — internal/emitter/traefik_atomic.go). The
|
||||
// assertion lives here (not in internal/emitter) to avoid an import
|
||||
// cycle: internal/emitter is imported by this package (fanout.go), so
|
||||
// internal/emitter cannot import this package.
|
||||
package sshpush_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/emitter"
|
||||
"git.cloudinit.dev/coreci/orca/internal/sshpush"
|
||||
)
|
||||
|
||||
// Compile-time assertion: *sshpush.Transport satisfies
|
||||
// emitter.AtomicWriter. WriteTraefikDynamic relies on this so the
|
||||
// Traefik dynamic-config file is written atomically (gate C-10).
|
||||
var _ emitter.AtomicWriter = (*sshpush.Transport)(nil)
|
||||
|
||||
func TestTransportSatisfiesAtomicWriter(t *testing.T) {
|
||||
// A trivial runtime check that the type conversion is valid; the
|
||||
// compile-time assertion above is the real test, but this gives
|
||||
// `go test` a function to run.
|
||||
tr := sshpush.NewTransport("/nonexistent", "/nonexistent")
|
||||
var w emitter.AtomicWriter = tr
|
||||
_ = w
|
||||
_ = context.Background()
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Package sshpush implements the v0.9 SSH-push transport layer (REQ-073,
|
||||
// R-001): the CLI on the operator host SSHes to each peer to render files,
|
||||
// apply configs, and run commands. It replaces the v0.8
|
||||
// internal/transport mTLS HTTP layer.
|
||||
//
|
||||
// The Transport reuses one *ssh.Client per peer across multiple
|
||||
// operations within a single CLI invocation (I-B-001), retries transient
|
||||
// failures with exponential backoff (100ms ×2, cap 5s, max 5 attempts —
|
||||
// reimplemented from the v0.8 transport/retry.go pattern, since
|
||||
// internal/transport is deprecated and not imported), applies per-call
|
||||
// timeouts (10s exec, 30s SCP per I-B-001), and fans out to many peers
|
||||
// with bounded concurrency (default 8, errgroup + semaphore).
|
||||
//
|
||||
// Idempotency is content-addressed (C-18): WriteFile / WriteFileIdempotent
|
||||
// compare the remote file's SHA-256 to the local content and skip the
|
||||
// write on match — the SSH-push equivalent of the v0.8 X-Orca-Idempotency-Key.
|
||||
//
|
||||
// Host-key verification reuses proxmox.TOFUHostKeyCallback (D-035), which
|
||||
// reads/writes the known_hosts file (certpaths.KnownHostsPath during the
|
||||
// v0.9 dual-write window; the move to paths.KnownHostsPath happens in
|
||||
// v0.10-P14). The known_hosts file is flock-protected inside the TOFU
|
||||
// callback, so the Transport does NOT re-lock.
|
||||
package sshpush
|
||||
@@ -0,0 +1,113 @@
|
||||
package sshpush
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/emitter"
|
||||
)
|
||||
|
||||
// DefaultFanoutConcurrency is the default bounded-concurrency limit for
|
||||
// fan-out operations (I-B-001). The Transport.ExecAll and WriteAll
|
||||
// methods use this when the caller does not override it.
|
||||
const DefaultFanoutConcurrency = 8
|
||||
|
||||
// ExecAll runs cmd on all peers in parallel with bounded concurrency
|
||||
// (default 8, I-B-001). Returns per-peer output and per-peer errors. A
|
||||
// nil entry in the errors map means that peer succeeded; the output map
|
||||
// contains that peer's stdout. The returned error is non-nil only if
|
||||
// the fan-out itself failed (e.g., context cancelled before any peer
|
||||
// ran); per-peer failures are in the errors map.
|
||||
func (t *Transport) ExecAll(ctx context.Context, peers []string, cmd string) (map[string][]byte, map[string]error) {
|
||||
return t.ExecAllWithConcurrency(ctx, peers, cmd, DefaultFanoutConcurrency)
|
||||
}
|
||||
|
||||
// ExecAllWithConcurrency is ExecAll with an explicit concurrency limit.
|
||||
// A limit <= 0 uses DefaultFanoutConcurrency.
|
||||
func (t *Transport) ExecAllWithConcurrency(ctx context.Context, peers []string, cmd string, concurrency int) (map[string][]byte, map[string]error) {
|
||||
if concurrency <= 0 {
|
||||
concurrency = DefaultFanoutConcurrency
|
||||
}
|
||||
out := make(map[string][]byte, len(peers))
|
||||
errs := make(map[string]error, len(peers))
|
||||
var mu sync.Mutex
|
||||
g, gctx := errgroup.WithContext(ctx)
|
||||
g.SetLimit(concurrency)
|
||||
for _, p := range peers {
|
||||
peer := p
|
||||
g.Go(func() error {
|
||||
o, err := t.Exec(gctx, peer, cmd)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs[peer] = err
|
||||
return nil // per-peer error; do not cancel the group
|
||||
}
|
||||
out[peer] = o
|
||||
return nil
|
||||
})
|
||||
}
|
||||
_ = g.Wait()
|
||||
return out, errs
|
||||
}
|
||||
|
||||
// WriteAll writes the given files to each peer in parallel with bounded
|
||||
// concurrency (default 8, I-B-001). The files map is keyed by peer; each
|
||||
// peer's files are written sequentially (to preserve order and avoid
|
||||
// intra-peer races on shared paths). Returns per-peer errors; a peer
|
||||
// missing from the map or with a nil entry succeeded. The returned
|
||||
// error is non-nil only if the fan-out itself failed (context cancelled).
|
||||
func (t *Transport) WriteAll(ctx context.Context, peers []string, files map[string][]emitter.File) map[string]error {
|
||||
return t.WriteAllWithConcurrency(ctx, peers, files, DefaultFanoutConcurrency)
|
||||
}
|
||||
|
||||
// WriteAllWithConcurrency is WriteAll with an explicit concurrency limit.
|
||||
// A limit <= 0 uses DefaultFanoutConcurrency.
|
||||
func (t *Transport) WriteAllWithConcurrency(ctx context.Context, peers []string, files map[string][]emitter.File, concurrency int) map[string]error {
|
||||
if concurrency <= 0 {
|
||||
concurrency = DefaultFanoutConcurrency
|
||||
}
|
||||
errs := make(map[string]error, len(peers))
|
||||
var mu sync.Mutex
|
||||
g, gctx := errgroup.WithContext(ctx)
|
||||
g.SetLimit(concurrency)
|
||||
for _, p := range peers {
|
||||
peer := p
|
||||
peerFiles := files[peer]
|
||||
g.Go(func() error {
|
||||
for _, f := range peerFiles {
|
||||
mode := parseMode(f.Mode)
|
||||
if _, err := t.WriteFileIdempotent(gctx, peer, f.Path, []byte(f.Content), mode); err != nil {
|
||||
mu.Lock()
|
||||
errs[peer] = fmt.Errorf("sshpush: write %s on %s: %w", f.Path, peer, err)
|
||||
mu.Unlock()
|
||||
return nil // per-peer error; do not cancel the group
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
_ = g.Wait()
|
||||
return errs
|
||||
}
|
||||
|
||||
// parseMode parses an octal mode string like "0644" into an os.FileMode.
|
||||
// Returns 0644 on parse failure (a safe default for non-executable
|
||||
// config files).
|
||||
func parseMode(s string) os.FileMode {
|
||||
var m uint32
|
||||
for _, r := range s {
|
||||
if r < '0' || r > '7' {
|
||||
return 0o644
|
||||
}
|
||||
m = m<<3 | uint32(r-'0')
|
||||
}
|
||||
if m == 0 {
|
||||
return 0o644
|
||||
}
|
||||
return os.FileMode(m)
|
||||
}
|
||||
@@ -0,0 +1,300 @@
|
||||
package sshpush
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/emitter"
|
||||
)
|
||||
|
||||
// --- ExecAll tests ---
|
||||
|
||||
func TestExecAll_AllSucceed(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
// Use one server for all peers (same addr).
|
||||
addr := srv.addr()
|
||||
peers := []string{addr, addr, addr}
|
||||
out, errs := tr.ExecAll(context.Background(), peers, "echo hello")
|
||||
for _, p := range peers {
|
||||
if e, ok := errs[p]; ok && e != nil {
|
||||
t.Errorf("peer %s: %v", p, e)
|
||||
}
|
||||
if string(out[p]) != "hello\n" {
|
||||
t.Errorf("out[%s] = %q, want hello\\n", p, out[p])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecAll_OneFails(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
addr := srv.addr()
|
||||
// Peer "bad" returns a permanent error via a mock session.
|
||||
goodPeers := []string{addr}
|
||||
badPeer := "127.0.0.1:1" // unreachable -> transient dial error, retried, fails
|
||||
peers := append(goodPeers, badPeer)
|
||||
out, errs := tr.ExecAll(context.Background(), peers, "echo hello")
|
||||
if string(out[addr]) != "hello\n" {
|
||||
t.Errorf("good peer out = %q, want hello\\n", out[addr])
|
||||
}
|
||||
if errs[badPeer] == nil {
|
||||
t.Error("bad peer should have an error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecAll_WithConcurrency(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
addr := srv.addr()
|
||||
peers := []string{addr, addr, addr, addr}
|
||||
out, errs := tr.ExecAllWithConcurrency(context.Background(), peers, "echo hello", 2)
|
||||
for _, p := range peers {
|
||||
if e := errs[p]; e != nil {
|
||||
t.Errorf("peer %s: %v", p, e)
|
||||
}
|
||||
if string(out[p]) != "hello\n" {
|
||||
t.Errorf("out[%s] = %q", p, out[p])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecAll_EmptyPeers(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
out, errs := tr.ExecAll(context.Background(), nil, "echo hello")
|
||||
if len(out) != 0 || len(errs) != 0 {
|
||||
t.Errorf("empty peers: out=%v errs=%v", out, errs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecAll_ContextCancelled(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
addr := srv.addr()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
out, errs := tr.ExecAll(ctx, []string{addr, addr}, "echo hello")
|
||||
// With a cancelled context, all peers should fail.
|
||||
for _, p := range []string{addr, addr} {
|
||||
if errs[p] == nil && string(out[p]) == "" {
|
||||
// acceptable: either error or no output
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- WriteAll tests ---
|
||||
|
||||
func TestWriteAll_AllSucceed(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
addr := srv.addr()
|
||||
files := map[string][]emitter.File{
|
||||
addr: {
|
||||
{Path: "/w/a", Content: "alpha\n", Mode: "0644"},
|
||||
{Path: "/w/b", Content: "beta\n", Mode: "0644"},
|
||||
},
|
||||
}
|
||||
errs := tr.WriteAll(context.Background(), []string{addr}, files)
|
||||
for p, e := range errs {
|
||||
if e != nil {
|
||||
t.Errorf("peer %s: %v", p, e)
|
||||
}
|
||||
}
|
||||
srv.mu.Lock()
|
||||
if srv.files["/w/a"] != "alpha\n" {
|
||||
t.Errorf("file a = %q", srv.files["/w/a"])
|
||||
}
|
||||
if srv.files["/w/b"] != "beta\n" {
|
||||
t.Errorf("file b = %q", srv.files["/w/b"])
|
||||
}
|
||||
srv.mu.Unlock()
|
||||
}
|
||||
|
||||
func TestWriteAll_OnePeerFails(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
addr := srv.addr()
|
||||
bad := "127.0.0.1:1"
|
||||
files := map[string][]emitter.File{
|
||||
addr: {{Path: "/ok/f", Content: "ok\n", Mode: "0644"}},
|
||||
bad: {{Path: "/fail/f", Content: "fail\n", Mode: "0644"}},
|
||||
}
|
||||
errs := tr.WriteAll(context.Background(), []string{addr, bad}, files)
|
||||
if errs[addr] != nil {
|
||||
t.Errorf("good peer should not have error, got %v", errs[addr])
|
||||
}
|
||||
if errs[bad] == nil {
|
||||
t.Error("bad peer should have error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteAll_EmptyPeers(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
errs := tr.WriteAll(context.Background(), nil, nil)
|
||||
if len(errs) != 0 {
|
||||
t.Errorf("empty peers: errs=%v", errs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteAll_WithConcurrency(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
addr := srv.addr()
|
||||
files := map[string][]emitter.File{
|
||||
addr: {{Path: "/c/f", Content: "c\n", Mode: "0644"}},
|
||||
}
|
||||
errs := tr.WriteAllWithConcurrency(context.Background(), []string{addr}, files, 4)
|
||||
for _, e := range errs {
|
||||
if e != nil {
|
||||
t.Errorf("peer err: %v", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteAll_Idempotent(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
srv.mu.Lock()
|
||||
srv.files["/i/f"] = "same\n"
|
||||
srv.mu.Unlock()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
addr := srv.addr()
|
||||
files := map[string][]emitter.File{
|
||||
addr: {{Path: "/i/f", Content: "same\n", Mode: "0644"}},
|
||||
}
|
||||
// Capture writes to verify idempotent skip.
|
||||
var writes int64
|
||||
tr.SetSessionFactory(func(c *ssh.Client) (sshSession, error) {
|
||||
return &writeCountingSession{srv: srv, writes: &writes}, nil
|
||||
})
|
||||
// Pre-populate pool.
|
||||
client, err := tr.dial(addr)
|
||||
if err != nil {
|
||||
t.Fatalf("dial: %v", err)
|
||||
}
|
||||
tr.pool.Store(addr, client)
|
||||
errs := tr.WriteAll(context.Background(), []string{addr}, files)
|
||||
if errs[addr] != nil {
|
||||
t.Errorf("WriteAll err: %v", errs[addr])
|
||||
}
|
||||
// sha256sum returns a hash that matches -> no write command.
|
||||
srv.mu.Lock()
|
||||
content := srv.files["/i/f"]
|
||||
srv.mu.Unlock()
|
||||
if content != "same\n" {
|
||||
t.Errorf("content changed to %q", content)
|
||||
}
|
||||
}
|
||||
|
||||
// writeCountingSession counts how many write commands (mkdir + cat >)
|
||||
// are issued; returns the server's file content for sha256sum.
|
||||
type writeCountingSession struct {
|
||||
srv *fakeSSHServer
|
||||
writes *int64
|
||||
}
|
||||
|
||||
func (w *writeCountingSession) CombinedOutput(cmd string) ([]byte, error) {
|
||||
c := trim(cmd)
|
||||
if startsWith(c, "sha256sum ") {
|
||||
path := unquote(trimPrefix(c, "sha256sum "))
|
||||
path = trimSuffix(path, " 2>/dev/null")
|
||||
w.srv.mu.Lock()
|
||||
content, ok := w.srv.files[path]
|
||||
w.srv.mu.Unlock()
|
||||
if !ok {
|
||||
return []byte(""), nil
|
||||
}
|
||||
sum := sha256HexStr([]byte(content))
|
||||
return []byte(sum + " " + path + "\n"), nil
|
||||
}
|
||||
if startsWith(c, "mkdir -p ") && contains(c, "cat >") {
|
||||
atomic.AddInt64(w.writes, 1)
|
||||
return nil, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
func (w *writeCountingSession) Close() error { return nil }
|
||||
|
||||
// Local string helpers to avoid importing strings in a way that
|
||||
// conflicts with the test's existing imports.
|
||||
func trim(s string) string {
|
||||
for len(s) > 0 && (s[0] == ' ' || s[0] == '\t') {
|
||||
s = s[1:]
|
||||
}
|
||||
for len(s) > 0 && (s[len(s)-1] == ' ' || s[len(s)-1] == '\t') {
|
||||
s = s[:len(s)-1]
|
||||
}
|
||||
return s
|
||||
}
|
||||
func startsWith(s, prefix string) bool { return len(s) >= len(prefix) && s[:len(prefix)] == prefix }
|
||||
func contains(s, sub string) bool {
|
||||
return len(sub) == 0 || (len(s) >= len(sub) && indexOf(s, sub) >= 0)
|
||||
}
|
||||
func indexOf(s, sub string) int {
|
||||
for i := 0; i+len(sub) <= len(s); i++ {
|
||||
if s[i:i+len(sub)] == sub {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
func trimPrefix(s, prefix string) string {
|
||||
if startsWith(s, prefix) {
|
||||
return s[len(prefix):]
|
||||
}
|
||||
return s
|
||||
}
|
||||
func trimSuffix(s, suffix string) string {
|
||||
if len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix {
|
||||
return s[:len(s)-len(suffix)]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func TestDefaultFanoutConcurrency(t *testing.T) {
|
||||
if DefaultFanoutConcurrency != 8 {
|
||||
t.Errorf("DefaultFanoutConcurrency = %d, want 8", DefaultFanoutConcurrency)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMode_Fanout(t *testing.T) {
|
||||
// parseMode is in fanout.go; sanity-check here too.
|
||||
for _, tc := range []struct{ in, want string }{
|
||||
{"0644", "644"},
|
||||
{"0755", "755"},
|
||||
{"bad", "644"},
|
||||
} {
|
||||
got := fmt.Sprintf("%o", parseMode(tc.in))
|
||||
if got != tc.want {
|
||||
t.Errorf("parseMode(%q) = %s, want %s", tc.in, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var _ = errors.New
|
||||
@@ -0,0 +1,101 @@
|
||||
package sshpush
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// WriteFileIdempotent writes content to peer:path atomically (write-to-tmp
|
||||
// + mv, REQ-074) with mode, but only if the remote file's SHA-256 differs
|
||||
// from the local content's SHA-256 (C-18 content-addressed idempotency —
|
||||
// the SSH-push equivalent of the v0.8 X-Orca-Idempotency-Key).
|
||||
//
|
||||
// Returns written=true if the file was written, written=false if the
|
||||
// content already matched (skip). The default per-SCP timeout is
|
||||
// SCPTimeout (I-B-001).
|
||||
//
|
||||
// Atomicity: the content is written to a temp file in the same directory
|
||||
// as the target, then `mv`'d into place. The temp file is mode-appended
|
||||
// (e.g. `/etc/orca/foo.conf.orca-tmp-<rand>`) so the rename is atomic on
|
||||
// POSIX filesystems.
|
||||
func (t *Transport) WriteFileIdempotent(ctx context.Context, peer string, path string, content []byte, mode os.FileMode) (bool, error) {
|
||||
localHash := sha256Hex(content)
|
||||
remoteHash, err := t.remoteSHA256(ctx, peer, path)
|
||||
if err == nil && remoteHash != "" && strings.EqualFold(remoteHash, localHash) {
|
||||
return false, nil
|
||||
}
|
||||
if err := t.writeFile(ctx, peer, path, content, mode); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// writeFile writes content to peer:path atomically (write-to-tmp + mv).
|
||||
// It writes the content via a single SSH exec (cat heredoc + chmod + mv),
|
||||
// keeping the transfer in one round-trip. The temp file lives next to the
|
||||
// target so the rename is atomic.
|
||||
func (t *Transport) writeFile(ctx context.Context, peer string, path string, content []byte, mode os.FileMode) error {
|
||||
dir, base := splitDir(path)
|
||||
tmpName := fmt.Sprintf(".orca-tmp-%s", randomToken(8))
|
||||
tmpPath := base + "/" + tmpName
|
||||
if dir == "" {
|
||||
tmpPath = tmpName
|
||||
}
|
||||
// Build the remote command: mkdir -p <dir> && cat > <tmp> <<'EOF'
|
||||
// ... EOF && chmod <mode> <tmp> && mv <tmp> <path>. The heredoc
|
||||
// delimiter is chosen to not appear in the content (we use a fixed
|
||||
// marker; content with the marker would break, but the marker is
|
||||
// sufficiently unusual).
|
||||
const eof = "ORCA_PUSH_EOF_a1b2c3"
|
||||
modeStr := fmt.Sprintf("%04o", uint32(mode.Perm()))
|
||||
cmd := fmt.Sprintf(
|
||||
"mkdir -p %s && cat > %s <<'%s'\n%s\n%s\nchmod %s %s && mv -f %s %s",
|
||||
shellQuote(base),
|
||||
shellQuote(tmpPath),
|
||||
eof,
|
||||
string(content),
|
||||
eof,
|
||||
modeStr,
|
||||
shellQuote(tmpPath),
|
||||
shellQuote(tmpPath),
|
||||
shellQuote(path),
|
||||
)
|
||||
execCtx, cancel := context.WithTimeout(ctx, SCPTimeout)
|
||||
defer cancel()
|
||||
if _, err := t.execWithRetry(execCtx, peer, cmd, true); err != nil {
|
||||
return fmt.Errorf("sshpush: write %s: %w", path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// sha256Hex returns the lowercase hex SHA-256 digest of b.
|
||||
func sha256Hex(b []byte) string {
|
||||
sum := sha256.Sum256(b)
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
// splitDir returns the directory and the directory itself (for mkdir).
|
||||
// For "/etc/orca/foo.conf" it returns ("/etc/orca", "/etc/orca"). For
|
||||
// "foo.conf" it returns ("", ".").
|
||||
func splitDir(path string) (dir, base string) {
|
||||
idx := strings.LastIndex(path, "/")
|
||||
if idx < 0 {
|
||||
return "", "."
|
||||
}
|
||||
return path[:idx], path[:idx]
|
||||
}
|
||||
|
||||
// randomToken returns a random hex token of the given byte length. Used
|
||||
// for temp-file naming to avoid collisions under parallel fan-out.
|
||||
func randomToken(n int) string {
|
||||
b := make([]byte, n)
|
||||
for i := range b {
|
||||
b[i] = byte(rand.Intn(256))
|
||||
}
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
@@ -0,0 +1,348 @@
|
||||
package sshpush
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
func TestSha256Hex(t *testing.T) {
|
||||
got := sha256Hex([]byte("hello"))
|
||||
want := "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
|
||||
if got != want {
|
||||
t.Errorf("sha256Hex = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitDir(t *testing.T) {
|
||||
dir, base := splitDir("/etc/orca/foo.conf")
|
||||
if dir != "/etc/orca" || base != "/etc/orca" {
|
||||
t.Errorf("splitDir(/etc/orca/foo.conf) = (%q,%q), want (/etc/orca,/etc/orca)", dir, base)
|
||||
}
|
||||
dir, base = splitDir("foo.conf")
|
||||
if dir != "" || base != "." {
|
||||
t.Errorf("splitDir(foo.conf) = (%q,%q), want (\"\",.)", dir, base)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomToken(t *testing.T) {
|
||||
a := randomToken(8)
|
||||
b := randomToken(8)
|
||||
if a == b {
|
||||
t.Error("randomToken returned same value twice")
|
||||
}
|
||||
if len(a) != 16 { // 8 bytes hex = 16 chars
|
||||
t.Errorf("randomToken(8) len = %d, want 16", len(a))
|
||||
}
|
||||
}
|
||||
|
||||
// --- WriteFileIdempotent tests via the fake SSH server ---
|
||||
|
||||
func TestWriteFileIdempotent_WritesWhenFileMissing(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
content := []byte("first content\n")
|
||||
written, err := tr.WriteFileIdempotent(context.Background(), srv.addr(), "/etc/orca/a.conf", content, 0o644)
|
||||
if err != nil {
|
||||
t.Fatalf("WriteFileIdempotent: %v", err)
|
||||
}
|
||||
if !written {
|
||||
t.Error("written=false, want true (file was missing)")
|
||||
}
|
||||
srv.mu.Lock()
|
||||
got := srv.files["/etc/orca/a.conf"]
|
||||
srv.mu.Unlock()
|
||||
if got != string(content) {
|
||||
t.Errorf("remote file = %q, want %q", got, string(content))
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteFileIdempotent_SkipsWhenContentMatches(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
content := []byte("same content\n")
|
||||
srv.mu.Lock()
|
||||
srv.files["/etc/orca/b.conf"] = string(content)
|
||||
srv.mu.Unlock()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
written, err := tr.WriteFileIdempotent(context.Background(), srv.addr(), "/etc/orca/b.conf", content, 0o644)
|
||||
if err != nil {
|
||||
t.Fatalf("WriteFileIdempotent: %v", err)
|
||||
}
|
||||
if written {
|
||||
t.Error("written=true, want false (content matched)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteFileIdempotent_WritesWhenContentDiffers(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
srv.mu.Lock()
|
||||
srv.files["/etc/orca/c.conf"] = "old content\n"
|
||||
srv.mu.Unlock()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
newContent := []byte("new content\n")
|
||||
written, err := tr.WriteFileIdempotent(context.Background(), srv.addr(), "/etc/orca/c.conf", newContent, 0o644)
|
||||
if err != nil {
|
||||
t.Fatalf("WriteFileIdempotent: %v", err)
|
||||
}
|
||||
if !written {
|
||||
t.Error("written=false, want true (content differed)")
|
||||
}
|
||||
srv.mu.Lock()
|
||||
got := srv.files["/etc/orca/c.conf"]
|
||||
srv.mu.Unlock()
|
||||
if got != string(newContent) {
|
||||
t.Errorf("remote file = %q, want %q", got, string(newContent))
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteFile_DelegatesToIdempotent(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
content := []byte("delegated\n")
|
||||
if err := tr.WriteFile(context.Background(), srv.addr(), "/etc/orca/d.conf", content, 0o600); err != nil {
|
||||
t.Fatalf("WriteFile: %v", err)
|
||||
}
|
||||
srv.mu.Lock()
|
||||
got := srv.files["/etc/orca/d.conf"]
|
||||
srv.mu.Unlock()
|
||||
if got != string(content) {
|
||||
t.Errorf("remote file = %q, want %q", got, string(content))
|
||||
}
|
||||
}
|
||||
|
||||
// --- Pure-logic idempotency tests via mock session (no SSH server) ---
|
||||
|
||||
// mockHashSession returns the hash of the file matching the sha256sum
|
||||
// command's path argument; for write commands (mkdir + cat >), it
|
||||
// records the write. This lets us test the idempotency decision logic
|
||||
// without a real SSH server.
|
||||
type mockHashSession struct {
|
||||
files map[string]string
|
||||
out []byte
|
||||
err error
|
||||
cmd string
|
||||
writeHook func(cmd string)
|
||||
}
|
||||
|
||||
func (m *mockHashSession) CombinedOutput(cmd string) ([]byte, error) {
|
||||
m.cmd = cmd
|
||||
c := strings.TrimSpace(cmd)
|
||||
if strings.HasPrefix(c, "sha256sum ") {
|
||||
rest := strings.TrimSpace(strings.TrimPrefix(c, "sha256sum "))
|
||||
rest = strings.TrimSuffix(rest, " 2>/dev/null")
|
||||
rest = strings.TrimSpace(rest)
|
||||
path := unquote(rest)
|
||||
content, ok := m.files[path]
|
||||
if !ok {
|
||||
return []byte(""), nil
|
||||
}
|
||||
sum := sha256.Sum256([]byte(content))
|
||||
return []byte(hex.EncodeToString(sum[:]) + " " + path + "\n"), nil
|
||||
}
|
||||
if strings.HasPrefix(c, "mkdir -p ") && strings.Contains(c, "cat >") {
|
||||
if m.writeHook != nil {
|
||||
m.writeHook(c)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
return m.out, m.err
|
||||
}
|
||||
func (m *mockHashSession) Close() error { return nil }
|
||||
|
||||
func TestWriteFileIdempotent_MockSkip(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
files := map[string]string{"/x/f": "match"}
|
||||
var writes int
|
||||
tr.SetSessionFactory(func(c *ssh.Client) (sshSession, error) {
|
||||
return &mockHashSession{
|
||||
files: files,
|
||||
writeHook: func(string) { writes++ },
|
||||
}, nil
|
||||
})
|
||||
client, err := tr.dial(srv.addr())
|
||||
if err != nil {
|
||||
t.Fatalf("dial: %v", err)
|
||||
}
|
||||
tr.pool.Store(srv.addr(), client)
|
||||
written, err := tr.WriteFileIdempotent(context.Background(), srv.addr(), "/x/f", []byte("match"), 0o644)
|
||||
if err != nil {
|
||||
t.Fatalf("WriteFileIdempotent: %v", err)
|
||||
}
|
||||
if written {
|
||||
t.Error("written=true, want false (hash matched)")
|
||||
}
|
||||
if writes != 0 {
|
||||
t.Errorf("writes = %d, want 0 (no write on hash match)", writes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteFileIdempotent_MockWrite(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
files := map[string]string{"/x/f": "old"}
|
||||
var writes int
|
||||
tr.SetSessionFactory(func(c *ssh.Client) (sshSession, error) {
|
||||
return &mockHashSession{
|
||||
files: files,
|
||||
writeHook: func(string) { writes++ },
|
||||
}, nil
|
||||
})
|
||||
client, err := tr.dial(srv.addr())
|
||||
if err != nil {
|
||||
t.Fatalf("dial: %v", err)
|
||||
}
|
||||
tr.pool.Store(srv.addr(), client)
|
||||
written, err := tr.WriteFileIdempotent(context.Background(), srv.addr(), "/x/f", []byte("new"), 0o644)
|
||||
if err != nil {
|
||||
t.Fatalf("WriteFileIdempotent: %v", err)
|
||||
}
|
||||
if !written {
|
||||
t.Error("written=false, want true (hash differed)")
|
||||
}
|
||||
if writes != 1 {
|
||||
t.Errorf("writes = %d, want 1", writes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteFileIdempotent_MockMissingFile(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
files := map[string]string{}
|
||||
var writes int
|
||||
tr.SetSessionFactory(func(c *ssh.Client) (sshSession, error) {
|
||||
return &mockHashSession{
|
||||
files: files,
|
||||
writeHook: func(string) { writes++ },
|
||||
}, nil
|
||||
})
|
||||
client, err := tr.dial(srv.addr())
|
||||
if err != nil {
|
||||
t.Fatalf("dial: %v", err)
|
||||
}
|
||||
tr.pool.Store(srv.addr(), client)
|
||||
written, err := tr.WriteFileIdempotent(context.Background(), srv.addr(), "/x/new", []byte("fresh"), 0o644)
|
||||
if err != nil {
|
||||
t.Fatalf("WriteFileIdempotent: %v", err)
|
||||
}
|
||||
if !written {
|
||||
t.Error("written=false, want true (file missing)")
|
||||
}
|
||||
if writes != 1 {
|
||||
t.Errorf("writes = %d, want 1", writes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoteSHA256_ParsesDigest(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
srv.mu.Lock()
|
||||
srv.files["/x/h"] = "abc"
|
||||
srv.mu.Unlock()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
got, err := tr.remoteSHA256(context.Background(), srv.addr(), "/x/h")
|
||||
if err != nil {
|
||||
t.Fatalf("remoteSHA256: %v", err)
|
||||
}
|
||||
want := fmt.Sprintf("%x", sha256.Sum256([]byte("abc")))
|
||||
if got != want {
|
||||
t.Errorf("remoteSHA256 = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoteSHA256_MissingFile(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
got, err := t_remoteSHA256_noErr(t, tr, srv.addr(), "/missing")
|
||||
if err != nil {
|
||||
t.Fatalf("remoteSHA256: %v", err)
|
||||
}
|
||||
if got != "" {
|
||||
t.Errorf("remoteSHA256 = %q, want empty for missing file", got)
|
||||
}
|
||||
}
|
||||
|
||||
func t_remoteSHA256_noErr(t *testing.T, tr *Transport, peer, path string) (string, error) {
|
||||
t.Helper()
|
||||
return tr.remoteSHA256(context.Background(), peer, path)
|
||||
}
|
||||
|
||||
func TestWriteFile_ModeApplied(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
if err := tr.WriteFile(context.Background(), srv.addr(), "/m/f", []byte("mode"), 0o755); err != nil {
|
||||
t.Fatalf("WriteFile: %v", err)
|
||||
}
|
||||
srv.mu.Lock()
|
||||
got := srv.files["/m/f"]
|
||||
srv.mu.Unlock()
|
||||
if got != "mode" {
|
||||
t.Errorf("content = %q, want mode", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteFileIdempotent_ExecError(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
tr.SetSessionFactory(func(c *ssh.Client) (sshSession, error) {
|
||||
return &mockSession{err: fmt.Errorf("%w: boom", ErrPermanent)}, nil
|
||||
})
|
||||
client, err := tr.dial(srv.addr())
|
||||
if err != nil {
|
||||
t.Fatalf("dial: %v", err)
|
||||
}
|
||||
tr.pool.Store(srv.addr(), client)
|
||||
_, err = tr.WriteFileIdempotent(context.Background(), srv.addr(), "/x/f", []byte("z"), 0o644)
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMode(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
in string
|
||||
want os.FileMode
|
||||
}{
|
||||
{"0644", 0o644},
|
||||
{"0755", 0o755},
|
||||
{"0600", 0o600},
|
||||
{"bad", 0o644},
|
||||
{"", 0o644},
|
||||
{"0", 0o644},
|
||||
} {
|
||||
got := parseMode(tc.in)
|
||||
if got != tc.want {
|
||||
t.Errorf("parseMode(%q) = %o, want %o", tc.in, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var _ = errors.New
|
||||
@@ -0,0 +1,483 @@
|
||||
package sshpush
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/proxmox"
|
||||
)
|
||||
|
||||
// Default timeouts and retry parameters (REQ-073, I-B-001).
|
||||
const (
|
||||
// ExecTimeout is the default per-exec timeout for a single SSH
|
||||
// command (I-B-001).
|
||||
ExecTimeout = 10 * time.Second
|
||||
// SCPTimeout is the default per-SCP timeout for a single file
|
||||
// transfer (I-B-001).
|
||||
SCPTimeout = 30 * time.Second
|
||||
// DialTimeout is the default SSH dial timeout.
|
||||
DialTimeout = 15 * time.Second
|
||||
// RetryInitial is the first backoff interval (v0.8 transport/retry.go).
|
||||
RetryInitial = 100 * time.Millisecond
|
||||
// RetryMax is the cap on backoff between attempts.
|
||||
RetryMax = 5 * time.Second
|
||||
// RetryMaxAttempts is the total attempt count (including the first).
|
||||
RetryMaxAttempts = 5
|
||||
)
|
||||
|
||||
// Sentinel errors. ErrTransient marks a transient failure worth
|
||||
// retrying; ErrPermanent marks a non-retryable failure (auth, host-key
|
||||
// mismatch, validation). These mirror the v0.8 transport sentinels
|
||||
// (reimplemented here since internal/transport is not imported).
|
||||
var (
|
||||
ErrTransient = errors.New("sshpush: transient error")
|
||||
ErrPermanent = errors.New("sshpush: permanent error")
|
||||
ErrNotConnected = errors.New("sshpush: not connected")
|
||||
)
|
||||
|
||||
// Transport is the SSH-push transport (REQ-073). It reuses one
|
||||
// *ssh.Client per peer across multiple operations within a single CLI
|
||||
// invocation (I-B-001). The zero value is NOT usable; construct one with
|
||||
// NewTransport.
|
||||
type Transport struct {
|
||||
// pool caches *ssh.Client per peer address ("host:port").
|
||||
pool sync.Map
|
||||
// keyPath is the SSH private key path (Ed25519, D-037).
|
||||
keyPath string
|
||||
// knownHostsPath is the v0.9 known_hosts path (paths.KnownHostsPath()
|
||||
// = ClusterDir()/known_hosts). It is stored for the v0.10-P14 migration
|
||||
// when proxmox.TOFUHostKeyCallback will accept a path parameter; today
|
||||
// the callback reads certpaths.KnownHostsPath() (the v0.8 flat layout)
|
||||
// directly, so this field is not yet read by dial(). Tests set
|
||||
// $ORCA_HOME so certpaths.KnownHostsPath() resolves under the temp dir.
|
||||
knownHostsPath string
|
||||
// user is the remote SSH user (default "orca", D-037).
|
||||
user string
|
||||
// signer is the parsed SSH private key signer, set lazily on first
|
||||
// dial.
|
||||
signer ssh.Signer
|
||||
signErr error
|
||||
// signerOnce guards signer initialization.
|
||||
signerOnce sync.Once
|
||||
|
||||
// dialer is the SSH dialer. Tests override it to inject a mock
|
||||
// server. The default uses ssh.DialContext via the context-aware
|
||||
// wrapper.
|
||||
dialer sshDialer
|
||||
|
||||
// sessionFactory returns a new session for a given client. Tests
|
||||
// override it to inject mock sessions without a real *ssh.Client.
|
||||
// When nil, the default (*ssh.Client).NewSession is used.
|
||||
sessionFactory func(*ssh.Client) (sshSession, error)
|
||||
|
||||
// mu guards the closed flag (pool iteration is sync.Map.Range).
|
||||
closed bool
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// sshSession is the minimal *ssh.Session surface the transport uses.
|
||||
// It lets tests substitute a mock without a real SSH server.
|
||||
type sshSession interface {
|
||||
CombinedOutput(cmd string) ([]byte, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
// sshDialer is the SSH dialer interface (mirrors proxmox.sshDialerType).
|
||||
// The default uses ssh.Dial; tests inject mocks that return a fake
|
||||
// *ssh.Client or an error.
|
||||
type sshDialer interface {
|
||||
DialContext(ctx context.Context, network, addr string, config *ssh.ClientConfig) (*ssh.Client, error)
|
||||
}
|
||||
|
||||
// defaultSSHDialer wraps ssh.Dial with a context-aware connect timeout.
|
||||
type defaultSSHDialer struct{}
|
||||
|
||||
func (defaultSSHDialer) DialContext(ctx context.Context, network, addr string, config *ssh.ClientConfig) (*ssh.Client, error) {
|
||||
d := net.Dialer{Timeout: config.Timeout}
|
||||
if d.Timeout == 0 {
|
||||
d.Timeout = DialTimeout
|
||||
}
|
||||
conn, err := d.DialContext(ctx, network, addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sshConn, chans, reqs, err := ssh.NewClientConn(conn, addr, config)
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
return ssh.NewClient(sshConn, chans, reqs), nil
|
||||
}
|
||||
|
||||
// NewTransport returns a Transport configured with the given SSH
|
||||
// private key path and known_hosts path. The known_hosts path is the v0.9
|
||||
// location (paths.KnownHostsPath); it is stored for the v0.10-P14
|
||||
// migration when the TOFU callback will accept a path parameter. Today
|
||||
// dial() delegates host-key verification to proxmox.TOFUHostKeyCallback,
|
||||
// which reads certpaths.KnownHostsPath() (the v0.8 flat layout under
|
||||
// $ORCA_HOME) directly — so callers must ensure $ORCA_HOME points at the
|
||||
// cluster root (the CLI sets this up). The remote user defaults to
|
||||
// "orca" (D-037); override with SetUser. The dialer defaults to the
|
||||
// real ssh.Dial-based dialer; tests call SetDialer to inject a mock.
|
||||
func NewTransport(keyPath, knownHostsPath string) *Transport {
|
||||
return &Transport{
|
||||
keyPath: keyPath,
|
||||
knownHostsPath: knownHostsPath,
|
||||
user: "orca",
|
||||
dialer: defaultSSHDialer{},
|
||||
}
|
||||
}
|
||||
|
||||
// SetUser overrides the remote SSH user (default "orca").
|
||||
func (t *Transport) SetUser(user string) {
|
||||
if user != "" {
|
||||
t.user = user
|
||||
}
|
||||
}
|
||||
|
||||
// SetDialer overrides the SSH dialer (for tests).
|
||||
func (t *Transport) SetDialer(d sshDialer) {
|
||||
if d != nil {
|
||||
t.dialer = d
|
||||
}
|
||||
}
|
||||
|
||||
// SetSessionFactory overrides the session factory (for tests). The
|
||||
// factory is called per-exec/write/read to obtain a fresh session; it
|
||||
// must close the session when the test mock is done, or the transport
|
||||
// will call Close on the returned session.
|
||||
func (t *Transport) SetSessionFactory(f func(*ssh.Client) (sshSession, error)) {
|
||||
t.sessionFactory = f
|
||||
}
|
||||
|
||||
// dial returns the cached *ssh.Client for peer, dialing and caching on
|
||||
// first use (I-B-001 connection pooling). Returns an error if the dial
|
||||
// fails or the transport is closed.
|
||||
func (t *Transport) dial(peer string) (*ssh.Client, error) {
|
||||
t.mu.Lock()
|
||||
if t.closed {
|
||||
t.mu.Unlock()
|
||||
return nil, ErrPermanent
|
||||
}
|
||||
t.mu.Unlock()
|
||||
if c, ok := t.pool.Load(peer); ok {
|
||||
return c.(*ssh.Client), nil
|
||||
}
|
||||
// Lazily parse the private key signer (once across all dials).
|
||||
t.signerOnce.Do(func() {
|
||||
keyBytes, err := os.ReadFile(t.keyPath)
|
||||
if err != nil {
|
||||
t.signErr = fmt.Errorf("sshpush: read key %s: %w", t.keyPath, err)
|
||||
return
|
||||
}
|
||||
s, err := ssh.ParsePrivateKey(keyBytes)
|
||||
if err != nil {
|
||||
t.signErr = fmt.Errorf("sshpush: parse key: %w", err)
|
||||
return
|
||||
}
|
||||
t.signer = s
|
||||
})
|
||||
if t.signErr != nil {
|
||||
return nil, t.signErr
|
||||
}
|
||||
// Host-key verification reuses the v0.8 TOFU wrapper (D-035). The
|
||||
// known_hosts file is flock-protected inside the callback on
|
||||
// first-connect capture, so we do NOT re-lock here.
|
||||
cb, err := proxmox.TOFUHostKeyCallback(peer, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sshpush: host-key callback: %w", err)
|
||||
}
|
||||
config := &ssh.ClientConfig{
|
||||
User: t.user,
|
||||
Auth: []ssh.AuthMethod{ssh.PublicKeys(t.signer)},
|
||||
HostKeyCallback: cb,
|
||||
Timeout: DialTimeout,
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), DialTimeout)
|
||||
defer cancel()
|
||||
client, err := t.dialer.DialContext(ctx, "tcp", peer, config)
|
||||
if err != nil {
|
||||
return nil, classifyDialErr(err)
|
||||
}
|
||||
// Race: two goroutines dialing the same peer concurrently both
|
||||
// create a client. Last-wins; the loser is closed. This is rare
|
||||
// (dial is rare and the pool hit short-circuits) and harmless.
|
||||
if existing, loaded := t.pool.LoadOrStore(peer, client); loaded {
|
||||
_ = client.Close()
|
||||
return existing.(*ssh.Client), nil
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// Exec runs cmd on peer over SSH and returns its combined output. The
|
||||
// default per-exec timeout is ExecTimeout (I-B-001); override by
|
||||
// passing a context with a shorter deadline. Transient failures are
|
||||
// retried with exponential backoff (100ms ×2, cap 5s, max 5 attempts —
|
||||
// the v0.8 transport/retry.go pattern, reimplemented here).
|
||||
func (t *Transport) Exec(ctx context.Context, peer string, cmd string) ([]byte, error) {
|
||||
return t.execWithRetry(ctx, peer, cmd, true)
|
||||
}
|
||||
|
||||
// execWithRetry runs the exec with retry. exec is treated as
|
||||
// idempotent (read-only) for retry purposes; the idempotency helpers
|
||||
// (WriteFileIdempotent) handle writes.
|
||||
func (t *Transport) execWithRetry(ctx context.Context, peer string, cmd string, idempotent bool) ([]byte, error) {
|
||||
var lastErr error
|
||||
for attempt := 1; attempt <= RetryMaxAttempts; attempt++ {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := t.execOnce(ctx, peer, cmd)
|
||||
if err == nil {
|
||||
return out, nil
|
||||
}
|
||||
if errors.Is(err, ErrPermanent) {
|
||||
return nil, err
|
||||
}
|
||||
lastErr = err
|
||||
if attempt == RetryMaxAttempts {
|
||||
break
|
||||
}
|
||||
if !isTransient(err) {
|
||||
return nil, err
|
||||
}
|
||||
wait := backoff(RetryInitial, RetryMax, attempt)
|
||||
timer := time.NewTimer(wait)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
timer.Stop()
|
||||
return nil, ctx.Err()
|
||||
case <-timer.C:
|
||||
}
|
||||
}
|
||||
return nil, lastErr
|
||||
}
|
||||
|
||||
// execOnce runs the command a single time against peer.
|
||||
func (t *Transport) execOnce(ctx context.Context, peer string, cmd string) ([]byte, error) {
|
||||
client, err := t.dial(peer)
|
||||
if err != nil {
|
||||
return nil, classifyDialErr(err)
|
||||
}
|
||||
sess, err := t.newSession(client)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sshpush: new session: %w", err)
|
||||
}
|
||||
defer sess.Close()
|
||||
type result struct {
|
||||
out []byte
|
||||
err error
|
||||
}
|
||||
ch := make(chan result, 1)
|
||||
go func() {
|
||||
out, err := sess.CombinedOutput(cmd)
|
||||
ch <- result{out, err}
|
||||
}()
|
||||
timeout := ExecTimeout
|
||||
if dl, ok := ctx.Deadline(); ok {
|
||||
if remaining := time.Until(dl); remaining > 0 && remaining < timeout {
|
||||
timeout = remaining
|
||||
}
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
case <-time.After(timeout):
|
||||
return nil, fmt.Errorf("sshpush: exec timeout after %s: %w", timeout, ErrTransient)
|
||||
case r := <-ch:
|
||||
if r.err != nil {
|
||||
return r.out, classifyExecErr(r.err)
|
||||
}
|
||||
return r.out, nil
|
||||
}
|
||||
}
|
||||
|
||||
// newSession returns a session for client, using the override factory
|
||||
// when set (tests), otherwise the real *ssh.Client.NewSession.
|
||||
func (t *Transport) newSession(client *ssh.Client) (sshSession, error) {
|
||||
if t.sessionFactory != nil {
|
||||
return t.sessionFactory(client)
|
||||
}
|
||||
s, err := client.NewSession()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &realSession{Session: s}, nil
|
||||
}
|
||||
|
||||
// realSession wraps *ssh.Session to satisfy the sshSession interface.
|
||||
type realSession struct {
|
||||
*ssh.Session
|
||||
}
|
||||
|
||||
func (r *realSession) CombinedOutput(cmd string) ([]byte, error) {
|
||||
return r.Session.CombinedOutput(cmd)
|
||||
}
|
||||
|
||||
// WriteFile SCPs content to peer:path atomically (write-to-tmp + mv,
|
||||
// REQ-074). The default per-SCP timeout is SCPTimeout (I-B-001).
|
||||
// Idempotency: if the file already exists with the same SHA-256, the
|
||||
// write is skipped (C-18). Use WriteFileIdempotent for the explicit
|
||||
// written/skipped result.
|
||||
func (t *Transport) WriteFile(ctx context.Context, peer string, path string, content []byte, mode os.FileMode) error {
|
||||
_, err := t.WriteFileIdempotent(ctx, peer, path, content, mode)
|
||||
return err
|
||||
}
|
||||
|
||||
// ReadFile reads the file at peer:path via SSH cat.
|
||||
func (t *Transport) ReadFile(ctx context.Context, peer string, path string) ([]byte, error) {
|
||||
cmd := fmt.Sprintf("cat %s", shellQuote(path))
|
||||
out, err := t.Exec(ctx, peer, cmd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Close closes all pooled SSH clients (REQ-073). Safe to call
|
||||
// multiple times; subsequent calls are no-ops.
|
||||
func (t *Transport) Close() error {
|
||||
t.mu.Lock()
|
||||
if t.closed {
|
||||
t.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
t.closed = true
|
||||
t.mu.Unlock()
|
||||
var firstErr error
|
||||
t.pool.Range(func(key, value any) bool {
|
||||
if c, ok := value.(*ssh.Client); ok {
|
||||
if err := c.Close(); err != nil && firstErr == nil {
|
||||
firstErr = err
|
||||
}
|
||||
}
|
||||
t.pool.Delete(key)
|
||||
return true
|
||||
})
|
||||
return firstErr
|
||||
}
|
||||
|
||||
// backoff returns the wait duration for the n-th attempt (1-indexed).
|
||||
// Formula: min(Initial * 2^(n-1), Max), with up to 25% jitter (matches
|
||||
// v0.8 transport/retry.go).
|
||||
func backoff(initial, max time.Duration, n int) time.Duration {
|
||||
d := initial
|
||||
for i := 1; i < n; i++ {
|
||||
d *= 2
|
||||
if d > max {
|
||||
d = max
|
||||
break
|
||||
}
|
||||
}
|
||||
if d <= 0 {
|
||||
return 0
|
||||
}
|
||||
jitter := time.Duration(rand.Int63n(int64(d) / 2))
|
||||
d = d - d/4 + jitter
|
||||
if d < 0 {
|
||||
d = 0
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// isTransient reports whether err looks like a transient failure worth
|
||||
// retrying (mirrors v0.8 transport.IsTransient, reimplemented here).
|
||||
func isTransient(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
if errors.Is(err, ErrTransient) {
|
||||
return true
|
||||
}
|
||||
if errors.Is(err, ErrPermanent) {
|
||||
return false
|
||||
}
|
||||
s := err.Error()
|
||||
for _, sub := range []string{
|
||||
"connection refused", "i/o timeout", "EOF",
|
||||
"no such host", "connection reset", "timeout",
|
||||
"deadline exceeded", "temporarily unavailable",
|
||||
} {
|
||||
if strings.Contains(s, sub) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// classifyDialErr converts a raw ssh.Dial error into a transport error
|
||||
// (transient vs permanent). Auth failures and host-key mismatches are
|
||||
// permanent; everything else is transient.
|
||||
func classifyDialErr(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
s := err.Error()
|
||||
if strings.Contains(s, "unable to authenticate") || strings.Contains(s, "handshake failed") {
|
||||
return fmt.Errorf("%w: %v", ErrPermanent, err)
|
||||
}
|
||||
if strings.Contains(s, "host key") && strings.Contains(s, "mismatch") {
|
||||
return fmt.Errorf("%w: %v", ErrPermanent, err)
|
||||
}
|
||||
if strings.Contains(s, "knownhosts") {
|
||||
return fmt.Errorf("%w: %v", ErrPermanent, err)
|
||||
}
|
||||
return fmt.Errorf("%w: %v", ErrTransient, err)
|
||||
}
|
||||
|
||||
// classifyExecErr converts a raw session exec error into a transport
|
||||
// error. Non-zero exit codes are NOT transient (the command ran; the
|
||||
// failure is logical, not network). Session-creation failures and
|
||||
// network-level errors are transient.
|
||||
func classifyExecErr(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
var exitErr *ssh.ExitError
|
||||
if errors.As(err, &exitErr) {
|
||||
return fmt.Errorf("%w: exit %d", ErrPermanent, exitErr.ExitStatus())
|
||||
}
|
||||
s := err.Error()
|
||||
for _, sub := range []string{"EOF", "session closed", "channel closed"} {
|
||||
if strings.Contains(s, sub) {
|
||||
return fmt.Errorf("%w: %v", ErrTransient, err)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("%w: %v", ErrPermanent, err)
|
||||
}
|
||||
|
||||
// shellQuote single-quotes a path for safe shell interpolation. It
|
||||
// escapes embedded single-quotes via the standard '\” idiom.
|
||||
func shellQuote(s string) string {
|
||||
return "'" + strings.ReplaceAll(s, "'", "'\\''") + "'"
|
||||
}
|
||||
|
||||
// remoteSHA256 returns the SHA-256 of the file at peer:path via SSH
|
||||
// `sha256sum`, or ("", error) if the file is missing or the command
|
||||
// fails. The returned hash is the hex digest (lowercase, no filename).
|
||||
func (t *Transport) remoteSHA256(ctx context.Context, peer string, path string) (string, error) {
|
||||
cmd := fmt.Sprintf("sha256sum %s 2>/dev/null", shellQuote(path))
|
||||
out, err := t.execWithRetry(ctx, peer, cmd, true)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
out = bytes.TrimSpace(out)
|
||||
if len(out) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
fields := strings.Fields(string(out))
|
||||
if len(fields) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
return fields[0], nil
|
||||
}
|
||||
@@ -0,0 +1,664 @@
|
||||
package sshpush
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ed25519"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
"golang.org/x/crypto/ssh/knownhosts"
|
||||
)
|
||||
|
||||
// --- fakeSSHServer: a minimal in-process SSH server for hermetic tests.
|
||||
|
||||
type fakeSSHServer struct {
|
||||
listener net.Listener
|
||||
config *ssh.ServerConfig
|
||||
done chan struct{}
|
||||
|
||||
mu sync.Mutex
|
||||
files map[string]string
|
||||
hostKey ssh.Signer
|
||||
cmdCount int64
|
||||
execDelay time.Duration
|
||||
}
|
||||
|
||||
func newFakeSSHServer(t *testing.T) *fakeSSHServer {
|
||||
t.Helper()
|
||||
_, priv, err := ed25519.GenerateKey(rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatalf("ed25519 gen: %v", err)
|
||||
}
|
||||
signer, err := ssh.NewSignerFromKey(priv)
|
||||
if err != nil {
|
||||
t.Fatalf("ssh signer: %v", err)
|
||||
}
|
||||
config := &ssh.ServerConfig{
|
||||
NoClientAuth: true,
|
||||
}
|
||||
config.AddHostKey(signer)
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("listen: %v", err)
|
||||
}
|
||||
srv := &fakeSSHServer{
|
||||
listener: ln,
|
||||
config: config,
|
||||
done: make(chan struct{}),
|
||||
files: make(map[string]string),
|
||||
hostKey: signer,
|
||||
}
|
||||
go srv.serve()
|
||||
return srv
|
||||
}
|
||||
|
||||
func (s *fakeSSHServer) addr() string { return s.listener.Addr().String() }
|
||||
func (s *fakeSSHServer) hostPublicKey() ssh.PublicKey { return s.hostKey.PublicKey() }
|
||||
|
||||
func (s *fakeSSHServer) close() {
|
||||
_ = s.listener.Close()
|
||||
<-s.done
|
||||
}
|
||||
|
||||
func (s *fakeSSHServer) setExecDelay(d time.Duration) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.execDelay = d
|
||||
}
|
||||
|
||||
func (s *fakeSSHServer) serve() {
|
||||
for {
|
||||
conn, err := s.listener.Accept()
|
||||
if err != nil {
|
||||
close(s.done)
|
||||
return
|
||||
}
|
||||
go s.handle(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *fakeSSHServer) handle(netConn net.Conn) {
|
||||
defer netConn.Close()
|
||||
_, chans, reqs, err := ssh.NewServerConn(netConn, s.config)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
go ssh.DiscardRequests(reqs)
|
||||
for newChan := range chans {
|
||||
if newChan.ChannelType() != "session" {
|
||||
newChan.Reject(ssh.UnknownChannelType, "only session")
|
||||
continue
|
||||
}
|
||||
go s.handleSession(newChan)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *fakeSSHServer) handleSession(newChan ssh.NewChannel) {
|
||||
ch, reqs, err := newChan.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer ch.Close()
|
||||
for req := range reqs {
|
||||
if req.Type != "exec" {
|
||||
req.Reply(false, nil)
|
||||
continue
|
||||
}
|
||||
var execReq struct{ Command string }
|
||||
if err := ssh.Unmarshal(req.Payload, &execReq); err != nil {
|
||||
req.Reply(false, nil)
|
||||
continue
|
||||
}
|
||||
req.Reply(true, nil)
|
||||
atomic.AddInt64(&s.cmdCount, 1)
|
||||
s.mu.Lock()
|
||||
delay := s.execDelay
|
||||
s.mu.Unlock()
|
||||
if delay > 0 {
|
||||
time.Sleep(delay)
|
||||
}
|
||||
out, code := s.runCommand(execReq.Command)
|
||||
_, _ = ch.Write(out)
|
||||
_, _ = ch.SendRequest("exit-status", false, ssh.Marshal(struct{ Code uint32 }{uint32(code)}))
|
||||
_ = ch.Close()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// runCommand implements the minimal command surface the transport uses:
|
||||
// echo (for exec tests), sha256sum (for idempotency), cat (read), and the
|
||||
// heredoc-based write (cat > tmp <<EOF ... EOF && chmod ... && mv ...).
|
||||
func (s *fakeSSHServer) runCommand(cmd string) ([]byte, int) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
trimmed := strings.TrimSpace(cmd)
|
||||
switch {
|
||||
case trimmed == "echo hello":
|
||||
return []byte("hello\n"), 0
|
||||
case strings.HasPrefix(trimmed, "sha256sum "):
|
||||
// Format: sha256sum '/path' 2>/dev/null
|
||||
rest := strings.TrimSpace(strings.TrimPrefix(trimmed, "sha256sum "))
|
||||
rest = strings.TrimSuffix(rest, " 2>/dev/null")
|
||||
rest = strings.TrimSpace(rest)
|
||||
path := unquote(rest)
|
||||
content, ok := s.files[path]
|
||||
if !ok {
|
||||
// `2>/dev/null` swallows the error; sha256sum exits 1 but
|
||||
// stderr is suppressed. The transport treats empty output as
|
||||
// "file missing" (no hash), so return ("", 0).
|
||||
return []byte(""), 0
|
||||
}
|
||||
sum := sha256HexStr([]byte(content))
|
||||
return []byte(sum + " " + path + "\n"), 0
|
||||
case strings.HasPrefix(trimmed, "cat '"):
|
||||
path := unquote(strings.TrimPrefix(trimmed, "cat "))
|
||||
content, ok := s.files[path]
|
||||
if !ok {
|
||||
return []byte("cat: " + path + ": No such file or directory\n"), 1
|
||||
}
|
||||
return []byte(content), 0
|
||||
case strings.HasPrefix(trimmed, "mkdir -p ") && strings.Contains(trimmed, "cat >"):
|
||||
return s.handleWrite(trimmed)
|
||||
default:
|
||||
return []byte("sh: command not found\n"), 127
|
||||
}
|
||||
}
|
||||
|
||||
// handleWrite parses the heredoc write command produced by writeFile.
|
||||
// Command format:
|
||||
//
|
||||
// mkdir -p '<dir>' && cat > '<tmp>' <<'ORCA_PUSH_EOF_a1b2c3'
|
||||
// <content>
|
||||
// ORCA_PUSH_EOF_a1b2c3
|
||||
// chmod <mode> '<tmp>' && mv -f '<tmp>' '<path>'
|
||||
func (s *fakeSSHServer) handleWrite(cmd string) ([]byte, int) {
|
||||
const eof = "ORCA_PUSH_EOF_a1b2c3"
|
||||
// Find the opening heredoc line: ... <<'EOF'\n
|
||||
openerIdx := strings.Index(cmd, "<<'"+eof+"'")
|
||||
if openerIdx < 0 {
|
||||
return []byte("sh: no heredoc opener\n"), 1
|
||||
}
|
||||
// Body starts after the opener line's newline.
|
||||
rest := cmd[openerIdx+len("<<'"+eof+"'"):]
|
||||
nl := strings.Index(rest, "\n")
|
||||
if nl < 0 {
|
||||
return []byte("sh: no body start\n"), 1
|
||||
}
|
||||
body := rest[nl+1:]
|
||||
// Body ends at the closing EOF marker on its own line.
|
||||
closeIdx := strings.Index(body, "\n"+eof+"\n")
|
||||
if closeIdx < 0 {
|
||||
// Maybe EOF is at the end without trailing newline.
|
||||
closeIdx = strings.Index(body, "\n"+eof)
|
||||
if closeIdx < 0 {
|
||||
return []byte("sh: no heredoc close\n"), 1
|
||||
}
|
||||
body = body[:closeIdx]
|
||||
} else {
|
||||
body = body[:closeIdx]
|
||||
}
|
||||
// Find the mv target: last quoted arg of "mv -f 'tmp' 'path'".
|
||||
mvIdx := strings.LastIndex(cmd, "mv -f ")
|
||||
if mvIdx < 0 {
|
||||
return []byte("sh: no mv\n"), 1
|
||||
}
|
||||
tail := cmd[mvIdx+len("mv -f "):]
|
||||
parts := splitQuoted(tail)
|
||||
if len(parts) < 2 {
|
||||
return []byte("sh: bad mv args\n"), 1
|
||||
}
|
||||
target := parts[1]
|
||||
s.files[target] = body
|
||||
return nil, 0
|
||||
}
|
||||
|
||||
// splitQuoted splits a string of the form "'a' 'b'" into ["a","b"].
|
||||
func splitQuoted(s string) []string {
|
||||
var out []string
|
||||
var cur strings.Builder
|
||||
in := false
|
||||
for _, r := range s {
|
||||
if r == '\'' {
|
||||
if in {
|
||||
out = append(out, cur.String())
|
||||
cur.Reset()
|
||||
}
|
||||
in = !in
|
||||
continue
|
||||
}
|
||||
if in {
|
||||
cur.WriteRune(r)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func unquote(s string) string {
|
||||
s = strings.TrimSpace(s)
|
||||
if len(s) >= 2 && s[0] == '\'' && s[len(s)-1] == '\'' {
|
||||
return s[1 : len(s)-1]
|
||||
}
|
||||
if len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"' {
|
||||
return s[1 : len(s)-1]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// sha256HexStr is a test-local copy of the sha256Hex helper.
|
||||
func sha256HexStr(b []byte) string {
|
||||
sum := sha256.Sum256(b)
|
||||
return fmt.Sprintf("%x", sum[:])
|
||||
}
|
||||
|
||||
// --- test helpers for transport setup ---
|
||||
|
||||
// setupORCAHome creates a temp ORCA_HOME with an empty known_hosts (at
|
||||
// the v0.8 flat location $ORCA_HOME/known_hosts, which is where
|
||||
// proxmox.TOFUHostKeyCallback reads via certpaths.KnownHostsPath()) and a
|
||||
// generated Ed25519 SSH key, returns the key path.
|
||||
func setupORCAHome(t *testing.T) (keyPath string) {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
t.Setenv("ORCA_HOME", dir)
|
||||
// certpaths.KnownHostsPath() = paths.Root()/known_hosts = $ORCA_HOME/known_hosts.
|
||||
knownHosts := filepath.Join(dir, "known_hosts")
|
||||
if err := os.WriteFile(knownHosts, []byte{}, 0o600); err != nil {
|
||||
t.Fatalf("create known_hosts: %v", err)
|
||||
}
|
||||
_, priv, err := ed25519.GenerateKey(rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatalf("ed25519 gen: %v", err)
|
||||
}
|
||||
der, err := x509.MarshalPKCS8PrivateKey(priv)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal key: %v", err)
|
||||
}
|
||||
pemBytes := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: der})
|
||||
keyPath = filepath.Join(dir, "orca_ssh_key")
|
||||
if err := os.WriteFile(keyPath, pemBytes, 0o600); err != nil {
|
||||
t.Fatalf("write key: %v", err)
|
||||
}
|
||||
return keyPath
|
||||
}
|
||||
|
||||
// realTransport returns a Transport wired to use the real SSH dialer
|
||||
// against a fake SSH server, with the server's host key pre-populated in
|
||||
// known_hosts (so the TOFU callback matches on first dial — no first-
|
||||
// connect write race in tests).
|
||||
func realTransport(t *testing.T, srv *fakeSSHServer) *Transport {
|
||||
t.Helper()
|
||||
keyPath := setupORCAHome(t)
|
||||
tr := NewTransport(keyPath, "")
|
||||
tr.SetUser("root")
|
||||
addr := srv.addr()
|
||||
line := knownhosts.Line([]string{knownhosts.Normalize(addr)}, srv.hostPublicKey())
|
||||
home := os.Getenv("ORCA_HOME")
|
||||
kh := filepath.Join(home, "known_hosts")
|
||||
if err := os.WriteFile(kh, []byte(line+"\n"), 0o600); err != nil {
|
||||
t.Fatalf("pre-pop known_hosts: %v", err)
|
||||
}
|
||||
return tr
|
||||
}
|
||||
|
||||
// --- mock dialer + mock session for pure-logic tests (no real SSH) ---
|
||||
|
||||
type mockDialer struct {
|
||||
client *ssh.Client
|
||||
err error
|
||||
calls int
|
||||
}
|
||||
|
||||
func (m *mockDialer) DialContext(ctx context.Context, network, addr string, cfg *ssh.ClientConfig) (*ssh.Client, error) {
|
||||
m.calls++
|
||||
if m.err != nil {
|
||||
return nil, m.err
|
||||
}
|
||||
return m.client, nil
|
||||
}
|
||||
|
||||
type mockSession struct {
|
||||
out []byte
|
||||
err error
|
||||
cmd string
|
||||
}
|
||||
|
||||
func (m *mockSession) CombinedOutput(cmd string) ([]byte, error) {
|
||||
m.cmd = cmd
|
||||
return m.out, m.err
|
||||
}
|
||||
func (m *mockSession) Close() error { return nil }
|
||||
|
||||
// --- tests ---
|
||||
|
||||
func TestNewTransport_Defaults(t *testing.T) {
|
||||
tr := NewTransport("/tmp/key", "/tmp/kh")
|
||||
if tr.keyPath != "/tmp/key" {
|
||||
t.Errorf("keyPath = %q", tr.keyPath)
|
||||
}
|
||||
if tr.user != "orca" {
|
||||
t.Errorf("default user = %q, want orca", tr.user)
|
||||
}
|
||||
if tr.dialer == nil {
|
||||
t.Error("dialer is nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetUser(t *testing.T) {
|
||||
tr := NewTransport("/tmp/key", "/tmp/kh")
|
||||
tr.SetUser("root")
|
||||
if tr.user != "root" {
|
||||
t.Errorf("user = %q, want root", tr.user)
|
||||
}
|
||||
tr.SetUser("")
|
||||
if tr.user != "root" {
|
||||
t.Errorf("user = %q, want root", tr.user)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackoff(t *testing.T) {
|
||||
// Without jitter, attempt 1 -> 100ms, 2 -> 200ms, ... up to 5s cap.
|
||||
// The jittered result is in [d/4, 3d/4) where d is the capped base,
|
||||
// so for high attempts the result can reach 3*d/4 < 1.5*d. We bound
|
||||
// the upper end at 2x the cap to allow jitter headroom.
|
||||
for _, tc := range []struct {
|
||||
attempt int
|
||||
max time.Duration
|
||||
}{
|
||||
{1, 200 * time.Millisecond},
|
||||
{2, 400 * time.Millisecond},
|
||||
{6, 2 * RetryMax},
|
||||
{10, 2 * RetryMax},
|
||||
} {
|
||||
got := backoff(RetryInitial, RetryMax, tc.attempt)
|
||||
if got < 0 || got > tc.max {
|
||||
t.Errorf("backoff(%d) = %s, want in [0, %s]", tc.attempt, got, tc.max)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsTransient(t *testing.T) {
|
||||
if isTransient(nil) {
|
||||
t.Error("nil should not be transient")
|
||||
}
|
||||
if !isTransient(ErrTransient) {
|
||||
t.Error("ErrTransient should be transient")
|
||||
}
|
||||
if isTransient(ErrPermanent) {
|
||||
t.Error("ErrPermanent should not be transient")
|
||||
}
|
||||
if !isTransient(errors.New("connection refused")) {
|
||||
t.Error("connection refused should be transient")
|
||||
}
|
||||
if !isTransient(errors.New("i/o timeout")) {
|
||||
t.Error("i/o timeout should be transient")
|
||||
}
|
||||
if isTransient(errors.New("some other error")) {
|
||||
t.Error("unknown error should not be transient")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyDialErr(t *testing.T) {
|
||||
if got := classifyDialErr(nil); got != nil {
|
||||
t.Errorf("nil -> nil, got %v", got)
|
||||
}
|
||||
perm := classifyDialErr(errors.New("ssh: unable to authenticate"))
|
||||
if !errors.Is(perm, ErrPermanent) {
|
||||
t.Errorf("auth failure should be permanent, got %v", perm)
|
||||
}
|
||||
perm2 := classifyDialErr(errors.New("host key mismatch"))
|
||||
if !errors.Is(perm2, ErrPermanent) {
|
||||
t.Errorf("host key mismatch should be permanent, got %v", perm2)
|
||||
}
|
||||
trans := classifyDialErr(errors.New("connection refused"))
|
||||
if !errors.Is(trans, ErrTransient) {
|
||||
t.Errorf("connection refused should be transient, got %v", trans)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyExecErr(t *testing.T) {
|
||||
exitErr := ssh.ExitError{}
|
||||
perm := classifyExecErr(&exitErr)
|
||||
if !errors.Is(perm, ErrPermanent) {
|
||||
t.Errorf("ExitError should be permanent, got %v", perm)
|
||||
}
|
||||
trans := classifyExecErr(errors.New("session closed"))
|
||||
if !errors.Is(trans, ErrTransient) {
|
||||
t.Errorf("session closed should be transient, got %v", trans)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShellQuote(t *testing.T) {
|
||||
got := shellQuote("/etc/orca/foo.conf")
|
||||
if got != "'/etc/orca/foo.conf'" {
|
||||
t.Errorf("shellQuote = %q", got)
|
||||
}
|
||||
got = shellQuote("it's a path")
|
||||
if got != "'it'\\''s a path'" {
|
||||
t.Errorf("shellQuote with quote = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransport_ExecSuccess_RealSSH(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
out, err := tr.Exec(context.Background(), srv.addr(), "echo hello")
|
||||
if err != nil {
|
||||
t.Fatalf("Exec: %v", err)
|
||||
}
|
||||
if strings.TrimSpace(string(out)) != "hello" {
|
||||
t.Errorf("out = %q, want hello", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransport_ExecRetry_TransientFailure(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
var calls int32
|
||||
tr.SetSessionFactory(func(c *ssh.Client) (sshSession, error) {
|
||||
n := atomic.AddInt32(&calls, 1)
|
||||
if n < 3 {
|
||||
return &mockSession{err: errors.New("EOF")}, nil
|
||||
}
|
||||
return &mockSession{out: []byte("ok\n")}, nil
|
||||
})
|
||||
client, err := tr.dial(srv.addr())
|
||||
if err != nil {
|
||||
t.Fatalf("dial: %v", err)
|
||||
}
|
||||
tr.pool.Store(srv.addr(), client)
|
||||
out, err := tr.Exec(context.Background(), srv.addr(), "echo ok")
|
||||
if err != nil {
|
||||
t.Fatalf("Exec: %v (calls=%d)", err, atomic.LoadInt32(&calls))
|
||||
}
|
||||
if string(out) != "ok\n" {
|
||||
t.Errorf("out = %q, want ok\\n", out)
|
||||
}
|
||||
if got := atomic.LoadInt32(&calls); got < 3 {
|
||||
t.Errorf("calls = %d, want >= 3 (retried)", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransport_ExecPermanentError_NoRetry(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
var calls int32
|
||||
tr.SetSessionFactory(func(c *ssh.Client) (sshSession, error) {
|
||||
atomic.AddInt32(&calls, 1)
|
||||
return &mockSession{err: &ssh.ExitError{}}, nil
|
||||
})
|
||||
client, err := tr.dial(srv.addr())
|
||||
if err != nil {
|
||||
t.Fatalf("dial: %v", err)
|
||||
}
|
||||
tr.pool.Store(srv.addr(), client)
|
||||
_, err = tr.Exec(context.Background(), srv.addr(), "exit 1")
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
if !errors.Is(err, ErrPermanent) {
|
||||
t.Errorf("err should be permanent, got %v", err)
|
||||
}
|
||||
if got := atomic.LoadInt32(&calls); got != 1 {
|
||||
t.Errorf("calls = %d, want 1 (no retry on permanent)", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransport_ExecTimeout(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
srv.setExecDelay(500 * time.Millisecond)
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
||||
defer cancel()
|
||||
_, err := tr.Exec(ctx, srv.addr(), "echo hello")
|
||||
if err == nil {
|
||||
t.Fatal("expected timeout error, got nil")
|
||||
}
|
||||
if !errors.Is(err, context.DeadlineExceeded) && !errors.Is(err, ErrTransient) {
|
||||
t.Errorf("err should be timeout/transient, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransport_ConnectionPoolReuse(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
addr := srv.addr()
|
||||
c1, err := tr.dial(addr)
|
||||
if err != nil {
|
||||
t.Fatalf("first dial: %v", err)
|
||||
}
|
||||
c2, err := tr.dial(addr)
|
||||
if err != nil {
|
||||
t.Fatalf("second dial: %v", err)
|
||||
}
|
||||
if c1 != c2 {
|
||||
t.Error("pool did not reuse client for same peer")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransport_CloseClosesAllClients(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
tr := realTransport(t, srv)
|
||||
if _, err := tr.dial(srv.addr()); err != nil {
|
||||
t.Fatalf("dial: %v", err)
|
||||
}
|
||||
count := 0
|
||||
tr.pool.Range(func(_, _ any) bool {
|
||||
count++
|
||||
return true
|
||||
})
|
||||
if count != 1 {
|
||||
t.Fatalf("pool has %d entries, want 1", count)
|
||||
}
|
||||
if err := tr.Close(); err != nil {
|
||||
t.Errorf("Close: %v", err)
|
||||
}
|
||||
_, err := tr.dial(srv.addr())
|
||||
if !errors.Is(err, ErrPermanent) {
|
||||
t.Errorf("dial after Close should be ErrPermanent, got %v", err)
|
||||
}
|
||||
if err := tr.Close(); err != nil {
|
||||
t.Errorf("second Close: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransport_ReadFile_RealSSH(t *testing.T) {
|
||||
srv := newFakeSSHServer(t)
|
||||
defer srv.close()
|
||||
srv.mu.Lock()
|
||||
srv.files["/etc/orca/test.conf"] = "content-line\n"
|
||||
srv.mu.Unlock()
|
||||
tr := realTransport(t, srv)
|
||||
defer tr.Close()
|
||||
out, err := tr.ReadFile(context.Background(), srv.addr(), "/etc/orca/test.conf")
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile: %v", err)
|
||||
}
|
||||
if string(out) != "content-line\n" {
|
||||
t.Errorf("out = %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransport_DialKeyParseFailure(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
t.Setenv("ORCA_HOME", dir)
|
||||
if err := os.WriteFile(filepath.Join(dir, "known_hosts"), []byte{}, 0o600); err != nil {
|
||||
t.Fatalf("kh: %v", err)
|
||||
}
|
||||
keyPath := filepath.Join(dir, "bad_key")
|
||||
if err := os.WriteFile(keyPath, []byte("not a key"), 0o600); err != nil {
|
||||
t.Fatalf("write key: %v", err)
|
||||
}
|
||||
tr := NewTransport(keyPath, "")
|
||||
tr.SetUser("root")
|
||||
_, err := tr.dial("127.0.0.1:1")
|
||||
if err == nil {
|
||||
t.Fatal("expected parse error, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "parse key") {
|
||||
t.Errorf("err should mention parse key, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransport_DialKeyMissing(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
t.Setenv("ORCA_HOME", dir)
|
||||
if err := os.WriteFile(filepath.Join(dir, "known_hosts"), []byte{}, 0o600); err != nil {
|
||||
t.Fatalf("kh: %v", err)
|
||||
}
|
||||
tr := NewTransport(filepath.Join(dir, "missing_key"), "")
|
||||
tr.SetUser("root")
|
||||
_, err := tr.dial("127.0.0.1:1")
|
||||
if err == nil {
|
||||
t.Fatal("expected read error, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "read key") {
|
||||
t.Errorf("err should mention read key, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransport_DialMockFailure(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
t.Setenv("ORCA_HOME", dir)
|
||||
if err := os.WriteFile(filepath.Join(dir, "known_hosts"), []byte{}, 0o600); err != nil {
|
||||
t.Fatalf("kh: %v", err)
|
||||
}
|
||||
_, priv, _ := ed25519.GenerateKey(rand.Reader)
|
||||
der, _ := x509.MarshalPKCS8PrivateKey(priv)
|
||||
pemBytes := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: der})
|
||||
keyPath := filepath.Join(dir, "orca_ssh_key")
|
||||
_ = os.WriteFile(keyPath, pemBytes, 0o600)
|
||||
tr := NewTransport(keyPath, "")
|
||||
tr.SetUser("root")
|
||||
tr.SetDialer(&mockDialer{err: errors.New("connection refused")})
|
||||
_, err := tr.dial("127.0.0.1:1")
|
||||
if err == nil {
|
||||
t.Fatal("expected dial error")
|
||||
}
|
||||
if !errors.Is(err, ErrTransient) {
|
||||
t.Errorf("connection refused should be transient, got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user