forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
factory.go
226 lines (191 loc) · 7.02 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
package deploymentconfig
import (
"fmt"
"time"
"github.com/golang/glog"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
kcoreinformers "k8s.io/client-go/informers/core/v1"
kclientset "k8s.io/client-go/kubernetes"
kv1core "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
"k8s.io/kubernetes/pkg/api/legacyscheme"
kcontroller "k8s.io/kubernetes/pkg/controller"
appsv1 "github.com/openshift/api/apps/v1"
appsv1client "github.com/openshift/client-go/apps/clientset/versioned"
appsv1informer "github.com/openshift/client-go/apps/informers/externalversions/apps/v1"
metrics "github.com/openshift/origin/pkg/apps/metrics/prometheus"
)
// NewDeploymentConfigController creates a new DeploymentConfigController.
func NewDeploymentConfigController(
dcInformer appsv1informer.DeploymentConfigInformer,
rcInformer kcoreinformers.ReplicationControllerInformer,
appsClientset appsv1client.Interface,
kubeClientset kclientset.Interface,
) *DeploymentConfigController {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(glog.Infof)
eventBroadcaster.StartRecordingToSink(&kv1core.EventSinkImpl{Interface: kubeClientset.CoreV1().Events("")})
recorder := eventBroadcaster.NewRecorder(legacyscheme.Scheme, v1.EventSource{Component: "deploymentconfig-controller"})
c := &DeploymentConfigController{
appsClient: appsClientset.AppsV1(),
kubeClient: kubeClientset.CoreV1(),
queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
rcLister: rcInformer.Lister(),
rcListerSynced: rcInformer.Informer().HasSynced,
rcControl: RealRCControl{
KubeClient: kubeClientset,
Recorder: recorder,
},
recorder: recorder,
}
c.dcLister = dcInformer.Lister()
dcInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: c.addDeploymentConfig,
UpdateFunc: c.updateDeploymentConfig,
DeleteFunc: c.deleteDeploymentConfig,
})
c.dcStoreSynced = dcInformer.Informer().HasSynced
c.dcIndex = dcInformer.Informer().GetIndexer()
rcInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: c.updateReplicationController,
DeleteFunc: c.deleteReplicationController,
})
return c
}
// Run begins watching and syncing.
func (c *DeploymentConfigController) Run(workers int, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
defer c.queue.ShutDown()
glog.Infof("Starting deploymentconfig controller")
// Wait for the rc and dc stores to sync before starting any work in this controller.
if !cache.WaitForCacheSync(stopCh, c.dcStoreSynced, c.rcListerSynced) {
return
}
glog.Info("deploymentconfig controller caches are synced. Starting workers.")
for i := 0; i < workers; i++ {
go wait.Until(c.worker, time.Second, stopCh)
}
metrics.InitializeMetricsCollector(c.rcLister)
<-stopCh
glog.Infof("Shutting down deploymentconfig controller")
}
func (c *DeploymentConfigController) addDeploymentConfig(obj interface{}) {
dc := obj.(*appsv1.DeploymentConfig)
glog.V(4).Infof("Adding deployment config %s/%s", dc.Namespace, dc.Name)
c.enqueueDeploymentConfig(dc)
}
func (c *DeploymentConfigController) updateDeploymentConfig(old, cur interface{}) {
newDc := cur.(*appsv1.DeploymentConfig)
oldDc := old.(*appsv1.DeploymentConfig)
glog.V(4).Infof("Updating deployment config %s/%s", oldDc.Namespace, oldDc.Name)
c.enqueueDeploymentConfig(newDc)
}
func (c *DeploymentConfigController) deleteDeploymentConfig(obj interface{}) {
dc, ok := obj.(*appsv1.DeploymentConfig)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
utilruntime.HandleError(fmt.Errorf("couldn't get object from tombstone %+v", obj))
return
}
dc, ok = tombstone.Obj.(*appsv1.DeploymentConfig)
if !ok {
utilruntime.HandleError(fmt.Errorf("tombstone contained object that is not a deployment config: %+v", obj))
return
}
}
glog.V(4).Infof("Deleting deployment config %s/%s", dc.Namespace, dc.Name)
c.enqueueDeploymentConfig(dc)
}
func (c *DeploymentConfigController) getConfigForController(rc *v1.ReplicationController) (*appsv1.DeploymentConfig, error) {
dcName := rc.Annotations[appsv1.DeploymentConfigAnnotation]
obj, exists, err := c.dcIndex.GetByKey(rc.Namespace + "/" + dcName)
if err != nil {
return nil, err
}
if !exists {
return nil, errors.NewNotFound(schema.GroupResource{Group: appsv1.GroupName, Resource: "deploymentconfig"}, dcName)
}
return obj.(*appsv1.DeploymentConfig), nil
}
// updateReplicationController figures out which deploymentconfig is managing this replication
// controller and requeues the deployment config.
func (c *DeploymentConfigController) updateReplicationController(old, cur interface{}) {
curRC := cur.(*v1.ReplicationController)
oldRC := old.(*v1.ReplicationController)
// We can safely ignore periodic re-lists on RCs as we react to periodic re-lists of DCs
if curRC.ResourceVersion == oldRC.ResourceVersion {
return
}
if dc, err := c.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 *v1.ReplicationController, or
// a DeletionFinalStateUnknown marker item.
func (c *DeploymentConfigController) deleteReplicationController(obj interface{}) {
rc, ok := obj.(*v1.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 {
utilruntime.HandleError(fmt.Errorf("couldn't get object from tombstone %#v", obj))
return
}
rc, ok = tombstone.Obj.(*v1.ReplicationController)
if !ok {
utilruntime.HandleError(fmt.Errorf("tombstone contained object that is not a replication controller %#v", obj))
return
}
}
if dc, err := c.getConfigForController(rc); err == nil && dc != nil {
c.enqueueDeploymentConfig(dc)
}
}
func (c *DeploymentConfigController) enqueueDeploymentConfig(dc *appsv1.DeploymentConfig) {
key, err := kcontroller.KeyFunc(dc)
if err != nil {
utilruntime.HandleError(fmt.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)
namespace, name, err := cache.SplitMetaNamespaceKey(key.(string))
if err != nil {
utilruntime.HandleError(err)
return false
}
dc, err := c.dcLister.DeploymentConfigs(namespace).Get(name)
if errors.IsNotFound(err) {
return false
}
if err != nil {
utilruntime.HandleError(err)
return false
}
err = c.Handle(dc)
c.handleErr(err, key)
return false
}