Skip to content

Commit db4b4de

Browse files
committed
Merge branch 'main' into 7452-banner-legacy-params
2 parents 4729d7d + 640fcf4 commit db4b4de

File tree

17 files changed

+160
-29
lines changed

17 files changed

+160
-29
lines changed

.vscode/settings.json

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
"codersdk",
2121
"cronstrue",
2222
"databasefake",
23+
"dbfake",
24+
"dbgen",
2325
"dbtype",
2426
"DERP",
2527
"derphttp",

cli/server.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,11 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
243243
notifyCtx, notifyStop := signal.NotifyContext(ctx, InterruptSignals...)
244244
defer notifyStop()
245245

246-
// Ensure we have a unique cache directory for this process.
247-
cacheDir := filepath.Join(cfg.CacheDir.String(), uuid.NewString())
246+
cacheDir := cfg.CacheDir.String()
248247
err = os.MkdirAll(cacheDir, 0o700)
249248
if err != nil {
250249
return xerrors.Errorf("create cache directory: %w", err)
251250
}
252-
defer os.RemoveAll(cacheDir)
253251

254252
// Clean up idle connections at the end, e.g.
255253
// embedded-postgres can leave an idle connection

cli/testdata/coder_list_--output_json.golden

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"name": "test-workspace",
4949
"autostart_schedule": "CRON_TZ=US/Central 30 9 * * 1-5",
5050
"ttl_ms": 28800000,
51-
"last_used_at": "[timestamp]"
51+
"last_used_at": "[timestamp]",
52+
"deleting_at": null
5253
}
5354
]

coderd/apidoc/docs.go

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/coderd.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/tls"
66
"crypto/x509"
77
"encoding/json"
8+
"flag"
89
"fmt"
910
"io"
1011
"net/http"
@@ -878,7 +879,12 @@ func (api *API) Close() error {
878879
}
879880

