-
Notifications
You must be signed in to change notification settings - Fork 905
refactor: workspace autostop_schedule -> ttl #1578
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
3980007
3ea3e90
ea49127
a06218f
58d72c3
6f67b79
f65e3de
77b8ad3
80d7b1c
32c92f6
b3168c1
d00fefb
d8ccd6c
22f0222
50a5823
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,6 @@ import ( | |
"github.com/coder/coder/cli/cliflag" | ||
"github.com/coder/coder/cli/cliui" | ||
"github.com/coder/coder/coderd/autobuild/notify" | ||
"github.com/coder/coder/coderd/autobuild/schedule" | ||
"github.com/coder/coder/codersdk" | ||
"github.com/coder/coder/cryptorand" | ||
) | ||
|
@@ -270,16 +269,11 @@ func notifyCondition(ctx context.Context, client *codersdk.Client, workspaceID u | |
return time.Time{}, nil | ||
} | ||
|
||
if ws.AutostopSchedule == "" { | ||
if ws.TTL == nil || *ws.TTL == 0 { | ||
return time.Time{}, nil | ||
} | ||
Comment on lines
+272
to
274
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. Instead of using a pointer why not just have 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. I considered that initially, but I think using the zero value could lead to bugs and/or unintended behaviour. Grey also preferred the semantics of 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. You do have a point, though -- 0 is already an invalid TTL. 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. Comment: I think the FE or other clients having to know As stated earlier, from v2 forwards I believe it's best that we look at the Coder system as a frontend for a backend, not the other way around. 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. Should we then consider that in the API response? We could return something like 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. It's certainly golang convention to make use of "zero-values" in this way, but I don't think we should extend that to the HTTP API, CLI, or frontend, which should behave in ways that are independent of golang's idioms. 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. @spikecurtis are you referring to @deansheather 's comment here: #1578 (comment)
Or the fact that we're using a pointer such that the value can be 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. Yeah, I feel like among go practitioners, 0 means disabled would be preferred to making it a pointer and |
||
|
||
sched, err := schedule.Weekly(ws.AutostopSchedule) | ||
if err != nil { | ||
return time.Time{}, nil | ||
} | ||
|
||
deadline = sched.Next(now) | ||
deadline = ws.LatestBuild.UpdatedAt.Add(*ws.TTL) | ||
greyscaled marked this conversation as resolved.
Show resolved
Hide resolved
|
||
callback = func() { | ||
ttl := deadline.Sub(now) | ||
var title, body string | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/codersdk" | ||
) | ||
|
||
const ttlDescriptionLong = `To have your workspace stop automatically after a configurable interval has passed. | ||
Minimum TTL is 1 minute. | ||
greyscaled marked this conversation as resolved.
Show resolved
Hide resolved
|
||
` | ||
|
||
func ttl() *cobra.Command { | ||
ttlCmd := &cobra.Command{ | ||
Annotations: workspaceCommand, | ||
Use: "ttl [command]", | ||
Short: "Schedule a workspace to automatically stop after a configurable interval", | ||
Long: ttlDescriptionLong, | ||
Example: "coder ttl set my-workspace 8h30m", | ||
} | ||
|
||
ttlCmd.AddCommand(ttlShow()) | ||
greyscaled marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ttlCmd.AddCommand(ttlset()) | ||
ttlCmd.AddCommand(ttlunset()) | ||
|
||
return ttlCmd | ||
} | ||
|
||
func ttlShow() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "show <workspace_name>", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
client, err := createClient(cmd) | ||
if err != nil { | ||
return xerrors.Errorf("create client: %w", err) | ||
} | ||
organization, err := currentOrganization(cmd, client) | ||
if err != nil { | ||
return xerrors.Errorf("get current org: %w", err) | ||
} | ||
|
||
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0]) | ||
if err != nil { | ||
return xerrors.Errorf("get workspace: %w", err) | ||
} | ||
|
||
if workspace.TTL == nil { | ||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "not set\n") | ||
return nil | ||
} | ||
|
||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", workspace.TTL) | ||
|
||
return nil | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func ttlset() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "set <workspace_name> <ttl>", | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
client, err := createClient(cmd) | ||
if err != nil { | ||
return xerrors.Errorf("create client: %w", err) | ||
} | ||
organization, err := currentOrganization(cmd, client) | ||
if err != nil { | ||
return xerrors.Errorf("get current org: %w", err) | ||
} | ||
|
||
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0]) | ||
if err != nil { | ||
return xerrors.Errorf("get workspace: %w", err) | ||
} | ||
|
||
ttl, err := time.ParseDuration(args[1]) | ||
if err != nil { | ||
return xerrors.Errorf("parse ttl: %w", err) | ||
} | ||
|
||
truncated := ttl.Truncate(time.Minute) | ||
|
||
if truncated == 0 { | ||
return xerrors.Errorf("ttl must be at least 1m") | ||
} | ||
|
||
if truncated != ttl { | ||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "warning: ttl rounded down to %s\n", truncated) | ||
} | ||
|
||
err = client.UpdateWorkspaceTTL(cmd.Context(), workspace.ID, codersdk.UpdateWorkspaceTTLRequest{ | ||
TTL: &truncated, | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("update workspace ttl: %w", err) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func ttlunset() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "unset <workspace_name>", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
client, err := createClient(cmd) | ||
if err != nil { | ||
return xerrors.Errorf("create client: %w", err) | ||
} | ||
organization, err := currentOrganization(cmd, client) | ||
if err != nil { | ||
return xerrors.Errorf("get current org: %w", err) | ||
} | ||
|
||
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0]) | ||
if err != nil { | ||
return xerrors.Errorf("get workspace: %w", err) | ||
} | ||
|
||
err = client.UpdateWorkspaceTTL(cmd.Context(), workspace.ID, codersdk.UpdateWorkspaceTTLRequest{ | ||
TTL: nil, | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("update workspace ttl: %w", err) | ||
} | ||
|
||
_, _ = fmt.Fprint(cmd.OutOrStdout(), "ttl unset\n", workspace.Name) | ||
|
||
return nil | ||
}, | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.