@@ -12,21 +12,34 @@ import (
12
12
// From https://golang.org/src/time/format.go
13
13
const GCPSnapshotTimestampLayout string = "2006-01-02T15:04:05Z07:00"
14
14
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
+ }
16
28
17
- for t := time . Tick ( time . Second * 60 ); ; <- t {
29
+ func ( w * Watcher ) Watch () {
18
30
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 )
21
34
22
35
log .WithFields (log.Fields {
23
- "Watch Interval in secs" : watchInterval ,
36
+ "Watch Interval in secs" : w . WatchInterval ,
24
37
"Retention Period Start" : retentionStart ,
25
38
"last accepted snap time" : lastAcceptedCreation ,
26
39
}).Info ("Initiating new disk watch cycle" )
27
40
28
41
// Get disks
29
- disks , err := gsc .GetDiskList ()
42
+ disks , err := w . GSC .GetDiskList ()
30
43
if err != nil {
31
44
log .Error (err )
32
45
continue
@@ -36,7 +49,7 @@ func Watch(gsc *snapshot.GCPSnapClient, watchInterval, retentionHours, intervalS
36
49
log .Debug ("Checking disk: " , disk .Name )
37
50
38
51
// Get snapshots per disk created by the snapshotter
39
- snaps , err := gsc .ListClientCreatedSnapshots (disk .SelfLink )
52
+ snaps , err := w . GSC .ListClientCreatedSnapshots (disk .SelfLink )
40
53
if err != nil {
41
54
log .Fatal (err )
42
55
}
@@ -65,53 +78,54 @@ func Watch(gsc *snapshot.GCPSnapClient, watchInterval, retentionHours, intervalS
65
78
}
66
79
67
80
// Delete old snaps
68
- if err := deleteSnapshots (gsc , snapsToDelete ); err != nil {
81
+ if err := w . deleteSnapshots (snapsToDelete ); err != nil {
69
82
log .Error (err )
70
83
}
71
84
72
85
// Take snapshot if needed
73
86
if snapNeeded {
74
- if err := createSnapshot (gsc , disk ); err != nil {
87
+ if err := w . createSnapshot (disk ); err != nil {
75
88
log .Error (err )
76
89
}
77
90
}
78
91
79
92
}
93
+
80
94
}
81
95
}
82
96
83
- func deleteSnapshots ( gsc * snapshot. GCPSnapClient , sl []compute.Snapshot ) error {
97
+ func ( w * Watcher ) deleteSnapshots ( sl []compute.Snapshot ) error {
84
98
for _ , s := range sl {
85
99
log .Info ("Attempting to delete snapshot: " , s .Name )
86
- op , err := gsc .DeleteSnapshot (s .Name )
100
+ op , err := w . GSC .DeleteSnapshot (s .Name )
87
101
if err != nil {
88
102
return err
89
103
}
90
104
91
105
// Delete snapshot is a global operation!!!
92
- go pollGlobalOperation (gsc , op )
106
+ go w . pollGlobalOperation (op )
93
107
94
108
}
95
109
return nil
96
110
}
97
111
98
- func createSnapshot ( gsc * snapshot. GCPSnapClient , d compute.Disk ) error {
112
+ func ( w * Watcher ) createSnapshot ( d compute.Disk ) error {
99
113
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 )
101
115
if err != nil {
102
116
return err
103
117
}
104
118
log .Info (fmt .Sprintf ("New snapshot of disk: %v operation: %v" , d .Name , op ))
105
119
106
120
// Create snapshot is a zonal operation!!!
107
- go pollZonalOperation (gsc , op , d .Zone )
121
+ go w . pollZonalOperation (op , d .Zone )
108
122
109
123
return nil
110
124
}
111
125
112
- func pollZonalOperation ( gsc * snapshot. GCPSnapClient , operation , zone string ) {
126
+ func ( w * Watcher ) pollZonalOperation ( operation , zone string ) {
113
127
for {
114
- status , err := gsc .GetZonalOperationStatus (operation , zone )
128
+ status , err := w . GSC .GetZonalOperationStatus (operation , zone )
115
129
if err != nil {
116
130
log .Error ("Operation failed: " , operation , err )
117
131
break
@@ -124,9 +138,9 @@ func pollZonalOperation(gsc *snapshot.GCPSnapClient, operation, zone string) {
124
138
}
125
139
}
126
140
127
- func pollGlobalOperation ( gsc * snapshot. GCPSnapClient , operation string ) {
141
+ func ( w * Watcher ) pollGlobalOperation ( operation string ) {
128
142
for {
129
- status , err := gsc .GetGlobalOperationStatus (operation )
143
+ status , err := w . GSC .GetGlobalOperationStatus (operation )
130
144
if err != nil {
131
145
log .Error ("Operation failed: " , operation , err )
132
146
break
0 commit comments