diff --git a/agent/consul/client_serf.go b/agent/consul/client_serf.go index 1fa28ab4da..eb10498b95 100644 --- a/agent/consul/client_serf.go +++ b/agent/consul/client_serf.go @@ -5,14 +5,15 @@ 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" libserf "github.com/hashicorp/consul/lib/serf" "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 @@ -67,7 +68,7 @@ func (c *Client) setupSerf(conf *serf.Config, ch chan serf.Event, path string) ( return nil, err } - c.addEnterpriseSerfTags(conf.Tags) + addEnterpriseSerfTags(conf.Tags) conf.ReconnectTimeoutOverride = libserf.NewReconnectOverride(c.logger) diff --git a/agent/consul/enterprise_client_oss.go b/agent/consul/enterprise_client_oss.go index 2b73e6db86..290077ea89 100644 --- a/agent/consul/enterprise_client_oss.go +++ b/agent/consul/enterprise_client_oss.go @@ -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 -} diff --git a/agent/consul/enterprise_server_oss.go b/agent/consul/enterprise_server_oss.go index e2ed043714..b1833b9cd7 100644 --- a/agent/consul/enterprise_server_oss.go +++ b/agent/consul/enterprise_server_oss.go @@ -7,10 +7,11 @@ import ( "net" "strings" - "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 ( @@ -73,7 +74,7 @@ func (s *Server) validateEnterpriseIntentionNamespace(ns string, _ bool) error { return errors.New("Namespaces is a Consul Enterprise feature") } -func (_ *Server) addEnterpriseSerfTags(_ map[string]string) { +func addEnterpriseSerfTags(_ map[string]string) { // do nothing } diff --git a/agent/consul/replication.go b/agent/consul/replication.go index 7a861901d7..4b6f74c5cd 100644 --- a/agent/consul/replication.go +++ b/agent/consul/replication.go @@ -252,9 +252,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 diff --git a/agent/consul/server_serf.go b/agent/consul/server_serf.go index d6b79f0a79..b0847eaa49 100644 --- a/agent/consul/server_serf.go +++ b/agent/consul/server_serf.go @@ -7,16 +7,17 @@ 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" libserf "github.com/hashicorp/consul/lib/serf" "github.com/hashicorp/consul/logging" - "github.com/hashicorp/go-hclog" - "github.com/hashicorp/memberlist" - "github.com/hashicorp/raft" - "github.com/hashicorp/serf/serf" ) const ( @@ -174,7 +175,7 @@ func (s *Server) setupSerf(conf *serf.Config, ch chan serf.Event, path string, w conf.ReconnectTimeoutOverride = libserf.NewReconnectOverride(s.logger) - s.addEnterpriseSerfTags(conf.Tags) + addEnterpriseSerfTags(conf.Tags) if s.config.OverrideInitialSerfTags != nil { s.config.OverrideInitialSerfTags(conf.Tags) diff --git a/agent/metadata/server.go b/agent/metadata/server.go index 5b35ec78ec..c85135d294 100644 --- a/agent/metadata/server.go +++ b/agent/metadata/server.go @@ -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 @@ -192,3 +193,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" + } +} diff --git a/agent/metadata/server_test.go b/agent/metadata/server_test.go index 4f479472c0..e83a4b0874 100644 --- a/agent/metadata/server_test.go +++ b/agent/metadata/server_test.go @@ -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) { @@ -187,8 +188,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") @@ -202,6 +205,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")