forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
deleted_dockercfg_secrets.go
172 lines (144 loc) · 5.33 KB
/
deleted_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
package controllers
import (
"fmt"
"time"
"github.com/golang/glog"
"k8s.io/api/core/v1"
kapierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
informers "k8s.io/client-go/informers/core/v1"
kclientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)
// NumServiceAccountUpdateRetries controls the number of times we will retry on conflict errors.
// This happens when multiple service account controllers update at the same time.
const NumServiceAccountUpdateRetries = 10
// DockercfgDeletedControllerOptions contains options for the DockercfgDeletedController
type DockercfgDeletedControllerOptions struct {
// Resync is the time.Duration at which to fully re-list secrets.
// If zero, re-list will be delayed as long as possible
Resync time.Duration
}
// NewDockercfgDeletedController returns a new *DockercfgDeletedController.
func NewDockercfgDeletedController(secrets informers.SecretInformer, cl kclientset.Interface, options DockercfgDeletedControllerOptions) *DockercfgDeletedController {
e := &DockercfgDeletedController{
client: cl,
}
e.secretController = secrets.Informer().GetController()
secrets.Informer().AddEventHandlerWithResyncPeriod(
cache.FilteringResourceEventHandler{
FilterFunc: func(obj interface{}) bool {
switch t := obj.(type) {
case *v1.Secret:
return t.Type == v1.SecretTypeDockercfg
default:
utilruntime.HandleError(fmt.Errorf("object passed to %T that is not expected: %T", e, obj))
return false
}
},
Handler: cache.ResourceEventHandlerFuncs{
DeleteFunc: e.secretDeleted,
},
},
options.Resync,
)
return e
}
// The DockercfgDeletedController watches for service account dockercfg secrets to be deleted
// It removes the corresponding token secret and service account references.
type DockercfgDeletedController struct {
client kclientset.Interface
secretController cache.Controller
}
// Run processes the queue.
func (e *DockercfgDeletedController) Run(stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
glog.Infof("Starting DockercfgDeletedController controller")
defer glog.Infof("Shutting down DockercfgDeletedController controller")
// Wait for the stores to fill
if !cache.WaitForCacheSync(stopCh, e.secretController.HasSynced) {
return
}
glog.V(1).Infof("caches synced")
<-stopCh
}
// secretDeleted reacts to a Secret being deleted by looking to see if it's a dockercfg secret for a service account, in which case it
// it removes the references from the service account and removes the token created to back the dockercfgSecret
func (e *DockercfgDeletedController) secretDeleted(obj interface{}) {
dockercfgSecret, ok := obj.(*v1.Secret)
if !ok {
return
}
if _, exists := dockercfgSecret.Annotations[ServiceAccountTokenSecretNameKey]; !exists {
return
}
for i := 1; i <= NumServiceAccountUpdateRetries; i++ {
if err := e.removeDockercfgSecretReference(dockercfgSecret); err != nil {
if kapierrors.IsConflict(err) && i < NumServiceAccountUpdateRetries {
time.Sleep(wait.Jitter(100*time.Millisecond, 0.0))
continue
}
glog.Error(err)
break
}
break
}
// remove the reference token secret
if err := e.client.Core().Secrets(dockercfgSecret.Namespace).Delete(dockercfgSecret.Annotations[ServiceAccountTokenSecretNameKey], nil); (err != nil) && !kapierrors.IsNotFound(err) {
utilruntime.HandleError(err)
}
}
// removeDockercfgSecretReference updates the given ServiceAccount to remove ImagePullSecret and Secret references
func (e *DockercfgDeletedController) removeDockercfgSecretReference(dockercfgSecret *v1.Secret) error {
serviceAccount, err := e.getServiceAccount(dockercfgSecret)
if kapierrors.IsNotFound(err) {
// if the service account is gone, no work to do
return nil
}
if err != nil {
return err
}
changed := false
secrets := []v1.ObjectReference{}
for _, s := range serviceAccount.Secrets {
if s.Name == dockercfgSecret.Name {
changed = true
continue
}
secrets = append(secrets, s)
}
serviceAccount.Secrets = secrets
imagePullSecrets := []v1.LocalObjectReference{}
for _, s := range serviceAccount.ImagePullSecrets {
if s.Name == dockercfgSecret.Name {
changed = true
continue
}
imagePullSecrets = append(imagePullSecrets, s)
}
serviceAccount.ImagePullSecrets = imagePullSecrets
if changed {
_, err = e.client.Core().ServiceAccounts(dockercfgSecret.Namespace).Update(serviceAccount)
if err != nil {
return err
}
}
return nil
}
// getServiceAccount returns the ServiceAccount referenced by the given secret. return nil, but no error if the secret doesn't reference a service account
func (e *DockercfgDeletedController) getServiceAccount(secret *v1.Secret) (*v1.ServiceAccount, error) {
saName, saUID := secret.Annotations[v1.ServiceAccountNameKey], secret.Annotations[v1.ServiceAccountUIDKey]
if len(saName) == 0 || len(saUID) == 0 {
return nil, nil
}
serviceAccount, err := e.client.Core().ServiceAccounts(secret.Namespace).Get(saName, metav1.GetOptions{})
if err != nil {
return nil, err
}
if saUID != string(serviceAccount.UID) {
return nil, fmt.Errorf("secret (%v) service account UID (%v) does not match service account (%v) UID (%v)", secret.Name, saUID, serviceAccount.Name, serviceAccount.UID)
}
return serviceAccount, nil
}