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 1 commit
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
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
Copy link
Member

Choose a reason for hiding this comment

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

agent/proto/version.go defines its version as the same version as tailnet/proto so I think we need to add a comment in tailnet/proto/version.go and bump the minor version.

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
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
75 changes: 75 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 @@ -364,6 +371,74 @@ 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{},
}

// Log got and expected with spew.
// t.Log("got:\n" + spew.Sdump(got))
// t.Log("expected:\n" + spew.Sdump(expected))

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

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

Expand Down
Loading