@@ -40,12 +40,23 @@ func (c *Controller) clusterResync(stopCh <-chan struct{}, wg *sync.WaitGroup) {
4040
4141// clusterListFunc obtains a list of all PostgreSQL clusters
4242func (c * Controller ) listClusters (options metav1.ListOptions ) (* acidv1.PostgresqlList , error ) {
43+ var pgList acidv1.PostgresqlList
44+
4345 // TODO: use the SharedInformer cache instead of quering Kubernetes API directly.
4446 list , err := c .KubeClient .AcidV1ClientSet .AcidV1 ().Postgresqls (c .opConfig .WatchedNamespace ).List (options )
4547 if err != nil {
4648 c .logger .Errorf ("could not list postgresql objects: %v" , err )
4749 }
48- return list , err
50+ if c .controllerID != "" {
51+ c .logger .Debugf ("watch only clusters with controllerID %q" , c .controllerID )
52+ }
53+ for _ , pg := range list .Items {
54+ if pg .Error == "" && c .hasOwnership (& pg ) {
55+ pgList .Items = append (pgList .Items , pg )
56+ }
57+ }
58+
59+ return & pgList , err
4960}
5061
5162// clusterListAndSync lists all manifests and decides whether to run the sync or repair.
@@ -455,41 +466,48 @@ func (c *Controller) queueClusterEvent(informerOldSpec, informerNewSpec *acidv1.
455466}
456467
457468func (c * Controller ) postgresqlAdd (obj interface {}) {
458- pg , ok := obj .( * acidv1. Postgresql )
459- if ! ok {
460- c . logger . Errorf ( "could not cast to postgresql spec" )
461- return
469+ pg := c . postgresqlCheck ( obj )
470+ if pg != nil {
471+ // We will not get multiple Add events for the same cluster
472+ c . queueClusterEvent ( nil , pg , EventAdd )
462473 }
463474
464- // We will not get multiple Add events for the same cluster
465- c .queueClusterEvent (nil , pg , EventAdd )
475+ return
466476}
467477
468478func (c * Controller ) postgresqlUpdate (prev , cur interface {}) {
469- pgOld , ok := prev .(* acidv1.Postgresql )
470- if ! ok {
471- c .logger .Errorf ("could not cast to postgresql spec" )
472- }
473- pgNew , ok := cur .(* acidv1.Postgresql )
474- if ! ok {
475- c .logger .Errorf ("could not cast to postgresql spec" )
476- }
477- // Avoid the inifinite recursion for status updates
478- if reflect .DeepEqual (pgOld .Spec , pgNew .Spec ) {
479- return
479+ pgOld := c .postgresqlCheck (prev )
480+ pgNew := c .postgresqlCheck (cur )
481+ if pgOld != nil && pgNew != nil {
482+ // Avoid the inifinite recursion for status updates
483+ if reflect .DeepEqual (pgOld .Spec , pgNew .Spec ) {
484+ return
485+ }
486+ c .queueClusterEvent (pgOld , pgNew , EventUpdate )
480487 }
481488
482- c . queueClusterEvent ( pgOld , pgNew , EventUpdate )
489+ return
483490}
484491
485492func (c * Controller ) postgresqlDelete (obj interface {}) {
493+ pg := c .postgresqlCheck (obj )
494+ if pg != nil {
495+ c .queueClusterEvent (pg , nil , EventDelete )
496+ }
497+
498+ return
499+ }
500+
501+ func (c * Controller ) postgresqlCheck (obj interface {}) * acidv1.Postgresql {
486502 pg , ok := obj .(* acidv1.Postgresql )
487503 if ! ok {
488504 c .logger .Errorf ("could not cast to postgresql spec" )
489- return
505+ return nil
490506 }
491-
492- c .queueClusterEvent (pg , nil , EventDelete )
507+ if ! c .hasOwnership (pg ) {
508+ return nil
509+ }
510+ return pg
493511}
494512
495513/*
0 commit comments