You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
235 lines
8.4 KiB
Go
235 lines
8.4 KiB
Go
// Command virt-cloud-agent runs the host-local control-plane adapter.
|
|
//
|
|
// The first release exposes health/capability probes plus guarded domain,
|
|
// power and seed artifact paths. Remaining VM lifecycle operations stay behind
|
|
// typed API gates until their host read-back contracts are implemented.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"errors"
|
|
"flag"
|
|
"log/slog"
|
|
"net"
|
|
"os"
|
|
"os/signal"
|
|
"runtime"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"g.luoli.live/root/virt/agent/internal/api"
|
|
"g.luoli.live/root/virt/agent/internal/auth"
|
|
"g.luoli.live/root/virt/agent/internal/capability"
|
|
"g.luoli.live/root/virt/agent/internal/config"
|
|
"g.luoli.live/root/virt/agent/internal/hypervisor"
|
|
"g.luoli.live/root/virt/agent/internal/operation"
|
|
)
|
|
|
|
var version = "dev"
|
|
|
|
func main() {
|
|
logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo}))
|
|
cfg, warnings, err := config.Load(os.Getenv)
|
|
if err != nil {
|
|
logger.Error("invalid agent configuration", "error", err)
|
|
os.Exit(2)
|
|
}
|
|
applyFlags(&cfg)
|
|
if err := cfg.Validate(); err != nil {
|
|
logger.Error("invalid agent configuration", "error", err)
|
|
os.Exit(2)
|
|
}
|
|
for _, warning := range warnings {
|
|
logger.Warn("agent configuration warning", "code", warning.Code, "message", warning.Message)
|
|
}
|
|
|
|
store, err := operation.Open(cfg.JournalPath)
|
|
if err != nil {
|
|
// Liveness must remain available when a mode/owner/path issue prevents
|
|
// the journal from opening. The API will report degraded readiness and
|
|
// reject all mutating requests.
|
|
logger.Warn("operation journal unavailable; starting degraded", "error", redactedPathError(err))
|
|
store = nil
|
|
}
|
|
if store != nil {
|
|
if recovered, recoverErr := store.RecoverInFlight(); recoverErr != nil {
|
|
logger.Warn("could not recover in-flight operations; writes remain degraded", "error", recoverErr)
|
|
} else if recovered > 0 {
|
|
logger.Warn("in-flight operations marked uncertain after agent restart", "count", recovered)
|
|
}
|
|
}
|
|
requestAuth, authErr := auth.FromFileWithNonceConsumerForAgent(cfg.HMACKeyFile, cfg.RequireSigned, store, cfg.AgentID)
|
|
if authErr != nil {
|
|
logger.Warn("request signing key unavailable; starting degraded", "error", authErr.Error())
|
|
// Preserve the same agent binding and durable nonce consumer while the
|
|
// verifier is degraded. This keeps a repaired key from changing the
|
|
// request envelope or replay boundary after startup. If signing was
|
|
// explicitly optional, keep it optional; an unreadable optional key
|
|
// must not silently upgrade the control plane to required signing.
|
|
requestAuth, _ = auth.NewWithNonceConsumerForAgent(nil, cfg.RequireSigned, store, cfg.AgentID)
|
|
}
|
|
provider, backendWarning := buildProvider(cfg, os.Getenv, runtime.GOOS)
|
|
if backendWarning != "" {
|
|
logger.Warn("host backend configuration warning", "message", backendWarning)
|
|
warnings = append(warnings, config.Warning{Code: "host_backend_warning", Message: backendWarning})
|
|
}
|
|
backend := backendFromProvider(provider)
|
|
hostBackendReady := false
|
|
if backend != nil {
|
|
probeCtx, cancel := context.WithTimeout(context.Background(), cfg.RequestTimeout)
|
|
_, probeErr := backend.Probe(probeCtx)
|
|
cancel()
|
|
if probeErr != nil {
|
|
backendWarning = redactedBackendError(probeErr)
|
|
logger.Warn("host backend capability probe failed; starting degraded", "message", backendWarning)
|
|
warnings = append(warnings, config.Warning{Code: "host_backend_unavailable", Message: "local libvirt capability probe failed; mutating operations remain disabled"})
|
|
} else {
|
|
hostBackendReady = true
|
|
}
|
|
}
|
|
server, err := api.New(api.Options{
|
|
Config: cfg,
|
|
Warnings: warnings,
|
|
Store: store,
|
|
Capabilities: provider,
|
|
Backend: backend,
|
|
HostBackendReady: hostBackendReady,
|
|
Auth: requestAuth,
|
|
Logger: logger,
|
|
})
|
|
if err != nil {
|
|
logger.Error("build agent server", "error", err)
|
|
os.Exit(2)
|
|
}
|
|
|
|
var tlsConfig *tls.Config
|
|
tlsReady := !cfg.RequireMTLS
|
|
if cfg.RequireMTLS {
|
|
var tlsErr error
|
|
tlsConfig, tlsErr = server.TLSConfig()
|
|
if tlsErr != nil {
|
|
if !cfg.IsLoopbackListen() {
|
|
// Never expose even a degraded plaintext listener on a remote
|
|
// address. Health remains available for a loopback-only config;
|
|
// a production remote bind fails closed until its certificate
|
|
// material is loadable.
|
|
logger.Error("TLS listener unavailable for remote bind; refusing plaintext listener", "error", tlsErr)
|
|
os.Exit(2)
|
|
}
|
|
logger.Warn("TLS listener unavailable; serving degraded loopback diagnostics", "error", tlsErr)
|
|
} else {
|
|
tlsReady = true
|
|
}
|
|
}
|
|
server.SetTLSReady(tlsReady)
|
|
|
|
listener, err := net.Listen("tcp", cfg.ListenAddr)
|
|
if err != nil {
|
|
logger.Error("listen for agent API", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
defer listener.Close()
|
|
|
|
httpServer := server.NewHTTPServer()
|
|
serveErr := make(chan error, 1)
|
|
go func() {
|
|
if tlsConfig != nil {
|
|
listener = tlsListener(listener, tlsConfig)
|
|
}
|
|
serveErr <- httpServer.Serve(listener)
|
|
}()
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
select {
|
|
case <-ctx.Done():
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), cfg.RequestTimeout)
|
|
defer cancel()
|
|
if err := httpServer.Shutdown(shutdownCtx); err != nil {
|
|
logger.Warn("agent graceful shutdown failed", "error", err)
|
|
}
|
|
case err := <-serveErr:
|
|
if !errors.Is(err, context.Canceled) && !errors.Is(err, net.ErrClosed) {
|
|
logger.Error("agent HTTP server stopped", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
}
|
|
|
|
// buildProvider keeps the local development binary useful on macOS while a
|
|
// Linux deployment automatically uses the host-local libvirt adapter. The
|
|
// backend can be forced to static only for tests; production Linux defaults to
|
|
// virsh and reports a degraded capability probe if libvirt is unavailable.
|
|
func buildProvider(cfg config.Config, getenv func(string) string, goos string) (capability.Provider, string) {
|
|
mode := strings.ToLower(strings.TrimSpace(getenv("VIRT_CLOUD_AGENT_BACKEND")))
|
|
if mode == "" {
|
|
if goos == "linux" {
|
|
mode = "virsh"
|
|
} else {
|
|
mode = "static"
|
|
}
|
|
}
|
|
if mode == "static" {
|
|
return capability.StaticProvider{AgentID: cfg.AgentID, ProtocolVersion: cfg.ProtocolVersion}, "static host backend selected"
|
|
}
|
|
if mode != "virsh" {
|
|
return capability.StaticProvider{AgentID: cfg.AgentID, ProtocolVersion: cfg.ProtocolVersion}, "unknown host backend; using conservative static provider"
|
|
}
|
|
connection := strings.TrimSpace(getenv("VIRT_CLOUD_AGENT_LIBVIRT_URI"))
|
|
binary := strings.TrimSpace(getenv("VIRT_CLOUD_AGENT_VIRSH"))
|
|
backend, err := hypervisor.NewVirshBackendWithStorageRoots(connection, binary, cfg.StorageRoots)
|
|
if err != nil {
|
|
return capability.StaticProvider{AgentID: cfg.AgentID, ProtocolVersion: cfg.ProtocolVersion}, redactedBackendError(err)
|
|
}
|
|
return capability.BackendProvider{Backend: backend, AgentID: cfg.AgentID, ProtocolVersion: cfg.ProtocolVersion}, ""
|
|
}
|
|
|
|
func redactedBackendError(err error) string {
|
|
if err == nil {
|
|
return ""
|
|
}
|
|
return "host backend is not configured with an allowlisted local libvirt transport"
|
|
}
|
|
|
|
// backendFromProvider exposes the optional host seam to the operation API
|
|
// without making the capability interface depend on libvirt details.
|
|
func backendFromProvider(provider capability.Provider) hypervisor.Backend {
|
|
if value, ok := provider.(capability.BackendProvider); ok {
|
|
return value.Backend
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func applyFlags(cfg *config.Config) {
|
|
listen := flag.String("listen", cfg.ListenAddr, "agent listen address")
|
|
agentID := flag.String("agent-id", cfg.AgentID, "agent identity")
|
|
journal := flag.String("journal", cfg.JournalPath, "local operation journal path")
|
|
flag.Parse()
|
|
cfg.ListenAddr = strings.TrimSpace(*listen)
|
|
cfg.AgentID = strings.TrimSpace(*agentID)
|
|
cfg.JournalPath = strings.TrimSpace(*journal)
|
|
// A flag may change a loopback default into a remote listener. Never let
|
|
// that override silently disable the remote mTLS requirement.
|
|
if !cfg.IsLoopbackListen() {
|
|
cfg.RequireMTLS = true
|
|
}
|
|
}
|
|
|
|
func tlsListener(listener net.Listener, cfg *tls.Config) net.Listener {
|
|
return tls.NewListener(listener, cfg)
|
|
}
|
|
|
|
func redactedPathError(err error) string {
|
|
// Error strings from os.PathError can include the configured path. Keep a
|
|
// stable diagnostic category in logs; the exact path is already in the
|
|
// operator's service configuration.
|
|
if err == nil {
|
|
return ""
|
|
}
|
|
if errors.Is(err, os.ErrPermission) {
|
|
return "operation journal unavailable: permission denied"
|
|
}
|
|
return "operation journal unavailable"
|
|
}
|