Skip to content

Commit

Permalink
cli: improve wildcard namespace prefix matches (#10648)
Browse files Browse the repository at this point in the history
When a wildcard namespace is used for `nomad job` commands that support prefix
matching, avoid asking the user for input if a prefix is an unambiguous exact
match so that the behavior is similar to the commands using a specific or
unset namespace.
  • Loading branch information
tgross committed May 24, 2021
1 parent 9135b42 commit 479b4f6
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
## 1.1.1 (Unreleased)

IMPROVEMENTS:
* cli: Cross-namespace `nomad job` commands will now select exact matches if the selection is unambiguous. [[GH-10648](https://github.com/hashicorp/nomad/issues/10648)]

BUG FIXES:
* api: Fixed event stream connection initialization when there are no events to send [[GH-10637](https://github.com/hashicorp/nomad/issues/10637)]
* cli: Fixed a bug where `quota status` and `namespace status` commands may panic if the CLI targets a pre-1.1.0 cluster
* csi: Fixed a bug where `mount_options` were not passed to CSI controller plugins for validation during volume creation and mounting. [[GH-10643](https://github.com/hashicorp/nomad/issues/10643)]


## 1.1.0 (May 18, 2021)

FEATURES:
Expand Down
15 changes: 11 additions & 4 deletions command/job_deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (c *JobDeploymentsCommand) Run(args []string) int {
return 1
}

jobID := args[0]
jobID := strings.TrimSpace(args[0])

// Check if the job exists
jobs, _, err := client.Jobs().PrefixList(jobID)
Expand All @@ -122,10 +122,17 @@ func (c *JobDeploymentsCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf("No job(s) with prefix or id %q found", jobID))
return 1
}
if len(jobs) > 1 && (c.allNamespaces() || strings.TrimSpace(jobID) != jobs[0].ID) {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
if len(jobs) > 1 {
if jobID != jobs[0].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
}
if c.allNamespaces() && jobs[0].ID == jobs[1].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
}
}

jobID = jobs[0].ID
q := &api.QueryOptions{Namespace: jobs[0].JobSummary.Namespace}

Expand Down
14 changes: 10 additions & 4 deletions command/job_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (c *JobHistoryCommand) Run(args []string) int {
return 1
}

jobID := args[0]
jobID := strings.TrimSpace(args[0])

// Check if the job exists
jobs, _, err := client.Jobs().PrefixList(jobID)
Expand All @@ -133,9 +133,15 @@ func (c *JobHistoryCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf("No job(s) with prefix or id %q found", jobID))
return 1
}
if len(jobs) > 1 && (c.allNamespaces() || strings.TrimSpace(jobID) != jobs[0].ID) {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
if len(jobs) > 1 {
if jobID != jobs[0].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
}
if c.allNamespaces() && jobs[0].ID == jobs[1].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
}
}

q := &api.QueryOptions{Namespace: jobs[0].JobSummary.Namespace}
Expand Down
14 changes: 10 additions & 4 deletions command/job_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (c *JobInspectCommand) Run(args []string) int {
c.Ui.Error(commandErrorText(c))
return 1
}
jobID := args[0]
jobID := strings.TrimSpace(args[0])

// Check if the job exists
jobs, _, err := client.Jobs().PrefixList(jobID)
Expand All @@ -129,9 +129,15 @@ func (c *JobInspectCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf("No job(s) with prefix or id %q found", jobID))
return 1
}
if len(jobs) > 1 && (c.allNamespaces() || strings.TrimSpace(jobID) != jobs[0].ID) {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
if len(jobs) > 1 {
if jobID != jobs[0].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
}
if c.allNamespaces() && jobs[0].ID == jobs[1].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
}
}

var version *uint64
Expand Down
14 changes: 10 additions & 4 deletions command/job_promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (c *JobPromoteCommand) Run(args []string) int {
}

// Check if the job exists
jobID := args[0]
jobID := strings.TrimSpace(args[0])
jobs, _, err := client.Jobs().PrefixList(jobID)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error promoting job: %s", err))
Expand All @@ -127,9 +127,15 @@ func (c *JobPromoteCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf("No job(s) with prefix or id %q found", jobID))
return 1
}
if len(jobs) > 1 && (c.allNamespaces() || strings.TrimSpace(jobID) != jobs[0].ID) {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
if len(jobs) > 1 {
if jobID != jobs[0].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
}
if c.allNamespaces() && jobs[0].ID == jobs[1].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
}
}
jobID = jobs[0].ID
q := &api.QueryOptions{Namespace: jobs[0].JobSummary.Namespace}
Expand Down
14 changes: 10 additions & 4 deletions command/job_revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (c *JobRevertCommand) Run(args []string) int {
vaultToken = os.Getenv("VAULT_TOKEN")
}

jobID := args[0]
jobID := strings.TrimSpace(args[0])
revertVersion, ok, err := parseVersion(args[1])
if !ok {
c.Ui.Error("The job version to revert to must be specified using the -job-version flag")
Expand All @@ -147,9 +147,15 @@ func (c *JobRevertCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf("No job(s) with prefix or id %q found", jobID))
return 1
}
if len(jobs) > 1 && (c.allNamespaces() || strings.TrimSpace(jobID) != jobs[0].ID) {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
if len(jobs) > 1 {
if jobID != jobs[0].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
}
if c.allNamespaces() && jobs[0].ID == jobs[1].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
}
}

// Prefix lookup matched a single job
Expand Down
15 changes: 11 additions & 4 deletions command/job_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (c *JobStatusCommand) Run(args []string) int {
}

// Try querying the job
jobID := args[0]
jobID := strings.TrimSpace(args[0])

jobs, _, err := client.Jobs().PrefixList(jobID)
if err != nil {
Expand All @@ -157,10 +157,17 @@ func (c *JobStatusCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf("No job(s) with prefix or id %q found", jobID))
return 1
}
if len(jobs) > 1 && (allNamespaces || strings.TrimSpace(jobID) != jobs[0].ID) {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, allNamespaces)))
return 1
if len(jobs) > 1 {
if jobID != jobs[0].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, allNamespaces)))
return 1
}
if allNamespaces && jobs[0].ID == jobs[1].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, allNamespaces)))
return 1
}
}

// Prefix lookup matched a single job
q := &api.QueryOptions{Namespace: jobs[0].JobSummary.Namespace}
job, _, err := client.Jobs().Info(jobs[0].ID, q)
Expand Down
15 changes: 11 additions & 4 deletions command/job_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (c *JobStopCommand) Run(args []string) int {
c.Ui.Error(commandErrorText(c))
return 1
}
jobID := args[0]
jobID := strings.TrimSpace(args[0])

// Get the HTTP client
client, err := c.Meta.Client()
Expand All @@ -135,10 +135,17 @@ func (c *JobStopCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf("No job(s) with prefix or id %q found", jobID))
return 1
}
if len(jobs) > 1 && (c.allNamespaces() || strings.TrimSpace(jobID) != jobs[0].ID) {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
if len(jobs) > 1 {
if jobID != jobs[0].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
}
if c.allNamespaces() && jobs[0].ID == jobs[1].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs, c.allNamespaces())))
return 1
}
}

// Prefix lookup matched a single job
q := &api.QueryOptions{Namespace: jobs[0].JobSummary.Namespace}
job, _, err := client.Jobs().Info(jobs[0].ID, q)
Expand Down

0 comments on commit 479b4f6

Please sign in to comment.