From 6ce6197e536c4aeeacfeaf4f854a23a54a2d1a03 Mon Sep 17 00:00:00 2001 From: Ewout Prangsma Date: Thu, 29 Mar 2018 16:44:23 +0200 Subject: [PATCH] Fixing PV cleanup --- pkg/storage/pv_cleanup.go | 3 ++- pkg/storage/pv_inspector.go | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/storage/pv_cleanup.go b/pkg/storage/pv_cleanup.go index bac653572..5d07f0220 100644 --- a/pkg/storage/pv_cleanup.go +++ b/pkg/storage/pv_cleanup.go @@ -34,6 +34,7 @@ import ( "k8s.io/client-go/kubernetes" "github.com/arangodb/kube-arangodb/pkg/storage/provisioner" + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" "github.com/arangodb/kube-arangodb/pkg/util/trigger" ) @@ -159,7 +160,7 @@ func (c *pvCleaner) clean(pv v1.PersistentVolume) error { } // Remove persistent volume - if err := c.cli.CoreV1().PersistentVolumes().Delete(pv.GetName(), &metav1.DeleteOptions{}); err != nil { + if err := c.cli.CoreV1().PersistentVolumes().Delete(pv.GetName(), &metav1.DeleteOptions{}); err != nil && !k8sutil.IsNotFound(err) { log.Debug().Err(err). Str("name", pv.GetName()). Msg("Failed to remove PersistentVolume") diff --git a/pkg/storage/pv_inspector.go b/pkg/storage/pv_inspector.go index b9ee52041..14fe12163 100644 --- a/pkg/storage/pv_inspector.go +++ b/pkg/storage/pv_inspector.go @@ -23,6 +23,8 @@ package storage import ( + "time" + "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -38,6 +40,7 @@ func (ls *LocalStorage) inspectPVs() (int, error) { } spec := ls.apiObject.Spec availableVolumes := 0 + cleanupBeforeTimestamp := time.Now().Add(time.Hour * -24) for _, pv := range list.Items { if pv.Spec.StorageClassName != spec.StorageClass.Name { // Not our storage class @@ -45,7 +48,20 @@ func (ls *LocalStorage) inspectPVs() (int, error) { } switch pv.Status.Phase { case v1.VolumeAvailable: - availableVolumes++ + // Is this an old volume? + if pv.GetObjectMeta().GetCreationTimestamp().Time.Before(cleanupBeforeTimestamp) { + // Let's clean it up + if ls.isOwnerOf(&pv) { + // Cleanup this volume + log.Debug().Str("name", pv.GetName()).Msg("Added PersistentVolume to cleaner") + ls.pvCleaner.Add(pv) + } else { + log.Debug().Str("name", pv.GetName()).Msg("PersistentVolume is not owned by us") + availableVolumes++ + } + } else { + availableVolumes++ + } case v1.VolumeReleased: if ls.isOwnerOf(&pv) { // Cleanup this volume