Skip to content

Commit 8975dfa

Browse files
committed
chore: minor review for retention.go
Signed-off-by: Armando Ruocco <[email protected]>
1 parent 8077709 commit 8975dfa

File tree

2 files changed

+18
-42
lines changed

2 files changed

+18
-42
lines changed

internal/cnpgi/instance/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func Start(ctx context.Context) error {
7777
Namespace: namespace,
7878
Name: clusterName,
7979
},
80-
PodName: podName,
80+
CurrentPodName: podName,
8181
}); err != nil {
8282
setupLog.Error(err, "unable to policy enforcement runnable")
8383
return err

internal/cnpgi/instance/retention.go

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,10 @@ const defaultRetentionPolicyInterval = time.Minute * 5
3232
// RetentionPolicyRunnable executes the retention policy described
3333
// in the BarmanObjectStore object periodically.
3434
type RetentionPolicyRunnable struct {
35-
Client client.Client
36-
Recorder record.EventRecorder
37-
38-
// ClusterKey are the coordinates at which the cluster is stored
39-
ClusterKey types.NamespacedName
40-
41-
// PodName is the current pod name
42-
PodName string
35+
Client client.Client
36+
Recorder record.EventRecorder
37+
ClusterKey types.NamespacedName
38+
CurrentPodName string
4339
}
4440

4541
// Start enforce the backup retention policies periodically, using the
@@ -61,15 +57,11 @@ func (c *RetentionPolicyRunnable) Start(ctx context.Context) error {
6157

6258
// Wait before running another cycle
6359
t := time.NewTimer(period)
64-
defer func() {
65-
t.Stop()
66-
}()
60+
defer t.Stop()
6761

6862
select {
6963
case <-ctx.Done():
70-
// The context was canceled
7164
return nil
72-
7365
case <-t.C:
7466
}
7567
}
@@ -107,32 +99,30 @@ func (c *RetentionPolicyRunnable) applyRetentionPolicy(
10799
objectStore *barmancloudv1.ObjectStore,
108100
) error {
109101
contextLogger := log.FromContext(ctx)
110-
111102
configuration := config.NewFromCluster(cluster)
112-
113103
retentionPolicy := objectStore.Spec.RetentionPolicy
104+
114105
if len(retentionPolicy) == 0 {
115106
contextLogger.Info("Skipping retention policy enforcement, no retention policy specified")
116107
return nil
117108
}
118-
if cluster.Status.CurrentPrimary != c.PodName {
109+
110+
if cluster.Status.CurrentPrimary != c.CurrentPodName {
119111
contextLogger.Info(
120112
"Skipping retention policy enforcement, not the current primary",
121-
"currentPrimary", cluster.Status.CurrentPrimary, "podName", c.PodName)
113+
"currentPrimary", cluster.Status.CurrentPrimary, "podName", c.CurrentPodName)
122114
return nil
123115
}
124116

125117
contextLogger.Info("Applying backup retention policy",
126118
"retentionPolicy", retentionPolicy)
127119

128-
osEnvironment := os.Environ()
129-
caBundleEnvironment := common.GetRestoreCABundleEnv(&objectStore.Spec.Configuration)
130120
env, err := barmanCredentials.EnvSetBackupCloudCredentials(
131121
ctx,
132122
c.Client,
133123
objectStore.Namespace,
134124
&objectStore.Spec.Configuration,
135-
common.MergeEnv(osEnvironment, caBundleEnvironment))
125+
common.MergeEnv(os.Environ(), common.GetRestoreCABundleEnv(&objectStore.Spec.Configuration)))
136126
if err != nil {
137127
contextLogger.Error(err, "while setting backup cloud credentials")
138128
return err
@@ -182,26 +172,20 @@ func (c *RetentionPolicyRunnable) updateRecoveryWindow(
182172
if t == nil {
183173
return nil
184174
}
185-
186175
return ptr.To(metav1.NewTime(*t))
187176
}
188177

189-
firstRecoverabilityPoint := backupList.GetFirstRecoverabilityPoint()
190-
lastSuccessfulBackupTime := backupList.GetLastSuccessfulBackupTime()
191178
recoveryWindow := barmancloudv1.RecoveryWindow{
192-
FirstRecoverabilityPoint: convertTime(firstRecoverabilityPoint),
193-
LastSuccessfulBackupTime: convertTime(lastSuccessfulBackupTime),
179+
FirstRecoverabilityPoint: convertTime(backupList.GetFirstRecoverabilityPoint()),
180+
LastSuccessfulBackupTime: convertTime(backupList.GetLastSuccessfulBackupTime()),
194181
}
195182

196183
if objectStore.Status.ServerRecoveryWindow == nil {
197184
objectStore.Status.ServerRecoveryWindow = make(map[string]barmancloudv1.RecoveryWindow)
198185
}
199186
objectStore.Status.ServerRecoveryWindow[serverName] = recoveryWindow
200-
if err := c.Client.Status().Update(ctx, objectStore); err != nil {
201-
return err
202-
}
203187

204-
return nil
188+
return c.Client.Status().Update(ctx, objectStore)
205189
}
206190

207191
// deleteBackupsNotInCatalog deletes all Backup objects pointing to the given cluster that are not
@@ -234,8 +218,7 @@ func deleteBackupsNotInCatalog(
234218
// We chose to go with B
235219

236220
backups := cnpgv1.BackupList{}
237-
err := cli.List(ctx, &backups, client.InNamespace(cluster.GetNamespace()))
238-
if err != nil {
221+
if err := cli.List(ctx, &backups, client.InNamespace(cluster.GetNamespace())); err != nil {
239222
return fmt.Errorf("while getting backups: %w", err)
240223
}
241224

@@ -250,8 +233,7 @@ func deleteBackupsNotInCatalog(
250233
// here we could add further checks, e.g. if the backup is not found but would still
251234
// be in the retention policy we could either not delete it or update it is status
252235
if !slices.Contains(backupIDs, backup.Status.BackupID) {
253-
err := cli.Delete(ctx, &backups.Items[id])
254-
if err != nil {
236+
if err := cli.Delete(ctx, &backups.Items[id]); err != nil {
255237
errors = append(errors, fmt.Errorf(
256238
"while deleting backup %s/%s: %w",
257239
backup.Namespace,
@@ -262,24 +244,18 @@ func deleteBackupsNotInCatalog(
262244
}
263245
}
264246

265-
if errors != nil {
247+
if len(errors) > 0 {
266248
return fmt.Errorf("got errors while deleting Backups not in the cluster: %v", errors)
267249
}
268250

269251
return nil
270252
}
271253

272-
// useSameBackupLocation checks whether the given backup was taken using the same configuration as provided
273254
func useSameBackupLocation(backup *cnpgv1.BackupStatus, cluster *cnpgv1.Cluster) bool {
274-
if cluster.Spec.Backup == nil {
275-
return false
276-
}
277-
278-
if backup.Method != cnpgv1.BackupMethodPlugin {
255+
if cluster.Spec.Backup == nil || backup.Method != cnpgv1.BackupMethodPlugin {
279256
return false
280257
}
281258

282259
meta := newBackupResultMetadataFromMap(backup.PluginMetadata)
283-
284260
return meta.clusterUID == string(cluster.UID) && meta.pluginName == metadata.PluginName
285261
}

0 commit comments

Comments
 (0)