Skip to content

Commit ae635c0

Browse files
committed
oauthclientscontroller: use a switched informer for oauthclients
So that it can be stopped when auth type is OIDC.
1 parent a70732c commit ae635c0

File tree

4 files changed

+59
-19
lines changed

4 files changed

+59
-19
lines changed

pkg/controllers/oauthclientscontroller/oauthclientscontroller.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import (
1414
"k8s.io/apimachinery/pkg/util/wait"
1515
"k8s.io/client-go/informers"
1616
corev1listers "k8s.io/client-go/listers/core/v1"
17+
"k8s.io/client-go/tools/cache"
1718
"k8s.io/client-go/util/retry"
1819

1920
configv1 "github.com/openshift/api/config/v1"
2021
oauthv1 "github.com/openshift/api/oauth/v1"
2122
configinformers "github.com/openshift/client-go/config/informers/externalversions"
2223
configv1listers "github.com/openshift/client-go/config/listers/config/v1"
2324
oauthclient "github.com/openshift/client-go/oauth/clientset/versioned/typed/oauth/v1"
24-
oauthinformers "github.com/openshift/client-go/oauth/informers/externalversions"
2525
oauthv1listers "github.com/openshift/client-go/oauth/listers/oauth/v1"
2626
operatorv1informers "github.com/openshift/client-go/operator/informers/externalversions/operator/v1"
2727
operatorv1listers "github.com/openshift/client-go/operator/listers/operator/v1"
@@ -35,14 +35,16 @@ import (
3535

3636
"github.com/openshift/cluster-authentication-operator/pkg/controllers/common"
3737
"github.com/openshift/cluster-authentication-operator/pkg/controllers/customroute"
38+
"github.com/openshift/cluster-authentication-operator/pkg/controllers/oauthclientsswitchedinformer"
3839
)
3940

4041
type oauthsClientsController struct {
4142
oauthClientClient oauthclient.OAuthClientInterface
4243

43-
oauthClientLister oauthv1listers.OAuthClientLister
44-
routeLister routev1listers.RouteLister
45-
ingressLister configv1listers.IngressLister
44+
oauthClientInformer cache.SharedIndexInformer
45+
oauthClientLister oauthv1listers.OAuthClientLister
46+
routeLister routev1listers.RouteLister
47+
ingressLister configv1listers.IngressLister
4648

4749
authLister configv1listers.AuthenticationLister
4850
kasLister operatorv1listers.KubeAPIServerLister
@@ -52,7 +54,7 @@ type oauthsClientsController struct {
5254
func NewOAuthClientsController(
5355
operatorClient v1helpers.OperatorClient,
5456
oauthsClientClient oauthclient.OAuthClientInterface,
55-
oauthInformers oauthinformers.SharedInformerFactory,
57+
oauthClientsSwitchedInformer *oauthclientsswitchedinformer.InformerWithSwitch,
5658
routeInformers routeinformers.SharedInformerFactory,
5759
operatorConfigInformers configinformers.SharedInformerFactory,
5860
kasInformer operatorv1informers.KubeAPIServerInformer,
@@ -62,9 +64,10 @@ func NewOAuthClientsController(
6264
c := &oauthsClientsController{
6365
oauthClientClient: oauthsClientClient,
6466

65-
oauthClientLister: oauthInformers.Oauth().V1().OAuthClients().Lister(),
66-
routeLister: routeInformers.Route().V1().Routes().Lister(),
67-
ingressLister: operatorConfigInformers.Config().V1().Ingresses().Lister(),
67+
oauthClientInformer: oauthClientsSwitchedInformer.Informer(),
68+
oauthClientLister: oauthv1listers.NewOAuthClientLister(oauthClientsSwitchedInformer.Informer().GetIndexer()),
69+
routeLister: routeInformers.Route().V1().Routes().Lister(),
70+
ingressLister: operatorConfigInformers.Config().V1().Ingresses().Lister(),
6871

6972
authLister: operatorConfigInformers.Config().V1().Authentications().Lister(),
7073
kasLister: kasInformer.Lister(),
@@ -76,7 +79,7 @@ func NewOAuthClientsController(
7679
WithSyncDegradedOnError(operatorClient).
7780
WithFilteredEventsInformers(
7881
factory.NamesFilter("openshift-browser-client", "openshift-challenging-client", "openshift-cli-client"),
79-
oauthInformers.Oauth().V1().OAuthClients().Informer(),
82+
oauthClientsSwitchedInformer.Informer(),
8083
).
8184
WithFilteredEventsInformers(
8285
factory.NamesFilter("oauth-openshift"),
@@ -114,6 +117,12 @@ func (c *oauthsClientsController) sync(ctx context.Context, syncCtx factory.Sync
114117
return err
115118
}
116119

120+
waitCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
121+
defer cancel()
122+
if !cache.WaitForCacheSync(waitCtx.Done(), c.oauthClientInformer.HasSynced) {
123+
return fmt.Errorf("timed out waiting for OAuthClients informer cache sync")
124+
}
125+
117126
return c.ensureBootstrappedOAuthClients(ctx, "https://"+routeHost)
118127
}
119128

pkg/controllers/oauthclientscontroller/oauthclientscontroller_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/base64"
66
"fmt"
77
"testing"
8+
"time"
89

910
corev1 "k8s.io/api/core/v1"
1011
"k8s.io/apimachinery/pkg/api/equality"
@@ -20,8 +21,10 @@ import (
2021
routev1 "github.com/openshift/api/route/v1"
2122
configv1listers "github.com/openshift/client-go/config/listers/config/v1"
2223
fakeoauthclient "github.com/openshift/client-go/oauth/clientset/versioned/fake"
24+
oauthinformers "github.com/openshift/client-go/oauth/informers/externalversions"
2325
oauthv1listers "github.com/openshift/client-go/oauth/listers/oauth/v1"
2426
routev1listers "github.com/openshift/client-go/route/listers/route/v1"
27+
"github.com/openshift/cluster-authentication-operator/pkg/controllers/oauthclientsswitchedinformer"
2528
"github.com/openshift/library-go/pkg/oauth/oauthdiscovery"
2629
"github.com/openshift/library-go/pkg/operator/events"
2730
)
@@ -118,12 +121,24 @@ func newAuthLister(t *testing.T) configv1listers.AuthenticationLister {
118121
}
119122

120123
func newTestOAuthsClientsController(t *testing.T) *oauthsClientsController {
124+
oauthClientset := fakeoauthclient.NewSimpleClientset()
125+
switchedInformer := oauthclientsswitchedinformer.NewSwitchedInformer(
126+
"TestOAuthClientsInformerWithSwitchController",
127+
context.TODO(),
128+
func() (bool, error) { return false, nil },
129+
oauthinformers.NewSharedInformerFactoryWithOptions(oauthClientset, 1*time.Minute).Oauth().V1().OAuthClients().Informer(),
130+
0,
131+
nil,
132+
events.NewInMemoryRecorder("oauthclientscontroller_test"),
133+
)
134+
121135
return &oauthsClientsController{
122-
oauthClientClient: fakeoauthclient.NewSimpleClientset().OauthV1().OAuthClients(),
123-
oauthClientLister: oauthv1listers.NewOAuthClientLister(cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{})),
124-
routeLister: newRouteLister(t, defaultRoute),
125-
ingressLister: newIngressLister(t, defaultIngress),
126-
authLister: newAuthLister(t),
136+
oauthClientInformer: switchedInformer.Informer(),
137+
oauthClientClient: oauthClientset.OauthV1().OAuthClients(),
138+
oauthClientLister: oauthv1listers.NewOAuthClientLister(cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{})),
139+
routeLister: newRouteLister(t, defaultRoute),
140+
ingressLister: newIngressLister(t, defaultIngress),
141+
authLister: newAuthLister(t),
127142
}
128143
}
129144

pkg/operator/replacement_starter.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
configclient "github.com/openshift/client-go/config/clientset/versioned"
2020
configinformer "github.com/openshift/client-go/config/informers/externalversions"
2121
oauthclient "github.com/openshift/client-go/oauth/clientset/versioned"
22-
oauthinformers "github.com/openshift/client-go/oauth/informers/externalversions"
2322
operatorclient "github.com/openshift/client-go/operator/clientset/versioned"
2423
operatorinformer "github.com/openshift/client-go/operator/informers/externalversions"
2524
routeclient "github.com/openshift/client-go/route/clientset/versioned"
@@ -214,7 +213,6 @@ type authenticationOperatorInformerFactories struct {
214213
kubeInformersForNamespaces v1helpers.KubeInformersForNamespaces
215214
operatorConfigInformer configinformer.SharedInformerFactory
216215
operatorInformer operatorinformer.SharedInformerFactory
217-
oauthInformers oauthinformers.SharedInformerFactory
218216
apiregistrationInformers apiregistrationinformers.SharedInformerFactory
219217
migrationInformer migrationv1alpha1informer.SharedInformerFactory
220218
// TODO remove
@@ -240,7 +238,6 @@ func newInformerFactories(authOperatorInput *authenticationOperatorInput) authen
240238
),
241239
operatorConfigInformer: configinformer.NewSharedInformerFactoryWithOptions(authOperatorInput.configClient, resync),
242240
operatorInformer: operatorinformer.NewSharedInformerFactory(authOperatorInput.operatorClient, 24*time.Hour),
243-
oauthInformers: oauthinformers.NewSharedInformerFactory(authOperatorInput.oauthClient, resync),
244241
apiregistrationInformers: apiregistrationinformers.NewSharedInformerFactory(authOperatorInput.apiregistrationv1Client, 10*time.Minute),
245242
migrationInformer: migrationv1alpha1informer.NewSharedInformerFactory(authOperatorInput.migrationClient, time.Minute*30),
246243
kubeInformers: kubeinformers.NewSharedInformerFactory(authOperatorInput.kubeClient, resync),
@@ -257,7 +254,6 @@ func (a authenticationOperatorInformerFactories) simplifiedInformerFactories() [
257254
libraryapplyconfiguration.GeneratedNamespacedInformerFactoryAdapter(a.kubeInformersForNamespaces),
258255
libraryapplyconfiguration.GeneratedInformerFactoryAdapter(a.operatorInformer),
259256
libraryapplyconfiguration.GeneratedInformerFactoryAdapter(a.operatorConfigInformer),
260-
libraryapplyconfiguration.GeneratedInformerFactoryAdapter(a.oauthInformers),
261257
libraryapplyconfiguration.GeneratedInformerFactoryAdapter(a.apiregistrationInformers),
262258
libraryapplyconfiguration.GeneratedInformerFactoryAdapter(a.migrationInformer),
263259
libraryapplyconfiguration.GeneratedInformerFactoryAdapter(a.kubeInformers),

pkg/operator/starter.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
operatorv1 "github.com/openshift/api/operator/v1"
1717
routev1 "github.com/openshift/api/route/v1"
1818
configv1listers "github.com/openshift/client-go/config/listers/config/v1"
19+
oauthinformers "github.com/openshift/client-go/oauth/informers/externalversions"
1920
applyoperatorv1 "github.com/openshift/client-go/operator/applyconfigurations/operator/v1"
2021
operatorv1listers "github.com/openshift/client-go/operator/listers/operator/v1"
2122
"github.com/openshift/cluster-authentication-operator/bindata"
@@ -28,6 +29,7 @@ import (
2829
"github.com/openshift/cluster-authentication-operator/pkg/controllers/ingressstate"
2930
"github.com/openshift/cluster-authentication-operator/pkg/controllers/metadata"
3031
"github.com/openshift/cluster-authentication-operator/pkg/controllers/oauthclientscontroller"
32+
"github.com/openshift/cluster-authentication-operator/pkg/controllers/oauthclientsswitchedinformer"
3133
"github.com/openshift/cluster-authentication-operator/pkg/controllers/oauthendpoints"
3234
"github.com/openshift/cluster-authentication-operator/pkg/controllers/payload"
3335
"github.com/openshift/cluster-authentication-operator/pkg/controllers/proxyconfig"
@@ -41,6 +43,7 @@ import (
4143
"github.com/openshift/cluster-authentication-operator/pkg/operator/workload"
4244
"github.com/openshift/library-go/pkg/authentication/bootstrapauthenticator"
4345
"github.com/openshift/library-go/pkg/controller/controllercmd"
46+
"github.com/openshift/library-go/pkg/controller/factory"
4447
workloadcontroller "github.com/openshift/library-go/pkg/operator/apiserver/controller/workload"
4548
apiservercontrollerset "github.com/openshift/library-go/pkg/operator/apiserver/controllerset"
4649
"github.com/openshift/library-go/pkg/operator/certrotation"
@@ -256,10 +259,25 @@ func prepareOauthOperator(
256259
authOperatorInput.eventRecorder,
257260
)
258261

262+
oauthClientsSwitchedInformer := oauthclientsswitchedinformer.NewSwitchedInformer(
263+
"OAuthClientsInformerWithSwitchController",
264+
ctx,
265+
func() (bool, error) {
266+
return common.ExternalOIDCConfigAvailable(authLister, kasLister, kasConfigMapLister)
267+
},
268+
oauthinformers.NewSharedInformerFactoryWithOptions(authOperatorInput.oauthClient, 1*time.Minute).Oauth().V1().OAuthClients().Informer(),
269+
0,
270+
[]factory.Informer{
271+
informerFactories.operatorInformer.Operator().V1().KubeAPIServers().Informer(),
272+
informerFactories.operatorConfigInformer.Config().V1().Authentications().Informer(),
273+
},
274+
authOperatorInput.eventRecorder,
275+
)
276+
259277
oauthClientsController := oauthclientscontroller.NewOAuthClientsController(
260278
authOperatorInput.authenticationOperatorClient,
261279
authOperatorInput.oauthClient.OauthV1().OAuthClients(),
262-
informerFactories.oauthInformers,
280+
oauthClientsSwitchedInformer,
263281
informerFactories.namespacedOpenshiftAuthenticationRoutes,
264282
informerFactories.operatorConfigInformer,
265283
informerFactories.operatorInformer.Operator().V1().KubeAPIServers(),
@@ -386,6 +404,7 @@ func prepareOauthOperator(
386404
libraryapplyconfiguration.AdaptSyncFn(authOperatorInput.eventRecorder, "TODO-deploymentController", deploymentController.Sync),
387405
libraryapplyconfiguration.AdaptSyncFn(authOperatorInput.eventRecorder, "TODO-managementStateController", managementStateController.Sync),
388406
libraryapplyconfiguration.AdaptSyncFn(authOperatorInput.eventRecorder, "TODO-metadataController", metadataController.Sync),
407+
libraryapplyconfiguration.AdaptSyncFn(authOperatorInput.eventRecorder, "TODO-oauthClientsSwitchedInformerController", oauthClientsSwitchedInformer.Controller().Sync),
389408
libraryapplyconfiguration.AdaptSyncFn(authOperatorInput.eventRecorder, "TODO-oauthClientsController", oauthClientsController.Sync),
390409
libraryapplyconfiguration.AdaptSyncFn(authOperatorInput.eventRecorder, "TODO-payloadConfigController", payloadConfigController.Sync),
391410
libraryapplyconfiguration.AdaptSyncFn(authOperatorInput.eventRecorder, "TODO-routerCertsController", routerCertsController.Sync),
@@ -408,6 +427,7 @@ func prepareOauthOperator(
408427
libraryapplyconfiguration.AdaptRunFn(deploymentController.Run),
409428
libraryapplyconfiguration.AdaptRunFn(managementStateController.Run),
410429
libraryapplyconfiguration.AdaptRunFn(metadataController.Run),
430+
libraryapplyconfiguration.AdaptRunFn(oauthClientsSwitchedInformer.Controller().Run),
411431
libraryapplyconfiguration.AdaptRunFn(oauthClientsController.Run),
412432
libraryapplyconfiguration.AdaptRunFn(payloadConfigController.Run),
413433
libraryapplyconfiguration.AdaptRunFn(routerCertsController.Run),

0 commit comments

Comments
 (0)