Skip to content

Commit

Permalink
feat(gc): leave only last 10 resources (#368)
Browse files Browse the repository at this point in the history
leave only last 10 resources
replace ttl for vmop with 24h
replace ttl for vmi migration with 24h
replace schedule for vmop with every hour
replace schedule for vmi migration with every hour

Signed-off-by: yaroslavborbat <yaroslav.752@gmail.com>
  • Loading branch information
yaroslavborbat authored Sep 17, 2024
1 parent 51cd316 commit 3d66dc6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
26 changes: 21 additions & 5 deletions images/virtualization-artifact/pkg/controller/gc/cron_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ limitations under the License.
package gc

import (
"cmp"
"context"
"fmt"
"log/slog"
"slices"
"time"

"github.com/robfig/cron/v3"
Expand Down Expand Up @@ -71,28 +73,42 @@ 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)
if !ok {
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
Expand Down
6 changes: 1 addition & 5 deletions images/virtualization-artifact/pkg/controller/vm/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -57,10 +56,7 @@ func SetupGC(
if !ok {
return false
}
if vmiMigrationIsFinal(migration) && helper.GetAge(migration) > ttl {
return true
}
return false
return vmiMigrationIsFinal(migration)
},
)
}
Expand Down
6 changes: 1 addition & 5 deletions images/virtualization-artifact/pkg/controller/vmop/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -57,10 +56,7 @@ func SetupGC(
if !ok {
return false
}
if vmopIsFinal(vmop) && helper.GetAge(vmop) > ttl {
return true
}
return false
return vmopIsFinal(vmop)
},
)
}
Expand Down
8 changes: 4 additions & 4 deletions templates/virtualization-controller/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3d66dc6

Please sign in to comment.