Skip to content

Commit

Permalink
balloons: add ignore container option to policy configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Antti Kervinen <[email protected]>
  • Loading branch information
askervin committed Sep 30, 2024
1 parent 7e10e3b commit 1be5ad3
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/plugins/balloons/policy/balloons-policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ func (p *balloons) AllocateResources(c cache.Container) error {
log.Infof("not handling resources of container %s, preserving CPUs %q and memory %q", c.PrettyName(), c.GetCpusetCpus(), c.GetCpusetMems())
return nil
}

if p.bpoptions.Ignore != nil {
rule, err := p.bpoptions.Ignore.MatchContainer(c)
if err != nil {
log.Errorf("error in matching container %s to ignore conditions: %s", c, err)
} else if rule != "" {
log.Debugf("ignore container %s due to matching %s", c, rule)
return nil
}
}

log.Debug("allocating resources for container %s (request %d mCPU, limit %d mCPU)...",
c.PrettyName(),
p.containerRequestedMilliCpus(c.GetID()),
Expand Down
48 changes: 48 additions & 0 deletions config/crd/bases/config.nri_balloonspolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,54 @@ spec:
IdleCpuClass controls how unusded CPUs outside any a
balloons are (re)configured.
type: string
ignore:
description: |-
Ignore specifies containers that should be completely
ignored by the policy.
properties:
matchExpressions:
description: MatchExpressions specifies one or more expressions.
items:
description: |-
Expression describes some runtime-evaluated condition. An expression
consist of a key, an operator and a set of values. An expressions is
evaluated against an object which implements the Evaluable interface.
Evaluating an expression consists of looking up the value for the key
in the object, then using the operator to check it agains the values
of the expression. The result is a single boolean value. An object is
said to satisfy the evaluated expression if this value is true. An
expression can contain 0, 1 or more values depending on the operator.
properties:
key:
description: Key is the expression key.
type: string
operator:
description: Op is the expression operator.
enum:
- Equals
- NotEqual
- In
- NotIn
- Exists
- NotExist
- AlwaysTrue
- Matches
- MatchesNot
- MatchesAny
- MatchesNone
type: string
values:
description: Values contains the values the key value is
evaluated against.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
type: object
instrumentation:
description: Config provides runtime configuration for instrumentation.
properties:
Expand Down
48 changes: 48 additions & 0 deletions deployment/helm/balloons/crds/config.nri_balloonspolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,54 @@ spec:
IdleCpuClass controls how unusded CPUs outside any a
balloons are (re)configured.
type: string
ignore:
description: |-
Ignore specifies containers that should be completely
ignored by the policy.
properties:
matchExpressions:
description: MatchExpressions specifies one or more expressions.
items:
description: |-
Expression describes some runtime-evaluated condition. An expression
consist of a key, an operator and a set of values. An expressions is
evaluated against an object which implements the Evaluable interface.
Evaluating an expression consists of looking up the value for the key
in the object, then using the operator to check it agains the values
of the expression. The result is a single boolean value. An object is
said to satisfy the evaluated expression if this value is true. An
expression can contain 0, 1 or more values depending on the operator.
properties:
key:
description: Key is the expression key.
type: string
operator:
description: Op is the expression operator.
enum:
- Equals
- NotEqual
- In
- NotIn
- Exists
- NotExist
- AlwaysTrue
- Matches
- MatchesNot
- MatchesAny
- MatchesNone
type: string
values:
description: Values contains the values the key value is
evaluated against.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
type: object
instrumentation:
description: Config provides runtime configuration for instrumentation.
properties:
Expand Down
27 changes: 27 additions & 0 deletions pkg/apis/config/v1alpha1/resmgr/policy/balloons/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
policy "github.com/containers/nri-plugins/pkg/apis/config/v1alpha1/resmgr/policy"
resmgr "github.com/containers/nri-plugins/pkg/apis/resmgr/v1alpha1"
"github.com/containers/nri-plugins/pkg/cpuallocator"
"github.com/containers/nri-plugins/pkg/resmgr/cache"
)

type (
Expand Down Expand Up @@ -78,6 +79,9 @@ type Config struct {
// Reserved (CPU) resources for kube-system namespace.
// +kubebuilder:validation:Required
ReservedResources Constraints `json:"reservedResources"`
// Ignore specifies containers that should be completely
// ignored by the policy.
Ignore *ContainerMatchConfig `json:"ignore,omitempty"`
}

type CPUTopologyLevel string
Expand Down Expand Up @@ -252,8 +256,31 @@ func (p CPUPriority) Value() cpuallocator.CPUPriority {
return cpuallocator.PriorityNone
}

// ContainerMatchConfig contains container matching configurations.
// +k8s:deepcopy-gen=true
type ContainerMatchConfig struct {
// MatchExpressions specifies one or more expressions.
MatchExpressions []resmgr.Expression `json:"matchExpressions,omitempty"`
}

func (cmc *ContainerMatchConfig) MatchContainer(c cache.Container) (string, error) {
for _, expr := range cmc.MatchExpressions {
if expr.Evaluate(c) {
return expr.String(), nil
}
}
return "", nil
}

func (c *Config) Validate() error {
errs := []error{}
if c.Ignore != nil {
for _, expr := range c.Ignore.MatchExpressions {
if err := expr.Validate(); err != nil {
errs = append(errs, err)
}
}
}
for _, blnDef := range c.BalloonDefs {
for _, expr := range blnDef.MatchExpressions {
if err := expr.Validate(); err != nil {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1be5ad3

Please sign in to comment.