Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 010c619

Browse files
deads2kmfojtik
authored andcommittedMay 22, 2018
make the docker registry secret always prime
1 parent 23a0420 commit 010c619

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed
 

‎pkg/serviceaccounts/controllers/docker_registry_service.go

+10-5
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

+36-9
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,25 @@ func controllerSetup(startingObjects []runtime.Object, t *testing.T, stopCh <-ch
5959
DockerURLsInitialized: make(chan struct{}),
6060
},
6161
)
62+
controller.initialSecretsCheckDone = true
6263
controller.secretsSynced = func() bool { return true }
6364
return kubeclient, fakeWatch, controller, informerFactory
6465
}
6566

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

@@ -129,7 +143,8 @@ func TestUpdateNewStyleSecret(t *testing.T) {
129143

130144
kubeclient, fakeWatch, controller, informerFactory := controllerSetup([]runtime.Object{newStyleDockercfgSecret}, t, stopChannel)
131145
controller.syncRegistryLocationHandler = wrapHandler(received, controller.syncRegistryLocationChange, t)
132-
controller.syncSecretHandler = wrapHandler(updatedSecret, controller.syncSecretUpdate, t)
146+
controller.syncSecretHandler = wrapStringHandler(updatedSecret, controller.syncSecretUpdate, t)
147+
controller.initialSecretsCheckDone = false
133148
informerFactory.Start(stopChannel)
134149
go controller.Run(5, stopChannel)
135150

@@ -139,6 +154,9 @@ func TestUpdateNewStyleSecret(t *testing.T) {
139154
case <-time.After(time.Duration(45 * time.Second)):
140155
t.Fatalf("failed to become ready")
141156
}
157+
if controller.initialSecretsCheckDone != false {
158+
t.Fatalf("initialSecretsCheckDone should be false")
159+
}
142160

143161
fakeWatch.Modify(registryService)
144162
t.Log("Waiting to reach syncRegistryLocationHandler")
@@ -147,6 +165,12 @@ func TestUpdateNewStyleSecret(t *testing.T) {
147165
case <-time.After(time.Duration(45 * time.Second)):
148166
t.Fatalf("failed to call into syncRegistryLocationHandler")
149167
}
168+
169+
// after this point the secrets should be added to the queue and initial check should be done.
170+
if controller.initialSecretsCheckDone != true {
171+
t.Fatalf("initialSecretsCheckDone should be true")
172+
}
173+
150174
t.Log("Waiting to update secret")
151175
select {
152176
case <-updatedSecret:
@@ -216,9 +240,9 @@ func TestUpdateOldStyleSecretWithKey(t *testing.T) {
216240
Data: map[string][]byte{v1.DockerConfigKey: dockercfgContent},
217241
}
218242

219-
kubeclient, fakeWatch, controller, informerFactory := controllerSetup([]runtime.Object{oldStyleDockercfgSecret}, t, stopChannel)
243+
kubeclient, _, controller, informerFactory := controllerSetup([]runtime.Object{registryService, oldStyleDockercfgSecret}, t, stopChannel)
220244
controller.syncRegistryLocationHandler = wrapHandler(received, controller.syncRegistryLocationChange, t)
221-
controller.syncSecretHandler = wrapHandler(updatedSecret, controller.syncSecretUpdate, t)
245+
controller.syncSecretHandler = wrapStringHandler(updatedSecret, controller.syncSecretUpdate, t)
222246
informerFactory.Start(stopChannel)
223247
go controller.Run(5, stopChannel)
224248

@@ -229,8 +253,6 @@ func TestUpdateOldStyleSecretWithKey(t *testing.T) {
229253
t.Fatalf("failed to become ready")
230254
}
231255

232-
fakeWatch.Modify(registryService)
233-
234256
t.Log("Waiting to reach syncRegistryLocationHandler")
235257
select {
236258
case <-received:
@@ -309,7 +331,7 @@ func TestUpdateOldStyleSecretWithoutKey(t *testing.T) {
309331
return true, tokenSecret, nil
310332
})
311333
controller.syncRegistryLocationHandler = wrapHandler(received, controller.syncRegistryLocationChange, t)
312-
controller.syncSecretHandler = wrapHandler(updatedSecret, controller.syncSecretUpdate, t)
334+
controller.syncSecretHandler = wrapStringHandler(updatedSecret, controller.syncSecretUpdate, t)
313335
informerFactory.Start(stopChannel)
314336
go controller.Run(5, stopChannel)
315337

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

401423
kubeclient, fakeWatch, controller, informerFactory := controllerSetup([]runtime.Object{registryService, oldStyleDockercfgSecret}, t, stopChannel)
402424
controller.syncRegistryLocationHandler = wrapHandler(received, controller.syncRegistryLocationChange, t)
403-
controller.syncSecretHandler = wrapHandler(updatedSecret, controller.syncSecretUpdate, t)
425+
controller.syncSecretHandler = wrapStringHandler(updatedSecret, controller.syncSecretUpdate, t)
404426
informerFactory.Start(stopChannel)
405427
go controller.Run(5, stopChannel)
406428

407429
t.Log("Waiting for ready")
408430
select {
409431
case <-controller.dockerURLsInitialized:
410432
case <-time.After(time.Duration(45 * time.Second)):
411-
t.Fatalf("failed to become ready")
433+
t.Fatalf("failed waiting for dockerURLsInitialized")
412434
}
413435

436+
t.Logf("deleting %s service", registryService.Name)
414437
fakeWatch.Delete(registryService)
415438

416439
t.Log("Waiting for first update")
@@ -419,6 +442,7 @@ func TestClearSecretAndRecreate(t *testing.T) {
419442
case <-time.After(time.Duration(45 * time.Second)):
420443
t.Fatalf("failed to call into syncRegistryLocationHandler")
421444
}
445+
422446
t.Log("Waiting to update secret")
423447
select {
424448
case <-updatedSecret:
@@ -449,6 +473,8 @@ func TestClearSecretAndRecreate(t *testing.T) {
449473
}
450474

451475
kubeclient.ClearActions()
476+
477+
t.Logf("adding %s service", registryService.Name)
452478
fakeWatch.Add(registryService)
453479

454480
t.Log("Waiting for second update")
@@ -457,6 +483,7 @@ func TestClearSecretAndRecreate(t *testing.T) {
457483
case <-time.After(time.Duration(45 * time.Second)):
458484
t.Fatalf("failed to call into syncRegistryLocationHandler")
459485
}
486+
460487
t.Log("Waiting to update secret")
461488
select {
462489
case <-updatedSecret:

0 commit comments

Comments
 (0)
Please sign in to comment.