Skip to content

Commit 1b45c1d

Browse files
improvements
1 parent 0462edb commit 1b45c1d

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

jobs/instances-snapshot-cleaner/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ You can exercice by adding the following features:
7272

7373
- Add tags to exclude
7474
- Add alerts if a Job goes in error
75-
- Use secret manager
75+
- Use Secret Manager instead of job environment variables
76+
- Support multiple zones dans projects
7677

7778
# Additional content
7879

jobs/instances-snapshot-cleaner/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module github.com/scaleway/serverless-examples/jobs/instances-snapshot
22

33
go 1.23
44

5-
require github.com/scaleway/scaleway-sdk-go v1.0.0-beta.31
5+
require github.com/scaleway/scaleway-sdk-go v1.0.0-beta.32
66

77
require gopkg.in/yaml.v2 v2.4.0 // indirect

jobs/instances-snapshot-cleaner/go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI=
22
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
3-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30 h1:yoKAVkEVwAqbGbR8n87rHQ1dulL25rKloGadb3vm770=
4-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30/go.mod h1:sH0u6fq6x4R5M7WxkoQFY/o7UaiItec0o1LinLCJNq8=
5-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.31 h1:Fj7jPyu9TQjqfXcLylINK5PANSzOWXIX4QtGmfp67AY=
6-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.31/go.mod h1:kzh+BSAvpoyHHdHBCDhmSWtBc1NbLMZ2lWHqnBoxFks=
3+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.32 h1:4+LP7qmsLSGbmc66m1s5dKRMBwztRppfxFKlYqYte/c=
4+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.32/go.mod h1:kzh+BSAvpoyHHdHBCDhmSWtBc1NbLMZ2lWHqnBoxFks=
75
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
86
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
97
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

jobs/instances-snapshot-cleaner/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ func main() {
8181
}
8282
}
8383

84+
// cleanSnapshots when called will clean snapshots in the project (if specified)
85+
// that are older than the number of days.
8486
func cleanSnapshots(days int, instanceAPI *instance.API) error {
87+
// Get the list of all snapshots
8588
snapshotsList, err := instanceAPI.ListSnapshots(&instance.ListSnapshotsRequest{
8689
Zone: scw.Zone(os.Getenv(envZone)),
8790
Project: scw.StringPtr(os.Getenv(envProjectID)),
@@ -95,10 +98,13 @@ func cleanSnapshots(days int, instanceAPI *instance.API) error {
9598

9699
currentTime := time.Now()
97100

101+
// For each snapshot, check conditions
98102
for _, snapshot := range snapshotsList.Snapshots {
103+
// Check if snapshot is in ready state and if it's older than the number of days definied.
99104
if snapshot.State == instance.SnapshotStateAvailable && (currentTime.Sub(*snapshot.CreationDate).Hours()/hoursPerDay) > float64(days) {
100105
fmt.Printf("\nDeleting snapshot <%s>:%s created at: %s\n", snapshot.ID, snapshot.Name, snapshot.CreationDate.Format(time.RFC3339))
101106

107+
// Delete snapshot found.
102108
err := instanceAPI.DeleteSnapshot(&instance.DeleteSnapshotRequest{
103109
SnapshotID: snapshot.ID,
104110
Zone: snapshot.Zone,
@@ -112,6 +118,7 @@ func cleanSnapshots(days int, instanceAPI *instance.API) error {
112118
return nil
113119
}
114120

121+
// Check for mandatory variables before starting to work.
115122
func init() {
116123
mandatoryVariables := [...]string{envOrgID, envAccessKey, envSecretKey, envZone, envProjectID}
117124

0 commit comments

Comments
 (0)