From 9fe88ebefeb0606075b1c242b8e2565817197415 Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Mon, 31 Jul 2023 13:15:15 -0400 Subject: [PATCH] cli: support wildcard namespace in alloc subcommands (#18095) The alloc exec and filesystem/logs commands allow passing the `-job` flag to select a random allocation. If the namespace for the command is set to `*`, the RPC handler doesn't handle this correctly as it's expecting to query for a specific job. Most commands handle this ambiguity by first verifying that only a single object of the type in question exists (ex. a single node or job). Update these commands so that when the `-job` flag is set we first verify there's a single job that matches. This also allows us to extend the functionality to allow for the `-job` flag to support prefix matching. Fixes: #12097 --- .changelog/18095.txt | 3 +++ command/alloc_exec.go | 11 +++++++--- command/alloc_exec_test.go | 2 +- command/alloc_fs.go | 22 ++++++++++++++------ command/alloc_logs.go | 12 ++++++++--- website/content/docs/commands/alloc/exec.mdx | 3 ++- website/content/docs/commands/alloc/fs.mdx | 4 ++-- website/content/docs/commands/alloc/logs.mdx | 4 ++-- 8 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 .changelog/18095.txt diff --git a/.changelog/18095.txt b/.changelog/18095.txt new file mode 100644 index 000000000..51fc23a2c --- /dev/null +++ b/.changelog/18095.txt @@ -0,0 +1,3 @@ +```release-note:improvement +cli: support wildcard namespaces in alloc subcommands when the `-job` flag is used +``` diff --git a/command/alloc_exec.go b/command/alloc_exec.go index 5bba969be..8c711739f 100644 --- a/command/alloc_exec.go +++ b/command/alloc_exec.go @@ -51,7 +51,7 @@ Exec Specific Options: Sets the task to exec command in -job - Use a random allocation from the specified job ID. + Use a random allocation from the specified job ID or prefix. -i Pass stdin to the container, defaults to true. Pass -i=false to disable. @@ -162,8 +162,13 @@ func (l *AllocExecCommand) Run(args []string) int { var allocStub *api.AllocationListStub if job { - jobID := args[0] - allocStub, err = getRandomJobAlloc(client, jobID) + jobID, ns, err := l.JobIDByPrefix(client, args[0], nil) + if err != nil { + l.Ui.Error(err.Error()) + return 1 + } + + allocStub, err = getRandomJobAlloc(client, jobID, ns) if err != nil { l.Ui.Error(fmt.Sprintf("Error fetching allocations: %v", err)) return 1 diff --git a/command/alloc_exec_test.go b/command/alloc_exec_test.go index 69ebc5a60..22e63be9e 100644 --- a/command/alloc_exec_test.go +++ b/command/alloc_exec_test.go @@ -60,7 +60,7 @@ func TestAllocExecCommand_Fails(t *testing.T) { { "job not found", []string{"-address=" + url, "-job", "example", "/bin/bash"}, - `job "example" doesn't exist`, + `No job(s) with prefix or ID "example" found`, }, { "command missing", diff --git a/command/alloc_fs.go b/command/alloc_fs.go index c21c7c28a..dcd6d7092 100644 --- a/command/alloc_fs.go +++ b/command/alloc_fs.go @@ -59,7 +59,7 @@ FS Specific Options: Show full information. -job - Use a random allocation from the specified job ID. + Use a random allocation from the specified job ID or prefix. -stat Show file stat information instead of displaying the file, or listing the directory. @@ -167,7 +167,13 @@ func (f *AllocFSCommand) Run(args []string) int { // If -job is specified, use random allocation, otherwise use provided allocation allocID := args[0] if job { - allocID, err = getRandomJobAllocID(client, args[0]) + jobID, ns, err := f.JobIDByPrefix(client, args[0], nil) + if err != nil { + f.Ui.Error(err.Error()) + return 1 + } + + allocID, err = getRandomJobAllocID(client, jobID, ns) if err != nil { f.Ui.Error(fmt.Sprintf("Error fetching allocations: %v", err)) return 1 @@ -381,9 +387,13 @@ func (f *AllocFSCommand) followFile(client *api.Client, alloc *api.Allocation, // Get Random Allocation from a known jobID. Prefer to use a running allocation, // but use a dead allocation if no running allocations are found -func getRandomJobAlloc(client *api.Client, jobID string) (*api.AllocationListStub, error) { +func getRandomJobAlloc(client *api.Client, jobID, namespace string) (*api.AllocationListStub, error) { var runningAllocs []*api.AllocationListStub - allocs, _, err := client.Jobs().Allocations(jobID, false, nil) + q := &api.QueryOptions{ + Namespace: namespace, + } + + allocs, _, err := client.Jobs().Allocations(jobID, false, q) if err != nil { return nil, fmt.Errorf("error querying job %q: %w", jobID, err) } @@ -409,8 +419,8 @@ func getRandomJobAlloc(client *api.Client, jobID string) (*api.AllocationListStu return alloc, err } -func getRandomJobAllocID(client *api.Client, jobID string) (string, error) { - alloc, err := getRandomJobAlloc(client, jobID) +func getRandomJobAllocID(client *api.Client, jobID, namespace string) (string, error) { + alloc, err := getRandomJobAlloc(client, jobID, namespace) if err != nil { return "", err } diff --git a/command/alloc_logs.go b/command/alloc_logs.go index 901dd2d4b..090f17a75 100644 --- a/command/alloc_logs.go +++ b/command/alloc_logs.go @@ -57,11 +57,11 @@ Logs Specific Options: Show full information. -task - Sets the task to view the logs. If task name is given with both an argument + Sets the task to view the logs. If task name is given with both an argument and the '-task' option, preference is given to the '-task' option. -job - Use a random allocation from the specified job ID. + Use a random allocation from the specified job ID or prefix. -f Causes the output to not stop when the end of the logs are reached, but @@ -167,7 +167,13 @@ func (l *AllocLogsCommand) Run(args []string) int { // If -job is specified, use random allocation, otherwise use provided allocation allocID := args[0] if l.job { - allocID, err = getRandomJobAllocID(client, args[0]) + jobID, ns, err := l.JobIDByPrefix(client, args[0], nil) + if err != nil { + l.Ui.Error(err.Error()) + return 1 + } + + allocID, err = getRandomJobAllocID(client, jobID, ns) if err != nil { l.Ui.Error(fmt.Sprintf("Error fetching allocations: %v", err)) return 1 diff --git a/website/content/docs/commands/alloc/exec.mdx b/website/content/docs/commands/alloc/exec.mdx index 9cee1bda4..87d66d3ed 100644 --- a/website/content/docs/commands/alloc/exec.mdx +++ b/website/content/docs/commands/alloc/exec.mdx @@ -41,7 +41,8 @@ capabilities for the allocation's namespace. - `-task`: Sets the task to exec command in. -- `-job`: Use a random allocation from the specified job ID. +- `-job`: Use a random allocation from the specified job or job ID prefix, + preferring a running allocation. - `-i`: Pass stdin to the container, defaults to true. Pass `-i=false` to disable explicitly. diff --git a/website/content/docs/commands/alloc/fs.mdx b/website/content/docs/commands/alloc/fs.mdx index b03e3ae9a..2fdb4e42c 100644 --- a/website/content/docs/commands/alloc/fs.mdx +++ b/website/content/docs/commands/alloc/fs.mdx @@ -48,8 +48,8 @@ When ACLs are enabled, this command requires a token with the `read-fs`, - `-verbose`: Display verbose output. -- `-job`: Use a random allocation from the specified job, preferring a running - allocation. +- `-job`: Use a random allocation from the specified job or job ID prefix, + preferring a running allocation. - `-stat`: Show stat information instead of displaying the file, or listing the directory. diff --git a/website/content/docs/commands/alloc/logs.mdx b/website/content/docs/commands/alloc/logs.mdx index c94a7f4f0..ddf16f7b7 100644 --- a/website/content/docs/commands/alloc/logs.mdx +++ b/website/content/docs/commands/alloc/logs.mdx @@ -43,8 +43,8 @@ When ACLs are enabled, this command requires a token with the `read-logs`, - `-verbose`: Display verbose output. -- `-job`: Use a random allocation from the specified job, preferring a running - allocation. +- `-job`: Use a random allocation from the specified job or job ID prefix, + preferring a running allocation. - `-task`: Specify the task to view the logs.