@@ -870,24 +870,36 @@ func (r *PostgresReconciler) ensureStandbySecrets(ctx context.Context, instance
870
870
return errors .New ("connectionInfo.secretName not configured" )
871
871
}
872
872
873
- // Check if secrets exist local in SERVICE Cluster
873
+ // Check if secret for standby user exist local in SERVICE Cluster
874
874
localStandbySecretName := pg .PostgresConfigReplicationUsername + "." + instance .ToPeripheralResourceName () + ".credentials"
875
875
localSecretNamespace := instance .ToPeripheralResourceNamespace ()
876
876
localStandbySecret := & corev1.Secret {}
877
877
r .Log .Info ("checking for local standby secret" , "namespace" , localSecretNamespace , "name" , localStandbySecretName )
878
878
err := r .SvcClient .Get (ctx , types.NamespacedName {Namespace : localSecretNamespace , Name : localStandbySecretName }, localStandbySecret )
879
879
880
880
if err == nil {
881
- r .Log .Info ("local standby secret found, no action needed" )
882
- return nil
881
+ r .Log .Info ("local standby secret found, checking for monitoring secret next" )
882
+ } else if ! apierrors .IsNotFound (err ) {
883
+ // we got an error other than not found, so we cannot continue!
884
+ return fmt .Errorf ("error while fetching local standby secret from service cluster: %w" , err )
883
885
}
884
886
885
- // we got an error other than not found, so we cannot continue!
886
- if ! apierrors .IsNotFound (err ) {
887
- return fmt .Errorf ("error while fetching local stadnby secret from service cluster: %w" , err )
887
+ // Check if secret for monitoring user exist local in SERVICE Cluster
888
+ localMonitoringSecretName := pg .PostgresConfigMonitoringUsername + "." + instance .ToPeripheralResourceName () + ".credentials"
889
+ localSecretNamespace = instance .ToPeripheralResourceNamespace ()
890
+ localStandbySecret = & corev1.Secret {}
891
+ r .Log .Info ("checking for local monitoring secret" , "namespace" , localSecretNamespace , "name" , localMonitoringSecretName )
892
+ err = r .SvcClient .Get (ctx , types.NamespacedName {Namespace : localSecretNamespace , Name : localMonitoringSecretName }, localStandbySecret )
893
+
894
+ if err == nil {
895
+ r .Log .Info ("local monitoring secret found, no action needed" )
896
+ return nil
897
+ } else if ! apierrors .IsNotFound (err ) {
898
+ // we got an error other than not found, so we cannot continue!
899
+ return fmt .Errorf ("error while fetching local monitoring secret from service cluster: %w" , err )
888
900
}
889
901
890
- r .Log .Info ("no local standby secret found, continuing to create one " )
902
+ r .Log .Info ("not all expected local secrets found, continuing to create them " )
891
903
892
904
remoteSecretNamespacedName := types.NamespacedName {
893
905
Namespace : instance .ObjectMeta .Namespace ,
@@ -968,6 +980,10 @@ func (r *PostgresReconciler) copySecrets(ctx context.Context, sourceSecret types
968
980
}
969
981
970
982
if err := r .SvcClient .Create (ctx , postgresSecret ); err != nil {
983
+ if apierrors .IsAlreadyExists (err ) {
984
+ r .Log .Info ("local postgres secret already exists, skipping" , "name" , currentSecretName )
985
+ continue
986
+ }
971
987
return fmt .Errorf ("error while creating local secrets in service cluster: %w" , err )
972
988
}
973
989
}
@@ -1576,42 +1592,45 @@ func (r *PostgresReconciler) ensureInitDBJob(ctx context.Context, instance *pg.P
1576
1592
if err := r .SvcClient .Get (ctx , ns , cm ); err == nil {
1577
1593
// configmap already exists, nothing to do here
1578
1594
r .Log .Info ("initdb ConfigMap already exists" )
1579
- // return nil // TODO return or update?
1580
- } else {
1581
- cm .Name = ns .Name
1582
- cm .Namespace = ns .Namespace
1583
- cm .Data = map [string ]string {}
1584
-
1585
- // only execute SQL when encountering a **new** database, not for standbies or clones
1586
- if instance .Spec .PostgresConnection == nil && instance .Spec .PostgresRestore == nil {
1587
- // TODO fetch central init job and copy its contents
1588
-
1589
- // try to fetch the global initjjob configmap
1590
- cns := types.NamespacedName {
1591
- Namespace : r .PostgresletNamespace ,
1592
- Name : r .InitDBJobConfigMapName ,
1593
- }
1594
- globalInitjobCM := & corev1.ConfigMap {}
1595
- if err := r .SvcClient .Get (ctx , cns , globalInitjobCM ); err == nil {
1596
- cm .Data = globalInitjobCM .Data
1597
- } else {
1598
- r .Log .Error (err , "global initdb ConfigMap could not be loaded, using dummy data" )
1599
- // fall back to dummy data
1600
- cm .Data ["initdb.sql" ] = initDBSQLDummy
1601
- }
1595
+ return nil
1596
+ }
1602
1597
1598
+ // create initDB configmap
1599
+ cm .Name = ns .Name
1600
+ cm .Namespace = ns .Namespace
1601
+ cm .Data = map [string ]string {}
1602
+
1603
+ // only execute SQL when encountering a **new** database, not for standbies or clones
1604
+ if instance .IsReplicationPrimary () && instance .Spec .PostgresRestore == nil {
1605
+ // try to fetch the global initjob configmap
1606
+ cns := types.NamespacedName {
1607
+ Namespace : r .PostgresletNamespace ,
1608
+ Name : r .InitDBJobConfigMapName ,
1609
+ }
1610
+ globalInitjobCM := & corev1.ConfigMap {}
1611
+ if err := r .SvcClient .Get (ctx , cns , globalInitjobCM ); err == nil {
1612
+ cm .Data = globalInitjobCM .Data
1603
1613
} else {
1604
- // use dummy job for standbies and clones
1614
+ r .Log .Error (err , "global initdb ConfigMap could not be loaded, using dummy data" )
1615
+ // fall back to dummy data
1605
1616
cm .Data ["initdb.sql" ] = initDBSQLDummy
1606
1617
}
1618
+ } else {
1619
+ // use dummy job for standbies and clones
1620
+ cm .Data ["initdb.sql" ] = initDBSQLDummy
1621
+ }
1607
1622
1608
- if err := r .SvcClient .Create (ctx , cm ); err != nil {
1609
- return fmt .Errorf ("error while creating the new initdb ConfigMap: %w" , err )
1610
- }
1623
+ if err := r .SvcClient .Create (ctx , cm ); err != nil {
1624
+ return fmt .Errorf ("error while creating the new initdb ConfigMap: %w" , err )
1625
+ }
1626
+ r .Log .Info ("new initdb ConfigMap created" )
1611
1627
1612
- r .Log .Info ("new initdb ConfigMap created" )
1628
+ if instance .IsReplicationTarget () || instance .Spec .PostgresRestore != nil {
1629
+ r .Log .Info ("initdb job not required" )
1630
+ return nil
1613
1631
}
1614
1632
1633
+ // create initDB job
1615
1634
j := & batchv1.Job {}
1616
1635
1617
1636
if err := r .SvcClient .Get (ctx , ns , j ); err == nil {
@@ -1690,6 +1709,7 @@ func (r *PostgresReconciler) ensureInitDBJob(ctx context.Context, instance *pg.P
1690
1709
if err := r .SvcClient .Create (ctx , j ); err != nil {
1691
1710
return fmt .Errorf ("error while creating the new initdb Job: %w" , err )
1692
1711
}
1712
+ r .Log .Info ("new initdb Job created" )
1693
1713
1694
1714
return nil
1695
1715
}
0 commit comments