Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warns if cleanup finds files bigger than 30MB #139

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 15 additions & 9 deletions pkg/cleanup/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cleanup

import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
Expand All @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/cleanup/cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cleanup

import (
"archive/zip"
"context"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -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",
Expand Down
Loading