-
Notifications
You must be signed in to change notification settings - Fork 881
refactor: generate application URL on backend side #9618
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
Changes from all commits
d726fab
96d8f59
ec977f2
e914801
55d7ff1
f56dbe3
826ef76
d998238
41459dd
fdc1845
74d9dbe
b0b2c33
da4ddde
fea0af3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,8 +64,42 @@ func (api *API) workspaceAgent(rw http.ResponseWriter, r *http.Request) { | |
}) | ||
return | ||
} | ||
|
||
resource, err := api.Database.GetWorkspaceResourceByID(ctx, workspaceAgent.ResourceID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error fetching workspace resource.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
build, err := api.Database.GetWorkspaceBuildByJobID(ctx, resource.JobID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error fetching workspace build.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
workspace, err := api.Database.GetWorkspaceByID(ctx, build.WorkspaceID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error fetching workspace.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
owner, err := api.Database.GetUserByID(ctx, workspace.OwnerID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error fetching workspace owner.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
apiAgent, err := convertWorkspaceAgent( | ||
api.DERPMap(), *api.TailnetCoordinator.Load(), workspaceAgent, convertApps(dbApps), api.AgentInactiveDisconnectTimeout, | ||
api.DERPMap(), *api.TailnetCoordinator.Load(), workspaceAgent, convertApps(dbApps, workspaceAgent, owner, workspace), api.AgentInactiveDisconnectTimeout, | ||
api.DeploymentValues.AgentFallbackTroubleshootingURL.String(), | ||
) | ||
if err != nil { | ||
|
@@ -165,7 +199,7 @@ func (api *API) workspaceAgentManifest(rw http.ResponseWriter, r *http.Request) | |
|
||
httpapi.Write(ctx, rw, http.StatusOK, agentsdk.Manifest{ | ||
AgentID: apiAgent.ID, | ||
Apps: convertApps(dbApps), | ||
Apps: convertApps(dbApps, workspaceAgent, owner, workspace), | ||
DERPMap: api.DERPMap(), | ||
DERPForceWebSockets: api.DeploymentValues.DERP.Config.ForceWebSockets.Value(), | ||
GitAuthConfigs: len(api.GitAuthConfigs), | ||
|
@@ -1281,19 +1315,40 @@ func (api *API) workspaceAgentClientCoordinate(rw http.ResponseWriter, r *http.R | |
} | ||
} | ||
|
||
func convertApps(dbApps []database.WorkspaceApp) []codersdk.WorkspaceApp { | ||
// convertProvisionedApps converts applications that are in the middle of provisioning process. | ||
// It means that they may not have an agent or workspace assigned (dry-run job). | ||
func convertProvisionedApps(dbApps []database.WorkspaceApp) []codersdk.WorkspaceApp { | ||
return convertApps(dbApps, database.WorkspaceAgent{}, database.User{}, database.Workspace{}) | ||
} | ||
|
||
Comment on lines
-1284
to
+1323
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: why have this extra function at all, you could simply just call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's true, I just personally find it "dirty" to call functions with multiple nil, nil, nil, {}, {}, {}, or "", "", "". 😅 |
||
func convertApps(dbApps []database.WorkspaceApp, agent database.WorkspaceAgent, owner database.User, workspace database.Workspace) []codersdk.WorkspaceApp { | ||
apps := make([]codersdk.WorkspaceApp, 0) | ||
for _, dbApp := range dbApps { | ||
var subdomainName string | ||
if dbApp.Subdomain && agent.Name != "" && owner.Username != "" && workspace.Name != "" { | ||
appSlug := dbApp.Slug | ||
if appSlug == "" { | ||
appSlug = dbApp.DisplayName | ||
} | ||
subdomainName = httpapi.ApplicationURL{ | ||
AppSlugOrPort: appSlug, | ||
AgentName: agent.Name, | ||
WorkspaceName: workspace.Name, | ||
Username: owner.Username, | ||
}.String() | ||
} | ||
|
||
apps = append(apps, codersdk.WorkspaceApp{ | ||
ID: dbApp.ID, | ||
URL: dbApp.Url.String, | ||
External: dbApp.External, | ||
Slug: dbApp.Slug, | ||
DisplayName: dbApp.DisplayName, | ||
Command: dbApp.Command.String, | ||
Icon: dbApp.Icon, | ||
Subdomain: dbApp.Subdomain, | ||
SharingLevel: codersdk.WorkspaceAppSharingLevel(dbApp.SharingLevel), | ||
ID: dbApp.ID, | ||
URL: dbApp.Url.String, | ||
External: dbApp.External, | ||
Slug: dbApp.Slug, | ||
DisplayName: dbApp.DisplayName, | ||
Command: dbApp.Command.String, | ||
Icon: dbApp.Icon, | ||
Subdomain: dbApp.Subdomain, | ||
SubdomainName: subdomainName, | ||
SharingLevel: codersdk.WorkspaceAppSharingLevel(dbApp.SharingLevel), | ||
Healthcheck: codersdk.Healthcheck{ | ||
URL: dbApp.HealthcheckUrl, | ||
Interval: dbApp.HealthcheckInterval, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential follow-up: would it make sense to modify
GetWorkspaceAppsByAgentID
to return the username and workspace name as well? Although this endpoint does not appear to be used by the UI at all, only by the CLI and unit tests.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had that in mind as potential optimization, but as long it is not used, and could be deprecated, I will focus on deprecation instead 👍