+// ManageDisks garbage collects managed compute disks (VM disk images) in the
+// configured resource group. It will delete disks which
+// have "namePrefix", are "available" (which means they are not
+// leased to a VM) and were created more than DeleteDanglingResourcesAfter seconds ago.
+// (Azure provides no modification timestamp on managed disks, there is only a
+// creation timestamp)
+func (az *azureInstanceSet) manageDisks() {
+
+ re := regexp.MustCompile(`^` + az.namePrefix + `.*-os$`)
+ timestamp := time.Now()
+
+ for {
+ response, err := az.disksClient.listByResourceGroup(az.ctx, az.imageResourceGroup)
+ if err != nil {
+ az.logger.WithError(err).Warn("Error listing disks")
+ return
+ }
+ for _, d := range response.Values() {
+ age := timestamp.Sub(d.DiskProperties.TimeCreated.ToTime())
+ if d.DiskProperties.DiskState == "Unattached" &&
+ re.MatchString(*d.Name) &&
+ age.Seconds() > az.azconfig.DeleteDanglingResourcesAfter.Duration().Seconds() {
+
+ az.logger.Printf("Disk %v is unlocked and not modified for %v seconds, will delete", *d.Name, age.Seconds())
+ az.deleteDisk <- d
+ }
+ }
+ if response.Values() != nil {
+ response.Next()
+ } else {
+ break
+ }
+ }
+}
+