Repository navigation
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011VSJ2TLnUV5rU9F24Q3BNz
# Conflicts: # core/events/netlink.go
|
/tofarev review |
ToFaRev review — partialReviewed bd4a8beb74d9 with Partial review. The findings below do not cover all changes.
Detailed findings:P4 -
|
Resolves the capture/local.go conflict and logs the new netem messages in links/netem_apply.go to the context logger. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011VSJ2TLnUV5rU9F24Q3BNz
Pulls for nodes added by apply share a reporter like deploy's, instead of each pull creating its own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011VSJ2TLnUV5rU9F24Q3BNz
|
The P4 is fixed in 141aa0f: apply now pulls images through one shared reporter, like deploy. I also merged main and moved the new netem log messages to the context logger. |
Heyo!
containerlab logs through the global charmbracelet/log logger. A program that embeds containerlab as a Go library and runs labs for several users at once, like Antimony, can't tell which lab a message belongs to, so it can't show a user the output of their own deploy, destroy or restart.
Today, Antimony runs the containerlab CLI as a subprocess and collects logs from stderr. This PR solves the largest upstream road block that keeps Antimony from switching from the legacy CLI subprocess calling to using the new clab runtime for docker as well as c9s.
Note: This change was largely generated by Claude, since its a large refactor. I also ran a review agent at high effort on this change double-checking its correctness and code health.
The change lets the library log to the logger stored in the operation's context, with charmbracelet's own context API:
log.FromContextreturns the default logger when the context holds none, so the CLI and every existing caller behave exactly as before.Change
Log calls: every library log call outside
cmd/that has the operation'sctxin scope now useslog.FromContext(ctx), about 620 calls in core, nodes, links, runtime (docker and podman), labruntime/clabernetes, capture, exec, mgmt and netconf.Passing the context down: helpers on the deploy, destroy, apply, start/stop/restart, save, exec, inspect, events and capture paths that logged without a context now take
ctx. That's only where all their callers already have one. This changes some exported signatures and interfaces:nodes.Node:GenerateConfig(ctx, dst, templ),DeleteNetnsSymlink(ctx)nodes.NodeOverwrites:VerifyContainerName(ctx),VerifyHostRequirements(ctx)dependency_manager.DependencyManager:CheckAcyclicity(ctx)definitions.ClabFirewall,firewall.NewFirewallClient)CLab.WriteState,exec.ExecCollection.Log,nodes.LoadStartupConfigFileVr,nodes.WriteSavedConfig,DefaultNode.LoadOrGenerateCertificate,types.HostRequirements.Verify,netconf.GetConfig/MultiExec,mgmt.PrepareMacvlanParent,progress.New,capture.ReportGap,links.SetNameMACAndUpInterface, and a few in utils and virt (DeleteNetnsSymlink,NSEthtoolTXOff,DisableTxOffloadByIndex,SpawnCLIviaExec,MacvlanHost.Remove,VerifyVirtSupport,GetSysMemory).The mocks are regenerated with mockgen v0.6.0.
Loggers handed to other code: the c9s compiler's logger and the kind provider's logger are now built from the context logger.
Image pulls:
progress.New(ctx, w)makes the plain reporter log to the context logger. A logger in the context also turns off the TUI progress bars, since its owner collects the output rather than a terminal.Contexts that dropped the logger: three places created their own background context although the caller's was at hand. These are the SR-SIM version detection (
createSROSConfigFiles), the SR Linux custom prompt lookup, and the SR-SIM distributed management IP lookup. They now use the caller'sctx, so they're also cancelled with the operation.Docs: a "Logging per lab" subsection under "Node access from Go" in the c9s runtime docs, with a note that it applies to Docker and Podman labs as well.
What keeps the default logger
NewContainerLab, its options, topology loading and parsing, nodeInit, link resolution and YAML unmarshalling take no context. That includes the copydestroybuilds per lab, so "Parsing & checking topology" still goes to the default logger. Giving construction a context is an API decision of its own (actxforNewContainerLab, or an option), so I left it for a follow-up.A few file copies in node PreDeploy steps (
clabutils.CopyFilefor licenses and startup configs) still passcontext.Background().CopyFiledoesn't log, and switching them would change their cancellation, so I left them alone.No lint rule enforces the convention yet.
forbidigocould ban barelog.Infoand friends outsidecmd/once construction takes a context too.Testing
go build ./...andgo build -tags podman ./...go test ./...andgo test -tags podman ./runtime/podman/...golangci-lint run ./...reports the same issues as on main.TestDestroyLogsToContextLogger: a destroy logs to the context logger and not to the default logger. Both are set to the debug level.TestNewLogsToContextLogger: image pull progress goes to the context logger.TestUseTUI: the progress bars are only used on a terminal, and not when the context holds a logger.TestNodeReadinessProgressReportsTransitions,TestExecCollectionLogPreservesAddOrder) now capture output through a context logger instead of swapping the default logger's output.log.FromContextargument with a small go/types scan: each one is a parameter of the enclosing function or closure, never a localcontext.Background(). No call passes a nil context.