feat: add filtering options to provisioners list (#19378)

## Summary

In this pull request we're adding support for additional filtering
options to the `provisioners list` CLI command and the
`/provisionerdaemons` API endpoint.

Resolves: https://github.com/coder/coder/issues/18783

### Changes

#### Added CLI Options

- `--show-offline`: When this option is provided, all provisioner
daemons will be returned. This means that when `--show-offline` is not
provided only `idle` and `busy` provisioner daemons will be returned.
- `--status=<list_of_statuses>`: When this option is provided with a
comma-separated list of valid statuses (`idle`, `busy`, or `offline`)
only provisioner daemons that have these statuses will be returned.
- `--max-age=<duration>`: When this option is provided with a valid
duration value (e.g., `24h`, `30s`) only provisioner daemons with a
`last_seen_at` timestamp within the provided max age will be returned.

#### Query Params

- `?offline=true`: Include offline provisioner daemons in the results.
Offline provisioner daemons will be excluded if `?offline=false` or if
offline is not provided.
- `?status=<list_of_statuses>`: Include provisioner daemons with the
specified statuses.
- `?max_age=<duration>`: Include provisioner daemons with a
`last_seen_at` timestamp within the max age duration.

#### Frontend

- Since offline provisioners will not be returned by default anymore
(`--show-offline` has to be provided to see them), a checkbox was added
to the provisioners list page to allow for offline provisioners to be
displayed
- A revamp of the provisioners page will be done in:
https://github.com/coder/coder/issues/17156, this checkbox change was
just added to maintain currently functionality with the backend updates

Current provisioners page (without checkbox)

<img width="1329" height="574" alt="Screenshot 2025-08-20 at 10 51
00 AM"
src="https://github.com/user-attachments/assets/77b73650-0b62-44f0-a77f-acbe5710809f"
/>

Provisioners page with checkbox (unchecked)

<img width="1314" height="626" alt="Screenshot 2025-08-20 at 10 48
40 AM"
src="https://github.com/user-attachments/assets/7ba164ad-6d3f-417b-bd39-338c0161b145"
/>

Provisioner page with checkbox (checked) and URL updated with query
parameters

<img width="1306" height="597" alt="Screenshot 2025-08-20 at 10 50
14 AM"
src="https://github.com/user-attachments/assets/e78d0986-bbf8-491b-9d56-b682973237a0"
/>

### Show Offline vs Offline Status

To list offline provisioner daemons, users can either:

1. Include the `--show-offline` option

OR

2. Include `offline` in the list of values provided to the `--status`
option
This commit is contained in:
Rafael Rodriguez
2025-08-21 16:03:34 -04:00
committed by GitHub
parent d77c3d0226
commit ad5e6785f4
25 changed files with 707 additions and 94 deletions
+31 -2
View File
@@ -2,10 +2,12 @@ package cli
import (
"fmt"
"time"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/coderd/util/slice"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/serpent"
)
@@ -39,7 +41,10 @@ func (r *RootCmd) provisionerList() *serpent.Command {
cliui.TableFormat([]provisionerDaemonRow{}, []string{"created at", "last seen at", "key name", "name", "version", "status", "tags"}),
cliui.JSONFormat(),
)
limit int64
limit int64
offline bool
status []string
maxAge time.Duration
)
cmd := &serpent.Command{
@@ -59,7 +64,10 @@ func (r *RootCmd) provisionerList() *serpent.Command {
}
daemons, err := client.OrganizationProvisionerDaemons(ctx, org.ID, &codersdk.OrganizationProvisionerDaemonsOptions{
Limit: int(limit),
Limit: int(limit),
Offline: offline,
Status: slice.StringEnums[codersdk.ProvisionerDaemonStatus](status),
MaxAge: maxAge,
})
if err != nil {
return xerrors.Errorf("list provisioner daemons: %w", err)
@@ -98,6 +106,27 @@ func (r *RootCmd) provisionerList() *serpent.Command {
Default: "50",
Value: serpent.Int64Of(&limit),
},
{
Flag: "show-offline",
FlagShorthand: "f",
Env: "CODER_PROVISIONER_SHOW_OFFLINE",
Description: "Show offline provisioners.",
Value: serpent.BoolOf(&offline),
},
{
Flag: "status",
FlagShorthand: "s",
Env: "CODER_PROVISIONER_LIST_STATUS",
Description: "Filter by provisioner status.",
Value: serpent.EnumArrayOf(&status, slice.ToStrings(codersdk.ProvisionerDaemonStatusEnums())...),
},
{
Flag: "max-age",
FlagShorthand: "m",
Env: "CODER_PROVISIONER_LIST_MAX_AGE",
Description: "Filter provisioners by maximum age.",
Value: serpent.DurationOf(&maxAge),
},
}...)
orgContext.AttachOptions(cmd)
+68
View File
@@ -197,6 +197,74 @@ func TestProvisioners_Golden(t *testing.T) {
clitest.TestGoldenFile(t, t.Name(), got.Bytes(), replace)
})
t.Run("list with offline provisioner daemons", func(t *testing.T) {
t.Parallel()
var got bytes.Buffer
inv, root := clitest.New(t,
"provisioners",
"list",
"--show-offline",
)
inv.Stdout = &got
clitest.SetupConfig(t, templateAdminClient, root)
err := inv.Run()
require.NoError(t, err)
clitest.TestGoldenFile(t, t.Name(), got.Bytes(), replace)
})
t.Run("list provisioner daemons by status", func(t *testing.T) {
t.Parallel()
var got bytes.Buffer
inv, root := clitest.New(t,
"provisioners",
"list",
"--status=idle,offline,busy",
)
inv.Stdout = &got
clitest.SetupConfig(t, templateAdminClient, root)
err := inv.Run()
require.NoError(t, err)
clitest.TestGoldenFile(t, t.Name(), got.Bytes(), replace)
})
t.Run("list provisioner daemons without offline", func(t *testing.T) {
t.Parallel()
var got bytes.Buffer
inv, root := clitest.New(t,
"provisioners",
"list",
"--status=idle,busy",
)
inv.Stdout = &got
clitest.SetupConfig(t, templateAdminClient, root)
err := inv.Run()
require.NoError(t, err)
clitest.TestGoldenFile(t, t.Name(), got.Bytes(), replace)
})
t.Run("list provisioner daemons by max age", func(t *testing.T) {
t.Parallel()
var got bytes.Buffer
inv, root := clitest.New(t,
"provisioners",
"list",
"--max-age=1h",
)
inv.Stdout = &got
clitest.SetupConfig(t, templateAdminClient, root)
err := inv.Run()
require.NoError(t, err)
clitest.TestGoldenFile(t, t.Name(), got.Bytes(), replace)
})
// Test jobs list with template admin as members are currently
// unable to access provisioner jobs. In the future (with RBAC
// changes), we may allow them to view _their_ jobs.
+4 -5
View File
@@ -1,5 +1,4 @@
ID CREATED AT LAST SEEN AT NAME VERSION TAGS KEY NAME STATUS CURRENT JOB ID CURRENT JOB STATUS PREVIOUS JOB ID PREVIOUS JOB STATUS ORGANIZATION
00000000-0000-0000-aaaa-000000000000 ====[timestamp]===== ====[timestamp]===== default-provisioner v0.0.0-devel map[owner: scope:organization] built-in idle <nil> <nil> 00000000-0000-0000-bbbb-000000000001 succeeded Coder
00000000-0000-0000-aaaa-000000000001 ====[timestamp]===== ====[timestamp]===== provisioner-1 v0.0.0 map[foo:bar owner: scope:organization] built-in busy 00000000-0000-0000-bbbb-000000000002 running <nil> <nil> Coder
00000000-0000-0000-aaaa-000000000002 ====[timestamp]===== ====[timestamp]===== provisioner-2 v0.0.0 map[owner: scope:organization] built-in offline <nil> <nil> 00000000-0000-0000-bbbb-000000000003 succeeded Coder
00000000-0000-0000-aaaa-000000000003 ====[timestamp]===== ====[timestamp]===== provisioner-3 v0.0.0 map[owner: scope:organization] built-in idle <nil> <nil> <nil> <nil> Coder
ID CREATED AT LAST SEEN AT NAME VERSION TAGS KEY NAME STATUS CURRENT JOB ID CURRENT JOB STATUS PREVIOUS JOB ID PREVIOUS JOB STATUS ORGANIZATION
00000000-0000-0000-aaaa-000000000000 ====[timestamp]===== ====[timestamp]===== default-provisioner v0.0.0-devel map[owner: scope:organization] built-in idle <nil> <nil> 00000000-0000-0000-bbbb-000000000001 succeeded Coder
00000000-0000-0000-aaaa-000000000001 ====[timestamp]===== ====[timestamp]===== provisioner-1 v0.0.0 map[foo:bar owner: scope:organization] built-in busy 00000000-0000-0000-bbbb-000000000002 running <nil> <nil> Coder
00000000-0000-0000-aaaa-000000000003 ====[timestamp]===== ====[timestamp]===== provisioner-3 v0.0.0 map[owner: scope:organization] built-in idle <nil> <nil> <nil> <nil> Coder
@@ -0,0 +1,4 @@
CREATED AT LAST SEEN AT KEY NAME NAME VERSION STATUS TAGS
====[timestamp]===== ====[timestamp]===== built-in default-provisioner v0.0.0-devel idle map[owner: scope:organization]
====[timestamp]===== ====[timestamp]===== built-in provisioner-1 v0.0.0 busy map[foo:bar owner: scope:organization]
====[timestamp]===== ====[timestamp]===== built-in provisioner-3 v0.0.0 idle map[owner: scope:organization]
@@ -0,0 +1,5 @@
CREATED AT LAST SEEN AT KEY NAME NAME VERSION STATUS TAGS
====[timestamp]===== ====[timestamp]===== built-in default-provisioner v0.0.0-devel idle map[owner: scope:organization]
====[timestamp]===== ====[timestamp]===== built-in provisioner-1 v0.0.0 busy map[foo:bar owner: scope:organization]
====[timestamp]===== ====[timestamp]===== built-in provisioner-2 v0.0.0 offline map[owner: scope:organization]
====[timestamp]===== ====[timestamp]===== built-in provisioner-3 v0.0.0 idle map[owner: scope:organization]
@@ -0,0 +1,4 @@
CREATED AT LAST SEEN AT KEY NAME NAME VERSION STATUS TAGS
====[timestamp]===== ====[timestamp]===== built-in default-provisioner v0.0.0-devel idle map[owner: scope:organization]
====[timestamp]===== ====[timestamp]===== built-in provisioner-1 v0.0.0 busy map[foo:bar owner: scope:organization]
====[timestamp]===== ====[timestamp]===== built-in provisioner-3 v0.0.0 idle map[owner: scope:organization]
@@ -0,0 +1,5 @@
CREATED AT LAST SEEN AT KEY NAME NAME VERSION STATUS TAGS
====[timestamp]===== ====[timestamp]===== built-in default-provisioner v0.0.0-devel idle map[owner: scope:organization]
====[timestamp]===== ====[timestamp]===== built-in provisioner-1 v0.0.0 busy map[foo:bar owner: scope:organization]
====[timestamp]===== ====[timestamp]===== built-in provisioner-2 v0.0.0 offline map[owner: scope:organization]
====[timestamp]===== ====[timestamp]===== built-in provisioner-3 v0.0.0 idle map[owner: scope:organization]
+9
View File
@@ -17,8 +17,17 @@ OPTIONS:
-l, --limit int, $CODER_PROVISIONER_LIST_LIMIT (default: 50)
Limit the number of provisioners returned.
-m, --max-age duration, $CODER_PROVISIONER_LIST_MAX_AGE
Filter provisioners by maximum age.
-o, --output table|json (default: table)
Output format.
-f, --show-offline bool, $CODER_PROVISIONER_SHOW_OFFLINE
Show offline provisioners.
-s, --status [offline|idle|busy], $CODER_PROVISIONER_LIST_STATUS
Filter by provisioner status.
———
Run `coder --help` for a list of global options.