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

feat(gc): leave only last 10 resources #368

Merged
merged 1 commit into from
Sep 17, 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
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
Loading