From e11a740be6bcf77f22246b169b7c26c683d5bbf4 Mon Sep 17 00:00:00 2001 From: yaroslavborbat Date: Thu, 12 Sep 2024 13:07:53 +0300 Subject: [PATCH] add Signed-off-by: yaroslavborbat --- .../pkg/controller/gc/cron_source.go | 26 +++++++++++++++---- .../pkg/controller/vm/gc.go | 6 +---- .../pkg/controller/vmop/gc.go | 6 +---- .../virtualization-controller/_helpers.tpl | 8 +++--- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/images/virtualization-artifact/pkg/controller/gc/cron_source.go b/images/virtualization-artifact/pkg/controller/gc/cron_source.go index fc79e8048..49cb8ee85 100644 --- a/images/virtualization-artifact/pkg/controller/gc/cron_source.go +++ b/images/virtualization-artifact/pkg/controller/gc/cron_source.go @@ -17,9 +17,11 @@ limitations under the License. package gc import ( + "cmp" "context" "fmt" "log/slog" + "slices" "time" "github.com/robfig/cron/v3" @@ -71,13 +73,14 @@ type CronSourceOption struct { func NewDefaultCronSourceOption(objs client.ObjectList, ttl time.Duration, log *slog.Logger) CronSourceOption { return CronSourceOption{ - GetOlder: DefaultGetOlder(objs, ttl, log), + GetOlder: DefaultGetOlder(objs, ttl, 10, log), } } -func DefaultGetOlder(objs client.ObjectList, ttl time.Duration, log *slog.Logger) func(objList client.ObjectList) client.ObjectList { +func DefaultGetOlder(objs client.ObjectList, ttl time.Duration, maxCount int, log *slog.Logger) func(objList client.ObjectList) client.ObjectList { return func(objList client.ObjectList) client.ObjectList { - var items []runtime.Object + var expiredItems []runtime.Object + var notExpiredItems []runtime.Object if err := meta.EachListItem(objList, func(o runtime.Object) error { obj, ok := o.(client.Object) @@ -85,14 +88,27 @@ func DefaultGetOlder(objs client.ObjectList, ttl time.Duration, log *slog.Logger return nil } if helper.GetAge(obj) > ttl { - items = append(items, o) + expiredItems = append(expiredItems, o) + } else { + notExpiredItems = append(notExpiredItems, o) } + return nil }); err != nil { log.Error("failed to populate list", logger.SlogErr(err)) } - if err := meta.SetList(objs, items); err != nil { + if maxCount != 0 && len(notExpiredItems) > maxCount { + slices.SortFunc(notExpiredItems, func(a, b runtime.Object) int { + aObj, _ := a.(client.Object) + bObj, _ := b.(client.Object) + + return cmp.Compare(helper.GetAge(aObj), helper.GetAge(bObj)) + }) + expiredItems = append(expiredItems, notExpiredItems[maxCount:]...) + } + + if err := meta.SetList(objs, expiredItems); err != nil { log.Error("failed to set list", logger.SlogErr(err)) } return objs diff --git a/images/virtualization-artifact/pkg/controller/vm/gc.go b/images/virtualization-artifact/pkg/controller/vm/gc.go index 9730ff47c..f108b5bf5 100644 --- a/images/virtualization-artifact/pkg/controller/vm/gc.go +++ b/images/virtualization-artifact/pkg/controller/vm/gc.go @@ -26,7 +26,6 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/config" "github.com/deckhouse/virtualization-controller/pkg/controller/gc" - "github.com/deckhouse/virtualization-controller/pkg/sdk/framework/helper" ) const gcVMMigrationControllerName = "vmi-migration-gc-controller" @@ -57,10 +56,7 @@ func SetupGC( if !ok { return false } - if vmiMigrationIsFinal(migration) && helper.GetAge(migration) > ttl { - return true - } - return false + return vmiMigrationIsFinal(migration) }, ) } diff --git a/images/virtualization-artifact/pkg/controller/vmop/gc.go b/images/virtualization-artifact/pkg/controller/vmop/gc.go index 10b320b8c..1d394d719 100644 --- a/images/virtualization-artifact/pkg/controller/vmop/gc.go +++ b/images/virtualization-artifact/pkg/controller/vmop/gc.go @@ -25,7 +25,6 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/config" "github.com/deckhouse/virtualization-controller/pkg/controller/gc" - "github.com/deckhouse/virtualization-controller/pkg/sdk/framework/helper" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" ) @@ -57,10 +56,7 @@ func SetupGC( if !ok { return false } - if vmopIsFinal(vmop) && helper.GetAge(vmop) > ttl { - return true - } - return false + return vmopIsFinal(vmop) }, ) } diff --git a/templates/virtualization-controller/_helpers.tpl b/templates/virtualization-controller/_helpers.tpl index 7723bb9e1..e5c246b8f 100644 --- a/templates/virtualization-controller/_helpers.tpl +++ b/templates/virtualization-controller/_helpers.tpl @@ -49,13 +49,13 @@ - name: PROVISIONING_POD_REQUESTS value: '{"cpu":"100m","memory":"60M"}' - name: GC_VMOP_TTL - value: "1h" + value: "24h" - name: GC_VMOP_SCHEDULE - value: "*/30 * * * *" + value: "0 * * * *" - name: GC_VMI_MIGRATION_TTL - value: "1h" + value: "24h" - name: GC_VMI_MIGRATION_SCHEDULE - value: "*/30 * * * *" + value: "0 * * * *" {{- if eq .Values.virtualization.logLevel "debug" }} - name: PPROF_BIND_ADDRESS