Skip to content

Commit

Permalink
consul: pr cleanup namespace probe function signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
shoenig committed Jun 7, 2021
1 parent 0bc8a33 commit 51fe1e0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 28 deletions.
2 changes: 1 addition & 1 deletion client/fingerprint/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,5 @@ func (f *ConsulFingerprint) grpc(info agentconsul.Self) (string, bool) {
}

func (f *ConsulFingerprint) namespaces(info agentconsul.Self) (string, bool) {
return agentconsul.Namespaces(info)
return strconv.FormatBool(agentconsul.Namespaces(info)), true
}
7 changes: 1 addition & 6 deletions command/agent/consul/namespaces_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,7 @@ func (ns *NamespacesClient) allowable(now time.Time) bool {
return ns.enabled
}

enabledStr, ok := Namespaces(self)
if !ok {
return ns.enabled
}

ns.enabled = enabledStr == "true"
ns.enabled = Namespaces(self)
ns.updated = now
return ns.enabled
}
Expand Down
19 changes: 9 additions & 10 deletions command/agent/consul/self.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,29 @@ func SKU(info Self) (string, bool) {
return "oss", true
}

func Namespaces(info Self) (string, bool) {
// Namespaces returns true if the "Namespaces" feature is enabled in Consul, and
// false otherwise. Consul OSS will always return false, and Consul ENT will return
// false if the license file does not contain the necessary feature.
func Namespaces(info Self) bool {
return feature("Namespaces", info)
}

// Feature returns whether the indicated feature is enabled by Consul and the
// feature returns whether the indicated feature is enabled by Consul and the
// associated License.
// possible values as of v1.9.5+ent:
// Automated Backups, Automated Upgrades, Enhanced Read Scalability,
// Network Segments, Redundancy Zone, Advanced Network Federation,
// Namespaces, SSO, Audit Logging
func feature(name string, info Self) (string, bool) {
func feature(name string, info Self) bool {
lic, licOK := info["Stats"]["license"].(map[string]interface{})
if !licOK {
return "", false
return false
}

features, exists := lic["features"].(string)
if !exists {
return "", false
}

if !strings.Contains(features, name) {
return "", false
return false
}

return "true", true
return strings.Contains(features, name)
}
21 changes: 10 additions & 11 deletions command/agent/consul/self_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,31 @@ func TestSelf_Namespaces(t *testing.T) {
t.Parallel()

t.Run("supports namespaces", func(t *testing.T) {
s, ok := Namespaces(Self{
enabled := Namespaces(Self{
"Stats": {"license": map[string]interface{}{"features": "Automated Backups, Automated Upgrades, Enhanced Read Scalability, Network Segments, Redundancy Zone, Advanced Network Federation, Namespaces, SSO, Audit Logging"}},
})
require.True(t, ok)
require.Equal(t, "true", s)
require.True(t, enabled)
})

t.Run("no namespaces", func(t *testing.T) {
_, ok := Namespaces(Self{
enabled := Namespaces(Self{
"Stats": {"license": map[string]interface{}{"features": "Automated Backups, Automated Upgrades, Enhanced Read Scalability, Network Segments, Redundancy Zone, Advanced Network Federation, SSO, Audit Logging"}},
})
require.False(t, ok)
require.False(t, enabled)
})

t.Run("stats missing", func(t *testing.T) {
_, ok := Namespaces(Self{})
require.False(t, ok)
enabled := Namespaces(Self{})
require.False(t, enabled)
})

t.Run("license missing", func(t *testing.T) {
_, ok := Namespaces(Self{"Stats": {}})
require.False(t, ok)
enabled := Namespaces(Self{"Stats": {}})
require.False(t, enabled)
})

t.Run("features missing", func(t *testing.T) {
_, ok := Namespaces(Self{"Stats": {"license": map[string]interface{}{}}})
require.False(t, ok)
enabled := Namespaces(Self{"Stats": {"license": map[string]interface{}{}}})
require.False(t, enabled)
})
}

0 comments on commit 51fe1e0

Please sign in to comment.