From e26eae499c7b21df27c170a3f174fbda5bd2c6b5 Mon Sep 17 00:00:00 2001 From: Matthias Bertschy Date: Mon, 26 Aug 2024 18:30:15 +0200 Subject: [PATCH] warns if cleanup finds files bigger than 30MB Signed-off-by: Matthias Bertschy --- main.go | 2 +- pkg/cleanup/cleanup.go | 24 +++++++++++++++--------- pkg/cleanup/cleanup_test.go | 3 ++- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index c9d75cca5..b8f6fffeb 100644 --- a/main.go +++ b/main.go @@ -81,7 +81,7 @@ func main() { file.DefaultStorageRoot, intervalDuration, kubernetesAPI) - go cleanupHandler.StartCleanupTask() + go cleanupHandler.StartCleanupTask(ctx) logger.L().Info("APIServer started") code := cli.Run(cmd) diff --git a/pkg/cleanup/cleanup.go b/pkg/cleanup/cleanup.go index 00a102129..bd255baeb 100644 --- a/pkg/cleanup/cleanup.go +++ b/pkg/cleanup/cleanup.go @@ -2,6 +2,7 @@ package cleanup import ( "bytes" + "context" "fmt" "os" "path/filepath" @@ -19,6 +20,10 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) +const ( + MinSizeToReport = 30 * 1024 * 1024 // 30MB +) + type TypeCleanupHandlerFunc func(kind, path string, metadata *metav1.ObjectMeta, resourceMaps ResourceMaps) bool var resourceKindToHandler = map[string]TypeCleanupHandlerFunc{ @@ -65,7 +70,7 @@ func NewResourcesCleanupHandler(appFs afero.Fs, root string, interval time.Durat } } -func (h *ResourcesCleanupHandler) StartCleanupTask() { +func (h *ResourcesCleanupHandler) StartCleanupTask(ctx context.Context) { for { logger.L().Info("started cleanup task", helpers.String("interval", h.interval.String())) var err error @@ -76,7 +81,6 @@ func (h *ResourcesCleanupHandler) StartCleanupTask() { continue } - var size int64 for resourceKind, handler := range resourceKindToHandler { v1beta1ApiVersionPath := filepath.Join(h.root, softwarecomposition.GroupName, resourceKind) exists, _ := afero.DirExists(h.appFs, v1beta1ApiVersionPath) @@ -88,9 +92,13 @@ func (h *ResourcesCleanupHandler) StartCleanupTask() { return err } - // sum all files in path - if !info.IsDir() { - size += info.Size() + // skip directories + if info.IsDir() { + return nil + } + + if size := info.Size(); size > MinSizeToReport { + logger.L().Ctx(ctx).Warning("large file detected, you may want to truncate it", helpers.String("path", path), helpers.String("size", fmt.Sprintf("%d bytes", size))) } // FIXME: migrate to gob files - to remove after some time @@ -127,8 +135,8 @@ func (h *ResourcesCleanupHandler) StartCleanupTask() { } } - // skip directories and files that are not metadata files - if info.IsDir() || !file.IsMetadataFile(path) { + // skip files that are not metadata files + if !file.IsMetadataFile(path) { return nil } @@ -157,8 +165,6 @@ func (h *ResourcesCleanupHandler) StartCleanupTask() { } } - logger.L().Info("storage size before cleanup", helpers.String("path", h.root), helpers.String("size", fmt.Sprintf("%d bytes", size))) - if h.interval == 0 { break } diff --git a/pkg/cleanup/cleanup_test.go b/pkg/cleanup/cleanup_test.go index 481ae30e0..36d0bcc5b 100644 --- a/pkg/cleanup/cleanup_test.go +++ b/pkg/cleanup/cleanup_test.go @@ -2,6 +2,7 @@ package cleanup import ( "archive/zip" + "context" "encoding/json" "fmt" "io" @@ -50,7 +51,7 @@ func TestCleanupTask(t *testing.T) { fetcher: &ResourcesFetchMock{}, deleteFunc: deleteFunc, } - handler.StartCleanupTask() + handler.StartCleanupTask(context.TODO()) expectedFilesToDelete := []string{ "/data/spdx.softwarecomposition.kubescape.io/applicationactivities/gadget/gadget-daemonset-gadget-0d7c-fd3c.g",