6
6
"time"
7
7
8
8
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"
9
12
apierrors "k8s.io/apimachinery/pkg/api/errors"
10
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
14
"k8s.io/apimachinery/pkg/util/wait"
@@ -55,6 +58,7 @@ type oauthClientsController struct {
55
58
authentication configv1client.AuthenticationInterface
56
59
authnLister configv1lister.AuthenticationLister
57
60
consoleOperatorLister operatorv1listers.ConsoleLister
61
+ crdLister apiexensionsv1listers.CustomResourceDefinitionLister
58
62
routesLister routev1listers.RouteLister
59
63
ingressConfigLister configv1lister.IngressLister
60
64
targetNSSecretsLister corev1listers.SecretLister
@@ -70,6 +74,7 @@ func NewOAuthClientsController(
70
74
operatorClient v1helpers.OperatorClient ,
71
75
oauthClient oauthclient.Interface ,
72
76
secretsClient corev1client.SecretsGetter ,
77
+ crdInformer apiexensionsv1informers.CustomResourceDefinitionInformer ,
73
78
authentication configv1client.AuthenticationInterface ,
74
79
authnInformer configv1informers.AuthenticationInformer ,
75
80
consoleOperatorInformer operatorv1informers.ConsoleInformer ,
@@ -98,6 +103,7 @@ func NewOAuthClientsController(
98
103
configNSSecretsLister : configNSSecretsInformer .Lister (),
99
104
targetNSConfigLister : targetNSConfigInformer .Lister (),
100
105
targetNSDeploymentsLister : targetNSDeploymentsInformer .Lister (),
106
+ crdLister : crdInformer .Lister (),
101
107
102
108
authStatusHandler : status .NewAuthStatusHandler (authentication , api .OpenShiftConsoleName , api .TargetNamespace , api .OpenShiftConsoleOperator ),
103
109
}
@@ -117,6 +123,10 @@ func NewOAuthClientsController(
117
123
factory .NamesFilter (api .OAuthClientName ),
118
124
oauthClientSwitchedInformer .Informer (),
119
125
).
126
+ WithFilteredEventsInformers (
127
+ factory .NamesFilter ("authentications.config.openshift.io" ),
128
+ crdInformer .Informer (),
129
+ ).
120
130
WithSyncDegradedOnError (operatorClient ).
121
131
ResyncEvery (wait .Jitter (time .Minute , 1.0 )).
122
132
ToController ("OAuthClientsController" , recorder .WithComponentSuffix ("oauth-clients-controller" ))
@@ -163,47 +173,42 @@ func (c *oauthClientsController) sync(ctx context.Context, controllerContext fac
163
173
waitCtx , cancel := context .WithTimeout (ctx , 10 * time .Second )
164
174
defer cancel ()
165
175
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" ))
168
177
}
169
178
170
179
clientSecret , secErr := c .syncSecret (ctx , operatorConfig , controllerContext .Recorder ())
171
180
statusHandler .AddConditions (status .HandleProgressingOrDegraded ("OAuthClientSecretSync" , "FailedApply" , secErr ))
172
181
if secErr != nil {
173
- syncErr = secErr
174
- break
182
+ return statusHandler .FlushAndReturn (secErr )
175
183
}
176
184
177
185
oauthErrReason , oauthErr := c .syncOAuthClient (ctx , clientSecret , consoleURL .String ())
178
186
statusHandler .AddConditions (status .HandleProgressingOrDegraded ("OAuthClientSync" , oauthErrReason , oauthErr ))
179
187
if oauthErr != nil {
180
- syncErr = oauthErr
181
- break
188
+ return statusHandler .FlushAndReturn (oauthErr )
182
189
}
183
190
184
191
case configv1 .AuthenticationTypeOIDC :
185
192
syncErr = c .syncAuthTypeOIDC (ctx , controllerContext , statusHandler , operatorConfig , authnConfig )
186
193
if syncErr != nil {
187
- break
194
+ return statusHandler . FlushAndReturn ( syncErr )
188
195
}
196
+ }
189
197
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 {
198
204
applyErr := c .authStatusHandler .Apply (ctx , authnConfig )
199
205
statusHandler .AddConditions (status .HandleProgressingOrDegraded ("AuthStatusHandler" , "FailedApply" , applyErr ))
200
206
if applyErr != nil {
201
- syncErr = applyErr
202
- break
207
+ return statusHandler .FlushAndReturn (applyErr )
203
208
}
204
209
}
205
210
206
- return statusHandler .FlushAndReturn (syncErr )
211
+ return statusHandler .FlushAndReturn (nil )
207
212
}
208
213
209
214
func (c * oauthClientsController ) syncAuthTypeOIDC (
@@ -370,3 +375,28 @@ func (c *oauthClientsController) deregisterClient(ctx context.Context) error {
370
375
return err
371
376
372
377
}
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
+ }
0 commit comments