Skip to content

feat(agent): add ParentId to agent manifest #17888

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

Merged
merged 5 commits into from
May 19, 2025
Merged
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
6 changes: 3 additions & 3 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ type Options struct {
}

type Client interface {
ConnectRPC24(ctx context.Context) (
proto.DRPCAgentClient24, tailnetproto.DRPCTailnetClient24, error,
ConnectRPC25(ctx context.Context) (
proto.DRPCAgentClient25, tailnetproto.DRPCTailnetClient25, error,
)
RewriteDERPMap(derpMap *tailcfg.DERPMap)
}
Expand Down Expand Up @@ -908,7 +908,7 @@ func (a *agent) run() (retErr error) {
a.sessionToken.Store(&sessionToken)

// ConnectRPC returns the dRPC connection we use for the Agent and Tailnet v2+ APIs
aAPI, tAPI, err := a.client.ConnectRPC24(a.hardCtx)
aAPI, tAPI, err := a.client.ConnectRPC25(a.hardCtx)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions agent/agenttest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func (c *Client) Close() {
c.derpMapOnce.Do(func() { close(c.derpMapUpdates) })
}

func (c *Client) ConnectRPC24(ctx context.Context) (
agentproto.DRPCAgentClient24, proto.DRPCTailnetClient24, error,
func (c *Client) ConnectRPC25(ctx context.Context) (
agentproto.DRPCAgentClient25, proto.DRPCTailnetClient25, error,
) {
conn, lis := drpcsdk.MemTransportPipe()
c.LastWorkspaceAgent = func() {
Expand Down
66 changes: 39 additions & 27 deletions agent/proto/agent.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions agent/proto/agent.proto
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ message Manifest {
string motd_path = 6;
bool disable_direct_connections = 7;
bool derp_force_websockets = 8;
optional bytes parent_id = 18;

coder.tailnet.v2.DERPMap derp_map = 9;
repeated WorkspaceAgentScript scripts = 10;
Expand Down
5 changes: 5 additions & 0 deletions agent/proto/agent_drpc_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ type DRPCAgentClient24 interface {
PushResourcesMonitoringUsage(ctx context.Context, in *PushResourcesMonitoringUsageRequest) (*PushResourcesMonitoringUsageResponse, error)
ReportConnection(ctx context.Context, in *ReportConnectionRequest) (*emptypb.Empty, error)
}

// DRPCAgentClient25 is the Agent API at v2.5.
type DRPCAgentClient25 interface {
DRPCAgentClient24
}
6 changes: 6 additions & 0 deletions coderd/agentapi/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ func (a *ManifestAPI) GetManifest(ctx context.Context, _ *agentproto.GetManifest
return nil, xerrors.Errorf("converting workspace apps: %w", err)
}

var parentID []byte
if workspaceAgent.ParentID.Valid {
parentID = workspaceAgent.ParentID.UUID[:]
}

return &agentproto.Manifest{
AgentId: workspaceAgent.ID[:],
AgentName: workspaceAgent.Name,
Expand All @@ -133,6 +138,7 @@ func (a *ManifestAPI) GetManifest(ctx context.Context, _ *agentproto.GetManifest
MotdPath: workspaceAgent.MOTDFile,
DisableDirectConnections: a.DisableDirectConnections,
DerpForceWebsockets: a.DerpForceWebSockets,
ParentId: parentID,

DerpMap: tailnet.DERPMapToProto(a.DerpMapFn()),
Scripts: dbAgentScriptsToProto(scripts),
Expand Down
72 changes: 72 additions & 0 deletions coderd/agentapi/manifest_test.go
Copy link
Member

Choose a reason for hiding this comment

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

Should we also assert that parent is nil if not specified in the manifest? (Essentially the inverse of the test you added)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the original test to have the ParentId: null explicit in the expected struct 👍

Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ func TestGetManifest(t *testing.T) {
Directory: "/cool/dir",
MOTDFile: "/cool/motd",
}
childAgent = database.WorkspaceAgent{
ID: uuid.New(),
Name: "cool-child-agent",
ParentID: uuid.NullUUID{Valid: true, UUID: agent.ID},
Directory: "/workspace/dir",
MOTDFile: "/workspace/motd",
}
apps = []database.WorkspaceApp{
{
ID: uuid.New(),
Expand Down Expand Up @@ -337,6 +344,7 @@ func TestGetManifest(t *testing.T) {
expected := &agentproto.Manifest{
AgentId: agent.ID[:],
AgentName: agent.Name,
ParentId: nil,
OwnerUsername: owner.Username,
WorkspaceId: workspace.ID[:],
WorkspaceName: workspace.Name,
Expand Down Expand Up @@ -364,6 +372,70 @@ func TestGetManifest(t *testing.T) {
require.Equal(t, expected, got)
})

t.Run("OK/Child", func(t *testing.T) {
t.Parallel()

mDB := dbmock.NewMockStore(gomock.NewController(t))

api := &agentapi.ManifestAPI{
AccessURL: &url.URL{Scheme: "https", Host: "example.com"},
AppHostname: "*--apps.example.com",
ExternalAuthConfigs: []*externalauth.Config{
{Type: string(codersdk.EnhancedExternalAuthProviderGitHub)},
{Type: "some-provider"},
{Type: string(codersdk.EnhancedExternalAuthProviderGitLab)},
},
DisableDirectConnections: true,
DerpForceWebSockets: true,

AgentFn: func(ctx context.Context) (database.WorkspaceAgent, error) {
return childAgent, nil
},
WorkspaceID: workspace.ID,
Database: mDB,
DerpMapFn: derpMapFn,
}

mDB.EXPECT().GetWorkspaceAppsByAgentID(gomock.Any(), childAgent.ID).Return([]database.WorkspaceApp{}, nil)
mDB.EXPECT().GetWorkspaceAgentScriptsByAgentIDs(gomock.Any(), []uuid.UUID{childAgent.ID}).Return([]database.WorkspaceAgentScript{}, nil)
mDB.EXPECT().GetWorkspaceAgentMetadata(gomock.Any(), database.GetWorkspaceAgentMetadataParams{
WorkspaceAgentID: childAgent.ID,
Keys: nil, // all
}).Return([]database.WorkspaceAgentMetadatum{}, nil)
mDB.EXPECT().GetWorkspaceAgentDevcontainersByAgentID(gomock.Any(), childAgent.ID).Return([]database.WorkspaceAgentDevcontainer{}, nil)
mDB.EXPECT().GetWorkspaceByID(gomock.Any(), workspace.ID).Return(workspace, nil)
mDB.EXPECT().GetUserByID(gomock.Any(), workspace.OwnerID).Return(owner, nil)

got, err := api.GetManifest(context.Background(), &agentproto.GetManifestRequest{})
require.NoError(t, err)

expected := &agentproto.Manifest{
AgentId: childAgent.ID[:],
AgentName: childAgent.Name,
ParentId: agent.ID[:],
OwnerUsername: owner.Username,
WorkspaceId: workspace.ID[:],
WorkspaceName: workspace.Name,
GitAuthConfigs: 2, // two "enhanced" external auth configs
EnvironmentVariables: nil,
Directory: childAgent.Directory,
VsCodePortProxyUri: fmt.Sprintf("https://{{port}}--%s--%s--%s--apps.example.com", childAgent.Name, workspace.Name, owner.Username),
MotdPath: childAgent.MOTDFile,
DisableDirectConnections: true,
DerpForceWebsockets: true,
// tailnet.DERPMapToProto() is extensively tested elsewhere, so it's
// not necessary to manually recreate a big DERP map here like we
// did for apps and metadata.
DerpMap: tailnet.DERPMapToProto(derpMapFn()),
Scripts: []*agentproto.WorkspaceAgentScript{},
Apps: []*agentproto.WorkspaceApp{},
Metadata: []*agentproto.WorkspaceAgentMetadata_Description{},
Devcontainers: []*agentproto.WorkspaceAgentDevcontainer{},
}

require.Equal(t, expected, got)
})

t.Run("NoAppHostname", func(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 1 addition & 1 deletion coderd/workspaceagents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2464,7 +2464,7 @@ func requireGetManifest(ctx context.Context, t testing.TB, aAPI agentproto.DRPCA
}

func postStartup(ctx context.Context, t testing.TB, client agent.Client, startup *agentproto.Startup) error {
aAPI, _, err := client.ConnectRPC24(ctx)
aAPI, _, err := client.ConnectRPC25(ctx)
require.NoError(t, err)
defer func() {
cErr := aAPI.DRPCConn().Close()
Expand Down
14 changes: 13 additions & 1 deletion codersdk/agentsdk/agentsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (c *Client) ConnectRPC23(ctx context.Context) (
}

// ConnectRPC24 returns a dRPC client to the Agent API v2.4. It is useful when you want to be
// maximally compatible with Coderd Release Versions from 2.xx+ // TODO @vincent: define version
// maximally compatible with Coderd Release Versions from 2.20+
func (c *Client) ConnectRPC24(ctx context.Context) (
proto.DRPCAgentClient24, tailnetproto.DRPCTailnetClient24, error,
) {
Expand All @@ -257,6 +257,18 @@ func (c *Client) ConnectRPC24(ctx context.Context) (
return proto.NewDRPCAgentClient(conn), tailnetproto.NewDRPCTailnetClient(conn), nil
}

// ConnectRPC25 returns a dRPC client to the Agent API v2.5. It is useful when you want to be
// maximally compatible with Coderd Release Versions from 2.xx+ // TODO(DanielleMaywood): Update version
func (c *Client) ConnectRPC25(ctx context.Context) (
proto.DRPCAgentClient25, tailnetproto.DRPCTailnetClient25, error,
) {
conn, err := c.connectRPCVersion(ctx, apiversion.New(2, 5))
if err != nil {
return nil, nil, err
}
return proto.NewDRPCAgentClient(conn), tailnetproto.NewDRPCTailnetClient(conn), nil
}

// ConnectRPC connects to the workspace agent API and tailnet API
func (c *Client) ConnectRPC(ctx context.Context) (drpc.Conn, error) {
return c.connectRPCVersion(ctx, proto.CurrentVersion)
Expand Down
5 changes: 5 additions & 0 deletions tailnet/proto/tailnet_drpc_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ type DRPCTailnetClient23 interface {
type DRPCTailnetClient24 interface {
DRPCTailnetClient23
}

// DRPCTailnetClient25 is the Tailnet API at v2.5.
type DRPCTailnetClient25 interface {
DRPCTailnetClient24
}
6 changes: 5 additions & 1 deletion tailnet/proto/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ import (
// PushResourcesMonitoringUsage RPCs on the Agent API.
// - Added support for reporting connection events for auditing via the
// ReportConnection RPC on the Agent API.
//
// API v2.5:
// - Shipped in Coder v2.xx.x // TODO(DanielleMaywood): Update version
Copy link
Member

Choose a reason for hiding this comment

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

I think we can make an educated guess at 2.23 :) but this is fine for now.

// - Added `ParentId` to the agent manifest.
const (
CurrentMajor = 2
CurrentMinor = 4
CurrentMinor = 5
)

var CurrentVersion = apiversion.New(CurrentMajor, CurrentMinor)
Loading