Skip to content

Commit

Permalink
cache: store creation time of pod and containers cache objects
Browse files Browse the repository at this point in the history
Enable sorting containers based on the time when they and their pods
have been added to the cache.

Signed-off-by: Antti Kervinen <[email protected]>
  • Loading branch information
askervin authored and klihub committed Feb 29, 2024
1 parent c9e90b9 commit f285001
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/resmgr/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"
"strings"
"sync"
"time"

nri "github.com/containerd/nri/pkg/api"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -81,6 +82,8 @@ type Pod interface {
GetName() string
// GetNamespace returns the namespace of the pod.
GetNamespace() string
// GetCtime returns the creation time of the pod cache object.
GetCtime() time.Time
// GetQOSClass returns the PodQOSClass of the pod.
GetQOSClass() v1.PodQOSClass
// GetLabel returns the value of the given label and whether it was found.
Expand Down Expand Up @@ -134,6 +137,7 @@ type pod struct {
QOSClass v1.PodQOSClass // pod QOS class
Affinity *podContainerAffinity // annotated container affinity
prettyName string // cached PrettyName()
ctime time.Time // time of pod creation
}

// ContainerState is the container state in the runtime.
Expand Down Expand Up @@ -166,6 +170,8 @@ type Container interface {
GetName() string
// GetNamespace returns the namespace of the container.
GetNamespace() string
// GetCtime returns the creation time of the container cache object.
GetCtime() time.Time
// UpdateState updates the state of the container.
UpdateState(ContainerState)
// GetState returns the ContainerState of the container.
Expand Down Expand Up @@ -320,7 +326,8 @@ type container struct {

pending map[string]struct{} // controllers with pending changes for this container

prettyName string // cached PrettyName()
prettyName string // cached PrettyName()
ctime time.Time // creation time of the container cache object
}

type Mount = nri.Mount
Expand Down
21 changes: 21 additions & 0 deletions pkg/resmgr/cache/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"sort"
"strconv"
"strings"
"time"

resmgr "github.com/containers/nri-plugins/pkg/apis/resmgr/v1alpha1"
"github.com/containers/nri-plugins/pkg/cgroups"
Expand Down Expand Up @@ -98,6 +99,7 @@ func (cch *cache) createContainer(nriCtr *nri.Container) (*container, error) {
Ctr: nriCtr,
State: nriCtr.GetState(),
Tags: make(map[string]string),
ctime: time.Now(),
}

c.generateTopologyHints()
Expand Down Expand Up @@ -329,6 +331,10 @@ func (c *container) GetNamespace() string {
return ""
}

func (c *container) GetCtime() time.Time {
return c.ctime
}

func (c *container) UpdateState(state ContainerState) {
c.State = state
}
Expand Down Expand Up @@ -1142,3 +1148,18 @@ func CompareCPU(ci, cj Container) int {
}
return 0
}

func CompareContainerCtime(ci, cj Container) int {
ti, tj := ci.GetCtime(), cj.GetCtime()
return ti.Compare(tj)
}

func ComparePodCtime(ci, cj Container) int {
pi, oki := ci.GetPod()
pj, okj := cj.GetPod()
if !oki || !okj {
return 0
}
ti, tj := pi.GetCtime(), pj.GetCtime()
return ti.Compare(tj)
}
6 changes: 6 additions & 0 deletions pkg/resmgr/cache/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cache

import (
"strings"
"time"

nri "github.com/containerd/nri/pkg/api"
v1 "k8s.io/api/core/v1"
Expand All @@ -30,6 +31,7 @@ func (cch *cache) createPod(nriPod *nri.PodSandbox) *pod {
p := &pod{
cache: cch,
Pod: nriPod,
ctime: time.Now(),
}

if err := p.parseCgroupForQOSClass(); err != nil {
Expand Down Expand Up @@ -67,6 +69,10 @@ func (p *pod) GetNamespace() string {
return p.Pod.GetNamespace()
}

func (p *pod) GetCtime() time.Time {
return p.ctime
}

func (p *pod) GetLabel(key string) (string, bool) {
value, ok := p.Pod.GetLabels()[key]
return value, ok
Expand Down

0 comments on commit f285001

Please sign in to comment.