Skip to content
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

CLI Allow querying all namespaces for jobs and allocations - Try 2 #8192

Merged
merged 5 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ var (
ClientConnTimeout = 1 * time.Second
)

const (
// AllNamespacesNamespace is a sentinel Namespace value to indicate that api should search for
// jobs and allocations in all the namespaces the requester can access.
AllNamespacesNamespace = "*"
)

// QueryOptions are used to parametrize a query
type QueryOptions struct {
// Providing a datacenter overwrites the region provided
Expand Down
7 changes: 0 additions & 7 deletions api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,6 @@ func (j *Jobs) PrefixList(prefix string) ([]*JobListStub, *QueryMeta, error) {
return j.List(&QueryOptions{Prefix: prefix})
}

// ListAll is used to list all of the existing jobs in all namespaces.
func (j *Jobs) ListAll() ([]*JobListStub, *QueryMeta, error) {
return j.List(&QueryOptions{
Params: map[string]string{"all_namespaces": "true"},
})
}

// Info is used to retrieve information about a particular
// job given its unique ID.
func (j *Jobs) Info(jobID string, q *QueryOptions) (*Job, *QueryMeta, error) {
Expand Down
1 change: 0 additions & 1 deletion command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func (s *HTTPServer) jobListRequest(resp http.ResponseWriter, req *http.Request)
return nil, nil
}

args.AllNamespaces, _ = strconv.ParseBool(req.URL.Query().Get("all_namespaces"))
var out structs.JobListResponse
if err := s.agent.RPC("Job.List", &args, &out); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion command/agent/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestHTTP_JobsList_AllNamespaces_OSS(t *testing.T) {
}

// Make the HTTP request
req, err := http.NewRequest("GET", "/v1/jobs?all_namespaces=true", nil)
req, err := http.NewRequest("GET", "/v1/jobs?namespace=*", nil)
require.NoError(t, err)
respW := httptest.NewRecorder()

Expand Down
3 changes: 2 additions & 1 deletion command/alloc_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ func (l *AllocExecCommand) Run(args []string) int {
return 1
}
// Prefix lookup matched a single allocation
alloc, _, err := client.Allocations().Info(allocs[0].ID, nil)
q := &api.QueryOptions{Namespace: allocs[0].Namespace}
alloc, _, err := client.Allocations().Info(allocs[0].ID, q)
if err != nil {
l.Ui.Error(fmt.Sprintf("Error querying allocation: %s", err))
return 1
Expand Down
3 changes: 2 additions & 1 deletion command/alloc_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ func (f *AllocFSCommand) Run(args []string) int {
return 1
}
// Prefix lookup matched a single allocation
alloc, _, err := client.Allocations().Info(allocs[0].ID, nil)
q := &api.QueryOptions{Namespace: allocs[0].Namespace}
alloc, _, err := client.Allocations().Info(allocs[0].ID, q)
if err != nil {
f.Ui.Error(fmt.Sprintf("Error querying allocation: %s", err))
return 1
Expand Down
3 changes: 2 additions & 1 deletion command/alloc_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ func (l *AllocLogsCommand) Run(args []string) int {
return 1
}
// Prefix lookup matched a single allocation
alloc, _, err := client.Allocations().Info(allocs[0].ID, nil)
q := &api.QueryOptions{Namespace: allocs[0].Namespace}
alloc, _, err := client.Allocations().Info(allocs[0].ID, q)
if err != nil {
l.Ui.Error(fmt.Sprintf("Error querying allocation: %s", err))
return 1
Expand Down
3 changes: 2 additions & 1 deletion command/alloc_restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ func (c *AllocRestartCommand) Run(args []string) int {
}

// Prefix lookup matched a single allocation
alloc, _, err := client.Allocations().Info(allocs[0].ID, nil)
q := &api.QueryOptions{Namespace: allocs[0].Namespace}
alloc, _, err := client.Allocations().Info(allocs[0].ID, q)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying allocation: %s", err))
return 1
Expand Down
4 changes: 3 additions & 1 deletion command/alloc_signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
)
Expand Down Expand Up @@ -100,7 +101,8 @@ func (c *AllocSignalCommand) Run(args []string) int {
}

// Prefix lookup matched a single allocation
alloc, _, err := client.Allocations().Info(allocs[0].ID, nil)
q := &api.QueryOptions{Namespace: allocs[0].Namespace}
alloc, _, err := client.Allocations().Info(allocs[0].ID, q)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying allocation: %s", err))
return 1
Expand Down
3 changes: 2 additions & 1 deletion command/alloc_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ func (c *AllocStatusCommand) Run(args []string) int {
return 0
}
// Prefix lookup matched a single allocation
alloc, _, err := client.Allocations().Info(allocs[0].ID, nil)
q := &api.QueryOptions{Namespace: allocs[0].Namespace}
alloc, _, err := client.Allocations().Info(allocs[0].ID, q)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying allocation: %s", err))
return 1
Expand Down
5 changes: 4 additions & 1 deletion command/alloc_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package command
import (
"fmt"
"strings"

"github.com/hashicorp/nomad/api"
)

type AllocStopCommand struct {
Expand Down Expand Up @@ -102,7 +104,8 @@ func (c *AllocStopCommand) Run(args []string) int {
}

// Prefix lookup matched a single allocation
alloc, _, err := client.Allocations().Info(allocs[0].ID, nil)
q := &api.QueryOptions{Namespace: allocs[0].Namespace}
alloc, _, err := client.Allocations().Info(allocs[0].ID, q)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying allocation: %s", err))
return 1
Expand Down
10 changes: 6 additions & 4 deletions command/job_deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
)
Expand Down Expand Up @@ -118,11 +119,12 @@ 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 && strings.TrimSpace(jobID) != jobs[0].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs)))
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
}
jobID = jobs[0].ID
q := &api.QueryOptions{Namespace: jobs[0].JobSummary.Namespace}

