agent: for various /v1/agent endpoints parse the partition parameter on the request (#11444)

Also update the corresponding CLI commands to send the parameter
appropriately.

NOTE: Behavioral changes are not happening in this PR.
This commit is contained in:
R.B. Boyer 2021-10-28 16:44:38 -05:00 committed by GitHub
parent 017e9d5ae4
commit d40d098321
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 61 additions and 13 deletions

3
.changelog/11444.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
agent: for various /v1/agent endpoints parse the partition parameter on the request
```

View File

@ -306,6 +306,7 @@ func (s *HTTPHandlers) AgentServices(resp http.ResponseWriter, req *http.Request
var token string
s.parseToken(req, &token)
// TODO(partitions): should this default to the agent's partition?
var entMeta structs.EnterpriseMeta
if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil {
return nil, err
@ -371,6 +372,7 @@ func (s *HTTPHandlers) AgentService(resp http.ResponseWriter, req *http.Request)
var token string
s.parseToken(req, &token)
// TODO(partitions): should this default to the agent's partition?
var entMeta structs.EnterpriseMeta
if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil {
return nil, err
@ -447,6 +449,7 @@ func (s *HTTPHandlers) AgentChecks(resp http.ResponseWriter, req *http.Request)
var token string
s.parseToken(req, &token)
// TODO(partitions): should this default to the agent's partition?
var entMeta structs.EnterpriseMeta
if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil {
return nil, err
@ -515,17 +518,26 @@ func (s *HTTPHandlers) AgentMembers(resp http.ResponseWriter, req *http.Request)
}
}
// TODO(partitions): likely partitions+segment integration will take care of this
// Get the request partition and default to that of the agent.
entMeta := s.agent.AgentEnterpriseMeta()
if err := s.parseEntMetaPartition(req, entMeta); err != nil {
return nil, err
}
var members []serf.Member
if wan {
members = s.agent.WANMembers()
} else {
filter := consul.LANMemberFilter{
// TODO(partitions): insert the partition from the request
Partition: entMeta.PartitionOrDefault(),
}
if segment == api.AllSegments {
filter.AllSegments = true
// Older 'consul members' calls will default to adding segment=_all
// so we only choose to use that request argument in the case where
// the partition is also the default and ignore it the rest of the time.
if structs.IsDefaultPartition(filter.Partition) {
filter.AllSegments = true
}
} else {
filter.Segment = segment
}
@ -557,6 +569,12 @@ func (s *HTTPHandlers) AgentJoin(resp http.ResponseWriter, req *http.Request) (i
return nil, acl.ErrPermissionDenied
}
// Get the request partition and default to that of the agent.
entMeta := s.agent.AgentEnterpriseMeta()
if err := s.parseEntMetaPartition(req, entMeta); err != nil {
return nil, err
}
// Check if the WAN is being queried
wan := false
if other := req.URL.Query().Get("wan"); other != "" {
@ -573,8 +591,7 @@ func (s *HTTPHandlers) AgentJoin(resp http.ResponseWriter, req *http.Request) (i
}
_, err = s.agent.JoinWAN([]string{addr})
} else {
// TODO(partitions): use the request entmeta
_, err = s.agent.JoinLAN([]string{addr}, nil)
_, err = s.agent.JoinLAN([]string{addr}, entMeta)
}
return nil, err
}
@ -614,12 +631,17 @@ func (s *HTTPHandlers) AgentForceLeave(resp http.ResponseWriter, req *http.Reque
return nil, acl.ErrPermissionDenied
}
// Get the request partition and default to that of the agent.
entMeta := s.agent.AgentEnterpriseMeta()
if err := s.parseEntMetaPartition(req, entMeta); err != nil {
return nil, err
}
// Check the value of the prune query
_, prune := req.URL.Query()["prune"]
addr := strings.TrimPrefix(req.URL.Path, "/v1/agent/force-leave/")
// TODO(partitions): use the request entmeta
return nil, s.agent.ForceLeave(addr, prune, nil)
return nil, s.agent.ForceLeave(addr, prune, entMeta)
}
// syncChanges is a helper function which wraps a blocking call to sync
@ -635,6 +657,7 @@ func (s *HTTPHandlers) AgentRegisterCheck(resp http.ResponseWriter, req *http.Re
var token string
s.parseToken(req, &token)
// TODO(partitions): should this default to the agent's partition?
var args structs.CheckDefinition
if err := s.parseEntMetaNoWildcard(req, &args.EnterpriseMeta); err != nil {
return nil, err
@ -711,6 +734,7 @@ func (s *HTTPHandlers) AgentDeregisterCheck(resp http.ResponseWriter, req *http.
var token string
s.parseToken(req, &token)
// TODO(partitions): should this default to the agent's partition?
if err := s.parseEntMetaNoWildcard(req, &checkID.EnterpriseMeta); err != nil {
return nil, err
}
@ -803,6 +827,7 @@ func (s *HTTPHandlers) agentCheckUpdate(resp http.ResponseWriter, req *http.Requ
var token string
s.parseToken(req, &token)
// TODO(partitions): should this default to the agent's partition?
if err := s.parseEntMetaNoWildcard(req, &cid.EnterpriseMeta); err != nil {
return nil, err
}
@ -878,6 +903,7 @@ func (s *HTTPHandlers) AgentHealthServiceByID(resp http.ResponseWriter, req *htt
return nil, &BadRequestError{Reason: "Missing serviceID"}
}
// TODO(partitions): should this default to the agent's partition?
var entMeta structs.EnterpriseMeta
if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil {
return nil, err
@ -936,6 +962,7 @@ func (s *HTTPHandlers) AgentHealthServiceByName(resp http.ResponseWriter, req *h
return nil, &BadRequestError{Reason: "Missing service Name"}
}
// TODO(partitions): should this default to the agent's partition?
var entMeta structs.EnterpriseMeta
if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil {
return nil, err
@ -999,6 +1026,7 @@ func (s *HTTPHandlers) AgentRegisterService(resp http.ResponseWriter, req *http.
var args structs.ServiceDefinition
// Fixup the type decode of TTL or Interval if a check if provided.
// TODO(partitions): should this default to the agent's partition?
if err := s.parseEntMetaNoWildcard(req, &args.EnterpriseMeta); err != nil {
return nil, err
}
@ -1165,6 +1193,7 @@ func (s *HTTPHandlers) AgentDeregisterService(resp http.ResponseWriter, req *htt
var token string
s.parseToken(req, &token)
// TODO(partitions): should this default to the agent's partition?
if err := s.parseEntMetaNoWildcard(req, &sid.EnterpriseMeta); err != nil {
return nil, err
}
@ -1222,6 +1251,7 @@ func (s *HTTPHandlers) AgentServiceMaintenance(resp http.ResponseWriter, req *ht
var token string
s.parseToken(req, &token)
// TODO(partitions): should this default to the agent's partition?
if err := s.parseEntMetaNoWildcard(req, &sid.EnterpriseMeta); err != nil {
return nil, err
}
@ -1480,6 +1510,7 @@ func (s *HTTPHandlers) AgentConnectCALeafCert(resp http.ResponseWriter, req *htt
}
var qOpts structs.QueryOptions
// TODO(partitions): should this default to the agent's partition?
if err := s.parseEntMetaNoWildcard(req, &args.EnterpriseMeta); err != nil {
return nil, err
}
@ -1527,6 +1558,7 @@ func (s *HTTPHandlers) AgentConnectAuthorize(resp http.ResponseWriter, req *http
var authReq structs.ConnectAuthorizeRequest
// TODO(partitions): should this default to the agent's partition?
if err := s.parseEntMetaNoWildcard(req, &authReq.EnterpriseMeta); err != nil {
return nil, err
}

View File

@ -4,8 +4,9 @@ import (
"flag"
"fmt"
"github.com/hashicorp/consul/command/flags"
"github.com/mitchellh/cli"
"github.com/hashicorp/consul/command/flags"
)
func New(ui cli.Ui) *cmd {
@ -30,6 +31,7 @@ func (c *cmd) init() {
"Remove agent completely from list of members")
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.PartitionFlag())
c.help = flags.Usage(help, c.flags)
}

View File

@ -18,6 +18,7 @@ func TestForceLeaveCommand_noTabs(t *testing.T) {
}
}
// TODO(partitions): split this test and verify it works in partitions
func TestForceLeaveCommand(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
@ -61,6 +62,7 @@ func TestForceLeaveCommand(t *testing.T) {
})
}
// TODO(partitions): split this test and verify it works in partitions
func TestForceLeaveCommand_NoNodeWithName(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")

View File

@ -4,8 +4,9 @@ import (
"flag"
"fmt"
"github.com/hashicorp/consul/command/flags"
"github.com/mitchellh/cli"
"github.com/hashicorp/consul/command/flags"
)
func New(ui cli.Ui) *cmd {
@ -29,6 +30,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.PartitionFlag())
c.help = flags.Usage(help, c.flags)
}

View File

@ -4,8 +4,9 @@ import (
"strings"
"testing"
"github.com/hashicorp/consul/agent"
"github.com/mitchellh/cli"
"github.com/hashicorp/consul/agent"
)
func TestJoinCommand_noTabs(t *testing.T) {
@ -15,6 +16,7 @@ func TestJoinCommand_noTabs(t *testing.T) {
}
}
// TODO(partitions): split this test and verify it works in partitions
func TestJoinCommandJoin_lan(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")

View File

@ -8,11 +8,12 @@ import (
"sort"
"strings"
consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/serf/serf"
"github.com/mitchellh/cli"
"github.com/ryanuber/columnize"
consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
)
// cmd is a Command implementation that queries a running
@ -52,6 +53,7 @@ func (c *cmd) init() {
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.PartitionFlag())
c.help = flags.Usage(help, c.flags)
}

View File

@ -5,10 +5,13 @@ import (
"strings"
"testing"
"github.com/hashicorp/consul/agent"
"github.com/mitchellh/cli"
"github.com/hashicorp/consul/agent"
)
// TODO(partitions): split these tests
func TestMembersCommand_noTabs(t *testing.T) {
t.Parallel()
if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {