Skip to content

Commit d934318

Browse files
eberlepGerrit91
andauthored
Making sure the newly created monitoring user is replicated properly (#557)
* Making sure the newly created monitoring user is replicated properly * Naming fix * Remove duplication by refactoring * Skip secrets that already exists (hence also do not update) * Small refactoring * Prevent recreation of job after it cleaned up itself * Don't create initDBJob for standbies (as the selector in the svc when using etcd doesn't work anyway) * Don't create initDBJob for standbies (as the selector in the svc when using etcd doesn't work anyway) * Revert "Don't create initDBJob for standbies (as the selector in the svc when using etcd doesn't work anyway)" This reverts commit ca16941. * Revert "Don't create initDBJob for standbies (as the selector in the svc when using etcd doesn't work anyway)" This reverts commit 4a9a999. * Don't create initDBJob for standbies (as the selector in the svc when using etcd doesn't work anyway) * Improved logging * Use proper checks * Apply suggestions from code review Let other people fix my typos Co-authored-by: Gerrit <[email protected]> --------- Co-authored-by: Gerrit <[email protected]>
1 parent 40d560a commit d934318

File tree

1 file changed

+55
-35
lines changed

1 file changed

+55
-35
lines changed

controllers/postgres_controller.go

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -870,24 +870,36 @@ func (r *PostgresReconciler) ensureStandbySecrets(ctx context.Context, instance
870870
return errors.New("connectionInfo.secretName not configured")
871871
}
872872

873-
// Check if secrets exist local in SERVICE Cluster
873+
// Check if secret for standby user exist local in SERVICE Cluster
874874
localStandbySecretName := pg.PostgresConfigReplicationUsername + "." + instance.ToPeripheralResourceName() + ".credentials"
875875
localSecretNamespace := instance.ToPeripheralResourceNamespace()
876876
localStandbySecret := &corev1.Secret{}
877877
r.Log.Info("checking for local standby secret", "namespace", localSecretNamespace, "name", localStandbySecretName)
878878
err := r.SvcClient.Get(ctx, types.NamespacedName{Namespace: localSecretNamespace, Name: localStandbySecretName}, localStandbySecret)
879879

880880
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)
883885
}
884886

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)
888900
}
889901

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")
891903

892904
remoteSecretNamespacedName := types.NamespacedName{
893905
Namespace: instance.ObjectMeta.Namespace,
@@ -968,6 +980,10 @@ func (r *PostgresReconciler) copySecrets(ctx context.Context, sourceSecret types
968980
}
969981

970982
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+
}
971987
return fmt.Errorf("error while creating local secrets in service cluster: %w", err)
972988
}
973989
}
@@ -1576,42 +1592,45 @@ func (r *PostgresReconciler) ensureInitDBJob(ctx context.Context, instance *pg.P
15761592
if err := r.SvcClient.Get(ctx, ns, cm); err == nil {
15771593
// configmap already exists, nothing to do here
15781594
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+
}
16021597

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
16031613
} 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
16051616
cm.Data["initdb.sql"] = initDBSQLDummy
16061617
}
1618+
} else {
1619+
// use dummy job for standbies and clones
1620+
cm.Data["initdb.sql"] = initDBSQLDummy
1621+
}
16071622

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")
16111627

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
16131631
}
16141632

1633+
// create initDB job
16151634
j := &batchv1.Job{}
16161635

16171636
if err := r.SvcClient.Get(ctx, ns, j); err == nil {
@@ -1690,6 +1709,7 @@ func (r *PostgresReconciler) ensureInitDBJob(ctx context.Context, instance *pg.P
16901709
if err := r.SvcClient.Create(ctx, j); err != nil {
16911710
return fmt.Errorf("error while creating the new initdb Job: %w", err)
16921711
}
1712+
r.Log.Info("new initdb Job created")
16931713

16941714
return nil
16951715
}

0 commit comments

Comments
 (0)