Skip to content

Commit 26ca796

Browse files
committed
handle multiple names for docker registry
1 parent de03ab9 commit 26ca796

File tree

4 files changed

+796
-136
lines changed

4 files changed

+796
-136
lines changed

pkg/cmd/server/origin/run_components.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,17 @@ func (c *MasterConfig) RunServiceAccountPullSecretsControllers() {
155155
serviceaccountcontrollers.NewDockercfgDeletedController(c.KubeClient(), serviceaccountcontrollers.DockercfgDeletedControllerOptions{}).Run()
156156
serviceaccountcontrollers.NewDockercfgTokenDeletedController(c.KubeClient(), serviceaccountcontrollers.DockercfgTokenDeletedControllerOptions{}).Run()
157157

158-
dockercfgController := serviceaccountcontrollers.NewDockercfgController(c.KubeClient(), serviceaccountcontrollers.DockercfgControllerOptions{DefaultDockerURL: serviceaccountcontrollers.DefaultOpenshiftDockerURL})
158+
dockerURLsIntialized := make(chan struct{})
159+
dockercfgController := serviceaccountcontrollers.NewDockercfgController(c.KubeClient(), serviceaccountcontrollers.DockercfgControllerOptions{DockerURLsIntialized: dockerURLsIntialized})
159160
go dockercfgController.Run(5, utilwait.NeverStop)
160161

161162
dockerRegistryControllerOptions := serviceaccountcontrollers.DockerRegistryServiceControllerOptions{
162-
RegistryNamespace: "default",
163-
RegistryServiceName: "docker-registry",
164-
DockercfgController: dockercfgController,
165-
DefaultDockerURL: serviceaccountcontrollers.DefaultOpenshiftDockerURL,
163+
RegistryNamespace: "default",
164+
RegistryServiceName: "docker-registry",
165+
DockercfgController: dockercfgController,
166+
DockerURLsIntialized: dockerURLsIntialized,
166167
}
167-
serviceaccountcontrollers.NewDockerRegistryServiceController(c.KubeClient(), dockerRegistryControllerOptions).Run()
168+
go serviceaccountcontrollers.NewDockerRegistryServiceController(c.KubeClient(), dockerRegistryControllerOptions).Run(10, make(chan struct{}))
168169
}
169170

170171
// RunPolicyCache starts the policy cache

