Skip to content

fix: namespaced agent events with agent prefix#5609

Open
exu wants to merge 1 commit intodevelopfrom
jacek/fix/emitters-debug
Open

fix: namespaced agent events with agent prefix#5609
exu wants to merge 1 commit intodevelopfrom
jacek/fix/emitters-debug

Conversation

@exu
Copy link
Copy Markdown
Contributor

@exu exu commented Jun 25, 2024

Pull request description

Control Plane and Agent are on same events prefix - some edge cases (like recent invalid NATS selector settings caused strange events ping pongs. to allow even use single NATS for On prem setup I've changed prefix to agent in agent

Checklist (choose whats happened)

  • breaking change! (describe)
  • tested locally
  • tested on cluster
  • added new dependencies
  • updated the docs
  • added a test

Breaking changes

Changes

Fixes

@netlify
Copy link
Copy Markdown

netlify Bot commented Jun 25, 2024

Deploy Preview for testkube-docs-preview failed.

Name Link
🔨 Latest commit a61016e
🔍 Latest deploy log https://app.netlify.com/sites/testkube-docs-preview/deploys/667aa3fe794b2e0008202983

@exu exu marked this pull request as ready for review June 26, 2024 08:04
@ed382 ed382 requested a review from a team as a code owner July 11, 2024 12:20
@olensmar
Copy link
Copy Markdown
Member

olensmar commented Mar 3, 2026

@greptile

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Mar 3, 2026

Greptile Summary

This PR successfully namespaces all agent-side NATS event subjects under an agent prefix (previously events) to prevent topic collisions between the Control Plane and Agent when they share the same NATS instance — a regression-prevention fix for on-prem single-NATS deployments.

Key changes:

  • pkg/api/v1/testkube/model_event_extended.go: Central Subscription = "agent" constant introduced; all Event.Topic() return values updated to use it.
  • pkg/event/bus/nats.go: SubscriptionName set to "agent"; TraceEvents subscription updated to agent.>.
  • pkg/event/emitter.go: Topic reference uses bus.SubscriptionName constant; structured log fields improved.
  • pkg/logs/events.go and pkg/logs/client/interface.go: Log stream start/stop subjects updated to agent.logs.*.
  • internal/graphql/services/executors.go: Executor subscription topic updated from events.executor.> to agent.executor.>.
  • All relevant tests updated to match new subjects.

All NATS topic strings in publishers and subscribers have been updated consistently. Two minor issues remain: (1) one hardcoded literal in executors.go should use the imported testkube.Subscription constant, and (2) a log message in TraceEvents still references the old "events" naming.

Confidence Score: 4/5

  • Safe to merge; the prefix rename is consistent across all publishers and subscribers with updated tests.
  • All NATS topic strings in publishers (Event.Topic(), TestStartSubject, LogStartSubject, etc.) and subscribers (emitter.go, executors.go, bus.go trace) have been updated in concert to use the agent.* namespace. Tests pass with the new subjects. Two minor code quality improvements remain unfixed: (1) a hardcoded string literal in executors.go could use the imported constant, and (2) a log message in TraceEvents is inconsistent with the renamed subscription.
  • internal/graphql/services/executors.go (hardcoded topic literal), pkg/event/bus/nats.go (misleading log message)

Sequence Diagram

sequenceDiagram
    participant Agent
    participant NATS
    participant ControlPlane

    Note over Agent,NATS: Before this PR — both Agent and CP used "events.*" prefix
    Agent->>NATS: publish  events.test.start
    ControlPlane->>NATS: subscribe events.>
    NATS-->>ControlPlane: (unintended delivery — event ping-pong)

    Note over Agent,NATS: After this PR — Agent uses "agent.*" prefix
    Agent->>NATS: publish  agent.test.start
    Agent->>NATS: publish  agent.logs.start
    Agent->>NATS: publish  agent.executor.create

    NATS-->>Agent: deliver  agent.> (Emitter / LogsService)
    Note over ControlPlane,NATS: CP still subscribes its own "events.*" namespace
    ControlPlane->>NATS: subscribe events.>
    Note right of ControlPlane: No cross-contamination
Loading

Last reviewed commit: a61016e

Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile


func (s *executorsService) SubscribeList(ctx context.Context, selector string) (<-chan []testkube.ExecutorDetails, error) {
return HandleSubscription(ctx, "events.executor.>", s, func() ([]testkube.ExecutorDetails, error) {
return HandleSubscription(ctx, "agent.executor.>", s, func() ([]testkube.ExecutorDetails, error) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The topic string "agent.executor.>" is hardcoded, but the testkube package is already imported and now exports a Subscription constant with value "agent". Using the constant prevents future prefix drift if the value ever changes.

Suggested change
return HandleSubscription(ctx, "agent.executor.>", s, func() ([]testkube.ExecutorDetails, error) {
return HandleSubscription(ctx, testkube.Subscription+".executor.>", s, func() ([]testkube.ExecutorDetails, error) {

Comment thread pkg/event/bus/nats.go
func (n *NATSBus) TraceEvents() {
s, err := n.nc.Subscribe(SubscriptionName+".>", func(event testkube.Event) {
log.Tracew(log.DefaultLogger, "all events.> trace", event.Log()...)
log.Tracew(log.DefaultLogger, "got message on events", event.Log()...)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subscription subject was renamed from events.> to agent.> (via SubscriptionName), but the log message still says "got message on events". This is confusing for operators reading trace logs who aren't aware of the rename.

Suggested change
log.Tracew(log.DefaultLogger, "got message on events", event.Log()...)
log.Tracew(log.DefaultLogger, "got message on agent", event.Log()...)

Copy link
Copy Markdown
Contributor

@mgazza mgazza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple more suggestions beyond what Greptile flagged.

TestStartSubject = "events.test.start"
TestStopSubject = "events.test.stop"
Subscription = "agent"
TestStartSubject = Subscription + ".test.start"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "agent" prefix is now defined in three places: here, pkg/logs/client/interface.go, and pkg/event/bus/nats.go (SubscriptionName). If any of them drifts, topics silently stop matching.

Consider importing from a single source of truth. If circular imports are a concern, a small shared package (e.g. pkg/event/constants) would work.


if e.ResourceId == "" {
return "events." + string(*e.Resource)
return Subscription + "." + string(*e.Resource)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Runtime string concatenation with + across multiple segments is harder to scan than strings.Join. The compile-time const expressions (like Subscription + ".test.start") are fine, but these runtime ones would benefit:

Suggested change
return Subscription + "." + string(*e.Resource)
return strings.Join([]string{Subscription, string(*e.Resource), e.ResourceId}, ".")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants