Merge pull request #3445 from hashicorp/members-only

Simplifies members command to not fetch segments.
pull/3448/head
James Phillips 2017-09-05 12:58:48 -07:00 committed by GitHub
commit f282273b54
8 changed files with 39 additions and 57 deletions

View File

@ -58,6 +58,7 @@ type delegate interface {
GetLANCoordinate() (lib.CoordinateSet, error) GetLANCoordinate() (lib.CoordinateSet, error)
Leave() error Leave() error
LANMembers() []serf.Member LANMembers() []serf.Member
LANMembersAllSegments() ([]serf.Member, error)
LANSegmentMembers(segment string) ([]serf.Member, error) LANSegmentMembers(segment string) ([]serf.Member, error)
LocalMember() serf.Member LocalMember() serf.Member
JoinLAN(addrs []string) (n int, err error) JoinLAN(addrs []string) (n int, err error)

View File

@ -167,14 +167,12 @@ func (s *HTTPServer) AgentMembers(resp http.ResponseWriter, req *http.Request) (
if wan { if wan {
members = s.agent.WANMembers() members = s.agent.WANMembers()
} else { } else {
// If the segment is blank when querying a client, use the agent's
// segment instead of the empty string.
if !s.agent.config.Server && segment == "" {
segment = s.agent.config.Segment
}
var err error var err error
members, err = s.agent.delegate.LANSegmentMembers(segment) if segment == api.AllSegments {
members, err = s.agent.delegate.LANMembersAllSegments()
} else {
members, err = s.agent.delegate.LANSegmentMembers(segment)
}
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -201,6 +201,11 @@ func (c *Client) LANMembers() []serf.Member {
return c.serf.Members() return c.serf.Members()
} }
// LANMembersAllSegments returns members from all segments.
func (c *Client) LANMembersAllSegments() ([]serf.Member, error) {
return c.serf.Members(), nil
}
// LANSegmentMembers only returns our own segment's members, because clients // LANSegmentMembers only returns our own segment's members, because clients
// can't be in multiple segments. // can't be in multiple segments.
func (c *Client) LANSegmentMembers(segment string) ([]serf.Member, error) { func (c *Client) LANSegmentMembers(segment string) ([]serf.Member, error) {

View File

@ -9,6 +9,11 @@ import (
"github.com/hashicorp/serf/serf" "github.com/hashicorp/serf/serf"
) )
// LANMembersAllSegments returns members from all segments.
func (s *Server) LANMembersAllSegments() ([]serf.Member, error) {
return s.LANMembers(), nil
}
// LANSegmentMembers is used to return the members of the given LAN segment. // LANSegmentMembers is used to return the members of the given LAN segment.
func (s *Server) LANSegmentMembers(segment string) ([]serf.Member, error) { func (s *Server) LANSegmentMembers(segment string) ([]serf.Member, error) {
if segment == "" { if segment == "" {

View File

@ -44,12 +44,16 @@ type AgentMember struct {
DelegateCur uint8 DelegateCur uint8
} }
// AllSegments is used to select for all segments in MembersOpts.
const AllSegments = "_all"
// MembersOpts is used for querying member information. // MembersOpts is used for querying member information.
type MembersOpts struct { type MembersOpts struct {
// WAN is whether to show members from the WAN. // WAN is whether to show members from the WAN.
WAN bool WAN bool
// Segment is the LAN segment to show members. // Segment is the LAN segment to show members for. Setting this to the
// AllSegments value above will show members in all segments.
Segment string Segment string
} }

View File

@ -44,7 +44,7 @@ func (c *MembersCommand) Run(args []string) int {
f.StringVar(&statusFilter, "status", ".*", f.StringVar(&statusFilter, "status", ".*",
"If provided, output is filtered to only nodes matching the regular "+ "If provided, output is filtered to only nodes matching the regular "+
"expression for status.") "expression for status.")
f.StringVar(&segment, "segment", "", f.StringVar(&segment, "segment", consulapi.AllSegments,
"(Enterprise-only) If provided, output is filtered to only nodes in"+ "(Enterprise-only) If provided, output is filtered to only nodes in"+
"the given segment.") "the given segment.")
@ -65,32 +65,15 @@ func (c *MembersCommand) Run(args []string) int {
return 1 return 1
} }
// Check if we queried a server and need to query for members in all segments. // Make the request.
var members []*consulapi.AgentMember opts := consulapi.MembersOpts{
if !wan && segment == "" { Segment: segment,
self, err := client.Agent().Self() WAN: wan,
if err != nil { }
c.UI.Error(fmt.Sprintf("Error retrieving agent info: %s", err)) members, err := client.Agent().MembersOpts(opts)
return 1 if err != nil {
} c.UI.Error(fmt.Sprintf("Error retrieving members: %s", err))
if self["Config"]["Server"].(bool) { return 1
segmentMembers, err := getSegmentMembers(client)
if err != nil {
c.UI.Error(fmt.Sprintf("Error retrieving members in segments: %s", err))
return 1
}
members = segmentMembers
}
} else {
var err error
members, err = client.Agent().MembersOpts(consulapi.MembersOpts{
WAN: wan,
Segment: segment,
})
if err != nil {
c.UI.Error(fmt.Sprintf("Error retrieving members: %s", err))
return 1
}
} }
// Filter the results // Filter the results
@ -99,9 +82,9 @@ func (c *MembersCommand) Run(args []string) int {
member := members[i] member := members[i]
if member.Tags["segment"] == "" { if member.Tags["segment"] == "" {
member.Tags["segment"] = "<default>" member.Tags["segment"] = "<default>"
if member.Tags["role"] == "consul" { }
member.Tags["segment"] = "<all>" if member.Tags["role"] == "consul" {
} member.Tags["segment"] = "<all>"
} }
statusString := serf.MemberStatus(member.Status).String() statusString := serf.MemberStatus(member.Status).String()
if !statusRe.MatchString(statusString) { if !statusRe.MatchString(statusString) {

View File

@ -1,16 +0,0 @@
// +build !ent
package command
import consulapi "github.com/hashicorp/consul/api"
// getSegmentMembers returns an empty list since network segments are not
// supported in OSS Consul.
func getSegmentMembers(client *consulapi.Client) ([]*consulapi.AgentMember, error) {
members, err := client.Agent().MembersOpts(consulapi.MembersOpts{})
if err != nil {
return nil, err
}
return members, nil
}

View File

@ -44,9 +44,11 @@ The table below shows this endpoint's support for
members (which is the default). This is only eligible for agents running in members (which is the default). This is only eligible for agents running in
**server mode**. This is specified as part of the URL as a query parameter. **server mode**. This is specified as part of the URL as a query parameter.
- `segment` `(string: "")` - (Enterprise-only) Specifies the segment to list members in. If left blank, - `segment` `(string: "")` - (Enterprise-only) Specifies the segment to list members for.
this will query for the default segment when connecting to a server and the agent's If left blank, this will query for the default segment when connecting to a server and
own segment when connecting to a client (clients can only be part of one network segment). the agent's own segment when connecting to a client (clients can only be part of one
network segment). When querying a server, setting this to the special string `_all`
will show members in all segments.
### Sample Request ### Sample Request