Skip to content

Commit db0cce6

Browse files
committed
Implement shared WebSocket connections for instant updates
1 parent 2104ea0 commit db0cce6

13 files changed

Lines changed: 1166 additions & 271 deletions

controllers/event_watcher_registry.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,17 @@
44
package controllers
55

66
import (
7-
"context"
8-
97
gocache "github.com/patrickmn/go-cache"
108
"k8s.io/apimachinery/pkg/types"
119
)
1210

13-
// eventWatcherMeta - metadata for managing an event watcher goroutine
11+
// eventWatcherMeta - metadata for tracking event subscriptions
1412
type eventWatcherMeta struct {
15-
// Cancel will close the watcher's context (and stop the watcher goroutine)
16-
Cancel context.CancelFunc `json:"-"`
17-
// StoppedCh lets the watcher goroutine signal the caller that it has
18-
// stopped (and removed itself from the registry)
19-
StoppedCh chan struct{} `json:"-"`
20-
// LastGeneration is the generation of the VaultStaticSecret resource, used
21-
// to detect if the event watcher needs to be recreated
13+
// LastGeneration is the generation of the resource, used
14+
// to detect if the event subscription needs to be recreated
2215
LastGeneration int64
23-
// LastClientID - vault client ID for the last successful connection, used
24-
// to detect if the Vault client has changed since the event watcher started
16+
// LastClientID - vault client ID for the last successful subscription, used
17+
// to detect if the Vault client has changed since the subscription started
2518
LastClientID string
2619
}
2720

controllers/event_watcher_registry_test.go

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package controllers
55

66
import (
7-
"context"
87
"testing"
98

109
"github.com/stretchr/testify/assert"
@@ -18,29 +17,21 @@ func TestEventWatcherRegistry(t *testing.T) {
1817
registry := newEventWatcherRegistry()
1918
assert.Equal(t, 0, registry.registry.ItemCount())
2019

21-
ctx, cancel := context.WithCancel(context.Background())
22-
stoppedCh := make(chan struct{}, 1)
23-
24-
// Create a new event watcher metadata
20+
// Create a new event subscription metadata
2521
meta := &eventWatcherMeta{
2622
LastGeneration: 123,
2723
LastClientID: "client-id",
28-
Cancel: cancel,
29-
StoppedCh: stoppedCh,
3024
}
3125

32-
// Register the event watcher
26+
// Register the event subscription
3327
itemName := types.NamespacedName{Name: "test", Namespace: "default"}
3428
registry.Register(itemName, meta)
3529
assert.Equal(t, 1, registry.registry.ItemCount())
3630

37-
// close the channel
38-
close(stoppedCh)
39-
40-
// Get the event watcher
31+
// Get the event subscription metadata
4132
got, ok := registry.Get(itemName)
42-
require.True(t, ok, "expected to get event watcher, got none")
43-
require.NotNil(t, got, "expected to get event watcher, got nil")
33+
require.True(t, ok, "expected to get event subscription metadata, got none")
34+
require.NotNil(t, got, "expected to get event subscription metadata, got nil")
4435

4536
assert.Equal(t, int64(123), got.LastGeneration)
4637
assert.Equal(t, "client-id", got.LastClientID)
@@ -52,25 +43,18 @@ func TestEventWatcherRegistry(t *testing.T) {
5243

5344
// Get again
5445
gotAgain, ok := registry.Get(itemName)
55-
require.True(t, ok, "expected to get event watcher again, got none")
56-
require.NotNil(t, gotAgain, "expected to get event watcher again, got nil")
46+
require.True(t, ok, "expected to get event subscription metadata again, got none")
47+
require.NotNil(t, gotAgain, "expected to get event subscription metadata again, got nil")
5748

5849
assert.Equal(t, int64(456), gotAgain.LastGeneration)
5950
assert.Equal(t, "client-id", gotAgain.LastClientID)
6051

61-
// Cancel context received from the registry, check the original
62-
gotAgain.Cancel()
63-
assert.Equal(t, ctx.Err(), context.Canceled)
64-
65-
_, isOpen := <-gotAgain.StoppedCh
66-
assert.False(t, isOpen, "expected stoppedCh from registry item to be closed")
67-
68-
// Delete the event watcher
52+
// Delete the event subscription
6953
registry.Delete(itemName)
7054
assert.Equal(t, 0, registry.registry.ItemCount())
7155

72-
// Get the event watcher
56+
// Get the event subscription metadata
7357
gotFinally, ok := registry.Get(itemName)
74-
assert.False(t, ok, "expected to not get event watcher, got one")
75-
assert.Nil(t, gotFinally, "expected nil event watcher")
58+
assert.False(t, ok, "expected to not get event subscription metadata, got one")
59+
assert.Nil(t, gotFinally, "expected nil event subscription metadata")
7660
}

0 commit comments

Comments
 (0)