forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_dockercfg_secrets.go
431 lines (364 loc) · 14.1 KB
/
create_dockercfg_secrets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package controllers
import (
"encoding/json"
"fmt"
"strings"
"sync"
"time"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api"
kapierrors "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/client/cache"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/controller/framework"
"k8s.io/kubernetes/pkg/credentialprovider"
"k8s.io/kubernetes/pkg/registry/secret"
"k8s.io/kubernetes/pkg/runtime"
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/pkg/util/wait"
"k8s.io/kubernetes/pkg/util/workqueue"
"k8s.io/kubernetes/pkg/watch"
osautil "github.com/openshift/origin/pkg/serviceaccounts/util"
)
const (
ServiceAccountTokenSecretNameKey = "openshift.io/token-secret.name"
MaxRetriesBeforeResync = 5
// ServiceAccountTokenValueAnnotation stores the actual value of the token so that a dockercfg secret can be
// made without having a value dockerURL
ServiceAccountTokenValueAnnotation = "openshift.io/token-secret.value"
)
// DockercfgControllerOptions contains options for the DockercfgController
type DockercfgControllerOptions struct {
// Resync is the time.Duration at which to fully re-list service accounts.
// If zero, re-list will be delayed as long as possible
Resync time.Duration
// DockerURLsIntialized is used to send a signal to this controller that it has the correct set of docker urls
// This is normally signaled from the DockerRegistryServiceController which watches for updates to the internal
// docker registry service.
DockerURLsIntialized chan struct{}
}
// NewDockercfgController returns a new *DockercfgController.
func NewDockercfgController(cl client.Interface, options DockercfgControllerOptions) *DockercfgController {
e := &DockercfgController{
client: cl,
queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
dockerURLsIntialized: options.DockerURLsIntialized,
}
var serviceAccountCache cache.Store
serviceAccountCache, e.serviceAccountController = framework.NewInformer(
&cache.ListWatch{
ListFunc: func(options api.ListOptions) (runtime.Object, error) {
return e.client.ServiceAccounts(api.NamespaceAll).List(options)
},
WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
return e.client.ServiceAccounts(api.NamespaceAll).Watch(options)
},
},
&api.ServiceAccount{},
options.Resync,
framework.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
serviceAccount := obj.(*api.ServiceAccount)
glog.V(5).Infof("Adding service account %s", serviceAccount.Name)
e.enqueueServiceAccount(serviceAccount)
},
UpdateFunc: func(old, cur interface{}) {
serviceAccount := cur.(*api.ServiceAccount)
glog.V(5).Infof("Updating service account %s", serviceAccount.Name)
// Resync on service object relist.
e.enqueueServiceAccount(serviceAccount)
},
},
)
e.serviceAccountCache = NewEtcdMutationCache(serviceAccountCache)
e.syncHandler = e.syncServiceAccount
return e
}
// DockercfgController manages dockercfg secrets for ServiceAccount objects
type DockercfgController struct {
client client.Interface
dockerURLLock sync.Mutex
dockerURLs []string
dockerURLsIntialized chan struct{}
serviceAccountCache MutationCache
serviceAccountController *framework.Controller
queue workqueue.RateLimitingInterface
// syncHandler does the work. It's factored out for unit testing
syncHandler func(serviceKey string) error
}
func (e *DockercfgController) Run(workers int, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
// Wait for the store to sync before starting any work in this controller.
ready := make(chan struct{})
go e.waitForDockerURLs(ready, stopCh)
select {
case <-ready:
case <-stopCh:
return
}
glog.Infof("Dockercfg secret controller initialized, starting.")
go e.serviceAccountController.Run(stopCh)
for i := 0; i < workers; i++ {
go wait.Until(e.worker, time.Second, stopCh)
}
<-stopCh
glog.Infof("Shutting down dockercfg secret controller")
e.queue.ShutDown()
}
func (c *DockercfgController) waitForDockerURLs(ready chan<- struct{}, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
// wait for the initialization to complete to be informed of a stop
select {
case <-c.dockerURLsIntialized:
case <-stopCh:
return
}
close(ready)
}
func (e *DockercfgController) enqueueServiceAccount(serviceAccount *api.ServiceAccount) {
if !needsDockercfgSecret(serviceAccount) {
return
}
key, err := controller.KeyFunc(serviceAccount)
if err != nil {
glog.Errorf("Couldn't get key for object %+v: %v", serviceAccount, err)
return
}
e.queue.Add(key)
}
// worker runs a worker thread that just dequeues items, processes them, and marks them done.
// It enforces that the syncHandler is never invoked concurrently with the same key.
func (e *DockercfgController) worker() {
for {
if !e.work() {
return
}
}
}
// work returns true if the worker thread should continue
func (e *DockercfgController) work() bool {
key, quit := e.queue.Get()
if quit {
return false
}
defer e.queue.Done(key)
if err := e.syncHandler(key.(string)); err == nil {
// this means the request was successfully handled. We should "forget" the item so that any retry
// later on is reset
e.queue.Forget(key)
} else {
// if we had an error it means that we didn't handle it, which means that we want to requeue the work
if e.queue.NumRequeues(key) > MaxRetriesBeforeResync {
utilruntime.HandleError(fmt.Errorf("error syncing service, it will be tried again on a resync %v: %v", key, err))
e.queue.Forget(key)
} else {
glog.V(4).Infof("error syncing service, it will be retried %v: %v", key, err)
e.queue.AddRateLimited(key)
}
}
return true
}
func (e *DockercfgController) SetDockerURLs(newDockerURLs ...string) {
e.dockerURLLock.Lock()
defer e.dockerURLLock.Unlock()
e.dockerURLs = newDockerURLs
}
func needsDockercfgSecret(serviceAccount *api.ServiceAccount) bool {
mountableDockercfgSecrets, imageDockercfgPullSecrets := getGeneratedDockercfgSecretNames(serviceAccount)
// look for an ImagePullSecret in the form
if len(imageDockercfgPullSecrets) > 0 && len(mountableDockercfgSecrets) > 0 {
return false
}
return true
}
func (e *DockercfgController) syncServiceAccount(key string) error {
obj, exists, err := e.serviceAccountCache.GetByKey(key)
if err != nil {
glog.V(4).Infof("Unable to retrieve service account %v from store: %v", key, err)
return err
}
if !exists {
glog.V(4).Infof("Service account has been deleted %v", key)
return nil
}
if !needsDockercfgSecret(obj.(*api.ServiceAccount)) {
return nil
}
uncastSA, err := api.Scheme.DeepCopy(obj)
if err != nil {
return err
}
serviceAccount := uncastSA.(*api.ServiceAccount)
mountableDockercfgSecrets, imageDockercfgPullSecrets := getGeneratedDockercfgSecretNames(serviceAccount)
// If we have a pull secret in one list, use it for the other. It must only be in one list because
// otherwise we wouldn't "needsDockercfgSecret"
foundPullSecret := len(imageDockercfgPullSecrets) > 0
foundMountableSecret := len(mountableDockercfgSecrets) > 0
if foundPullSecret || foundMountableSecret {
switch {
case foundPullSecret:
serviceAccount.Secrets = append(serviceAccount.Secrets, api.ObjectReference{Name: imageDockercfgPullSecrets.List()[0]})
case foundMountableSecret:
serviceAccount.ImagePullSecrets = append(serviceAccount.ImagePullSecrets, api.LocalObjectReference{Name: mountableDockercfgSecrets.List()[0]})
}
updatedSA, err := e.client.ServiceAccounts(serviceAccount.Namespace).Update(serviceAccount)
if err == nil {
e.serviceAccountCache.Mutation(updatedSA)
}
return err
}
dockercfgSecret, err := e.createDockerPullSecret(serviceAccount)
if err != nil {
return err
}
first := true
err = client.RetryOnConflict(client.DefaultBackoff, func() error {
if !first {
obj, exists, err := e.serviceAccountCache.GetByKey(key)
if err != nil {
return err
}
if !exists || !needsDockercfgSecret(obj.(*api.ServiceAccount)) || serviceAccount.UID != obj.(*api.ServiceAccount).UID {
// somehow a dockercfg secret appeared or the SA disappeared. cleanup the secret we made and return
glog.V(2).Infof("Deleting secret because the work is already done %s/%s", dockercfgSecret.Namespace, dockercfgSecret.Name)
e.client.Secrets(dockercfgSecret.Namespace).Delete(dockercfgSecret.Name)
return nil
}
uncastSA, err := api.Scheme.DeepCopy(obj)
if err != nil {
return err
}
serviceAccount = uncastSA.(*api.ServiceAccount)
}
first = false
serviceAccount.Secrets = append(serviceAccount.Secrets, api.ObjectReference{Name: dockercfgSecret.Name})
serviceAccount.ImagePullSecrets = append(serviceAccount.ImagePullSecrets, api.LocalObjectReference{Name: dockercfgSecret.Name})
updatedSA, err := e.client.ServiceAccounts(serviceAccount.Namespace).Update(serviceAccount)
if err == nil {
e.serviceAccountCache.Mutation(updatedSA)
}
return err
})
if err != nil {
// nothing to do. Our choice was stale or we got a conflict. Either way that means that the service account was updated. We simply need to return because we'll get an update notification later
// we do need to clean up our dockercfgSecret. token secrets are cleaned up by the controller handling service account dockercfg secret deletes
glog.V(2).Infof("Deleting secret %s/%s (err=%v)", dockercfgSecret.Namespace, dockercfgSecret.Name, err)
e.client.Secrets(dockercfgSecret.Namespace).Delete(dockercfgSecret.Name)
}
return err
}
const (
tokenSecretWaitInterval = 20 * time.Millisecond
tokenSecretWaitTimes = 100
)
// createTokenSecret creates a token secret for a given service account. Returns the name of the token
func (e *DockercfgController) createTokenSecret(serviceAccount *api.ServiceAccount) (*api.Secret, error) {
tokenSecret := &api.Secret{
ObjectMeta: api.ObjectMeta{
Name: secret.Strategy.GenerateName(osautil.GetTokenSecretNamePrefix(serviceAccount)),
Namespace: serviceAccount.Namespace,
Annotations: map[string]string{
api.ServiceAccountNameKey: serviceAccount.Name,
api.ServiceAccountUIDKey: string(serviceAccount.UID),
},
},
Type: api.SecretTypeServiceAccountToken,
Data: map[string][]byte{},
}
_, err := e.client.Secrets(tokenSecret.Namespace).Create(tokenSecret)
if err != nil {
return nil, err
}
// now we have to wait for the service account token controller to make this valid
// TODO remove this once we have a create-token endpoint
for i := 0; i <= tokenSecretWaitTimes; i++ {
liveTokenSecret, err2 := e.client.Secrets(tokenSecret.Namespace).Get(tokenSecret.Name)
if err2 != nil {
return nil, err2
}
if len(liveTokenSecret.Data[api.ServiceAccountTokenKey]) > 0 {
return liveTokenSecret, nil
}
time.Sleep(wait.Jitter(tokenSecretWaitInterval, 0.0))
}
// the token wasn't ever created, attempt deletion
glog.Warningf("Deleting unfilled token secret %s/%s", tokenSecret.Namespace, tokenSecret.Name)
if deleteErr := e.client.Secrets(tokenSecret.Namespace).Delete(tokenSecret.Name); (deleteErr != nil) && !kapierrors.IsNotFound(deleteErr) {
utilruntime.HandleError(deleteErr)
}
return nil, fmt.Errorf("token never generated for %s", tokenSecret.Name)
}
// createDockerPullSecret creates a dockercfg secret based on the token secret
func (e *DockercfgController) createDockerPullSecret(serviceAccount *api.ServiceAccount) (*api.Secret, error) {
glog.V(4).Infof("Creating secret for %s/%s", serviceAccount.Namespace, serviceAccount.Name)
tokenSecret, err := e.createTokenSecret(serviceAccount)
if err != nil {
return nil, err
}
dockercfgSecret := &api.Secret{
ObjectMeta: api.ObjectMeta{
Name: secret.Strategy.GenerateName(osautil.GetDockercfgSecretNamePrefix(serviceAccount)),
Namespace: tokenSecret.Namespace,
Annotations: map[string]string{
api.ServiceAccountNameKey: serviceAccount.Name,
api.ServiceAccountUIDKey: string(serviceAccount.UID),
ServiceAccountTokenSecretNameKey: string(tokenSecret.Name),
ServiceAccountTokenValueAnnotation: string(tokenSecret.Data[api.ServiceAccountTokenKey]),
},
},
Type: api.SecretTypeDockercfg,
Data: map[string][]byte{},
}
// prevent updating the DockerURL until we've created the secret
e.dockerURLLock.Lock()
defer e.dockerURLLock.Unlock()
dockercfg := credentialprovider.DockerConfig{}
for _, dockerURL := range e.dockerURLs {
dockercfg[dockerURL] = credentialprovider.DockerConfigEntry{
Username: "serviceaccount",
Password: string(tokenSecret.Data[api.ServiceAccountTokenKey]),
Email: "serviceaccount@example.org",
}
}
dockercfgContent, err := json.Marshal(&dockercfg)
if err != nil {
return nil, err
}
dockercfgSecret.Data[api.DockerConfigKey] = dockercfgContent
// Save the secret
createdSecret, err := e.client.Secrets(tokenSecret.Namespace).Create(dockercfgSecret)
if err != nil {
// Clean up the generated token secret if we're not going to use it
glog.V(2).Infof("deleting unused token secret %s/%s, error creating dockercfgSecret: %v", tokenSecret.Namespace, tokenSecret.Name, err)
if deleteErr := e.client.Secrets(tokenSecret.Namespace).Delete(tokenSecret.Name); (deleteErr != nil) && !kapierrors.IsNotFound(deleteErr) {
utilruntime.HandleError(deleteErr)
}
return nil, err
}
return createdSecret, err
}
func getSecretReferences(serviceAccount *api.ServiceAccount) sets.String {
references := sets.NewString()
for _, secret := range serviceAccount.Secrets {
references.Insert(secret.Name)
}
return references
}
func getGeneratedDockercfgSecretNames(serviceAccount *api.ServiceAccount) (sets.String, sets.String) {
mountableDockercfgSecrets := sets.String{}
imageDockercfgPullSecrets := sets.String{}
secretNamePrefix := osautil.GetDockercfgSecretNamePrefix(serviceAccount)
for _, s := range serviceAccount.Secrets {
if strings.HasPrefix(s.Name, secretNamePrefix) {
mountableDockercfgSecrets.Insert(s.Name)
}
}
for _, s := range serviceAccount.ImagePullSecrets {
if strings.HasPrefix(s.Name, secretNamePrefix) {
imageDockercfgPullSecrets.Insert(s.Name)
}
}
return mountableDockercfgSecrets, imageDockercfgPullSecrets
}