-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Allow lookups based on short identifiers #575
Changes from 7 commits
7ae975e
cec0170
91075e1
bdf4347
5d3bd1b
23bfbbf
7d2f1c6
dcecac1
2a809b9
1af7c57
e89b5af
9057422
db82564
8218442
a73aaf1
c65fdc3
6295108
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ import ( | |
"fmt" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/hashicorp/nomad/api" | ||
) | ||
|
||
type NodeStatusCommand struct { | ||
|
@@ -78,12 +80,14 @@ func (c *NodeStatusCommand) Run(args []string) int { | |
return 0 | ||
} | ||
|
||
shortenNodeId := shouldShortenNodeIds(nodes) | ||
|
||
// Format the nodes list | ||
out := make([]string, len(nodes)+1) | ||
out[0] = "ID|DC|Name|Class|Drain|Status" | ||
for i, node := range nodes { | ||
out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s", | ||
node.ID, | ||
shortenId(node.ID, shortenNodeId), | ||
node.Datacenter, | ||
node.Name, | ||
node.NodeClass, | ||
|
@@ -132,7 +136,7 @@ func (c *NodeStatusCommand) Run(args []string) int { | |
var allocs []string | ||
if !short { | ||
// Query the node allocations | ||
nodeAllocs, _, err := client.Nodes().Allocations(nodeID, nil) | ||
nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil) | ||
if err != nil { | ||
c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err)) | ||
return 1 | ||
|
@@ -160,3 +164,27 @@ func (c *NodeStatusCommand) Run(args []string) int { | |
} | ||
return 0 | ||
} | ||
|
||
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 would remove both these methods and replace them with one that takes the original nodes and returns a map of their original ids to their shortened ones.
With this method I think you could be a bit more advanced with how you shorten. You could start at 8 characters and if there is a collision, then up it to 9, etc, until you get no collisions. Further it is probably still worth while to shorten the non-custom ones if there exists a custom one. |
||
// check if there is a collision if we shorten the Node ids | ||
func shouldShortenNodeIds(nodes []*api.NodeListStub) bool { | ||
ids := map[string]bool{} | ||
|
||
for _, node := range nodes { | ||
if len(node.ID) != 36 { | ||
return false //We have a custom ID, don't shorten anything | ||
} else if ids[node.ID[:8]] == true { | ||
return false //There is a collision | ||
} else { | ||
ids[node.ID[:8]] = true | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// shorten an UUID syntax XXXXXXXX-XX... to 8 chars XXXXXXXX | ||
func shortenId(id string, shouldShortenId bool) string { | ||
if shouldShortenId == true { | ||
return id[:8] | ||
} | ||
return id | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package nomad | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/armon/go-metrics" | ||
|
@@ -80,17 +81,48 @@ func (a *Alloc) GetAlloc(args *structs.AllocSpecificRequest, | |
if err != nil { | ||
return err | ||
} | ||
out, err := snap.AllocByID(args.AllocID) | ||
if err != nil { | ||
return err | ||
|
||
var out *structs.Allocation | ||
|
||
// Exact lookup if the identifier length is 36 (full UUID) | ||
if len(args.AllocID) == 36 { | ||
out, err = snap.AllocByID(args.AllocID) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
iter, err := snap.AllocByIDPrefix(args.AllocID) | ||
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. So as I mentioned earlier we don't want to put this logic here (for all the endpoints). These endpoint are for looking up the particular id. I think the best way to add this feature is to add a You would have to add:
This has the benefit that it makes the list endpoint more usable, maintains the API of the get endpoints, makes everything much more testable, and we aren't using the error as a return value. |
||
if err != nil { | ||
return err | ||
} | ||
|
||
// Gather all matching allocations | ||
var allocs []*structs.Allocation | ||
var allocIds []string | ||
for { | ||
raw := iter.Next() | ||
if raw == nil { | ||
break | ||
} | ||
alloc := raw.(*structs.Allocation) | ||
allocIds = append(allocIds, alloc.ID) | ||
allocs = append(allocs, alloc) | ||
} | ||
|
||
if len(allocs) == 1 { | ||
// Return unique allocation | ||
out = allocs[0] | ||
} else if len(allocs) > 1 { | ||
return fmt.Errorf("Ambiguous identifier: %+v", allocIds) | ||
} | ||
} | ||
|
||
// Setup the output | ||
reply.Alloc = out | ||
if out != nil { | ||
reply.Index = out.ModifyIndex | ||
} else { | ||
// Use the last index that affected the nodes table | ||
// Use the last index that affected the allocs table | ||
index, err := snap.Index("allocs") | ||
if err != nil { | ||
return err | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should probably be a verbose flag that does not shorten the node ids