Skip to content

Commit e28f239

Browse files
author
Foivos Filippopoulos
committed
Create Watcher struct and add some tests
1 parent 274019f commit e28f239

File tree

5 files changed

+332
-21
lines changed

5 files changed

+332
-21
lines changed

main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ func main() {
8484
gsc := snapshot.CreateGCPSnapClient(project, snapPrefix, zones, *labels)
8585

8686
// Start watching
87-
watch.Watch(gsc, watchInterval, retentionHours, intervalSecs)
87+
watcher := &watch.Watcher{
88+
GSC: gsc,
89+
WatchInterval: watchInterval,
90+
RetentionHours: retentionHours,
91+
IntervalSecs: intervalSecs,
92+
}
93+
watcher.Watch()
8894

8995
}

snapshot/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type GCPSnapClientInterface interface {
3434
GetDiskList() ([]compute.Disk, error)
3535
ListSnapshots(diskSelfLink string) ([]compute.Snapshot, error)
3636
ListClientCreatedSnapshots(diskSelfLink string) ([]compute.Snapshot, error)
37-
CreateSnapshot() (string, error)
37+
CreateSnapshot(diskName, zone string) (string, error)
3838
DeleteSnapshot(snapName string) (string, error)
3939
GetZonalOperationStatus(operation, zone string) (string, error)
4040
GetGlobalOperationStatus(operation string) (string, error)

snapshot/mock_client.go

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

watch/watch.go

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,34 @@ import (
1212
// From https://golang.org/src/time/format.go
1313
const GCPSnapshotTimestampLayout string = "2006-01-02T15:04:05Z07:00"
1414

15-
func Watch(gsc *snapshot.GCPSnapClient, watchInterval, retentionHours, intervalSecs int) {
15+
type Watcher struct {
16+
GSC snapshot.GCPSnapClientInterface
17+
WatchInterval int
18+
RetentionHours int
19+
IntervalSecs int
20+
}
21+
22+
type WatcherInterface interface {
23+
Watch()
24+
deleteSnapshots(sl []compute.Snapshot)
25+
createSnapshot(d compute.Disk)
26+
pollZonalOperation(operation, zone string)
27+
}
1628

17-
for t := time.Tick(time.Second * 60); ; <-t {
29+
func (w *Watcher) Watch() {
1830

19-
retentionStart := time.Now().Add(-time.Duration(retentionHours) * time.Hour)
20-
lastAcceptedCreation := time.Now().Add(time.Duration(-intervalSecs) * time.Second)
31+
for t := time.Tick(time.Second * time.Duration(w.IntervalSecs)); ; <-t {
32+
retentionStart := time.Now().Add(-time.Duration(w.RetentionHours) * time.Hour)
33+
lastAcceptedCreation := time.Now().Add(time.Duration(-w.IntervalSecs) * time.Second)
2134

2235
log.WithFields(log.Fields{
23-
"Watch Interval in secs": watchInterval,
36+
"Watch Interval in secs": w.WatchInterval,
2437
"Retention Period Start": retentionStart,
2538
"last accepted snap time": lastAcceptedCreation,
2639
}).Info("Initiating new disk watch cycle")
2740

2841
// Get disks
29-
disks, err := gsc.GetDiskList()
42+
disks, err := w.GSC.GetDiskList()
3043
if err != nil {
3144
log.Error(err)
3245
continue
@@ -36,7 +49,7 @@ func Watch(gsc *snapshot.GCPSnapClient, watchInterval, retentionHours, intervalS
3649
log.Debug("Checking disk: ", disk.Name)
3750

3851
// Get snapshots per disk created by the snapshotter
39-
snaps, err := gsc.ListClientCreatedSnapshots(disk.SelfLink)
52+
snaps, err := w.GSC.ListClientCreatedSnapshots(disk.SelfLink)
4053
if err != nil {
4154
log.Fatal(err)
4255
}
@@ -65,53 +78,54 @@ func Watch(gsc *snapshot.GCPSnapClient, watchInterval, retentionHours, intervalS
6578
}
6679

6780
// Delete old snaps
68-
if err := deleteSnapshots(gsc, snapsToDelete); err != nil {
81+
if err := w.deleteSnapshots(snapsToDelete); err != nil {
6982
log.Error(err)
7083
}
7184

7285
// Take snapshot if needed
7386
if snapNeeded {
74-
if err := createSnapshot(gsc, disk); err != nil {
87+
if err := w.createSnapshot(disk); err != nil {
7588
log.Error(err)
7689
}
7790
}
7891

7992
}
93+
8094
}
8195
}
8296

83-
func deleteSnapshots(gsc *snapshot.GCPSnapClient, sl []compute.Snapshot) error {
97+
func (w *Watcher) deleteSnapshots(sl []compute.Snapshot) error {
8498
for _, s := range sl {
8599
log.Info("Attempting to delete snapshot: ", s.Name)
86-
op, err := gsc.DeleteSnapshot(s.Name)
100+
op, err := w.GSC.DeleteSnapshot(s.Name)
87101
if err != nil {
88102
return err
89103
}
90104

91105
// Delete snapshot is a global operation!!!
92-
go pollGlobalOperation(gsc, op)
106+
go w.pollGlobalOperation(op)
93107

94108
}
95109
return nil
96110
}
97111

98-
func createSnapshot(gsc *snapshot.GCPSnapClient, d compute.Disk) error {
112+
func (w *Watcher) createSnapshot(d compute.Disk) error {
99113
log.Debug("Attempt to take snapshot of disk: ", d.Name)
100-
op, err := gsc.CreateSnapshot(d.Name, d.Zone)
114+
op, err := w.GSC.CreateSnapshot(d.Name, d.Zone)
101115
if err != nil {
102116
return err
103117
}
104118
log.Info(fmt.Sprintf("New snapshot of disk: %v operation: %v", d.Name, op))
105119

106120
// Create snapshot is a zonal operation!!!
107-
go pollZonalOperation(gsc, op, d.Zone)
121+
go w.pollZonalOperation(op, d.Zone)
108122

109123
return nil
110124
}
111125

112-
func pollZonalOperation(gsc *snapshot.GCPSnapClient, operation, zone string) {
126+
func (w *Watcher) pollZonalOperation(operation, zone string) {
113127
for {
114-
status, err := gsc.GetZonalOperationStatus(operation, zone)
128+
status, err := w.GSC.GetZonalOperationStatus(operation, zone)
115129
if err != nil {
116130
log.Error("Operation failed: ", operation, err)
117131
break
@@ -124,9 +138,9 @@ func pollZonalOperation(gsc *snapshot.GCPSnapClient, operation, zone string) {
124138
}
125139
}
126140

127-
func pollGlobalOperation(gsc *snapshot.GCPSnapClient, operation string) {
141+
func (w *Watcher) pollGlobalOperation(operation string) {
128142
for {
129-
status, err := gsc.GetGlobalOperationStatus(operation)
143+
status, err := w.GSC.GetGlobalOperationStatus(operation)
130144
if err != nil {
131145
log.Error("Operation failed: ", operation, err)
132146
break

0 commit comments

Comments
 (0)