Skip to content

Commit 8febe2b

Browse files
Merge pull request #757 from kramaranya/OCPBUGS-48176
OCPBUGS-48176: Avoid duplicate OAuth client creation
2 parents e0ebd57 + c6e0cd0 commit 8febe2b

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

pkg/controllers/oauthclientscontroller/oauthclientscontroller.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (c *oauthsClientsController) ensureBootstrappedOAuthClients(ctx context.Con
138138
GrantMethod: oauthv1.GrantHandlerAuto,
139139
},
140140
} {
141-
if err := ensureOAuthClient(ctx, c.oauthClientClient, client); err != nil {
141+
if err := c.ensureOAuthClient(ctx, client); err != nil {
142142
return fmt.Errorf("unable to ensure existence of a bootstrapped OAuth client %q: %w", client.Name, err)
143143
}
144144
}
@@ -158,14 +158,19 @@ func randomBits(bits uint) []byte {
158158
return b
159159
}
160160

161-
func ensureOAuthClient(ctx context.Context, oauthClients oauthclient.OAuthClientInterface, client oauthv1.OAuthClient) error {
162-
_, err := oauthClients.Create(ctx, &client, metav1.CreateOptions{})
163-
if err == nil || !apierrors.IsAlreadyExists(err) {
161+
func (c *oauthsClientsController) ensureOAuthClient(ctx context.Context, client oauthv1.OAuthClient) error {
162+
_, err := c.oauthClientLister.Get(client.Name)
163+
if apierrors.IsNotFound(err) {
164+
_, err = c.oauthClientClient.Create(ctx, &client, metav1.CreateOptions{})
165+
return err
166+
}
167+
168+
if err != nil {
164169
return err
165170
}
166171

167172
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
168-
existing, err := oauthClients.Get(ctx, client.Name, metav1.GetOptions{})
173+
existing, err := c.oauthClientLister.Get(client.Name)
169174
if err != nil {
170175
return err
171176
}
@@ -188,7 +193,7 @@ func ensureOAuthClient(ctx context.Context, oauthClients oauthclient.OAuthClient
188193
return nil
189194
}
190195

191-
_, err = oauthClients.Update(ctx, existingCopy, metav1.UpdateOptions{})
196+
_, err = c.oauthClientClient.Update(ctx, existingCopy, metav1.UpdateOptions{})
192197
return err
193198
})
194199
}

pkg/controllers/oauthclientscontroller/oauthclientscontroller_test.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,19 @@ func newRouteLister(t *testing.T, routes ...*routev1.Route) routev1listers.Route
104104
return routev1listers.NewRouteLister(routeIndexer)
105105
}
106106

107-
func newTestOAuthsClientsController(t *testing.T) *oauthsClientsController {
107+
func newTestOAuthsClientsController(t *testing.T) (*oauthsClientsController, cache.Indexer) {
108+
indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{})
108109
return &oauthsClientsController{
109110
oauthClientClient: fakeoauthclient.NewSimpleClientset().OauthV1().OAuthClients(),
110-
oauthClientLister: oauthv1listers.NewOAuthClientLister(cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{})),
111+
oauthClientLister: oauthv1listers.NewOAuthClientLister(indexer),
111112
routeLister: newRouteLister(t, defaultRoute),
112113
ingressLister: newIngressLister(t, defaultIngress),
113-
}
114+
}, indexer
114115
}
115116

