Skip to content

Commit 8b887af

Browse files
committed
streaming: store services with a unique ID that includes namespace
1 parent bbc6d07 commit 8b887af

File tree

3 files changed

+101
-4
lines changed

3 files changed

+101
-4
lines changed

agent/cache-types/streaming_health_services.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,18 @@ func (s *healthView) Update(events []*pbsubscribe.Event) error {
154154
return fmt.Errorf("unexpected event type for service health view: %T",
155155
event.GetPayload())
156156
}
157-
node := serviceHealth.CheckServiceNode
158-
id := fmt.Sprintf("%s/%s", node.Node.Node, node.Service.ID)
159157

158+
id := serviceHealth.CheckServiceNode.UniqueID()
160159
switch serviceHealth.Op {
161160
case pbsubscribe.CatalogOp_Register:
162-
checkServiceNode := pbservice.CheckServiceNodeToStructs(serviceHealth.CheckServiceNode)
163-
s.state[id] = *checkServiceNode
161+
csn := pbservice.CheckServiceNodeToStructs(serviceHealth.CheckServiceNode)
162+
s.state[id] = *csn
164163
case pbsubscribe.CatalogOp_Deregister:
165164
delete(s.state, id)
166165
}
167166
}
167+
// TODO(streaming): should this filter be applied to only the new CheckServiceNode
168+
// instead of the full map, which should already be filtered.
168169
if s.filter != nil {
169170
filtered, err := s.filter.Execute(s.state)
170171
if err != nil {

proto/pbservice/ids.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package pbservice
2+
3+
import (
4+
"strings"
5+
)
6+
7+
// UniqueID returns a unique identifier for this CheckServiceNode, which includes
8+
// the node name, service namespace, and service ID.
9+
//
10+
// The returned ID uses slashes to separate the identifiers, however the node name
11+
// may also contain a slash, so it is not possible to parse this identifier to
12+
// retrieve its constituent parts.
13+
//
14+
// This function is similar to structs.UniqueID, however at this time no guarantees
15+
// are made that it will remain the same.
16+
func (m *CheckServiceNode) UniqueID() string {
17+
if m == nil {
18+
return ""
19+
}
20+
builder := new(strings.Builder)
21+
if m.Node != nil {
22+
builder.WriteString(m.Node.Node + "/")
23+
}
24+
if m.Service != nil {
25+
builder.WriteString(m.Service.EnterpriseMeta.Namespace + "/")
26+
builder.WriteString(m.Service.ID)
27+
}
28+
return builder.String()
29+
}

proto/pbservice/ids_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package pbservice
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/hashicorp/consul/proto/pbcommon"
9+
)
10+
11+
func TestCheckServiceNode_UniqueID(t *testing.T) {
12+
type testCase struct {
13+
name string
14+
csn CheckServiceNode
15+
expected string
16+
}
17+
fn := func(t *testing.T, tc testCase) {
18+
require.Equal(t, tc.expected, tc.csn.UniqueID())
19+
}
20+
21+
var testCases = []testCase{
22+
{
23+
name: "full",
24+
csn: CheckServiceNode{
25+
Node: &Node{Node: "the-node-name"},
26+
Service: &NodeService{
27+
ID: "the-service-id",
28+
EnterpriseMeta: pbcommon.EnterpriseMeta{Namespace: "the-namespace"},
29+
},
30+
},
31+
expected: "the-node-name/the-namespace/the-service-id",
32+
},
33+
{
34+
name: "without node",
35+
csn: CheckServiceNode{
36+
Service: &NodeService{
37+
ID: "the-service-id",
38+
EnterpriseMeta: pbcommon.EnterpriseMeta{Namespace: "the-namespace"},
39+
},
40+
},
41+
expected: "the-namespace/the-service-id",
42+
},
43+
{
44+
name: "without service",
45+
csn: CheckServiceNode{
46+
Node: &Node{Node: "the-node-name"},
47+
},
48+
expected: "the-node-name/",
49+
},
50+
{
51+
name: "without namespace",
52+
csn: CheckServiceNode{
53+
Node: &Node{Node: "the-node-name"},
54+
Service: &NodeService{
55+
ID: "the-service-id",
56+
},
57+
},
58+
expected: "the-node-name//the-service-id",
59+
},
60+
}
61+
for _, tc := range testCases {
62+
t.Run(tc.name, func(t *testing.T) {
63+
fn(t, tc)
64+
})
65+
}
66+
67+
}

0 commit comments

Comments
 (0)