44 "context"
55 "fmt"
66 "strings"
7+ "time"
78
89 "github.com/r3labs/diff"
910 "github.com/sirupsen/logrus"
@@ -20,6 +21,7 @@ import (
2021 "github.com/zalando/postgres-operator/pkg/util/config"
2122 "github.com/zalando/postgres-operator/pkg/util/constants"
2223 "github.com/zalando/postgres-operator/pkg/util/k8sutil"
24+ "github.com/zalando/postgres-operator/pkg/util/retryutil"
2325)
2426
2527// ConnectionPoolerObjects K8s objects that are belong to connection pooler
@@ -73,27 +75,36 @@ func needReplicaConnectionPoolerWorker(spec *acidv1.PostgresSpec) bool {
7375 * spec .EnableReplicaConnectionPooler
7476}
7577
78+ // when listing pooler k8s objects
79+ func (c * Cluster ) poolerLabelsSet (addExtraLabels bool ) labels.Set {
80+ poolerLabels := c .labelsSet (addExtraLabels )
81+
82+ // TODO should be config values
83+ poolerLabels ["application" ] = "db-connection-pooler"
84+
85+ return poolerLabels
86+ }
87+
7688// Return connection pooler labels selector, which should from one point of view
7789// inherit most of the labels from the cluster itself, but at the same time
7890// have e.g. different `application` label, so that recreatePod operation will
7991// not interfere with it (it lists all the pods via labels, and if there would
8092// be no difference, it will recreate also pooler pods).
8193func (c * Cluster ) connectionPoolerLabels (role PostgresRole , addExtraLabels bool ) * metav1.LabelSelector {
82- poolerLabels := c .labelsSet (addExtraLabels )
94+ poolerLabelsSet := c .poolerLabelsSet (addExtraLabels )
8395
8496 // TODO should be config values
85- poolerLabels ["application" ] = "db-connection-pooler"
86- poolerLabels ["connection-pooler" ] = c .connectionPoolerName (role )
97+ poolerLabelsSet ["connection-pooler" ] = c .connectionPoolerName (role )
8798
8899 if addExtraLabels {
89100 extraLabels := map [string ]string {}
90101 extraLabels [c .OpConfig .PodRoleLabel ] = string (role )
91102
92- poolerLabels = labels .Merge (poolerLabels , extraLabels )
103+ poolerLabelsSet = labels .Merge (poolerLabelsSet , extraLabels )
93104 }
94105
95106 return & metav1.LabelSelector {
96- MatchLabels : poolerLabels ,
107+ MatchLabels : poolerLabelsSet ,
97108 MatchExpressions : nil ,
98109 }
99110}
@@ -442,6 +453,14 @@ func (c *Cluster) shouldCreateLoadBalancerForPoolerService(role PostgresRole, sp
442453 }
443454}
444455
456+ func (c * Cluster ) listPoolerPods (listOptions metav1.ListOptions ) ([]v1.Pod , error ) {
457+ pods , err := c .KubeClient .Pods (c .Namespace ).List (context .TODO (), listOptions )
458+ if err != nil {
459+ return nil , fmt .Errorf ("could not get list of pooler pods: %v" , err )
460+ }
461+ return pods .Items , nil
462+ }
463+
445464//delete connection pooler
446465func (c * Cluster ) deleteConnectionPooler (role PostgresRole ) (err error ) {
447466 c .logger .Infof ("deleting connection pooler spilo-role=%s" , role )
@@ -820,6 +839,7 @@ func (c *Cluster) syncConnectionPoolerWorker(oldSpec, newSpec *acidv1.Postgresql
820839 var (
821840 deployment * appsv1.Deployment
822841 newDeployment * appsv1.Deployment
842+ pods []v1.Pod
823843 service * v1.Service
824844 newService * v1.Service
825845 err error
@@ -909,6 +929,34 @@ func (c *Cluster) syncConnectionPoolerWorker(oldSpec, newSpec *acidv1.Postgresql
909929 c .ConnectionPooler [role ].Deployment = deployment
910930 }
911931
932+ // check if pooler pods must be replaced due to secret update
933+ listOptions := metav1.ListOptions {
934+ LabelSelector : labels .Set (c .connectionPoolerLabels (role , true ).MatchLabels ).String (),
935+ }
936+ pods , err = c .listPoolerPods (listOptions )
937+ if err != nil {
938+ return nil , err
939+ }
940+ for i , pod := range pods {
941+ if c .getRollingUpdateFlagFromPod (& pod ) {
942+ podName := util .NameFromMeta (pods [i ].ObjectMeta )
943+ err := retryutil .Retry (1 * time .Second , 5 * time .Second ,
944+ func () (bool , error ) {
945+ err2 := c .KubeClient .Pods (podName .Namespace ).Delete (
946+ context .TODO (),
947+ podName .Name ,
948+ c .deleteOptions )
949+ if err2 != nil {
950+ return false , err2
951+ }
952+ return true , nil
953+ })
954+ if err != nil {
955+ return nil , fmt .Errorf ("could not delete pooler pod: %v" , err )
956+ }
957+ }
958+ }
959+
912960 if service , err = c .KubeClient .Services (c .Namespace ).Get (context .TODO (), c .connectionPoolerName (role ), metav1.GetOptions {}); err == nil {
913961 c .ConnectionPooler [role ].Service = service
914962 desiredSvc := c .generateConnectionPoolerService (c .ConnectionPooler [role ])
0 commit comments