Skip to content

Commit

Permalink
chore: review
Browse files Browse the repository at this point in the history
Signed-off-by: Armando Ruocco <[email protected]>
  • Loading branch information
armru committed Dec 17, 2024
1 parent 1afb124 commit 78ea5e7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
8 changes: 4 additions & 4 deletions internal/controller/backup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func (r *BackupReconciler) reconcileSnapshotBackup(
}
}

if errCond := conditions.Update(ctx, r.Client, cluster, apiv1.BackupStartingCondition); errCond != nil {
if errCond := conditions.OptimisticLockPatch(ctx, r.Client, cluster, apiv1.BackupStartingCondition); errCond != nil {
contextLogger.Error(errCond, "Error while updating backup condition (backup starting)")
}

Expand All @@ -440,7 +440,7 @@ func (r *BackupReconciler) reconcileSnapshotBackup(
// and un-fence the Pod
contextLogger.Error(err, "while executing snapshot backup")
// Update backup status in cluster conditions
if errCond := conditions.Update(
if errCond := conditions.OptimisticLockPatch(
ctx,
r.Client,
cluster,
Expand All @@ -458,7 +458,7 @@ func (r *BackupReconciler) reconcileSnapshotBackup(
return res, nil
}

if err := conditions.Update(ctx, r.Client, cluster, apiv1.BackupSucceededCondition); err != nil {
if err := conditions.OptimisticLockPatch(ctx, r.Client, cluster, apiv1.BackupSucceededCondition); err != nil {
contextLogger.Error(err, "Can't update the cluster with the completed snapshot backup data")
}

Expand Down Expand Up @@ -638,7 +638,7 @@ func startInstanceManagerBackup(
status.CommandError = stdout

// Update backup status in cluster conditions
if errCond := conditions.Update(ctx, client, cluster, apiv1.BuildClusterBackupFailedCondition(err)); errCond != nil {
if errCond := conditions.OptimisticLockPatch(ctx, client, cluster, apiv1.BuildClusterBackupFailedCondition(err)); errCond != nil {
log.FromContext(ctx).Error(errCond, "Error while updating backup condition (backup failed)")
}
return postgres.PatchBackupStatusAndRetry(ctx, client, backup)
Expand Down
14 changes: 7 additions & 7 deletions pkg/conditions/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ import (
apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
)

// Update will update a particular condition in cluster status.
// OptimisticLockPatch will update a particular condition in cluster status.
// This function may update the conditions in the passed cluster
// with the latest ones that were found from the API server.
func Update(
func OptimisticLockPatch(
ctx context.Context,
c client.Client,
cluster *apiv1.Cluster,
condition ...metav1.Condition,
conditions ...metav1.Condition,
) error {
if cluster == nil || len(condition) == 0 {
if cluster == nil || len(conditions) == 0 {
return nil
}

tx := func(cluster *apiv1.Cluster) bool {
applyConditions := func(cluster *apiv1.Cluster) bool {
changed := false
for _, c := range condition {
for _, c := range conditions {
changed = changed || meta.SetStatusCondition(&cluster.Status.Conditions, c)
}
return changed
Expand All @@ -56,7 +56,7 @@ func Update(
}

updatedCluster := currentCluster.DeepCopy()
if changed := tx(updatedCluster); !changed {
if changed := applyConditions(updatedCluster); !changed {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/management/postgres/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (b *BackupCommand) takeBackup(ctx context.Context) error {

// Update backup status in cluster conditions on startup
if err := b.retryWithRefreshedCluster(ctx, func() error {
return conditions.Update(ctx, b.Client, b.Cluster, apiv1.BackupStartingCondition)
return conditions.OptimisticLockPatch(ctx, b.Client, b.Cluster, apiv1.BackupStartingCondition)
}); err != nil {
b.Log.Error(err, "Error changing backup condition (backup started)")
// We do not terminate here because we could still have a good backup
Expand Down Expand Up @@ -261,7 +261,7 @@ func (b *BackupCommand) takeBackup(ctx context.Context) error {

// Update backup status in cluster conditions on backup completion
if err := b.retryWithRefreshedCluster(ctx, func() error {
return conditions.Update(ctx, b.Client, b.Cluster, apiv1.BackupSucceededCondition)
return conditions.OptimisticLockPatch(ctx, b.Client, b.Cluster, apiv1.BackupSucceededCondition)
}); err != nil {
b.Log.Error(err, "Can't update the cluster with the completed backup data")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/management/postgres/webserver/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func (ws *localWebserverEndpoints) setWALArchiveStatusCondition(w http.ResponseW
return
}

if errCond := conditions.Update(ctx, ws.typedClient, cluster, asr.getContinuousArchivingCondition()); errCond != nil {
if errCond := conditions.OptimisticLockPatch(ctx, ws.typedClient, cluster, asr.getContinuousArchivingCondition()); errCond != nil {
contextLogger.Error(errCond, "Error changing wal archiving condition",
"condition", asr.getContinuousArchivingCondition())
http.Error(
Expand Down
4 changes: 2 additions & 2 deletions pkg/management/postgres/webserver/plugin_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (b *PluginBackupCommand) invokeStart(ctx context.Context) {

// Update backup status in cluster conditions on startup
if err := b.retryWithRefreshedCluster(ctx, func() error {
return conditions.Update(ctx, b.Client, b.Cluster, apiv1.BackupStartingCondition)
return conditions.OptimisticLockPatch(ctx, b.Client, b.Cluster, apiv1.BackupStartingCondition)
}); err != nil {
contextLogger.Error(err, "Error changing backup condition (backup started)")
// We do not terminate here because we could still have a good backup
Expand Down Expand Up @@ -153,7 +153,7 @@ func (b *PluginBackupCommand) invokeStart(ctx context.Context) {

// Update backup status in cluster conditions on backup completion
if err := b.retryWithRefreshedCluster(ctx, func() error {
return conditions.Update(ctx, b.Client, b.Cluster, apiv1.BackupSucceededCondition)
return conditions.OptimisticLockPatch(ctx, b.Client, b.Cluster, apiv1.BackupSucceededCondition)
}); err != nil {
contextLogger.Error(err, "Can't update the cluster with the completed backup data")
}
Expand Down

0 comments on commit 78ea5e7

Please sign in to comment.