116117
func Test_sync(t *testing.T) {
117118
ctx := context.TODO()
118119
syncCtx := &fakeSyncContext{}
119-
c := newTestOAuthsClientsController(t)
120120

121121
tests := []struct {
122122
name string
@@ -133,6 +133,7 @@ func Test_sync(t *testing.T) {
133133

134134
for _, tt := range tests {
135135
t.Run(tt.name, func(t *testing.T) {
136+
c, _ := newTestOAuthsClientsController(t)
136137
if tt.withIngressLister != nil {
137138
c.ingressLister = tt.withIngressLister
138139
}
@@ -150,7 +151,7 @@ func Test_sync(t *testing.T) {
150151
}
151152

152153
func Test_getIngressConfig(t *testing.T) {
153-
c := newTestOAuthsClientsController(t)
154+
c, _ := newTestOAuthsClientsController(t)
154155

155156
tests := []struct {
156157
name string
@@ -193,7 +194,7 @@ func Test_getCanonicalRouteHost(t *testing.T) {
193194
{"namespace-not-found", masterPublicURL, "not-openshift-authentication", "oauth-openshift", "", true},
194195
}
195196

196-
c := newTestOAuthsClientsController(t)
197+
c, _ := newTestOAuthsClientsController(t)
197198

198199
for _, tt := range tests {
199200
t.Run(tt.host, func(t *testing.T) {
@@ -219,7 +220,7 @@ func Test_ensureBootstrappedOAuthClients(t *testing.T) {
219220
ctx := context.TODO()
220221

221222
t.Run("bootstrapped-oauth-clients-succeed", func(t *testing.T) {
222-
c := newTestOAuthsClientsController(t)
223+
c, _ := newTestOAuthsClientsController(t)
223224

224225
if err := c.ensureBootstrappedOAuthClients(ctx, masterPublicURL); err != nil {
225226
t.Errorf("got unexpected error: %v", err)
@@ -232,7 +233,7 @@ func Test_ensureBootstrappedOAuthClients(t *testing.T) {
232233
return true, nil, fmt.Errorf("%s %s fake error", action.GetVerb(), action.GetResource().Resource)
233234
})
234235

235-
c := newTestOAuthsClientsController(t)
236+
c, _ := newTestOAuthsClientsController(t)
236237
c.oauthClientClient = fakeClientset.OauthV1().OAuthClients()
237238

238239
if err := c.ensureBootstrappedOAuthClients(ctx, masterPublicURL); err == nil {
@@ -277,6 +278,7 @@ func Test_ensureOAuthClient(t *testing.T) {
277278
updateOAuthClient *oauthv1.OAuthClient
278279
oauthClientClient *fakeoauthclient.Clientset
279280

281+
alreadyExists bool
280282
wantEnsureErr bool
281283
wantUpdateErr bool
282284
}{
@@ -406,6 +408,7 @@ func Test_ensureOAuthClient(t *testing.T) {
406408
},
407409
},
408410
oauthClientClient: newFakeOAuthClientClient(nil),
411+
alreadyExists: true,
409412
},
410413
{
411414
name: "valid-oauth-client-when-already-exists-with-updates",
@@ -428,6 +431,7 @@ func Test_ensureOAuthClient(t *testing.T) {
428431
},
429432
},
430433
oauthClientClient: newFakeOAuthClientClient(nil),
434+
alreadyExists: true,
431435
},
432436
{
433437
name: "valid-oauth-client-when-already-exists-with-updated-empty-secret",
@@ -440,6 +444,7 @@ func Test_ensureOAuthClient(t *testing.T) {
440444
Secret: "",
441445
},
442446
oauthClientClient: newFakeOAuthClientClient(nil),
447+
alreadyExists: true,
443448
},
444449
{
445450
name: "valid-oauth-client-when-already-exists-with-updated-new-secret",
@@ -451,6 +456,7 @@ func Test_ensureOAuthClient(t *testing.T) {
451456
Secret: "secret",
452457
},
453458
oauthClientClient: newFakeOAuthClientClient(nil),
459+
alreadyExists: true,
454460
},
455461
{
456462
name: "valid-oauth-client-when-already-exists-with-updated-longer-secret",
@@ -463,6 +469,7 @@ func Test_ensureOAuthClient(t *testing.T) {
463469
Secret: "secretbutlonger",
464470
},
465471
oauthClientClient: newFakeOAuthClientClient(nil),
472+
alreadyExists: true,
466473
},
467474
{
468475
name: "valid-oauth-client-when-already-exists-with-updated-same-length-secret",
@@ -498,16 +505,26 @@ func Test_ensureOAuthClient(t *testing.T) {
498505
t.SkipNow()
499506
}
500507

501-
c := newTestOAuthsClientsController(t)
508+
c, indexer := newTestOAuthsClientsController(t)
502509
c.oauthClientClient = tt.oauthClientClient.OauthV1().OAuthClients()
503510

504-
err := ensureOAuthClient(ctx, c.oauthClientClient, *tt.oauthClient)
511+
if tt.alreadyExists && tt.oauthClient != nil {
512+
_, err := c.oauthClientClient.Create(ctx, tt.oauthClient, metav1.CreateOptions{})
513+
if err != nil {
514+
t.Fatalf("got unexpected err when creating in fake clientset: %v", err)
515+
}
516+
if err := indexer.Add(tt.oauthClient); err != nil {
517+
t.Fatalf("got unexpected err when adding to lister indexer: %v", err)
518+
}
519+
}
520+
521+
err := c.ensureOAuthClient(ctx, *tt.oauthClient)
505522
if (err != nil) != tt.wantEnsureErr {
506523
t.Fatalf("got error: %v; want error: %v", err, tt.wantEnsureErr)
507524
}
508525

509526
if tt.updateOAuthClient != nil {
510-
updateErr := ensureOAuthClient(ctx, c.oauthClientClient, *tt.updateOAuthClient)
527+
updateErr := c.ensureOAuthClient(ctx, *tt.updateOAuthClient)
511528
if (updateErr != nil) != tt.wantUpdateErr {
512529
t.Fatalf("got error: %v; want error: %v", updateErr, tt.wantUpdateErr)
513530
}

0 commit comments

Comments
 (0)