@@ -192,6 +192,11 @@ func (c *Cluster) Sync(newSpec *cpov1.Postgresql) error {
192192 return fmt .Errorf ("error refreshing restore configmap: %v" , err )
193193 }
194194
195+ // sync monitoring
196+ if err = c .syncMonitoringSecret (& oldSpec , newSpec ); err != nil {
197+ return fmt .Errorf ("could not sync monitoring: %v" , err )
198+ }
199+
195200 if err = c .initUsers (); err != nil {
196201 err = fmt .Errorf ("could not init users: %v" , err )
197202 return err
@@ -282,11 +287,6 @@ func (c *Cluster) Sync(newSpec *cpov1.Postgresql) error {
282287 return fmt .Errorf ("could not sync connection pooler: %v" , err )
283288 }
284289
285- // sync monitoring
286- if err = c .syncMonitoringSecret (& oldSpec , newSpec ); err != nil {
287- return fmt .Errorf ("could not sync monitoring: %v" , err )
288- }
289-
290290 if len (c .Spec .Streams ) > 0 {
291291 c .logger .Debug ("syncing streams" )
292292 if err = c .syncStreams (); err != nil {
@@ -1266,6 +1266,9 @@ DBUSERS:
12661266 continue DBUSERS
12671267 }
12681268 }
1269+ if dbUser .Name == monitorUsername && dbUser .Deleted {
1270+ delete (dbUsers , dbUser .Name )
1271+ }
12691272
12701273 // update pgUsers where a deleted role was found
12711274 // so that they are skipped in ProduceSyncRequests
@@ -1693,7 +1696,7 @@ func (c *Cluster) createMonitoringSecret() error {
16931696 },
16941697 Type : v1 .SecretTypeOpaque ,
16951698 Data : map [string ][]byte {
1696- "username" : []byte (c . getMonitoringSecretName () ),
1699+ "username" : []byte (monitorUsername ),
16971700 "password" : []byte (fmt .Sprintf ("%x" , generatedKey )),
16981701 },
16991702 }
@@ -1734,8 +1737,6 @@ func (c *Cluster) deleteMonitoringSecret() (err error) {
17341737// 1. Update sts to in/exclude the exporter contianer
17351738// 2. Add/Delete the respective user
17361739// 3. Add/Delete the respective secret
1737- // Point 1 and 2 are taken care in Update func, so we only need to take care
1738- // Point 3 here.
17391740func (c * Cluster ) syncMonitoringSecret (oldSpec , newSpec * cpov1.Postgresql ) error {
17401741 c .logger .Info ("syncing Monitoring secret" )
17411742 c .setProcessName ("syncing Monitoring secret" )
@@ -1744,12 +1745,28 @@ func (c *Cluster) syncMonitoringSecret(oldSpec, newSpec *cpov1.Postgresql) error
17441745 // Create monitoring secret
17451746 if err := c .createMonitoringSecret (); err != nil {
17461747 return fmt .Errorf ("could not create the monitoring secret: %v" , err )
1748+ } else {
1749+ flags := []string {constants .RoleFlagLogin }
1750+ monitorUser := map [string ]spec.PgUser {
1751+ monitorUsername : {
1752+ Origin : spec .RoleOriginInfrastructure ,
1753+ Name : monitorUsername ,
1754+ Namespace : c .Namespace ,
1755+ Flags : flags ,
1756+ },
1757+ }
1758+ c .pgUsers [monitorUsername ] = monitorUser [monitorUsername ]
17471759 }
17481760 c .logger .Info ("monitoring secret was successfully created" )
17491761 } else if newSpec .Spec .Monitoring == nil && oldSpec .Spec .Monitoring != nil {
17501762 // Delete the monitoring secret
17511763 if err := c .deleteMonitoringSecret (); err != nil {
17521764 return fmt .Errorf ("could not delete the monitoring secret: %v" , err )
1765+ } else {
1766+ // Delete the monitoring user
1767+ monitorUser := c .pgUsers [monitorUsername ]
1768+ monitorUser .Deleted = true
1769+ c .pgUsers [monitorUsername ] = monitorUser
17531770 }
17541771 c .logger .Info ("monitoring secret was successfully deleted" )
17551772 }
0 commit comments