Skip to content

Commit

Permalink
Add os as a query param to /v1/nodes.
Browse files Browse the repository at this point in the history
Signed-off-by: Shishir Mahajan <smahajan@roblox.com>
  • Loading branch information
shishir-a412ed committed Apr 15, 2022
1 parent b1cc41e commit cb1aaf5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion api/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ func (v *StatValue) String() string {
type NodeListStub struct {
Address string
ID string
Attributes map[string]string
Attributes map[string]string `json:",omitempty"`
Datacenter string
Name string
NodeClass string
Expand Down
17 changes: 14 additions & 3 deletions command/agent/node_endpoint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package agent

import (
"fmt"
"net/http"
"strings"

Expand All @@ -18,15 +19,24 @@ func (s *HTTPServer) NodesRequest(resp http.ResponseWriter, req *http.Request) (
return nil, nil
}

args.Fields = &structs.NodeStubFields{}
// Parse resources field selection
resources, err := parseBool(req, "resources")
if err != nil {
return nil, err
}
if resources != nil {
args.Fields = &structs.NodeStubFields{
Resources: *resources,
}
args.Fields.Resources = *resources
}

// Parse OS
os, err := parseBool(req, "os")
if err != nil {
return nil, err
}

if os != nil {
args.Fields.OS = *os
}

var out structs.NodeListResponse
Expand All @@ -38,6 +48,7 @@ func (s *HTTPServer) NodesRequest(resp http.ResponseWriter, req *http.Request) (
if out.Nodes == nil {
out.Nodes = make([]*structs.NodeListStub, 0)
}

return out.Nodes, nil
}

Expand Down
7 changes: 6 additions & 1 deletion command/node_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,13 @@ func (c *NodeStatusCommand) Run(args []string) int {
return 1
}

var q *api.QueryOptions
if c.os {
q = &api.QueryOptions{Params: map[string]string{"os": "true"}}
}

// Query the node info
nodes, _, err := client.Nodes().List(nil)
nodes, _, err := client.Nodes().List(q)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying node status: %s", err))
return 1
Expand Down
16 changes: 9 additions & 7 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2189,16 +2189,10 @@ func (n *Node) ComparableResources() *ComparableResources {

// Stub returns a summarized version of the node
func (n *Node) Stub(fields *NodeStubFields) *NodeListStub {

addr, _, _ := net.SplitHostPort(n.HTTPAddr)

// Fetch key attributes from the main Attributes map.
m := make(map[string]string)
m["os.name"] = n.Attributes["os.name"]

s := &NodeListStub{
Address: addr,
Attributes: m,
ID: n.ID,
Datacenter: n.Datacenter,
Name: n.Name,
Expand All @@ -2220,6 +2214,13 @@ func (n *Node) Stub(fields *NodeStubFields) *NodeListStub {
s.NodeResources = n.NodeResources
s.ReservedResources = n.ReservedResources
}

// Fetch key attributes from the main Attributes map.
if fields.OS {
m := make(map[string]string)
m["os.name"] = n.Attributes["os.name"]
s.Attributes = m
}
}

return s
Expand All @@ -2230,7 +2231,7 @@ func (n *Node) Stub(fields *NodeStubFields) *NodeListStub {
type NodeListStub struct {
Address string
ID string
Attributes map[string]string
Attributes map[string]string `json:",omitempty"`
Datacenter string
Name string
NodeClass string
Expand All @@ -2251,6 +2252,7 @@ type NodeListStub struct {
// NodeStubFields defines which fields are included in the NodeListStub.
type NodeStubFields struct {
Resources bool
OS bool
}

// Resources is used to define the resources available
Expand Down

0 comments on commit cb1aaf5

Please sign in to comment.