maglevc - Rewrite '?' handler (birdc-style): show full command paths from current position to every leaf, right-aligned help column, dynamic slot values displayed as an indented block when cursor is at a slot position. - Collapse show frontends/frontend, backends/backend, healthchecks/healthcheck into single plural-noun nodes with an optional <name> slot. Allows 'sh ba' (list all) and 'sh ba nginx0' (show one) without ambiguity. - Add 'config reload' command. - Fix tabwriter ANSI alignment: continuation lines in transition output now carry the same label() byte overhead as the header line. - Fix broken Walk for 'set frontend' command: setFrontendPoolName and setWeightValue were fixed-word nodes that couldn't capture user input; mark them as slot nodes with dynNone. - Add tree_test.go covering expandPaths, cycle detection, prefix matching, and the full weight-command walk. gRPC / proto - Add ReloadConfig RPC: checks config then applies it to the running checker, returning ok/parse_error/semantic_error/reload_error. - Add logging to CheckConfig (config-check-start/config-check-done at INFO level). maglevd - SIGHUP handler now calls maglevServer.TriggerReload(), sharing the same code path as the gRPC ReloadConfig RPC. docs - Collapse show command documentation to use [<name>] optional syntax. - Remove developer-facing 'Command tree and parser' section. - Document 'config reload'.
136 lines
4.0 KiB
Go
136 lines
4.0 KiB
Go
// Copyright (c) 2026, Pim van Pelt <pim@ipng.ch>
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"log/slog"
|
|
"net"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/reflection"
|
|
|
|
buildinfo "git.ipng.ch/ipng/vpp-maglev/cmd"
|
|
"git.ipng.ch/ipng/vpp-maglev/internal/checker"
|
|
"git.ipng.ch/ipng/vpp-maglev/internal/config"
|
|
"git.ipng.ch/ipng/vpp-maglev/internal/grpcapi"
|
|
)
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
slog.Error("startup-fatal", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run() error {
|
|
// ---- flags / env --------------------------------------------------------
|
|
printVersion := flag.Bool("version", false, "print version and exit")
|
|
checkOnly := flag.Bool("check", false, "check config file and exit (0=ok, 1=parse error, 2=semantic error)")
|
|
enableReflection := flag.Bool("reflection", true, "enable gRPC server reflection (for grpcurl)")
|
|
configPath := stringFlag("config", "/etc/vpp-maglev/maglev.yaml", "MAGLEV_CONFIG", "path to maglev.yaml")
|
|
grpcAddr := stringFlag("grpc-addr", ":9090", "MAGLEV_GRPC_ADDR", "gRPC listen address")
|
|
logLevel := stringFlag("log-level", "info", "MAGLEV_LOG_LEVEL", "log level (debug|info|warn|error)")
|
|
flag.Parse()
|
|
|
|
if *printVersion {
|
|
fmt.Printf("maglevd %s (commit %s, built %s)\n",
|
|
buildinfo.Version(), buildinfo.Commit(), buildinfo.Date())
|
|
return nil
|
|
}
|
|
|
|
if *checkOnly {
|
|
_, result := config.Check(*configPath)
|
|
if result.OK() {
|
|
fmt.Printf("config ok: %s\n", *configPath)
|
|
return nil
|
|
}
|
|
if result.ParseError != "" {
|
|
fmt.Fprintf(os.Stderr, "parse error: %s\n", result.ParseError)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Fprintf(os.Stderr, "semantic error: %s\n", result.SemanticError)
|
|
os.Exit(2)
|
|
}
|
|
|
|
// ---- logging ------------------------------------------------------------
|
|
var level slog.Level
|
|
if err := level.UnmarshalText([]byte(*logLevel)); err != nil {
|
|
return fmt.Errorf("invalid log level %q: %w", *logLevel, err)
|
|
}
|
|
jsonHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: level})
|
|
logBroadcaster := grpcapi.NewLogBroadcaster(jsonHandler)
|
|
slog.SetDefault(slog.New(logBroadcaster))
|
|
slog.Info("starting", "version", buildinfo.Version(), "commit", buildinfo.Commit(), "date", buildinfo.Date())
|
|
|
|
// ---- config -------------------------------------------------------------
|
|
cfg, err := config.Load(*configPath)
|
|
if err != nil {
|
|
return fmt.Errorf("load config: %w", err)
|
|
}
|
|
slog.Info("config-loaded", "path", *configPath, "frontends", len(cfg.Frontends))
|
|
|
|
// ---- checker ------------------------------------------------------------
|
|
chkr := checker.New(cfg)
|
|
|
|
ctx, rootCancel := context.WithCancel(context.Background())
|
|
defer rootCancel()
|
|
|
|
go func() {
|
|
if err := chkr.Run(ctx); err != nil {
|
|
slog.Error("checker-exited", "err", err)
|
|
}
|
|
}()
|
|
|
|
// ---- gRPC server --------------------------------------------------------
|
|
lis, err := net.Listen("tcp", *grpcAddr)
|
|
if err != nil {
|
|
return fmt.Errorf("listen %s: %w", *grpcAddr, err)
|
|
}
|
|
srv := grpc.NewServer()
|
|
maglevServer := grpcapi.NewServer(ctx, chkr, logBroadcaster, *configPath)
|
|
grpcapi.RegisterMaglevServer(srv, maglevServer)
|
|
if *enableReflection {
|
|
reflection.Register(srv)
|
|
}
|
|
slog.Info("grpc-listening", "addr", *grpcAddr, "reflection", *enableReflection)
|
|
|
|
go func() {
|
|
if err := srv.Serve(lis); err != nil {
|
|
slog.Error("grpc-serve-error", "err", err)
|
|
}
|
|
}()
|
|
|
|
// ---- signal handling ----------------------------------------------------
|
|
sigCh := make(chan os.Signal, 1)
|
|
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
for sig := range sigCh {
|
|
switch sig {
|
|
case syscall.SIGHUP:
|
|
maglevServer.TriggerReload()
|
|
|
|
case syscall.SIGTERM, syscall.SIGINT:
|
|
slog.Info("shutdown", "signal", sig)
|
|
rootCancel()
|
|
srv.GracefulStop()
|
|
return nil
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// stringFlag declares a flag that falls back to an environment variable.
|
|
func stringFlag(name, defaultVal, envKey, usage string) *string {
|
|
val := defaultVal
|
|
if v := os.Getenv(envKey); v != "" {
|
|
val = v
|
|
}
|
|
return flag.String(name, val, fmt.Sprintf("%s (env: %s)", usage, envKey))
|
|
}
|