// Truncate the id unless full length is requested
length := shortId
Expand All @@ -131,7 +133,7 @@ func (c *JobDeploymentsCommand) Run(args []string) int {
}

if latest {
deploy, _, err := client.Jobs().LatestDeployment(jobID, nil)
deploy, _, err := client.Jobs().LatestDeployment(jobID, q)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving deployments: %s", err))
return 1
Expand All @@ -152,7 +154,7 @@ func (c *JobDeploymentsCommand) Run(args []string) int {
return 0
}

deploys, _, err := client.Jobs().Deployments(jobID, all, nil)
deploys, _, err := client.Jobs().Deployments(jobID, all, q)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving deployments: %s", err))
return 1
Expand Down
8 changes: 5 additions & 3 deletions command/job_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,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 && strings.TrimSpace(jobID) != jobs[0].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs)))
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
}

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

// Prefix lookup matched a single job
versions, diffs, _, err := client.Jobs().Versions(jobs[0].ID, diff, nil)
versions, diffs, _, err := client.Jobs().Versions(jobs[0].ID, diff, q)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving job versions: %s", err))
return 1
Expand Down
16 changes: 10 additions & 6 deletions command/job_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ 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 && strings.TrimSpace(jobID) != jobs[0].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs)))
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
}

Expand All @@ -143,7 +143,7 @@ func (c *JobInspectCommand) Run(args []string) int {
}

// Prefix lookup matched a single job
job, err := getJob(client, jobs[0].ID, version)
job, err := getJob(client, jobs[0].JobSummary.Namespace, jobs[0].ID, version)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error inspecting job: %s", err))
return 1
Expand Down Expand Up @@ -183,13 +183,17 @@ func (c *JobInspectCommand) Run(args []string) int {
}

// getJob retrieves the job optionally at a particular version.
func getJob(client *api.Client, jobID string, version *uint64) (*api.Job, error) {
func getJob(client *api.Client, namespace, jobID string, version *uint64) (*api.Job, error) {
var q *api.QueryOptions
if namespace != "" {
q = &api.QueryOptions{Namespace: namespace}
}
if version == nil {
job, _, err := client.Jobs().Info(jobID, nil)
job, _, err := client.Jobs().Info(jobID, q)
return job, err
}

versions, _, _, err := client.Jobs().Versions(jobID, false, nil)
versions, _, _, err := client.Jobs().Versions(jobID, false, q)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions command/job_periodic_force.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,14 @@ func (c *JobPeriodicForceCommand) Run(args []string) int {
return 1
}
if len(periodicJobs) > 1 {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple periodic jobs\n\n%s", createStatusListOutput(periodicJobs)))
c.Ui.Error(fmt.Sprintf("Prefix matched multiple periodic jobs\n\n%s", createStatusListOutput(periodicJobs, c.allNamespaces())))
return 1
}
jobID = periodicJobs[0].ID
q := &api.WriteOptions{Namespace: periodicJobs[0].JobSummary.Namespace}

// force the evaluation
evalID, _, err := client.Jobs().PeriodicForce(jobID, nil)
evalID, _, err := client.Jobs().PeriodicForce(jobID, q)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error forcing periodic job %q: %s", jobID, err))
return 1
Expand Down
12 changes: 7 additions & 5 deletions command/job_promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,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 && strings.TrimSpace(jobID) != jobs[0].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs)))
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
}
jobID = jobs[0].ID
q := &api.QueryOptions{Namespace: jobs[0].JobSummary.Namespace}

// Do a prefix lookup
deploy, _, err := client.Jobs().LatestDeployment(jobID, nil)
deploy, _, err := client.Jobs().LatestDeployment(jobID, q)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving deployment: %s", err))
return 1
Expand All @@ -142,11 +143,12 @@ func (c *JobPromoteCommand) Run(args []string) int {
return 1
}

wq := &api.WriteOptions{Namespace: jobs[0].JobSummary.Namespace}
var u *api.DeploymentUpdateResponse
if len(groups) == 0 {
u, _, err = client.Deployments().PromoteAll(deploy.ID, nil)
u, _, err = client.Deployments().PromoteAll(deploy.ID, wq)
} else {
u, _, err = client.Deployments().PromoteGroups(deploy.ID, groups, nil)
u, _, err = client.Deployments().PromoteGroups(deploy.ID, groups, wq)
}

if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions command/job_revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"

"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
)
Expand Down Expand Up @@ -143,13 +144,14 @@ 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 && strings.TrimSpace(jobID) != jobs[0].ID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs)))
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
}

// Prefix lookup matched a single job
resp, _, err := client.Jobs().Revert(jobs[0].ID, revertVersion, nil, nil, consulToken, vaultToken)
q := &api.WriteOptions{Namespace: jobs[0].JobSummary.Namespace}
resp, _, err := client.Jobs().Revert(jobs[0].ID, revertVersion, nil, q, consulToken, vaultToken)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving job versions: %s", err))
return 1
Expand Down
Loading