Skip to content

fix(coderd/agentapi): publish workspace update event on sub agent #18461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions coderd/agentapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,14 @@ func New(opts Options) *API {
}

api.SubAgentAPI = &SubAgentAPI{
OwnerID: opts.OwnerID,
OrganizationID: opts.OrganizationID,
AgentID: opts.AgentID,
AgentFn: api.agent,
Log: opts.Log,
Clock: opts.Clock,
Database: opts.Database,
OwnerID: opts.OwnerID,
OrganizationID: opts.OrganizationID,
AgentID: opts.AgentID,
AgentFn: api.agent,
PublishWorkspaceUpdateFn: api.publishWorkspaceUpdate,
Log: opts.Log,
Clock: opts.Clock,
Database: opts.Database,
}

return api
Expand Down
45 changes: 41 additions & 4 deletions coderd/agentapi/subagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ import (
agentproto "github.com/coder/coder/v2/agent/proto"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/wspubsub"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/provisioner"
)

type SubAgentAPI struct {
OwnerID uuid.UUID
OrganizationID uuid.UUID
AgentID uuid.UUID
AgentFn func(context.Context) (database.WorkspaceAgent, error)
OwnerID uuid.UUID
OrganizationID uuid.UUID
AgentID uuid.UUID
AgentFn func(context.Context) (database.WorkspaceAgent, error)
PublishWorkspaceUpdateFn func(context.Context, *database.WorkspaceAgent, wspubsub.WorkspaceEventKind) error

Log slog.Logger
Clock quartz.Clock
Expand Down Expand Up @@ -228,6 +230,18 @@ func (a *SubAgentAPI) CreateSubAgent(ctx context.Context, req *agentproto.Create
}
}

if a.PublishWorkspaceUpdateFn != nil {
err = a.PublishWorkspaceUpdateFn(ctx, &subAgent, wspubsub.WorkspaceEventKindStateChange)
if err != nil {
a.Log.Error(ctx, "failed to publish workspace update after creating sub agent",
slog.Error(err),
slog.F("sub_agent_id", subAgent.ID),
slog.F("sub_agent_name", subAgent.Name),
)
// We don't return an error here because we created the sub agent.
}
}

return &agentproto.CreateSubAgentResponse{
Agent: &agentproto.SubAgent{
Name: subAgent.Name,
Expand All @@ -247,10 +261,33 @@ func (a *SubAgentAPI) DeleteSubAgent(ctx context.Context, req *agentproto.Delete
return nil, err
}

subAgent, err := a.Database.GetWorkspaceAgentByID(ctx, subAgentID)
if err != nil {
if xerrors.Is(err, sql.ErrNoRows) {
return nil, codersdk.ValidationError{
Field: "id",
Detail: fmt.Sprintf("sub agent with id %q does not exist", req.Id),
}
}
return nil, xerrors.Errorf("get workspace agent by id %q: %w", subAgentID, err)
}

if err := a.Database.DeleteWorkspaceSubAgentByID(ctx, subAgentID); err != nil {
return nil, err
}

if a.PublishWorkspaceUpdateFn != nil {
err = a.PublishWorkspaceUpdateFn(ctx, &subAgent, wspubsub.WorkspaceEventKindStateChange)
if err != nil {
a.Log.Error(ctx, "failed to publish workspace update after deleting sub agent",
slog.Error(err),
slog.F("sub_agent_id", subAgent.ID),
slog.F("sub_agent_name", subAgent.Name),
)
// We don't return an error here because we deleted the sub agent.
}
}

return &agentproto.DeleteSubAgentResponse{}, nil
}

Expand Down
Loading