Skip to content

Commit 0a70473

Browse files
committed
Add RecoveryWindow to status and populate with backup info
Extend RepositoryStatus with a new `recoveryWindow` field. Generate CRD schema for RecoveryWindow, including first/last backup details. Update Backup() function to compute first/last backups and update repository status when taking a backup.
1 parent 37fc88d commit 0a70473

9 files changed

Lines changed: 367 additions & 49 deletions

File tree

api/v1/repository_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ type RepositoryStatus struct {
5959
// +listMapKey=type
6060
// +optional
6161
Conditions []metav1.Condition `json:"conditions,omitempty"`
62+
63+
// +optional
64+
RecoveryWindow pgbackrestapi.RecoveryWindow `json:"recoveryWindow"`
6265
}
6366

6467
// +kubebuilder:object:root=true

api/v1/zz_generated.deepcopy.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/pgbackrest.dalibo.com_repositories.yaml

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,108 @@ spec:
272272
x-kubernetes-list-map-keys:
273273
- type
274274
x-kubernetes-list-type: map
275+
recoveryWindow:
276+
properties:
277+
firstBackup:
278+
properties:
279+
archive:
280+
properties:
281+
start:
282+
type: string
283+
stop:
284+
type: string
285+
required:
286+
- start
287+
- stop
288+
type: object
289+
label:
290+
type: string
291+
lsn:
292+
properties:
293+
start:
294+
type: string
295+
stop:
296+
type: string
297+
required:
298+
- start
299+
- stop
300+
type: object
301+
prior:
302+
type: string
303+
timestamp:
304+
properties:
305+
start:
306+
format: int64
307+
type: integer
308+
stop:
309+
format: int64
310+
type: integer
311+
required:
312+
- start
313+
- stop
314+
type: object
315+
type:
316+
type: string
317+
required:
318+
- archive
319+
- label
320+
- lsn
321+
- prior
322+
- timestamp
323+
- type
324+
type: object
325+
lastBackup:
326+
properties:
327+
archive:
328+
properties:
329+
start:
330+
type: string
331+
stop:
332+
type: string
333+
required:
334+
- start
335+
- stop
336+
type: object
337+
label:
338+
type: string
339+
lsn:
340+
properties:
341+
start:
342+
type: string
343+
stop:
344+
type: string
345+
required:
346+
- start
347+
- stop
348+
type: object
349+
prior:
350+
type: string
351+
timestamp:
352+
properties:
353+
start:
354+
format: int64
355+
type: integer
356+
stop:
357+
format: int64
358+
type: integer
359+
required:
360+
- start
361+
- stop
362+
type: object
363+
type:
364+
type: string
365+
required:
366+
- archive
367+
- label
368+
- lsn
369+
- prior
370+
- timestamp
371+
- type
372+
type: object
373+
required:
374+
- firstBackup
375+
- lastBackup
376+
type: object
275377
type: object
276378
required:
277379
- spec

internal/instance/backup.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import (
1010
"strconv"
1111

1212
"github.com/cloudnative-pg/cnpg-i/pkg/backup"
13+
apipgbackrestv1 "github.com/dalibo/cnpg-i-pgbackrest/api/v1"
1314
"github.com/dalibo/cnpg-i-pgbackrest/internal/metadata"
1415
"github.com/dalibo/cnpg-i-pgbackrest/internal/operator"
1516
"github.com/dalibo/cnpg-i-pgbackrest/internal/pgbackrest"
1617
apipgbackrest "github.com/dalibo/cnpg-i-pgbackrest/internal/pgbackrest/api"
18+
"k8s.io/client-go/util/retry"
1719
"sigs.k8s.io/controller-runtime/pkg/client"
1820
"sigs.k8s.io/controller-runtime/pkg/log"
1921
)
@@ -51,6 +53,20 @@ func getEnvVarBackupRepoDest(repo apipgbackrest.Repository, selectedRepo string)
5153
return fmt.Sprintf("PGBACKREST_REPO=%d", sRepo), nil
5254
}
5355

56+
func updateBackupInfo(
57+
ctx context.Context,
58+
c client.Client,
59+
repo *apipgbackrestv1.Repository,
60+
firstBackup apipgbackrest.BackupInfo,
61+
lastBackup apipgbackrest.BackupInfo,
62+
) error {
63+
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
64+
repo.Status.RecoveryWindow.FirstBackup = firstBackup
65+
repo.Status.RecoveryWindow.LastBackup = lastBackup
66+
return c.Status().Update(ctx, repo)
67+
})
68+
}
69+
5470
func (b BackupServiceImplementation) Backup(
5571
ctx context.Context,
5672
request *backup.BackupRequest,
@@ -86,11 +102,13 @@ func (b BackupServiceImplementation) Backup(
86102
contextLogger.Error(err, "can't backup")
87103
return nil, err
88104
}
89-
backups, err := pgb.GetBackupInfo()
105+
backupsList, err := pgb.GetBackupInfo()
90106
if err != nil {
91107
return nil, err
92108
}
93-
lastBackup := pgbackrest.LatestBackup(backups)
109+
lastBackup := pgbackrest.LatestBackup(backupsList)
110+
firstBackup := pgbackrest.FirstBackup(backupsList)
111+
updateBackupInfo(ctx, b.Client, repo, *firstBackup, *lastBackup)
94112
contextLogger.Info("Backup done!")
95113
return &backup.BackupResult{
96114
BackupName: lastBackup.Label,

internal/pgbackrest/api/repo.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,39 @@ import (
1212
"github.com/dalibo/cnpg-i-pgbackrest/internal/utils"
1313
)
1414

15+
type Timestamp struct {
16+
Start int64 `json:"start"`
17+
Stop int64 `json:"stop"`
18+
}
19+
20+
type BackupData struct {
21+
Backup []BackupInfo `json:"backup"`
22+
}
23+
24+
type Lsn struct {
25+
Start string `json:"start"`
26+
Stop string `json:"stop"`
27+
}
28+
29+
type Archive struct {
30+
Start string `json:"start"`
31+
Stop string `json:"stop"`
32+
}
33+
34+
type BackupInfo struct {
35+
Archive Archive `json:"archive"`
36+
Label string `json:"label"`
37+
Lsn Lsn `json:"lsn"`
38+
Prior string `json:"prior"`
39+
Timestamp Timestamp `json:"timestamp"`
40+
Type string `json:"type"`
41+
}
42+
43+
type RecoveryWindow struct {
44+
FirstBackup BackupInfo `json:"firstBackup"`
45+
LastBackup BackupInfo `json:"lastBackup"`
46+
}
47+
1548
// Define retention strategy for a repository
1649
type Retention struct {
1750
// Number of backups worth of continuous WAL to retain.

internal/pgbackrest/api/zz_generated.deepcopy.go

Lines changed: 100 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)