mirror of
https://github.com/zalando/postgres-operator.git
synced 2026-09-30 21:50:01 +02:00
Use queues for the pod events (#30)
This commit is contained in:
+19
-16
@@ -62,10 +62,11 @@ func (c *Controller) podAdd(obj interface{}) {
|
||||
}
|
||||
|
||||
podEvent := spec.PodEvent{
|
||||
ClusterName: c.PodClusterName(pod),
|
||||
PodName: util.NameFromMeta(pod.ObjectMeta),
|
||||
CurPod: pod,
|
||||
EventType: spec.EventAdd,
|
||||
ClusterName: c.PodClusterName(pod),
|
||||
PodName: util.NameFromMeta(pod.ObjectMeta),
|
||||
CurPod: pod,
|
||||
EventType: spec.EventAdd,
|
||||
ResourceVersion: pod.ResourceVersion,
|
||||
}
|
||||
|
||||
c.podCh <- podEvent
|
||||
@@ -83,11 +84,12 @@ func (c *Controller) podUpdate(prev, cur interface{}) {
|
||||
}
|
||||
|
||||
podEvent := spec.PodEvent{
|
||||
ClusterName: c.PodClusterName(curPod),
|
||||
PodName: util.NameFromMeta(curPod.ObjectMeta),
|
||||
PrevPod: prevPod,
|
||||
CurPod: curPod,
|
||||
EventType: spec.EventUpdate,
|
||||
ClusterName: c.PodClusterName(curPod),
|
||||
PodName: util.NameFromMeta(curPod.ObjectMeta),
|
||||
PrevPod: prevPod,
|
||||
CurPod: curPod,
|
||||
EventType: spec.EventUpdate,
|
||||
ResourceVersion: curPod.ResourceVersion,
|
||||
}
|
||||
|
||||
c.podCh <- podEvent
|
||||
@@ -100,27 +102,28 @@ func (c *Controller) podDelete(obj interface{}) {
|
||||
}
|
||||
|
||||
podEvent := spec.PodEvent{
|
||||
ClusterName: c.PodClusterName(pod),
|
||||
PodName: util.NameFromMeta(pod.ObjectMeta),
|
||||
CurPod: pod,
|
||||
EventType: spec.EventDelete,
|
||||
ClusterName: c.PodClusterName(pod),
|
||||
PodName: util.NameFromMeta(pod.ObjectMeta),
|
||||
CurPod: pod,
|
||||
EventType: spec.EventDelete,
|
||||
ResourceVersion: pod.ResourceVersion,
|
||||
}
|
||||
|
||||
c.podCh <- podEvent
|
||||
}
|
||||
|
||||
func (c *Controller) podEventsDispatcher(stopCh <-chan struct{}) {
|
||||
c.logger.Infof("Watching all pod events")
|
||||
c.logger.Debugln("Watching all pod events")
|
||||
for {
|
||||
select {
|
||||
case event := <-c.podCh:
|
||||
c.clustersMu.RLock()
|
||||
subscriber, ok := c.clusters[event.ClusterName]
|
||||
cluster, ok := c.clusters[event.ClusterName]
|
||||
c.clustersMu.RUnlock()
|
||||
|
||||
if ok {
|
||||
c.logger.Debugf("Sending %s event of pod '%s' to the '%s' cluster channel", event.EventType, event.PodName, event.ClusterName)
|
||||
go subscriber.ReceivePodEvent(event)
|
||||
cluster.ReceivePodEvent(event)
|
||||
}
|
||||
case <-stopCh:
|
||||
return
|
||||
|
||||
@@ -91,7 +91,6 @@ func (c *Controller) processEvent(obj interface{}) error {
|
||||
|
||||
c.clustersMu.RLock()
|
||||
cl, clusterFound := c.clusters[clusterName]
|
||||
stopCh := c.stopChs[clusterName]
|
||||
c.clustersMu.RUnlock()
|
||||
|
||||
switch event.EventType {
|
||||
@@ -105,13 +104,14 @@ func (c *Controller) processEvent(obj interface{}) error {
|
||||
|
||||
stopCh := make(chan struct{})
|
||||
cl = cluster.New(c.makeClusterConfig(), *event.NewSpec, logger)
|
||||
cl.Run(stopCh)
|
||||
|
||||
c.clustersMu.Lock()
|
||||
c.clusters[clusterName] = cl
|
||||
c.stopChs[clusterName] = stopCh
|
||||
c.clustersMu.Unlock()
|
||||
|
||||
if err := cl.Create(stopCh); err != nil {
|
||||
if err := cl.Create(); err != nil {
|
||||
cl.Error = fmt.Errorf("could not create cluster: %v", err)
|
||||
logger.Errorf("%v", cl.Error)
|
||||
|
||||
@@ -158,8 +158,9 @@ func (c *Controller) processEvent(obj interface{}) error {
|
||||
|
||||
// no race condition because a cluster is always processed by single worker
|
||||
if !clusterFound {
|
||||
stopCh := make(chan struct{})
|
||||
cl = cluster.New(c.makeClusterConfig(), *event.NewSpec, logger)
|
||||
stopCh = make(chan struct{})
|
||||
cl.Run(stopCh)
|
||||
|
||||
c.clustersMu.Lock()
|
||||
c.clusters[clusterName] = cl
|
||||
@@ -167,7 +168,7 @@ func (c *Controller) processEvent(obj interface{}) error {
|
||||
c.clustersMu.Unlock()
|
||||
}
|
||||
|
||||
if err := cl.Sync(stopCh); err != nil {
|
||||
if err := cl.Sync(); err != nil {
|
||||
cl.Error = fmt.Errorf("could not sync cluster '%s': %s", clusterName, err)
|
||||
logger.Errorf("%v", cl)
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user