Skip to content

Commit a0255f5

Browse files
deads2kmfojtik
authored andcommitted
make the docker registry secret always prime
1 parent 23a0420 commit a0255f5

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

pkg/serviceaccounts/controllers/docker_registry_service.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ type DockerRegistryServiceController struct {
103103
serviceLister listers.ServiceLister
104104
servicesSynced func() bool
105105

106-
syncRegistryLocationHandler func(key string) error
106+
syncRegistryLocationHandler func() error
107107

108108
secretCache cache.Store
109109
secretsSynced func() bool
@@ -115,6 +115,11 @@ type DockerRegistryServiceController struct {
115115
secretsToUpdate workqueue.RateLimitingInterface
116116

117117
dockerURLsInitialized chan struct{}
118+
119+
// initialSecretsCheckDone is used to indicate that the controller should perform a full resync of all secrets
120+
// regardless of whether the registry location changed or not. This check is usually done on controller start
121+
// to verify the content of dockercfg entries in secrets
122+
initialSecretsCheckDone bool
118123
}
119124

120125
// Runs controller loops and returns immediately
@@ -190,7 +195,7 @@ func (e *DockerRegistryServiceController) watchForDockerURLChanges() {
190195
}
191196
defer e.registryLocationQueue.Done(key)
192197

193-
if err := e.syncRegistryLocationHandler(key.(string)); err == nil {
198+
if err := e.syncRegistryLocationHandler(); err == nil {
194199
// this means the request was successfully handled. We should "forget" the item so that any retry
195200
// later on is reset
196201
e.registryLocationQueue.Forget(key)
@@ -238,18 +243,18 @@ func getDockerRegistryLocations(lister listers.ServiceLister, location serviceLo
238243
}
239244

240245
// syncRegistryLocationChange goes through all service account dockercfg secrets and updates them to point at a new docker-registry location
241-
func (e *DockerRegistryServiceController) syncRegistryLocationChange(key string) error {
246+
func (e *DockerRegistryServiceController) syncRegistryLocationChange() error {
242247
newLocations := e.getDockerRegistryLocations()
243248
newDockerRegistryLocations := sets.NewString(newLocations...)
244249
existingURLs := e.getRegistryURLs()
245-
if existingURLs.Equal(newDockerRegistryLocations) {
246-
glog.V(4).Infof("No effective update: %v", newDockerRegistryLocations)
250+
if existingURLs.Equal(newDockerRegistryLocations) && e.initialSecretsCheckDone {
247251
return nil
248252
}
249253

250254
// make sure that new dockercfg secrets get the correct locations
251255
e.dockercfgController.SetDockerURLs(newDockerRegistryLocations.List()...)
252256
e.setRegistryURLs(newDockerRegistryLocations.List()...)
257+
e.initialSecretsCheckDone = true
253258

254259
// we've changed the docker registry URL. Add items to the work queue for all known secrets
255260
// new secrets will already get the updated value.

pkg/serviceaccounts/controllers/docker_registry_service_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,20 @@ func controllerSetup(startingObjects []runtime.Object, t *testing.T, stopCh <-ch
6363
return kubeclient, fakeWatch, controller, informerFactory
6464
}
6565

66-
func wrapHandler(indicator chan bool, handler func(string) error, t *testing.T) func(string) error {
66+
func wrapHandler(indicator chan bool, handler func() error, t *testing.T) func() error {
67+
return func() error {
68+
defer func() { indicator <- true }()
69+
70+
err := handler()
71+
if err != nil {
72+
t.Errorf("unexpected error: %v", err)
73+
}
74+
75+
return err
76+
}
77+
}
78+
79+
func wrapStringHandler(indicator chan bool, handler func(string) error, t *testing.T) func(string) error {
6780
return func(key string) error {
6881
defer func() { indicator <- true }()
6982

@@ -129,7 +142,7 @@ func TestUpdateNewStyleSecret(t *testing.T) {
129142

130143
kubeclient, fakeWatch, controller, informerFactory := controllerSetup([]runtime.Object{newStyleDockercfgSecret}, t, stopChannel)
131144
controller.syncRegistryLocationHandler = wrapHandler(received, controller.syncRegistryLocationChange, t)
132-
controller.syncSecretHandler = wrapHandler(updatedSecret, controller.syncSecretUpdate, t)
145+
controller.syncSecretHandler = wrapStringHandler(updatedSecret, controller.syncSecretUpdate, t)
133146
informerFactory.Start(stopChannel)
134147
go controller.Run(5, stopChannel)
135148

@@ -218,7 +231,7 @@ func TestUpdateOldStyleSecretWithKey(t *testing.T) {
218231

219232
kubeclient, fakeWatch, controller, informerFactory := controllerSetup([]runtime.Object{oldStyleDockercfgSecret}, t, stopChannel)
220233
controller.syncRegistryLocationHandler = wrapHandler(received, controller.syncRegistryLocationChange, t)
221-
controller.syncSecretHandler = wrapHandler(updatedSecret, controller.syncSecretUpdate, t)
234+
controller.syncSecretHandler = wrapStringHandler(updatedSecret, controller.syncSecretUpdate, t)
222235
informerFactory.Start(stopChannel)
223236
go controller.Run(5, stopChannel)
224237

@@ -309,7 +322,7 @@ func TestUpdateOldStyleSecretWithoutKey(t *testing.T) {
309322
return true, tokenSecret, nil
310323
})
311324
controller.syncRegistryLocationHandler = wrapHandler(received, controller.syncRegistryLocationChange, t)
312-
controller.syncSecretHandler = wrapHandler(updatedSecret, controller.syncSecretUpdate, t)
325+
controller.syncSecretHandler = wrapStringHandler(updatedSecret, controller.syncSecretUpdate, t)
313326
informerFactory.Start(stopChannel)
314327
go controller.Run(5, stopChannel)
315328

@@ -400,17 +413,18 @@ func TestClearSecretAndRecreate(t *testing.T) {
400413

401414
kubeclient, fakeWatch, controller, informerFactory := controllerSetup([]runtime.Object{registryService, oldStyleDockercfgSecret}, t, stopChannel)
402415
controller.syncRegistryLocationHandler = wrapHandler(received, controller.syncRegistryLocationChange, t)
403-
controller.syncSecretHandler = wrapHandler(updatedSecret, controller.syncSecretUpdate, t)
416+
controller.syncSecretHandler = wrapStringHandler(updatedSecret, controller.syncSecretUpdate, t)
404417
informerFactory.Start(stopChannel)
405418
go controller.Run(5, stopChannel)
406419

407420
t.Log("Waiting for ready")
408421
select {
409422
case <-controller.dockerURLsInitialized:
410423
case <-time.After(time.Duration(45 * time.Second)):
411-
t.Fatalf("failed to become ready")
424+
t.Fatalf("failed waiting for dockerURLsInitialized")
412425
}
413426

427+
t.Logf("deleting %s service", registryService.Name)
414428
fakeWatch.Delete(registryService)
415429

416430
t.Log("Waiting for first update")
@@ -419,6 +433,7 @@ func TestClearSecretAndRecreate(t *testing.T) {
419433
case <-time.After(time.Duration(45 * time.Second)):
420434
t.Fatalf("failed to call into syncRegistryLocationHandler")
421435
}
436+
422437
t.Log("Waiting to update secret")
423438
select {
424439
case <-updatedSecret:
@@ -449,6 +464,8 @@ func TestClearSecretAndRecreate(t *testing.T) {
449464
}
450465

451466
kubeclient.ClearActions()
467+
468+
t.Logf("adding %s service", registryService.Name)
452469
fakeWatch.Add(registryService)
453470

454471
t.Log("Waiting for second update")
@@ -457,6 +474,7 @@ func TestClearSecretAndRecreate(t *testing.T) {
457474
case <-time.After(time.Duration(45 * time.Second)):
458475
t.Fatalf("failed to call into syncRegistryLocationHandler")
459476
}
477+
460478
t.Log("Waiting to update secret")
461479
select {
462480
case <-updatedSecret:

0 commit comments

Comments
 (0)