pkg/serviceaccounts/controllers/create_dockercfg_secrets.go

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ import (
2929
const (
3030
ServiceAccountTokenSecretNameKey = "openshift.io/token-secret.name"
3131
MaxRetriesBeforeResync = 5
32+
33+
// ServiceAccountTokenValueAnnotation stores the actual value of the token so that a dockercfg secret can be
34+
// made without having a value dockerURL
35+
ServiceAccountTokenValueAnnotation = "openshift.io/token-secret.value"
3236
)
3337

3438
// DockercfgControllerOptions contains options for the DockercfgController
@@ -37,14 +41,16 @@ type DockercfgControllerOptions struct {
3741
// If zero, re-list will be delayed as long as possible
3842
Resync time.Duration
3943

40-
DefaultDockerURL string
44+
// DockerURLsIntialized is used to send a signal to this controller that it has the correct set of docker urls
45+
DockerURLsIntialized chan struct{}
4146
}
4247

4348
// NewDockercfgController returns a new *DockercfgController.
4449
func NewDockercfgController(cl client.Interface, options DockercfgControllerOptions) *DockercfgController {
4550
e := &DockercfgController{
46-
client: cl,
47-
queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
51+
client: cl,
52+
queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
53+
dockerURLsIntialized: options.DockerURLsIntialized,
4854
}
4955

5056
var serviceAccountCache cache.Store
@@ -76,7 +82,6 @@ func NewDockercfgController(cl client.Interface, options DockercfgControllerOpti
7682

7783
e.serviceAccountCache = NewEtcdMutationCache(serviceAccountCache)
7884
e.syncHandler = e.syncServiceAccount
79-
e.dockerURL = options.DefaultDockerURL
8085

8186
return e
8287
}
@@ -85,8 +90,9 @@ func NewDockercfgController(cl client.Interface, options DockercfgControllerOpti
8590
type DockercfgController struct {
8691
client client.Interface
8792

88-
dockerURL string
89-
dockerURLLock sync.Mutex
93+
dockerURLs []string
94+
dockerURLLock sync.Mutex
95+
dockerURLsIntialized chan struct{}
9096

9197
serviceAccountCache MutationCache
9298
serviceAccountController *framework.Controller
@@ -99,16 +105,30 @@ type DockercfgController struct {
99105

100106
func (e *DockercfgController) Run(workers int, stopCh <-chan struct{}) {
101107
defer utilruntime.HandleCrash()
102-
go e.serviceAccountController.Run(stopCh)
103-
for i := 0; i < workers; i++ {
104-
go wait.Until(e.worker, time.Second, stopCh)
105-
}
108+
go e.waitForDockerURLs(workers, stopCh)
106109

107110
<-stopCh
108111
glog.Infof("Shutting down dockercfg secret controller")
109112
e.queue.ShutDown()
110113
}
111114

115+
// waitForDockerURLs blocks until the dockerURLs are ready for use. Otherwise, we'll create a bunch of useless dockercfg secrets
116+
func (e *DockercfgController) waitForDockerURLs(workers int, stopCh <-chan struct{}) {
117+
defer utilruntime.HandleCrash()
118+
119+
// wait for the initialization to complete to be informed of a stop
120+
select {
121+
case <-e.dockerURLsIntialized:
122+
case <-stopCh:
123+
return
124+
}
125+
126+
go e.serviceAccountController.Run(stopCh)
127+
for i := 0; i < workers; i++ {
128+
go wait.Until(e.worker, time.Second, stopCh)
129+
}
130+
}
131+
112132
func (e *DockercfgController) enqueueServiceAccount(serviceAccount *api.ServiceAccount) {
113133
if !needsDockercfgSecret(serviceAccount) {
114134
return
@@ -160,14 +180,15 @@ func (e *DockercfgController) worker_inner() bool {
160180
return true
161181
}
162182

163-
func (e *DockercfgController) SetDockerURL(newDockerURL string) {
183+
func (e *DockercfgController) SetDockerURLs(newDockerURLs ...string) {
164184
e.dockerURLLock.Lock()
165185
defer e.dockerURLLock.Unlock()
166186

167-
e.dockerURL = newDockerURL
187+
e.dockerURLs = newDockerURLs
168188
}
169189

170190
func needsDockercfgSecret(serviceAccount *api.ServiceAccount) bool {
191+
171192
mountableDockercfgSecrets, imageDockercfgPullSecrets := getGeneratedDockercfgSecretNames(serviceAccount)
172193

173194
// look for an ImagePullSecret in the form
@@ -328,9 +349,10 @@ func (e *DockercfgController) createDockerPullSecret(serviceAccount *api.Service
328349
Name: secret.Strategy.GenerateName(osautil.GetDockercfgSecretNamePrefix(serviceAccount)),
329350
Namespace: tokenSecret.Namespace,
330351
Annotations: map[string]string{
331-
api.ServiceAccountNameKey: serviceAccount.Name,
332-
api.ServiceAccountUIDKey: string(serviceAccount.UID),
333-
ServiceAccountTokenSecretNameKey: string(tokenSecret.Name),
352+
api.ServiceAccountNameKey: serviceAccount.Name,
353+
api.ServiceAccountUIDKey: string(serviceAccount.UID),
354+
ServiceAccountTokenSecretNameKey: string(tokenSecret.Name),
355+
ServiceAccountTokenValueAnnotation: string(tokenSecret.Data[api.ServiceAccountTokenKey]),
334356
},
335357
},
336358
Type: api.SecretTypeDockercfg,
@@ -341,14 +363,15 @@ func (e *DockercfgController) createDockerPullSecret(serviceAccount *api.Service
341363
e.dockerURLLock.Lock()
342364
defer e.dockerURLLock.Unlock()
343365

344-
dockercfg := &credentialprovider.DockerConfig{
345-
e.dockerURL: credentialprovider.DockerConfigEntry{
366+
dockercfg := credentialprovider.DockerConfig{}
367+
for _, dockerURL := range e.dockerURLs {
368+
dockercfg[dockerURL] = credentialprovider.DockerConfigEntry{
346369
Username: "serviceaccount",
347370
Password: string(tokenSecret.Data[api.ServiceAccountTokenKey]),
348371
349-
},
372+
}
350373
}
351-
dockercfgContent, err := json.Marshal(dockercfg)
374+
dockercfgContent, err := json.Marshal(&dockercfg)
352375
if err != nil {
353376
return nil, err
354377
}

0 commit comments

Comments
 (0)