Skip to content

Commit 5af6420

Browse files
authored
Get from API server when cache fails (#1003)
Originating issue: [IBMPrivateCloud/roadmap#66250](https://github.ibm.com/IBMPrivateCloud/roadmap/issues/66250) Signed-off-by: Rob Hundley <[email protected]>
1 parent c6fd307 commit 5af6420

11 files changed

+82
-18
lines changed

controllers/oidc.security/client_controller.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,20 @@ var log = logf.Log.WithName(controllerName)
7676
// ClientReconciler reconciles a Client object
7777
type ClientReconciler struct {
7878
runtimeClient.Client
79+
Reader runtimeClient.Reader
7980
Scheme *runtime.Scheme
8081
Recorder record.EventRecorder
8182
}
8283

84+
// Get first tries to GET the object from the cache; if this fails, it attempts
85+
// a GET from the API server directly.
86+
func (r *ClientReconciler) Get(ctx context.Context, objkey runtimeClient.ObjectKey, obj runtimeClient.Object) (err error) {
87+
if err = r.Client.Get(ctx, objkey, obj); k8sErrors.IsNotFound(err) {
88+
return r.Reader.Get(ctx, objkey, obj)
89+
}
90+
return
91+
}
92+
8393
// Reconcile is part of the main kubernetes reconciliation loop which aims to
8494
// move the current state of the cluster closer to the desired state.
8595
func (r *ClientReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, err error) {
@@ -329,7 +339,7 @@ func (r *ClientReconciler) processOidcRegistration(ctx context.Context, req ctrl
329339
}
330340

331341
config := &AuthenticationConfig{}
332-
err = GetConfig(ctx, &r.Client, config)
342+
err = GetConfig(ctx, r, config)
333343
if err != nil {
334344
reqLogger.Error(err, "Failed to gather Authentication configuration")
335345
return subreconciler.RequeueWithError(err)
@@ -479,7 +489,7 @@ func (r *ClientReconciler) processZenRegistration(ctx context.Context, req ctrl.
479489
}
480490

481491
config := &AuthenticationConfig{}
482-
err = GetConfig(ctx, &r.Client, config)
492+
err = GetConfig(ctx, r, config)
483493
if err != nil {
484494
reqLogger.Error(err, "Failed to gather Authentication configuration")
485495
return subreconciler.RequeueWithError(err)
@@ -635,7 +645,7 @@ func (r *ClientReconciler) finalizeClient(ctx context.Context, req ctrl.Request)
635645
}
636646

637647
config := &AuthenticationConfig{}
638-
err = GetConfig(ctx, &r.Client, config)
648+
err = GetConfig(ctx, r, config)
639649
if err != nil {
640650
reqLogger.Error(err, "Failed to gather Authentication configuration")
641651
return subreconciler.RequeueWithError(err)

controllers/oidc.security/client_controller_config.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
ctrlCommon "github.com/IBM/ibm-iam-operator/controllers/common"
2525
corev1 "k8s.io/api/core/v1"
2626
"k8s.io/apimachinery/pkg/types"
27-
"sigs.k8s.io/controller-runtime/pkg/client"
2827
)
2928

3029
// AuthenticationConfig collects relevant Authentication configuration from Secrets and ConfigMaps and provides that
@@ -280,15 +279,15 @@ func (c AuthenticationConfig) GetCSCATLSKey() (value []byte, err error) {
280279
return c.getConfigValue(key)
281280
}
282281

283-
func GetConfig(ctx context.Context, k8sClient *client.Client, config *AuthenticationConfig) (err error) {
284-
servicesNamespace, err := ctrlCommon.GetServicesNamespace(ctx, k8sClient)
282+
func GetConfig(ctx context.Context, r *ClientReconciler, config *AuthenticationConfig) (err error) {
283+
servicesNamespace, err := ctrlCommon.GetServicesNamespace(ctx, &r.Client)
285284
if err != nil {
286285
return fmt.Errorf("failed to get ConfigMap: %w", err)
287286
}
288287
config.ApplyAuthenticationNamespace(servicesNamespace)
289288

290289
configMap := &corev1.ConfigMap{}
291-
err = (*k8sClient).Get(ctx, types.NamespacedName{Name: PlatformAuthIDPConfigMapName, Namespace: servicesNamespace}, configMap)
290+
err = r.Get(ctx, types.NamespacedName{Name: PlatformAuthIDPConfigMapName, Namespace: servicesNamespace}, configMap)
292291
if err != nil {
293292
return fmt.Errorf("client failed to GET ConfigMap: %w", err)
294293
}
@@ -298,7 +297,7 @@ func GetConfig(ctx context.Context, k8sClient *client.Client, config *Authentica
298297
}
299298

300299
platformAuthIDPCredentialsSecret := &corev1.Secret{}
301-
err = (*k8sClient).Get(ctx, types.NamespacedName{Name: PlatformAuthIDPCredentialsSecretName, Namespace: servicesNamespace}, platformAuthIDPCredentialsSecret)
300+
err = r.Get(ctx, types.NamespacedName{Name: PlatformAuthIDPCredentialsSecretName, Namespace: servicesNamespace}, platformAuthIDPCredentialsSecret)
302301
if err != nil {
303302
return
304303
}
@@ -308,7 +307,7 @@ func GetConfig(ctx context.Context, k8sClient *client.Client, config *Authentica
308307
}
309308

310309
platformOIDCCredentialsSecret := &corev1.Secret{}
311-
err = (*k8sClient).Get(ctx, types.NamespacedName{Name: PlatformOIDCCredentialsSecretName, Namespace: servicesNamespace}, platformOIDCCredentialsSecret)
310+
err = r.Get(ctx, types.NamespacedName{Name: PlatformOIDCCredentialsSecretName, Namespace: servicesNamespace}, platformOIDCCredentialsSecret)
312311
if err != nil {
313312
return
314313
}
@@ -318,7 +317,7 @@ func GetConfig(ctx context.Context, k8sClient *client.Client, config *Authentica
318317
}
319318

320319
csCACertificateSecret := &corev1.Secret{}
321-
err = (*k8sClient).Get(ctx, types.NamespacedName{Name: CSCACertificateSecretName, Namespace: servicesNamespace}, csCACertificateSecret)
320+
err = r.Get(ctx, types.NamespacedName{Name: CSCACertificateSecretName, Namespace: servicesNamespace}, csCACertificateSecret)
322321
if err != nil {
323322
return
324323
}

controllers/operator/authentication_controller.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,23 @@ func needsAuditServiceDummyDataReset(a *operatorv1alpha1.Authentication) bool {
186186
// AuthenticationReconciler reconciles a Authentication object
187187
type AuthenticationReconciler struct {
188188
client.Client
189+
Reader client.Reader
189190
Scheme *runtime.Scheme
190191
DiscoveryClient discovery.DiscoveryClient
191192
Mutex sync.Mutex
192193
clusterType ctrlcommon.ClusterType
193194
dbSetupChan chan *migration.Result
194195
}
195196

197+
// GetFromCacheOrAPI first tries to GET the object from the cache; if this
198+
// fails, it attempts a GET from the API server directly.
199+
func (r *AuthenticationReconciler) Get(ctx context.Context, objkey client.ObjectKey, obj client.Object) (err error) {
200+
if err = r.Client.Get(ctx, objkey, obj); k8sErrors.IsNotFound(err) {
201+
return r.Reader.Get(ctx, objkey, obj)
202+
}
203+
return
204+
}
205+
196206
// Reconcile is part of the main kubernetes reconciliation loop which aims to
197207
// move the current state of the cluster closer to the desired state.
198208
// TODO(user): Modify the Reconcile function to compare the state specified by

controllers/operator/certificate_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ var _ = Describe("Certificate handling", Ordered, func() {
7777

7878
r = &AuthenticationReconciler{
7979
Client: cl,
80+
Reader: cl,
8081
DiscoveryClient: *dc,
8182
}
8283
ctx = context.Background()
@@ -211,6 +212,7 @@ var _ = Describe("Certificate handling", Ordered, func() {
211212
It("will produce a function that signals to requeue with an error when an unexpected error occurs", func() {
212213
rFailing := &AuthenticationReconciler{
213214
Client: testutil.NewFakeTimeoutClient(cl),
215+
Reader: testutil.NewFakeTimeoutClient(cl),
214216
DiscoveryClient: *dc,
215217
}
216218
fieldsList := generateCertificateFieldsList(authCR)
@@ -374,6 +376,9 @@ var _ = Describe("Certificate handling", Ordered, func() {
374376
Client: &testutil.FakeTimeoutClient{
375377
Client: cl,
376378
},
379+
Reader: &testutil.FakeTimeoutClient{
380+
Client: cl,
381+
},
377382
DiscoveryClient: *dc,
378383
}
379384
remainingCertsCount := 4
@@ -529,6 +534,7 @@ var _ = Describe("Certificate handling", Ordered, func() {
529534
timeoutClient.GetAllowed = true
530535
rFailing := &AuthenticationReconciler{
531536
Client: timeoutClient,
537+
Reader: timeoutClient,
532538
DiscoveryClient: *dc,
533539
}
534540
for _, fields := range fieldsList {
@@ -703,6 +709,7 @@ var _ = Describe("Certificate handling", Ordered, func() {
703709
timeoutClient.GetAllowed = true
704710
rFailing := &AuthenticationReconciler{
705711
Client: timeoutClient,
712+
Reader: timeoutClient,
706713
DiscoveryClient: *dc,
707714
}
708715
fn := rFailing.createV1CertificatesIfNotPresent(authCR, fieldsList)

controllers/operator/configmap.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -766,21 +766,19 @@ func getHostFromDummyRoute(ctx context.Context, cl client.Client, authCR *operat
766766
if err = controllerutil.SetControllerReference(authCR, dummyRoute, cl.Scheme()); err != nil {
767767
return
768768
}
769-
dummyKey := types.NamespacedName{Name: "domain-test", Namespace: authCR.Namespace}
770769
if err = cl.Create(ctx, dummyRoute); err != nil && !k8sErrors.IsAlreadyExists(err) {
771770
return
772771
}
773-
if err = cl.Get(ctx, dummyKey, dummyRoute); err != nil {
774-
return
775-
}
776772
reqLogger.Info("Got dummy route", "spec", dummyRoute.Spec)
777773

774+
host = dummyRoute.Spec.Host
775+
778776
if err = cl.Delete(ctx, dummyRoute); err != nil && !k8sErrors.IsNotFound(err) {
779777
reqLogger.Error(err, "Failed to delete dummy Route")
780778
return "", err
781779
}
782780

783-
return dummyRoute.Spec.Host, nil
781+
return
784782
}
785783

786784
func (r *AuthenticationReconciler) getDomain(ctx context.Context, authCR *operatorv1alpha1.Authentication) (domain string, err error) {

controllers/operator/configmap_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ var _ = Describe("ConfigMap handling", func() {
6262

6363
r = &AuthenticationReconciler{
6464
Client: cl,
65+
Reader: cl,
6566
DiscoveryClient: *dc,
6667
}
6768
ctx = context.Background()
@@ -137,6 +138,7 @@ var _ = Describe("ConfigMap handling", func() {
137138

138139
r = &AuthenticationReconciler{
139140
Client: cl,
141+
Reader: cl,
140142
DiscoveryClient: *dc,
141143
}
142144
ctx = context.Background()
@@ -421,6 +423,7 @@ var _ = Describe("ConfigMap handling", func() {
421423

422424
r = &AuthenticationReconciler{
423425
Client: cl,
426+
Reader: cl,
424427
DiscoveryClient: *dc,
425428
}
426429
ctx = context.Background()
@@ -866,6 +869,7 @@ var _ = Describe("ConfigMap handling", func() {
866869

867870
r = &AuthenticationReconciler{
868871
Client: cl,
872+
Reader: cl,
869873
DiscoveryClient: *dc,
870874
}
871875
ctx = context.Background()
@@ -1015,6 +1019,7 @@ var _ = Describe("ConfigMap handling", func() {
10151019

10161020
r = &AuthenticationReconciler{
10171021
Client: cl,
1022+
Reader: cl,
10181023
DiscoveryClient: *dc,
10191024
}
10201025
ctx = context.Background()

controllers/operator/operandbindinfo_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ var _ = Describe("OperandBindInfo handling", func() {
7676

7777
r = &AuthenticationReconciler{
7878
Client: cl,
79+
Reader: cl,
7980
DiscoveryClient: *dc,
8081
}
8182
ctx = context.Background()

controllers/operator/operandrequest_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ var _ = Describe("OperandRequest handling", func() {
7878
cl = cb.Build()
7979
r = &AuthenticationReconciler{
8080
Client: cl,
81+
Reader: cl,
8182
}
8283
})
8384
It("should add the embedded EDB entry to the list of Operands", func() {
@@ -170,6 +171,9 @@ var _ = Describe("OperandRequest handling", func() {
170171
Client: &testutil.FakeTimeoutClient{
171172
Client: cl,
172173
},
174+
Reader: &testutil.FakeTimeoutClient{
175+
Client: cl,
176+
},
173177
}
174178
By("failing to get the ConfigMap for some reason")
175179
err := rFailing.addEmbeddedEDBIfNeeded(context.Background(), authCR, operands)
@@ -315,6 +319,7 @@ var _ = Describe("OperandRequest handling", func() {
315319
cl = cb.Build()
316320
r = &AuthenticationReconciler{
317321
Client: cl,
322+
Reader: cl,
318323
}
319324
})
320325
It("returns false when IS_EMBEDDED is not set", func() {
@@ -358,6 +363,9 @@ var _ = Describe("OperandRequest handling", func() {
358363
Client: &testutil.FakeTimeoutClient{
359364
Client: cl,
360365
},
366+
Reader: &testutil.FakeTimeoutClient{
367+
Client: cl,
368+
},
361369
}
362370
isExternal, err := rFailing.isConfiguredForExternalEDB(context.Background(), authCR)
363371
Expect(isExternal).To(BeFalse())

controllers/operator/routes_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ var _ = Describe("Route handling", func() {
7979

8080
r = &AuthenticationReconciler{
8181
Client: cl,
82+
Reader: cl,
8283
DiscoveryClient: *dc,
8384
}
8485
ctx = context.Background()
@@ -101,6 +102,9 @@ var _ = Describe("Route handling", func() {
101102
Client: &testutil.FakeTimeoutClient{
102103
Client: cl,
103104
},
105+
Reader: &testutil.FakeTimeoutClient{
106+
Client: cl,
107+
},
104108
}
105109
fn := rFailing.getClusterInfoConfigMap(authCR, cm)
106110
result, err := fn(ctx)
@@ -140,6 +144,7 @@ var _ = Describe("Route handling", func() {
140144
cl = cb.Build()
141145
r = &AuthenticationReconciler{
142146
Client: cl,
147+
Reader: cl,
143148
}
144149
ctx = context.Background()
145150
})
@@ -194,6 +199,7 @@ var _ = Describe("Route handling", func() {
194199
cl = cb.Build()
195200
r = &AuthenticationReconciler{
196201
Client: cl,
202+
Reader: cl,
197203
}
198204
ctx = context.Background()
199205
})
@@ -253,6 +259,7 @@ var _ = Describe("Route handling", func() {
253259
cl = cb.Build()
254260
r = &AuthenticationReconciler{
255261
Client: cl,
262+
Reader: cl,
256263
}
257264
ctx = context.Background()
258265
})
@@ -295,6 +302,9 @@ var _ = Describe("Route handling", func() {
295302
Client: &testutil.FakeTimeoutClient{
296303
Client: cl,
297304
},
305+
Reader: &testutil.FakeTimeoutClient{
306+
Client: cl,
307+
},
298308
}
299309
fn := rFailing.ensureConfigMapHasEqualFields(authCR, fields, cm)
300310
result, err := fn(ctx)
@@ -338,6 +348,7 @@ var _ = Describe("Route handling", func() {
338348
cl = cb.Build()
339349
r = &AuthenticationReconciler{
340350
Client: cl,
351+
Reader: cl,
341352
}
342353
ctx = context.Background()
343354
wlpClientID = ""
@@ -361,6 +372,9 @@ var _ = Describe("Route handling", func() {
361372
Client: &testutil.FakeTimeoutClient{
362373
Client: cl,
363374
},
375+
Reader: &testutil.FakeTimeoutClient{
376+
Client: cl,
377+
},
364378
}
365379
fn := rFailing.getWlpClientID(authCR, &wlpClientID)
366380
result, err := fn(ctx)
@@ -428,6 +442,7 @@ var _ = Describe("Route handling", func() {
428442
cl = cb.Build()
429443
r = &AuthenticationReconciler{
430444
Client: cl,
445+
Reader: cl,
431446
}
432447
ctx = context.Background()
433448

@@ -455,6 +470,9 @@ var _ = Describe("Route handling", func() {
455470
Client: &testutil.FakeTimeoutClient{
456471
Client: cl,
457472
},
473+
Reader: &testutil.FakeTimeoutClient{
474+
Client: cl,
475+
},
458476
}
459477
fn := rFailing.getCertificateForService(serviceName, authCR, &certificate)
460478
result, err := fn(ctx)
@@ -538,6 +556,7 @@ var _ = Describe("Route handling", func() {
538556
cl = cb.Build()
539557
r = &AuthenticationReconciler{
540558
Client: cl,
559+
Reader: cl,
541560
}
542561
clusterAddress = ""
543562
})
@@ -565,6 +584,9 @@ var _ = Describe("Route handling", func() {
565584
Client: &testutil.FakeTimeoutClient{
566585
Client: cl,
567586
},
587+
Reader: &testutil.FakeTimeoutClient{
588+
Client: cl,
589+
},
568590
}
569591
fn := rFailing.getClusterAddress(authCR, &clusterAddress)
570592
result, err := fn(ctx)
@@ -697,6 +719,7 @@ var _ = Describe("Route handling", func() {
697719

698720
r = &AuthenticationReconciler{
699721
Client: cl,
722+
Reader: cl,
700723
DiscoveryClient: *dc,
701724
}
702725
})
@@ -1012,6 +1035,7 @@ var _ = Describe("Route handling", func() {
10121035
Expect(resources).To(BeNil())
10131036
r = &AuthenticationReconciler{
10141037
Client: cl,
1038+
Reader: cl,
10151039
DiscoveryClient: *dc,
10161040
}
10171041
result, err := r.handleRoutes(ctx,

0 commit comments

Comments
 (0)