@@ -10,6 +10,7 @@ import (
10
10
"path"
11
11
"strconv"
12
12
"strings"
13
+ "sync"
13
14
"time"
14
15
"unicode"
15
16
@@ -142,8 +143,10 @@ func (e *EmptyPoolError) Error() string {
142
143
143
144
// Manager describes a filesystem manager for ZFS.
144
145
type Manager struct {
145
- runner runners.Runner
146
- config Config
146
+ runner runners.Runner
147
+ config Config
148
+ mu * sync.Mutex
149
+ snapshots []resources.Snapshot
147
150
}
148
151
149
152
// Config defines configuration for ZFS filesystem manager.
@@ -158,6 +161,7 @@ func NewFSManager(runner runners.Runner, config Config) *Manager {
158
161
m := Manager {
159
162
runner : runner ,
160
163
config : config ,
164
+ mu : & sync.Mutex {},
161
165
}
162
166
163
167
return & m
@@ -307,6 +311,20 @@ func (m *Manager) CreateSnapshot(poolSuffix, dataStateAt string) (string, error)
307
311
}
308
312
}
309
313
314
+ dataStateTime , err := util .ParseCustomTime (strings .TrimSuffix (dataStateAt , m .config .PreSnapshotSuffix ))
315
+ if err != nil {
316
+ return "" , fmt .Errorf ("failed to parse dataStateAt: %w" , err )
317
+ }
318
+
319
+ m .addSnapshotToList (resources.Snapshot {
320
+ ID : snapshotName ,
321
+ CreatedAt : time .Now (),
322
+ DataStateAt : dataStateTime ,
323
+ Pool : m .config .Pool .Name ,
324
+ })
325
+
326
+ go m .RefreshSnapshotList ()
327
+
310
328
return snapshotName , nil
311
329
}
312
330
@@ -334,6 +352,8 @@ func (m *Manager) DestroySnapshot(snapshotName string) error {
334
352
return errors .Wrap (err , "failed to run command" )
335
353
}
336
354
355
+ m .removeSnapshotFromList (snapshotName )
356
+
337
357
return nil
338
358
}
339
359
@@ -360,6 +380,8 @@ func (m *Manager) CleanupSnapshots(retentionLimit int) ([]string, error) {
360
380
361
381
lines := strings .Split (out , "\n " )
362
382
383
+ m .RefreshSnapshotList ()
384
+
363
385
return lines , nil
364
386
}
365
387
@@ -487,8 +509,26 @@ func (m *Manager) GetFilesystemState() (models.FileSystem, error) {
487
509
return fileSystem , nil
488
510
}
489
511
490
- // GetSnapshots returns a snapshot list.
491
- func (m * Manager ) GetSnapshots () ([]resources.Snapshot , error ) {
512
+ // SnapshotList returns a list of snapshots.
513
+ func (m * Manager ) SnapshotList () []resources.Snapshot {
514
+ snapshotList := m .snapshots
515
+ return snapshotList
516
+ }
517
+
518
+ // RefreshSnapshotList updates the list of snapshots.
519
+ func (m * Manager ) RefreshSnapshotList () {
520
+ snapshots , err := m .getSnapshots ()
521
+ if err != nil {
522
+ log .Err ("Failed to refresh snapshot list: " , err )
523
+ return
524
+ }
525
+
526
+ m .mu .Lock ()
527
+ m .snapshots = snapshots
528
+ m .mu .Unlock ()
529
+ }
530
+
531
+ func (m * Manager ) getSnapshots () ([]resources.Snapshot , error ) {
492
532
entries , err := m .listSnapshots (m .config .Pool .Name )
493
533
if err != nil {
494
534
return nil , fmt .Errorf ("failed to list snapshots: %w" , err )
@@ -517,6 +557,24 @@ func (m *Manager) GetSnapshots() ([]resources.Snapshot, error) {
517
557
return snapshots , nil
518
558
}
519
559
560
+ func (m * Manager ) addSnapshotToList (snapshot resources.Snapshot ) {
561
+ m .mu .Lock ()
562
+ m .snapshots = append (m .snapshots , snapshot )
563
+ m .mu .Unlock ()
564
+ }
565
+
566
+ func (m * Manager ) removeSnapshotFromList (snapshotName string ) {
567
+ for i , snapshot := range m .snapshots {
568
+ if snapshot .ID == snapshotName {
569
+ m .mu .Lock ()
570
+ m .snapshots = append (m .snapshots [:i ], m .snapshots [i + 1 :]... )
571
+ m .mu .Unlock ()
572
+
573
+ break
574
+ }
575
+ }
576
+ }
577
+
520
578
// ListFilesystems lists ZFS file systems (clones, pools).
521
579
func (m * Manager ) listFilesystems (pool string ) ([]* ListEntry , error ) {
522
580
filter := snapshotFilter {
0 commit comments