-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathfactory.go
250 lines (215 loc) · 7.7 KB
/
factory.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
package deploymentconfig
import (
"time"
"github.com/golang/glog"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/client/record"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
kcontroller "k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/controller/framework"
"k8s.io/kubernetes/pkg/runtime"
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
"k8s.io/kubernetes/pkg/util/wait"
"k8s.io/kubernetes/pkg/util/workqueue"
osclient "github.com/openshift/origin/pkg/client"
deployapi "github.com/openshift/origin/pkg/deploy/api"
deployutil "github.com/openshift/origin/pkg/deploy/util"
)
const (
// We must avoid creating new replication controllers until the deployment config and replication
// controller stores have synced. If it hasn't synced, to avoid a hot loop, we'll wait this long
// between checks.
StoreSyncedPollPeriod = 100 * time.Millisecond
// MaxRetries is the number of times a deployment config will be retried before it is dropped out
// of the queue.
MaxRetries = 5
)
// NewDeploymentConfigController creates a new DeploymentConfigController.
func NewDeploymentConfigController(dcInformer, rcInformer, podInformer framework.SharedIndexInformer, oc osclient.Interface, kc kclient.Interface, codec runtime.Codec) *DeploymentConfigController {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartRecordingToSink(kc.Events(""))
recorder := eventBroadcaster.NewRecorder(kapi.EventSource{Component: "deploymentconfig-controller"})
c := &DeploymentConfigController{
dn: oc,
rn: kc,
queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
recorder: recorder,
codec: codec,
}
c.dcStore.Indexer = dcInformer.GetIndexer()
dcInformer.AddEventHandler(framework.ResourceEventHandlerFuncs{
AddFunc: c.addDeploymentConfig,
UpdateFunc: c.updateDeploymentConfig,
DeleteFunc: c.deleteDeploymentConfig,
})
c.rcStore.Indexer = rcInformer.GetIndexer()
rcInformer.AddEventHandler(framework.ResourceEventHandlerFuncs{
AddFunc: c.addReplicationController,
UpdateFunc: c.updateReplicationController,
DeleteFunc: c.deleteReplicationController,
})
c.podStore.Indexer = podInformer.GetIndexer()
c.dcStoreSynced = dcInformer.HasSynced
c.rcStoreSynced = rcInformer.HasSynced
c.podStoreSynced = podInformer.HasSynced
return c
}
// Run begins watching and syncing.
func (c *DeploymentConfigController) Run(workers int, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
// Wait for the rc and dc stores to sync before starting any work in this controller.
ready := make(chan struct{})
go c.waitForSyncedStores(ready, stopCh)
select {
case <-ready:
case <-stopCh:
return
}
for i := 0; i < workers; i++ {
go wait.Until(c.worker, time.Second, stopCh)
}
<-stopCh
glog.Infof("Shutting down deploymentconfig controller")
c.queue.ShutDown()
}
func (c *DeploymentConfigController) waitForSyncedStores(ready chan<- struct{}, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
for !c.dcStoreSynced() || !c.rcStoreSynced() || !c.podStoreSynced() {
glog.V(4).Infof("Waiting for the dc, rc, and pod caches to sync before starting the deployment config controller workers")
select {
case <-time.After(StoreSyncedPollPeriod):
case <-stopCh:
return
}
}
close(ready)
}
func (c *DeploymentConfigController) addDeploymentConfig(obj interface{}) {
dc := obj.(*deployapi.DeploymentConfig)
glog.V(4).Infof("Adding deployment config %q", dc.Name)
c.enqueueDeploymentConfig(dc)
}
func (c *DeploymentConfigController) updateDeploymentConfig(old, cur interface{}) {
dc := cur.(*deployapi.DeploymentConfig)
glog.V(4).Infof("Updating deployment config %q", dc.Name)
c.enqueueDeploymentConfig(dc)
}
func (c *DeploymentConfigController) deleteDeploymentConfig(obj interface{}) {
dc, ok := obj.(*deployapi.DeploymentConfig)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
glog.Errorf("Couldn't get object from tombstone %+v", obj)
return
}
dc, ok = tombstone.Obj.(*deployapi.DeploymentConfig)
if !ok {
glog.Errorf("Tombstone contained object that is not a deployment config: %+v", obj)
return
}
}
glog.V(4).Infof("Deleting deployment config %q", dc.Name)
c.enqueueDeploymentConfig(dc)
}
// addReplicationController figures out which deploymentconfig is managing this replication
// controller and requeues the deployment config.
// TODO: Determine if we need to resync here. Would be useful for adoption but we cannot
// adopt right now.
func (c *DeploymentConfigController) addReplicationController(obj interface{}) {
rc := obj.(*kapi.ReplicationController)
glog.V(4).Infof("Replication controller %q added.", rc.Name)
// We are waiting for the deployment config store to sync but still there are pathological
// cases of highly latent watches.
if dc, err := c.dcStore.GetConfigForController(rc); err == nil && dc != nil {
c.enqueueDeploymentConfig(dc)
}
}
// updateReplicationController figures out which deploymentconfig is managing this replication
// controller and requeues the deployment config.
func (c *DeploymentConfigController) updateReplicationController(old, cur interface{}) {
// A periodic relist will send update events for all known controllers.
if kapi.Semantic.DeepEqual(old, cur) {
return
}
curRC := cur.(*kapi.ReplicationController)
glog.V(4).Infof("Replication controller %q updated.", curRC.Name)
if dc, err := c.dcStore.GetConfigForController(curRC); err == nil && dc != nil {
c.enqueueDeploymentConfig(dc)
}
}
// deleteReplicationController enqueues the deployment that manages a replicationcontroller when
// the replicationcontroller is deleted. obj could be an *kapi.ReplicationController, or
// a DeletionFinalStateUnknown marker item.
func (c *DeploymentConfigController) deleteReplicationController(obj interface{}) {
rc, ok := obj.(*kapi.ReplicationController)
// When a delete is dropped, the relist will notice a pod in the store not
// in the list, leading to the insertion of a tombstone object which contains
// the deleted key/value.
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
glog.Errorf("Couldn't get object from tombstone %#v", obj)
return
}
rc, ok = tombstone.Obj.(*kapi.ReplicationController)
if !ok {
glog.Errorf("Tombstone contained object that is not a replication controller %#v", obj)
return
}
}
glog.V(4).Infof("Replication controller %q deleted.", rc.Name)
if dc, err := c.dcStore.GetConfigForController(rc); err == nil && dc != nil {
c.enqueueDeploymentConfig(dc)
}
}
func (c *DeploymentConfigController) enqueueDeploymentConfig(dc *deployapi.DeploymentConfig) {
key, err := kcontroller.KeyFunc(dc)
if err != nil {
glog.Errorf("Couldn't get key for object %#v: %v", dc, err)
return
}
c.queue.Add(key)
}
func (c *DeploymentConfigController) worker() {
for {
if quit := c.work(); quit {
return
}
}
}
func (c *DeploymentConfigController) work() bool {
key, quit := c.queue.Get()
if quit {
return true
}
defer c.queue.Done(key)
dc, err := c.getByKey(key.(string))
if err != nil {
glog.Error(err.Error())
}
if dc == nil {
return false
}
copied, err := deployutil.DeploymentConfigDeepCopy(dc)
if err != nil {
glog.Error(err.Error())
return false
}
err = c.Handle(copied)
c.handleErr(err, key)
return false
}
func (c *DeploymentConfigController) getByKey(key string) (*deployapi.DeploymentConfig, error) {
obj, exists, err := c.dcStore.Indexer.GetByKey(key)
if err != nil {
glog.V(2).Infof("Unable to retrieve deployment config %q from store: %v", key, err)
c.queue.AddRateLimited(key)
return nil, err
}
if !exists {
glog.V(4).Infof("Deployment config %q has been deleted", key)
return nil, nil
}
return obj.(*deployapi.DeploymentConfig), nil
}