880881
func compressHandler(h http.Handler) http.Handler {
881-
cmp := middleware.NewCompressor(5,
882+
level := 5
883+
if flag.Lookup("test.v") != nil {
884+
level = 1
885+
}
886+
887+
cmp := middleware.NewCompressor(level,
882888
"text/*",
883889
"application/*",
884890
"image/*",

coderd/workspaces.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,10 @@ func convertWorkspace(
11691169
autostartSchedule = &workspace.AutostartSchedule.String
11701170
}
11711171

1172-
ttlMillis := convertWorkspaceTTLMillis(workspace.Ttl)
1172+
var (
1173+
ttlMillis = convertWorkspaceTTLMillis(workspace.Ttl)
1174+
deletingAt = calculateDeletingAt(workspace, template)
1175+
)
11731176
return codersdk.Workspace{
11741177
ID: workspace.ID,
11751178
CreatedAt: workspace.CreatedAt,
@@ -1188,6 +1191,7 @@ func convertWorkspace(
11881191
AutostartSchedule: autostartSchedule,
11891192
TTLMillis: ttlMillis,
11901193
LastUsedAt: workspace.LastUsedAt,
1194+
DeletingAt: deletingAt,
11911195
}
11921196
}
11931197

@@ -1200,6 +1204,22 @@ func convertWorkspaceTTLMillis(i sql.NullInt64) *int64 {
12001204
return &millis
12011205
}
12021206

1207+
// Calculate the time of the upcoming workspace deletion, if applicable; otherwise, return nil.
1208+
// Workspaces may have impending deletions if InactivityTTL feature is turned on and the workspace is inactive.
1209+
func calculateDeletingAt(workspace database.Workspace, template database.Template) *time.Time {
1210+
var (
1211+
year, month, day = time.Now().Date()
1212+
beginningOfToday = time.Date(year, month, day, 0, 0, 0, 0, time.Now().Location())
1213+
)
1214+
// If InactivityTTL is turned off (set to 0), if the workspace has already been deleted,
1215+
// or if the workspace was used sometime within the last day, there is no impending deletion
1216+
if template.InactivityTTL == 0 || workspace.Deleted || workspace.LastUsedAt.After(beginningOfToday) {
1217+
return nil
1218+
}
1219+
1220+
return ptr.Ref(workspace.LastUsedAt.Add(time.Duration(template.InactivityTTL) * time.Nanosecond))
1221+
}
1222+
12031223
func validWorkspaceTTLMillis(millis *int64, templateDefault, templateMax time.Duration) (sql.NullInt64, error) {
12041224
if templateDefault == 0 && templateMax != 0 || (templateMax > 0 && templateDefault > templateMax) {
12051225
templateDefault = templateMax

coderd/workspaces_internal_test.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package coderd
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/coder/coder/coderd/database"
10+
"github.com/coder/coder/coderd/util/ptr"
11+
)
12+
13+
func Test_calculateDeletingAt(t *testing.T) {
14+
t.Parallel()
15+
16+
testCases := []struct {
17+
name string
18+
workspace database.Workspace
19+
template database.Template
20+
expected *time.Time
21+
}{
22+
{
23+
name: "DeletingAt",
24+
workspace: database.Workspace{
25+
Deleted: false,
26+
LastUsedAt: time.Now().Add(time.Duration(-10) * time.Hour * 24), // 10 days ago
27+
},
28+
template: database.Template{
29+
InactivityTTL: int64(9 * 24 * time.Hour), // 9 days
30+
},
31+
expected: ptr.Ref(time.Now().Add(time.Duration(-1) * time.Hour * 24)), // yesterday
32+
},
33+
{
34+
name: "InactivityTTLUnset",
35+
workspace: database.Workspace{
36+
Deleted: false,
37+
LastUsedAt: time.Now().Add(time.Duration(-10) * time.Hour * 24),
38+
},
39+
template: database.Template{
40+
InactivityTTL: 0,
41+
},
42+
expected: nil,
43+
},
44+
{
45+
name: "DeletedWorkspace",
46+
workspace: database.Workspace{
47+
Deleted: true,
48+
LastUsedAt: time.Now().Add(time.Duration(-10) * time.Hour * 24),
49+
},
50+
template: database.Template{
51+
InactivityTTL: int64(9 * 24 * time.Hour),
52+
},
53+
expected: nil,
54+
},
55+
{
56+
name: "ActiveWorkspace",
57+
workspace: database.Workspace{
58+
Deleted: true,
59+
LastUsedAt: time.Now().Add(time.Duration(-5) * time.Hour), // 5 hours ago
60+
},
61+
template: database.Template{
62+
InactivityTTL: int64(1 * 24 * time.Hour), // 1 day
63+
},
64+
expected: nil,
65+
},
66+
}
67+
68+
for _, tc := range testCases {
69+
tc := tc
70+
t.Run(tc.name, func(t *testing.T) {
71+
t.Parallel()
72+
73+
found := calculateDeletingAt(tc.workspace, tc.template)
74+
if tc.expected == nil {
75+
require.Nil(t, found, "impending deletion should be nil")
76+
} else {
77+
require.NotNil(t, found)
78+
require.WithinDuration(t, *tc.expected, *found, time.Second, "incorrect impending deletion")
79+
}
80+
})
81+
}
82+
}

codersdk/workspaces.go

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ type Workspace struct {
3434
AutostartSchedule *string `json:"autostart_schedule,omitempty"`
3535
TTLMillis *int64 `json:"ttl_ms,omitempty"`
3636
LastUsedAt time.Time `json:"last_used_at" format:"date-time"`
37+
38+
// DeletingAt indicates the time of the upcoming workspace deletion, if applicable; otherwise it is nil.
39+
// Workspaces may have impending deletions if Template.InactivityTTL feature is turned on and the workspace is inactive.
40+
DeletingAt *time.Time `json:"deleting_at" format:"date-time"`
3741
}
3842

3943
type WorkspacesRequest struct {

docs/api/schemas.md

+22-19
Original file line numberDiff line numberDiff line change
@@ -4610,6 +4610,7 @@ Parameter represents a set value for the scope.
46104610
{
46114611
"autostart_schedule": "string",
46124612
"created_at": "2019-08-24T14:15:22Z",
4613+
"deleting_at": "2019-08-24T14:15:22Z",
46134614
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
46144615
"last_used_at": "2019-08-24T14:15:22Z",
46154616
"latest_build": {
@@ -4747,25 +4748,26 @@ Parameter represents a set value for the scope.
47474748

47484749
### Properties
47494750

4750-
| Name | Type | Required | Restrictions | Description |
4751-
| ------------------------------------------- | -------------------------------------------------- | -------- | ------------ | ----------- |
4752-
| `autostart_schedule` | string | false | | |
4753-
| `created_at` | string | false | | |
4754-
| `id` | string | false | | |
4755-
| `last_used_at` | string | false | | |
4756-
| `latest_build` | [codersdk.WorkspaceBuild](#codersdkworkspacebuild) | false | | |
4757-
| `name` | string | false | | |
4758-
| `organization_id` | string | false | | |
4759-
| `outdated` | boolean | false | | |
4760-
| `owner_id` | string | false | | |
4761-
| `owner_name` | string | false | | |
4762-
| `template_allow_user_cancel_workspace_jobs` | boolean | false | | |
4763-
| `template_display_name` | string | false | | |
4764-
| `template_icon` | string | false | | |
4765-
| `template_id` | string | false | | |
4766-
| `template_name` | string | false | | |
4767-
| `ttl_ms` | integer | false | | |
4768-
| `updated_at` | string | false | | |
4751+
| Name | Type | Required | Restrictions | Description |
4752+
| ------------------------------------------- | -------------------------------------------------- | -------- | ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
4753+
| `autostart_schedule` | string | false | | |
4754+
| `created_at` | string | false | | |
4755+
| `deleting_at` | string | false | | Deleting at indicates the time of the upcoming workspace deletion, if applicable; otherwise it is nil. Workspaces may have impending deletions if Template.InactivityTTL feature is turned on and the workspace is inactive. |
4756+
| `id` | string | false | | |
4757+
| `last_used_at` | string | false | | |
4758+
| `latest_build` | [codersdk.WorkspaceBuild](#codersdkworkspacebuild) | false | | |
4759+
| `name` | string | false | | |
4760+
| `organization_id` | string | false | | |
4761+
| `outdated` | boolean | false | | |
4762+
| `owner_id` | string | false | | |
4763+
| `owner_name` | string | false | | |
4764+
| `template_allow_user_cancel_workspace_jobs` | boolean | false | | |
4765+
| `template_display_name` | string | false | | |
4766+
| `template_icon` | string | false | | |
4767+
| `template_id` | string | false | | |
4768+
| `template_name` | string | false | | |
4769+
| `ttl_ms` | integer | false | | |
4770+
| `updated_at` | string | false | | |
47694771

47704772
## codersdk.WorkspaceAgent
47714773

@@ -5612,6 +5614,7 @@ Parameter represents a set value for the scope.
56125614
{
56135615
"autostart_schedule": "string",
56145616
"created_at": "2019-08-24T14:15:22Z",
5617+
"deleting_at": "2019-08-24T14:15:22Z",
56155618
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
56165619
"last_used_at": "2019-08-24T14:15:22Z",
56175620
"latest_build": {

docs/api/workspaces.md

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ curl -X POST http://coder-server:8080/api/v2/organizations/{organization}/member
5656
{
5757
"autostart_schedule": "string",
5858
"created_at": "2019-08-24T14:15:22Z",
59+
"deleting_at": "2019-08-24T14:15:22Z",
5960
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
6061
"last_used_at": "2019-08-24T14:15:22Z",
6162
"latest_build": {
@@ -228,6 +229,7 @@ curl -X GET http://coder-server:8080/api/v2/users/{user}/workspace/{workspacenam
228229
{
229230
"autostart_schedule": "string",
230231
"created_at": "2019-08-24T14:15:22Z",
232+
"deleting_at": "2019-08-24T14:15:22Z",
231233
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
232234
"last_used_at": "2019-08-24T14:15:22Z",
233235
"latest_build": {
@@ -423,6 +425,7 @@ curl -X GET http://coder-server:8080/api/v2/workspaces \
423425
{
424426
"autostart_schedule": "string",
425427
"created_at": "2019-08-24T14:15:22Z",
428+
"deleting_at": "2019-08-24T14:15:22Z",
426429
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
427430
"last_used_at": "2019-08-24T14:15:22Z",
428431
"latest_build": {
@@ -592,6 +595,7 @@ curl -X GET http://coder-server:8080/api/v2/workspaces/{workspace} \
592595
{
593596
"autostart_schedule": "string",
594597
"created_at": "2019-08-24T14:15:22Z",
598+
"deleting_at": "2019-08-24T14:15:22Z",
595599
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
596600
"last_used_at": "2019-08-24T14:15:22Z",
597601
"latest_build": {

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -2636,7 +2636,5 @@ sigs.k8s.io/structured-merge-diff/v4 v4.1.2/go.mod h1:j/nl6xW8vLS49O8YvXW1ocPhZa
26362636
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
26372637
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
26382638
software.sslmate.com/src/go-pkcs12 v0.2.0 h1:nlFkj7bTysH6VkC4fGphtjXRbezREPgrHuJG20hBGPE=
2639-
storj.io/drpc v0.0.33-0.20220622181519-9206537a4db7 h1:6jIp39oQGZMjfrG3kiafK2tcL0Fbprh2kvaoJNfhvuM=
2640-
storj.io/drpc v0.0.33-0.20220622181519-9206537a4db7/go.mod h1:6rcOyR/QQkSTX/9L5ZGtlZaE2PtXTTZl8d+ulSeeYEg=
26412639
storj.io/drpc v0.0.33-0.20230420154621-9716137f6037 h1:SYRl2YUthhsXNkrP30KwxkDGN9TESdNrbpr14rOxsnM=
26422640
storj.io/drpc v0.0.33-0.20230420154621-9716137f6037/go.mod h1:vR804UNzhBa49NOJ6HeLjd2H3MakC1j5Gv8bsOQT6N4=

helm/templates/coder.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ spec:
6464
- /opt/coder
6565
args:
6666
{{- if .Values.coder.workspaceProxy }}
67-
- proxy
67+
- wsproxy
6868
{{- end }}
6969
- server
7070
resources:

helm/tests/testdata/workspace_proxy.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ spec:
137137
command:
138138
- /opt/coder
139139
args:
140-
- proxy
140+
- wsproxy
141141
- server
142142
resources:
143143
{}

0 commit comments

Comments
 (0)