Merge pull request #10277 from hashicorp/dnephin/backport-serf-tag-refactor

[1.8.x] Backport some ent changes for serf tags
This commit is contained in:
Daniel Nephin 2021-05-21 16:43:24 -04:00 committed by GitHub
commit f45ab674ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 22 deletions

3
.changelog/_917.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
namespaces: **(Enterprise only)** fixes a problem where the logs would contain many warnings about namespaces not being licensed.
```

View File

@ -5,13 +5,14 @@ import (
"path/filepath"
"strings"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/serf/serf"
"github.com/hashicorp/consul/agent/metadata"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/consul/logging"
"github.com/hashicorp/consul/types"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/serf/serf"
)
// setupSerf is used to setup and initialize a Serf
@ -63,7 +64,7 @@ func (c *Client) setupSerf(conf *serf.Config, ch chan serf.Event, path string) (
return nil, err
}
c.addEnterpriseSerfTags(conf.Tags)
addEnterpriseSerfTags(conf.Tags)
return serf.Create(conf)
}

View File

@ -23,7 +23,3 @@ func (c *Client) handleEnterpriseUserEvents(event serf.UserEvent) bool {
func (c *Client) enterpriseStats() map[string]map[string]string {
return nil
}
func (_ *Client) addEnterpriseSerfTags(_ map[string]string) {
// do nothing
}

View File

@ -5,10 +5,11 @@ package consul
import (
"net"
"github.com/hashicorp/consul/agent/pool"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/go-version"
"github.com/hashicorp/serf/serf"
"github.com/hashicorp/consul/agent/pool"
"github.com/hashicorp/consul/agent/structs"
)
var (
@ -59,7 +60,7 @@ func (s *Server) validateEnterpriseRequest(entMeta *structs.EnterpriseMeta, writ
return nil
}
func (_ *Server) addEnterpriseSerfTags(_ map[string]string) {
func addEnterpriseSerfTags(_ map[string]string) {
// do nothing
}

View File

@ -205,9 +205,7 @@ func (r *IndexReplicator) Replicate(ctx context.Context, lastRemoteIndex uint64,
return 0, false, fmt.Errorf("failed to retrieve %s: %w", r.Delegate.PluralNoun(), err)
}
r.Logger.Debug("finished fetching remote objects",
"amount", lenRemote,
)
r.Logger.Debug("finished fetching remote objects", "amount", lenRemote)
// Need to check if we should be stopping. This will be common as the fetching process is a blocking
// RPC which could have been hanging around for a long time and during that time leadership could

View File

@ -7,15 +7,16 @@ import (
"strings"
"time"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/memberlist"
"github.com/hashicorp/raft"
"github.com/hashicorp/serf/serf"
"github.com/hashicorp/consul/agent/consul/wanfed"
"github.com/hashicorp/consul/agent/metadata"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/consul/logging"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/memberlist"
"github.com/hashicorp/raft"
"github.com/hashicorp/serf/serf"
)
const (
@ -165,7 +166,7 @@ func (s *Server) setupSerf(conf *serf.Config, ch chan serf.Event, path string, w
return nil, err
}
s.addEnterpriseSerfTags(conf.Tags)
addEnterpriseSerfTags(conf.Tags)
return serf.Create(conf)
}

View File

@ -7,9 +7,10 @@ import (
"strconv"
"strings"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/go-version"
"github.com/hashicorp/serf/serf"
"github.com/hashicorp/consul/agent/structs"
)
// Key is used in maps and for equality tests. A key is based on endpoints.
@ -120,8 +121,8 @@ func IsConsulServer(m serf.Member) (bool, *Server) {
segmentName := strings.TrimPrefix(name, "sl_")
segmentAddrs[segmentName] = addr
segmentPorts[segmentName] = segmentPort
} else if strings.HasPrefix(name, "ft_") {
featureName := strings.TrimPrefix(name, "ft_")
} else if strings.HasPrefix(name, featureFlagPrefix) {
featureName := strings.TrimPrefix(name, featureFlagPrefix)
featureState, err := strconv.Atoi(value)
if err != nil {
return false, nil
@ -188,3 +189,14 @@ func IsConsulServer(m serf.Member) (bool, *Server) {
}
return true, parts
}
const featureFlagPrefix = "ft_"
// AddFeatureFlags to the tags. The tags map is expected to be a serf.Config.Tags.
// The feature flags are encoded in the tags so that IsConsulServer can decode them
// and populate the Server.FeatureFlags map.
func AddFeatureFlags(tags map[string]string, flags ...string) {
for _, flag := range flags {
tags[featureFlagPrefix+flag] = "1"
}
}

View File

@ -4,9 +4,10 @@ import (
"net"
"testing"
"github.com/hashicorp/consul/agent/metadata"
"github.com/hashicorp/serf/serf"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/agent/metadata"
)
func TestServer_Key_params(t *testing.T) {
@ -181,8 +182,10 @@ func TestIsConsulServer_Optional(t *testing.T) {
if parts.RaftVersion != 0 {
t.Fatalf("bad: %v", parts.RaftVersion)
}
m.Tags["bootstrap"] = "1"
m.Tags["disabled"] = "1"
m.Tags["ft_ns"] = "1"
ok, parts = metadata.IsConsulServer(m)
if !ok {
t.Fatalf("expected a valid consul server")
@ -196,6 +199,9 @@ func TestIsConsulServer_Optional(t *testing.T) {
if parts.Version != 1 {
t.Fatalf("bad: %v", parts)
}
expectedFlags := map[string]int{"ns": 1}
require.Equal(t, expectedFlags, parts.FeatureFlags)
m.Tags["expect"] = "3"
delete(m.Tags, "bootstrap")
delete(m.Tags, "disabled")