Skip to content

Commit c29bbd5

Browse files
Merge pull request #857 from stlaz/oauthclients_schema_bcp
CONSOLE-3912: always sync oidcClients in authn status if the field is present
2 parents 01b0542 + e13ef5f commit c29bbd5

File tree

15 files changed

+937
-18
lines changed

15 files changed

+937
-18
lines changed

pkg/console/controllers/oauthclients/oauthclients.go

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
"time"
77

88
corev1 "k8s.io/api/core/v1"
9+
apiexensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
10+
apiexensionsv1informers "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions/apiextensions/v1"
11+
apiexensionsv1listers "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1"
912
apierrors "k8s.io/apimachinery/pkg/api/errors"
1013
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1114
"k8s.io/apimachinery/pkg/util/wait"
@@ -55,6 +58,7 @@ type oauthClientsController struct {
5558
authentication configv1client.AuthenticationInterface
5659
authnLister configv1lister.AuthenticationLister
5760
consoleOperatorLister operatorv1listers.ConsoleLister
61+
crdLister apiexensionsv1listers.CustomResourceDefinitionLister
5862
routesLister routev1listers.RouteLister
5963
ingressConfigLister configv1lister.IngressLister
6064
targetNSSecretsLister corev1listers.SecretLister
@@ -70,6 +74,7 @@ func NewOAuthClientsController(
7074
operatorClient v1helpers.OperatorClient,
7175
oauthClient oauthclient.Interface,
7276
secretsClient corev1client.SecretsGetter,
77+
crdInformer apiexensionsv1informers.CustomResourceDefinitionInformer,
7378
authentication configv1client.AuthenticationInterface,
7479
authnInformer configv1informers.AuthenticationInformer,
7580
consoleOperatorInformer operatorv1informers.ConsoleInformer,
@@ -98,6 +103,7 @@ func NewOAuthClientsController(
98103
configNSSecretsLister: configNSSecretsInformer.Lister(),
99104
targetNSConfigLister: targetNSConfigInformer.Lister(),
100105
targetNSDeploymentsLister: targetNSDeploymentsInformer.Lister(),
106+
crdLister: crdInformer.Lister(),
101107

102108
authStatusHandler: status.NewAuthStatusHandler(authentication, api.OpenShiftConsoleName, api.TargetNamespace, api.OpenShiftConsoleOperator),
103109
}
@@ -117,6 +123,10 @@ func NewOAuthClientsController(
117123
factory.NamesFilter(api.OAuthClientName),
118124
oauthClientSwitchedInformer.Informer(),
119125
).
126+
WithFilteredEventsInformers(
127+
factory.NamesFilter("authentications.config.openshift.io"),
128+
crdInformer.Informer(),
129+
).
120130
WithSyncDegradedOnError(operatorClient).
121131
ResyncEvery(wait.Jitter(time.Minute, 1.0)).
122132
ToController("OAuthClientsController", recorder.WithComponentSuffix("oauth-clients-controller"))
@@ -163,47 +173,42 @@ func (c *oauthClientsController) sync(ctx context.Context, controllerContext fac
163173
waitCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
164174
defer cancel()
165175
if !cache.WaitForCacheSync(waitCtx.Done(), c.oauthClientSwitchedInformer.Informer().HasSynced) {
166-
syncErr = fmt.Errorf("timed out waiting for OAuthClients cache sync")
167-
break
176+
return statusHandler.FlushAndReturn(fmt.Errorf("timed out waiting for OAuthClients cache sync"))
168177
}
169178

170179
clientSecret, secErr := c.syncSecret(ctx, operatorConfig, controllerContext.Recorder())
171180
statusHandler.AddConditions(status.HandleProgressingOrDegraded("OAuthClientSecretSync", "FailedApply", secErr))
172181
if secErr != nil {
173-
syncErr = secErr
174-
break
182+
return statusHandler.FlushAndReturn(secErr)
175183
}
176184

177185
oauthErrReason, oauthErr := c.syncOAuthClient(ctx, clientSecret, consoleURL.String())
178186
statusHandler.AddConditions(status.HandleProgressingOrDegraded("OAuthClientSync", oauthErrReason, oauthErr))
179187
if oauthErr != nil {
180-
syncErr = oauthErr
181-
break
188+
return statusHandler.FlushAndReturn(oauthErr)
182189
}
183190

184191
case configv1.AuthenticationTypeOIDC:
185192
syncErr = c.syncAuthTypeOIDC(ctx, controllerContext, statusHandler, operatorConfig, authnConfig)
186193
if syncErr != nil {
187-
break
194+
return statusHandler.FlushAndReturn(syncErr)
188195
}
196+
}
189197

190-
// FIXME: once we're able to distinguish featuregates for HCP (on by default)
191-
// and OCP (currently only in TechPreview), move this outside of the switch.
192-
// If you don't, GitOps people will give you a lot of hate - the API validation
193-
// does not allow setting the OIDC providers' client in the provider if it
194-
// doesn't already appear in the status, which is what the following does.
195-
// This means that you cannot get to the desired state in a single update
196-
// as you first need to set the Authn type to OIDC, wait for the operator to
197-
// set the client, and only then you can configure the client in the provider.
198+
oidcClientsSchema, err := authnConfigHasOIDCFields(c.crdLister)
199+
if err != nil {
200+
return statusHandler.FlushAndReturn(err)
201+
}
202+
203+
if oidcClientsSchema {
198204
applyErr := c.authStatusHandler.Apply(ctx, authnConfig)
199205
statusHandler.AddConditions(status.HandleProgressingOrDegraded("AuthStatusHandler", "FailedApply", applyErr))
200206
if applyErr != nil {
201-
syncErr = applyErr
202-
break
207+
return statusHandler.FlushAndReturn(applyErr)
203208
}
204209
}
205210

206-
return statusHandler.FlushAndReturn(syncErr)
211+
return statusHandler.FlushAndReturn(nil)
207212
}
208213

209214
func (c *oauthClientsController) syncAuthTypeOIDC(
@@ -370,3 +375,28 @@ func (c *oauthClientsController) deregisterClient(ctx context.Context) error {
370375
return err
371376

372377
}
378+
379+
func authnConfigHasOIDCFields(crdLister apiexensionsv1listers.CustomResourceDefinitionLister) (bool, error) {
380+
authnCRD, err := crdLister.Get("authentications.config.openshift.io")
381+
if err != nil {
382+
return false, err
383+
}
384+
385+
var authnV1Config *apiexensionsv1.CustomResourceDefinitionVersion
386+
for _, version := range authnCRD.Spec.Versions {
387+
if version.Name == "v1" && version.Served && version.Storage {
388+
authnV1Config = &version
389+
break
390+
}
391+
}
392+
393+
if authnV1Config == nil {
394+
return false, fmt.Errorf("authentications.config.openshift.io is not served or stored as v1")
395+
}
396+
397+
schema := authnV1Config.Schema.OpenAPIV3Schema
398+
_, clientsExist := schema.Properties["status"].Properties["oidcClients"]
399+
400+
return clientsExist, nil
401+
402+
}

pkg/console/starter/starter.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
// kube
1010
corev1 "k8s.io/api/core/v1"
11+
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
12+
apiexensionsinformers "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions"
1113
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1214
"k8s.io/client-go/dynamic"
1315
"k8s.io/client-go/dynamic/dynamicinformer"
@@ -202,11 +204,18 @@ func RunOperator(ctx context.Context, controllerContext *controllercmd.Controlle
202204
resourceSyncer,
203205
)
204206

207+
apiextensionsClient, err := apiextensionsclient.NewForConfig(controllerContext.KubeConfig)
208+
if err != nil {
209+
return err
210+
}
211+
apiextensionsInformers := apiexensionsinformers.NewSharedInformerFactory(apiextensionsClient, resync)
212+
205213
oauthClientController := oauthclients.NewOAuthClientsController(
206214
ctx,
207215
operatorClient,
208216
oauthClient,
209217
kubeClient.CoreV1(),
218+
apiextensionsInformers.Apiextensions().V1().CustomResourceDefinitions(),
210219
configClient.ConfigV1().Authentications(),
211220
configInformers.Config().V1().Authentications(),
212221
operatorConfigInformers.Operator().V1().Consoles(),
@@ -453,6 +462,7 @@ func RunOperator(ctx context.Context, controllerContext *controllercmd.Controlle
453462
for _, informer := range []interface {
454463
Start(stopCh <-chan struct{})
455464
}{
465+
apiextensionsInformers,
456466
kubeInformersNamespaced,
457467
kubeInformersConfigNamespaced,
458468
kubeInformersManagedNamespaced,

vendor/k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions/apiextensions/interface.go

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions/apiextensions/v1/customresourcedefinition.go

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions/apiextensions/v1/interface.go

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)