Skip to content

Commit 10df2fd

Browse files
authored
feat: add new required slug property to coder_app, use in URLs (#4573)
1 parent 90f77a3 commit 10df2fd

File tree

75 files changed

+852
-460
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+852
-460
lines changed

agent/apphealth.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func NewWorkspaceAppHealthReporter(logger slog.Logger, apps []codersdk.Workspace
3333
hasHealthchecksEnabled := false
3434
health := make(map[string]codersdk.WorkspaceAppHealth, 0)
3535
for _, app := range apps {
36-
health[app.Name] = app.Health
36+
health[app.DisplayName] = app.Health
3737
if !hasHealthchecksEnabled && app.Health != codersdk.WorkspaceAppHealthDisabled {
3838
hasHealthchecksEnabled = true
3939
}
@@ -85,21 +85,21 @@ func NewWorkspaceAppHealthReporter(logger slog.Logger, apps []codersdk.Workspace
8585
}()
8686
if err != nil {
8787
mu.Lock()
88-
if failures[app.Name] < int(app.Healthcheck.Threshold) {
88+
if failures[app.DisplayName] < int(app.Healthcheck.Threshold) {
8989
// increment the failure count and keep status the same.
9090
// we will change it when we hit the threshold.
91-
failures[app.Name]++
91+
failures[app.DisplayName]++
9292
} else {
9393
// set to unhealthy if we hit the failure threshold.
9494
// we stop incrementing at the threshold to prevent the failure value from increasing forever.
95-
health[app.Name] = codersdk.WorkspaceAppHealthUnhealthy
95+
health[app.DisplayName] = codersdk.WorkspaceAppHealthUnhealthy
9696
}
9797
mu.Unlock()
9898
} else {
9999
mu.Lock()
100100
// we only need one successful health check to be considered healthy.
101-
health[app.Name] = codersdk.WorkspaceAppHealthHealthy
102-
failures[app.Name] = 0
101+
health[app.DisplayName] = codersdk.WorkspaceAppHealthHealthy
102+
failures[app.DisplayName] = 0
103103
mu.Unlock()
104104
}
105105

agent/apphealth_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ func TestAppHealth(t *testing.T) {
2727
defer cancel()
2828
apps := []codersdk.WorkspaceApp{
2929
{
30-
Name: "app1",
30+
DisplayName: "app1",
3131
Healthcheck: codersdk.Healthcheck{},
3232
Health: codersdk.WorkspaceAppHealthDisabled,
3333
},
3434
{
35-
Name: "app2",
35+
DisplayName: "app2",
3636
Healthcheck: codersdk.Healthcheck{
3737
// URL: We don't set the URL for this test because the setup will
3838
// create a httptest server for us and set it for us.
@@ -69,7 +69,7 @@ func TestAppHealth(t *testing.T) {
6969
defer cancel()
7070
apps := []codersdk.WorkspaceApp{
7171
{
72-
Name: "app2",
72+
DisplayName: "app2",
7373
Healthcheck: codersdk.Healthcheck{
7474
// URL: We don't set the URL for this test because the setup will
7575
// create a httptest server for us and set it for us.
@@ -102,7 +102,7 @@ func TestAppHealth(t *testing.T) {
102102
defer cancel()
103103
apps := []codersdk.WorkspaceApp{
104104
{
105-
Name: "app2",
105+
DisplayName: "app2",
106106
Healthcheck: codersdk.Healthcheck{
107107
// URL: We don't set the URL for this test because the setup will
108108
// create a httptest server for us and set it for us.
@@ -137,7 +137,7 @@ func TestAppHealth(t *testing.T) {
137137
defer cancel()
138138
apps := []codersdk.WorkspaceApp{
139139
{
140-
Name: "app2",
140+
DisplayName: "app2",
141141
Healthcheck: codersdk.Healthcheck{
142142
// URL: We don't set the URL for this test because the setup will
143143
// create a httptest server for us and set it for us.
@@ -187,7 +187,7 @@ func setupAppReporter(ctx context.Context, t *testing.T, apps []codersdk.Workspa
187187
mu.Lock()
188188
for name, health := range req.Healths {
189189
for i, app := range apps {
190-
if app.Name != name {
190+
if app.DisplayName != name {
191191
continue
192192
}
193193
app.Health = health

cli/schedule_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ func TestScheduleShow(t *testing.T) {
5151
lines := strings.Split(strings.TrimSpace(stdoutBuf.String()), "\n")
5252
if assert.Len(t, lines, 4) {
5353
assert.Contains(t, lines[0], "Starts at 7:30AM Mon-Fri (Europe/Dublin)")
54-
assert.Contains(t, lines[1], "Starts next 7:30AM IST on ")
54+
assert.Contains(t, lines[1], "Starts next 7:30AM")
55+
// it should have either IST or GMT
56+
if !strings.Contains(lines[1], "IST") && !strings.Contains(lines[1], "GMT") {
57+
t.Error("expected either IST or GMT")
58+
}
5559
assert.Contains(t, lines[2], "Stops at 8h after start")
5660
assert.NotContains(t, lines[3], "Stops next -")
5761
}
@@ -137,7 +141,11 @@ func TestScheduleStart(t *testing.T) {
137141
lines := strings.Split(strings.TrimSpace(stdoutBuf.String()), "\n")
138142
if assert.Len(t, lines, 4) {
139143
assert.Contains(t, lines[0], "Starts at 9:30AM Mon-Fri (Europe/Dublin)")
140-
assert.Contains(t, lines[1], "Starts next 9:30AM IST on")
144+
assert.Contains(t, lines[1], "Starts next 9:30AM")
145+
// it should have either IST or GMT
146+
if !strings.Contains(lines[1], "IST") && !strings.Contains(lines[1], "GMT") {
147+
t.Error("expected either IST or GMT")
148+
}
141149
}
142150

143151
// Ensure autostart schedule updated

coderd/coderdtest/authorize.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,9 @@ func NewAuthTester(ctx context.Context, t *testing.T, client *codersdk.Client, a
331331
Id: "something",
332332
Auth: &proto.Agent_Token{},
333333
Apps: []*proto.App{{
334-
Name: "testapp",
335-
Url: "http://localhost:3000",
334+
Slug: "testapp",
335+
DisplayName: "testapp",
336+
Url: "http://localhost:3000",
336337
}},
337338
}},
338339
}},
@@ -372,7 +373,7 @@ func NewAuthTester(ctx context.Context, t *testing.T, client *codersdk.Client, a
372373
"{template}": template.ID.String(),
373374
"{fileID}": file.ID.String(),
374375
"{workspaceresource}": workspace.LatestBuild.Resources[0].ID.String(),
375-
"{workspaceapp}": workspace.LatestBuild.Resources[0].Agents[0].Apps[0].Name,
376+
"{workspaceapp}": workspace.LatestBuild.Resources[0].Agents[0].Apps[0].Slug,
376377
"{templateversion}": version.ID.String(),
377378
"{jobID}": templateVersionDryRun.ID.String(),
378379
"{templatename}": template.Name,

coderd/database/databasefake/databasefake.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,15 +1861,15 @@ func (q *fakeQuerier) GetWorkspaceAgentsCreatedAfter(_ context.Context, after ti
18611861
return workspaceAgents, nil
18621862
}
18631863

1864-
func (q *fakeQuerier) GetWorkspaceAppByAgentIDAndName(_ context.Context, arg database.GetWorkspaceAppByAgentIDAndNameParams) (database.WorkspaceApp, error) {
1864+
func (q *fakeQuerier) GetWorkspaceAppByAgentIDAndSlug(_ context.Context, arg database.GetWorkspaceAppByAgentIDAndSlugParams) (database.WorkspaceApp, error) {
18651865
q.mutex.RLock()
18661866
defer q.mutex.RUnlock()
18671867

18681868
for _, app := range q.workspaceApps {
18691869
if app.AgentID != arg.AgentID {
18701870
continue
18711871
}
1872-
if app.Name != arg.Name {
1872+
if app.Slug != arg.Slug {
18731873
continue
18741874
}
18751875
return app, nil
@@ -2522,7 +2522,8 @@ func (q *fakeQuerier) InsertWorkspaceApp(_ context.Context, arg database.InsertW
25222522
ID: arg.ID,
25232523
AgentID: arg.AgentID,
25242524
CreatedAt: arg.CreatedAt,
2525-
Name: arg.Name,
2525+
Slug: arg.Slug,
2526+
DisplayName: arg.DisplayName,
25262527
Icon: arg.Icon,
25272528
Command: arg.Command,
25282529
Url: arg.Url,

coderd/database/dump.sql

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- drop unique index on "slug" column
2+
ALTER TABLE "workspace_apps" DROP CONSTRAINT IF EXISTS "workspace_apps_agent_id_slug_idx";
3+
4+
-- drop "slug" column from "workspace_apps" table
5+
ALTER TABLE "workspace_apps" DROP COLUMN "slug";
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
BEGIN;
2+
3+
-- add "slug" column to "workspace_apps" table
4+
ALTER TABLE "workspace_apps" ADD COLUMN "slug" text DEFAULT '';
5+
6+
-- copy the "name" column for each workspace app to the "slug" column
7+
UPDATE "workspace_apps" SET "slug" = "name";
8+
9+
-- make "slug" column not nullable and remove default
10+
ALTER TABLE "workspace_apps" ALTER COLUMN "slug" SET NOT NULL;
11+
ALTER TABLE "workspace_apps" ALTER COLUMN "slug" DROP DEFAULT;
12+
13+
-- add unique index on "slug" column
14+
ALTER TABLE "workspace_apps" ADD CONSTRAINT "workspace_apps_agent_id_slug_idx" UNIQUE ("agent_id", "slug");
15+
16+
COMMIT;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
BEGIN;
2+
3+
-- Select all apps with an extra "row_number" column that determines the "rank"
4+
-- of the display name against other display names in the same agent.
5+
WITH row_numbers AS (
6+
SELECT
7+
*,
8+
row_number() OVER (PARTITION BY agent_id, display_name ORDER BY display_name ASC) AS row_number
9+
FROM
10+
workspace_apps
11+
)
12+
13+
-- Update any app with a "row_number" greater than 1 to have the row number
14+
-- appended to the display name. This effectively means that all lowercase
15+
-- display names remain untouched, while non-unique mixed case usernames are
16+
-- appended with a unique number. If you had three apps called all called asdf,
17+
-- they would then be renamed to e.g. asdf, asdf1234, and asdf5678.
18+
UPDATE
19+
workspace_apps
20+
SET
21+
display_name = workspace_apps.display_name || floor(random() * 10000)::text
22+
FROM
23+
row_numbers
24+
WHERE
25+
workspace_apps.id = row_numbers.id AND
26+
row_numbers.row_number > 1;
27+
28+
-- rename column "display_name" to "name" on "workspace_apps"
29+
ALTER TABLE "workspace_apps" RENAME COLUMN "display_name" TO "name";
30+
31+
-- restore unique index on "workspace_apps" table
32+
ALTER TABLE workspace_apps ADD CONSTRAINT workspace_apps_agent_id_name_key UNIQUE ("agent_id", "name");
33+
34+
COMMIT;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
BEGIN;
2+
3+
-- rename column "name" to "display_name" on "workspace_apps"
4+
ALTER TABLE "workspace_apps" RENAME COLUMN "name" TO "display_name";
5+
6+
-- drop constraint "workspace_apps_agent_id_name_key" on "workspace_apps".
7+
ALTER TABLE ONLY workspace_apps DROP CONSTRAINT IF EXISTS workspace_apps_agent_id_name_key;
8+
9+
COMMIT;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy