Skip to content

Commit 32a630f

Browse files
committed
improve error handling
On-behalf-of: @SAP [email protected]
1 parent be2d5ff commit 32a630f

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

pkg/authentication/authentication_controller.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func NewController(
7373
shardInformer corev1alpha1informers.ShardInformer,
7474
clientGetter ClusterClientGetter,
7575
baseAudiences authenticator.Audiences,
76-
) *Controller {
76+
) (*Controller, error) {
7777
queue := workqueue.NewTypedRateLimitingQueueWithConfig(
7878
workqueue.DefaultTypedControllerRateLimiter[string](),
7979
workqueue.TypedRateLimitingQueueConfig[string]{
@@ -93,7 +93,7 @@ func NewController(
9393
shardWatchers: map[string]*shardWatcher{},
9494
}
9595

96-
_, _ = shardInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
96+
_, err := shardInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
9797
AddFunc: func(obj interface{}) {
9898
shard := obj.(*corev1alpha1.Shard)
9999
c.enqueueShard(ctx, shard)
@@ -116,8 +116,11 @@ func NewController(
116116
c.stopShard(shard.Name)
117117
},
118118
})
119+
if err != nil {
120+
return nil, fmt.Errorf("failed to start Shard informer: %w", err)
121+
}
119122

120-
return c
123+
return c, nil
121124
}
122125

123126
// Start the controller. It does not really do anything, but to keep the shape of a normal
@@ -215,7 +218,11 @@ func (c *Controller) process(ctx context.Context, key string) error {
215218
return err
216219
}
217220

218-
watcher := NewShardWatcher(ctx, shard.Name, client, &c.authIndex)
221+
watcher, err := NewShardWatcher(ctx, shard.Name, client, &c.authIndex)
222+
if err != nil {
223+
return fmt.Errorf("failed to start shard watcher: %w", err)
224+
}
225+
219226
c.shardWatchers[shard.Name] = watcher
220227
}
221228

@@ -250,9 +257,9 @@ func NewShardWatcher(
250257
shardName string,
251258
shardClient kcpclientset.ClusterInterface,
252259
state *state,
253-
) *shardWatcher {
260+
) (*shardWatcher, error) {
254261
wacInformer := tenancyv1alpha1informers.NewWorkspaceAuthenticationConfigurationClusterInformer(shardClient, resyncPeriod, nil)
255-
_, _ = wacInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
262+
_, err := wacInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
256263
AddFunc: func(obj interface{}) {
257264
wac := obj.(*tenancyv1alpha1.WorkspaceAuthenticationConfiguration)
258265
state.UpsertWorkspaceAuthenticationConfiguration(shardName, wac)
@@ -269,9 +276,12 @@ func NewShardWatcher(
269276
state.DeleteWorkspaceAuthenticationConfiguration(shardName, wac)
270277
},
271278
})
279+
if err != nil {
280+
return nil, fmt.Errorf("failed to start WorkspaceAuthenticationConfiguration informer: %w", err)
281+
}
272282

273283
wtInformer := tenancyv1alpha1informers.NewWorkspaceTypeClusterInformer(shardClient, resyncPeriod, nil)
274-
_, _ = wtInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
284+
_, err = wtInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
275285
AddFunc: func(obj interface{}) {
276286
wt := obj.(*tenancyv1alpha1.WorkspaceType)
277287
state.UpsertWorkspaceType(shardName, wt)
@@ -288,6 +298,9 @@ func NewShardWatcher(
288298
state.DeleteWorkspaceType(shardName, wt)
289299
},
290300
})
301+
if err != nil {
302+
return nil, fmt.Errorf("failed to start WorkspaceType informer: %w", err)
303+
}
291304

292305
ctx, cancel := context.WithCancel(ctx)
293306

@@ -303,7 +316,7 @@ func NewShardWatcher(
303316

304317
// no need to wait. We only care about events and they arrive when they arrive.
305318

306-
return watcher
319+
return watcher, nil
307320
}
308321

309322
func (w *shardWatcher) Stop() {

pkg/proxy/server.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ func NewServer(ctx context.Context, c CompletedConfig) (*Server, error) {
115115

116116
if hasWorkspaceAuth {
117117
// This controller is similar to the index controller, but keeps track of the per-workspace authenticators.
118-
s.AuthController = authentication.NewController(ctx, s.KcpSharedInformerFactory.Core().V1alpha1().Shards(), getClientFunc, nil)
118+
ctrl, err := authentication.NewController(ctx, s.KcpSharedInformerFactory.Core().V1alpha1().Shards(), getClientFunc, nil)
119+
if err != nil {
120+
return nil, fmt.Errorf("failed to start authentication controller: %w", err)
121+
}
122+
123+
s.AuthController = ctrl
119124

120125
// When workspace auth is enabled, it depends on the target cluster whether
121126
// a custom authenticator exists or not. This needs to be determined before

pkg/server/config.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,22 @@ func NewConfig(ctx context.Context, opts kcpserveroptions.CompletedOptions) (*Co
409409
// Make sure to set our RequestInfoResolver that is capable of populating a RequestInfo even for /services/... URLs.
410410
c.GenericConfig.RequestInfoResolver = requestinfo.NewKCPRequestInfoResolver()
411411

412+
// Prepare an authentication index to be used later by a middleware. We start it early
413+
// because it can potentially fail and the BuildHandlerChainFunc() has no way to return
414+
// an error.
415+
var authIndex authentication.AuthenticatorIndex
416+
if kcpfeatures.DefaultFeatureGate.Enabled(kcpfeatures.WorkspaceAuthentication) {
417+
// Start an index and a shard watcher to fill this index;
418+
// the shard watcher's lifetime is tied to the given context.
419+
authIndexState := authentication.NewIndex(ctx, c.GenericConfig.Authentication.APIAudiences)
420+
_, err := authentication.NewShardWatcher(ctx, c.Options.Extra.ShardName, c.KcpClusterClient, authIndexState)
421+
if err != nil {
422+
return nil, fmt.Errorf("failed to start shard watcher: %w", err)
423+
}
424+
425+
authIndex = authIndexState
426+
}
427+
412428
// preHandlerChainMux is called before the actual handler chain. Note that BuildHandlerChainFunc below
413429
// is called multiple times, but only one of the handler chain will actually be used. Hence, we wrap it
414430
// to give handlers below one mux.Handle func to call.
@@ -463,17 +479,11 @@ func NewConfig(ctx context.Context, opts kcpserveroptions.CompletedOptions) (*Co
463479
// Wrap authenticator with a per-workspace authenticator if desired. This authenticator
464480
// requires the WorkspaceAuth middleware to have looked up and injected the relevant
465481
// authenticator into the request context already.
466-
var authIndex authentication.AuthenticatorIndex
467482
if kcpfeatures.DefaultFeatureGate.Enabled(kcpfeatures.WorkspaceAuthentication) {
468-
authIndexState := authentication.NewIndex(ctx, genericConfig.Authentication.APIAudiences)
469-
authentication.NewShardWatcher(ctx, c.Options.Extra.ShardName, c.KcpClusterClient, authIndexState)
470-
471483
genericConfig.Authentication.Authenticator = authenticatorunion.New(
472484
genericConfig.Authentication.Authenticator,
473485
authentication.NewWorkspaceAuthenticator(),
474486
)
475-
476-
authIndex = authIndexState
477487
}
478488

479489
// There is ordering here in play:

0 commit comments

Comments
 (0)