Skip to content

Commit d247b51

Browse files
committed
cancel and stop shard watchers when servers are stopped
On-behalf-of: @SAP [email protected]
1 parent 74e8a17 commit d247b51

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

pkg/authentication/authentication_controller.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (c *Controller) process(ctx context.Context, key string) error {
215215
return err
216216
}
217217

218-
watcher := NewShardWatcher(shard.Name, client, &c.authIndex)
218+
watcher := NewShardWatcher(ctx, shard.Name, client, &c.authIndex)
219219
c.shardWatchers[shard.Name] = watcher
220220
}
221221

@@ -242,10 +242,11 @@ type shardWatcher struct {
242242
state *state
243243
workspaceTypeInformer cache.SharedIndexInformer
244244
workspaceAuthConfigInformer cache.SharedIndexInformer
245-
stopCh chan struct{}
245+
cancel context.CancelFunc
246246
}
247247

248248
func NewShardWatcher(
249+
ctx context.Context,
249250
shardName string,
250251
shardClient kcpclientset.ClusterInterface,
251252
state *state,
@@ -288,23 +289,28 @@ func NewShardWatcher(
288289
},
289290
})
290291

292+
ctx, cancel := context.WithCancel(ctx)
293+
291294
watcher := &shardWatcher{
292295
state: state,
293296
workspaceTypeInformer: wtInformer,
294297
workspaceAuthConfigInformer: wacInformer,
295-
stopCh: make(chan struct{}),
298+
cancel: cancel,
296299
}
297300

298-
go wacInformer.Run(watcher.stopCh)
299-
go wtInformer.Run(watcher.stopCh)
301+
go wacInformer.Run(ctx.Done())
302+
go wtInformer.Run(ctx.Done())
300303

301304
// no need to wait. We only care about events and they arrive when they arrive.
302305

303306
return watcher
304307
}
305308

306309
func (w *shardWatcher) Stop() {
307-
close(w.stopCh)
310+
if w.cancel != nil {
311+
w.cancel()
312+
w.cancel = nil
313+
}
308314
}
309315

310316
func (w *shardWatcher) Lookup(wsType logicalcluster.Path) (authenticator.Request, bool) {

pkg/authentication/index.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ func (c *state) Lookup(wsType logicalcluster.Path) (authenticator.Request, bool)
207207
authenticatorKeys []authenticatorKey
208208
)
209209

210+
c.lock.RLock()
211+
defer c.lock.RUnlock()
212+
210213
for shardKey, authenticatorsMap := range c.workspaceTypeAuthenticators {
211214
var found bool
212215
authenticatorKeys, found = authenticatorsMap[wsType]

pkg/server/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ func NewConfig(ctx context.Context, opts kcpserveroptions.CompletedOptions) (*Co
466466
var authIndex authentication.AuthenticatorIndex
467467
if kcpfeatures.DefaultFeatureGate.Enabled(kcpfeatures.WorkspaceAuthentication) {
468468
authIndexState := authentication.NewIndex(ctx, genericConfig.Authentication.APIAudiences)
469-
authentication.NewShardWatcher(c.Options.Extra.ShardName, c.KcpClusterClient, authIndexState)
469+
authentication.NewShardWatcher(ctx, c.Options.Extra.ShardName, c.KcpClusterClient, authIndexState)
470470

471471
genericConfig.Authentication.Authenticator = authenticatorunion.New(
472472
genericConfig.Authentication.Authenticator,

0 commit comments

Comments
 (0)