Skip to content

Commit

Permalink
WiP: balloons: allow balloon match by expressions.
Browse files Browse the repository at this point in the history
Implement assigning containers to balloon instances by
container match expression defined in balloon definitions.
Match expressions are evaluated for containers which are
not explicitly assigned to any balloon by an annotation,
before namespace-based assignments. If an expression in a
balloon definition matches a container, that container is
assigned to a balloon instance of that definition.

For instance, given this balloon definition among the
configuration

...
  balloonTypes:
  - name: specialBalloon
    matchContainers:
      - key: pod/labels/app.kubernetes.io/component
        operator: Equals
        values: [ "SpecialComponent" ]
...

any container not explicitly annotated to some other balloon
will get assigned to instances of specialBalloon, if the pod
of the container has the label
  app.kubernetes.io/component=SpecialComponent

Similarly, given this balloon definition among the
configuration

...
  balloonTypes:
  - name: podBalloon
    matchContainers:
      - key: pod/name
        operator: In
        values: [ "pod1", "pod2", "pod3" ]
...

all containers of pods named pod1, pod2, and pod3 will be
assigned to instances of podBalloon.

Signed-off-by: Krisztian Litkey <[email protected]>
  • Loading branch information
klihub committed Feb 9, 2024
1 parent e8fe97e commit bb5b403
Show file tree
Hide file tree
Showing 5 changed files with 115 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 @@ -409,6 +409,17 @@ func (p *balloons) chooseBalloonDef(c cache.Container) (*BalloonDef, error) {
return blnDef, nil
}

for _, blnDef := range append([]*BalloonDef{p.reservedBalloonDef, p.defaultBalloonDef}, p.bpoptions.BalloonDefs...) {
for _, expr := range blnDef.MatchContainers {
log.Debugf("- checking expression %s of balloon %s against container %s...",
blnDef.Name, expr.String(), c.PrettyName())
if expr.Evaluate(c) {
log.Debugf(" => matches")
return blnDef, nil
}
}
}

// BalloonDef is defined by a special namespace (kube-system +
// ReservedPoolNamespaces)?
if namespaceMatches(c.GetNamespace(), append(p.bpoptions.ReservedPoolNamespaces, metav1.NamespaceSystem)) {
Expand Down
45 changes: 45 additions & 0 deletions config/crd/bases/config.nri_balloonspolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,51 @@ spec:
description: CpuClass controls how CPUs of a balloon are (re)configured
whenever a balloon is created, inflated or deflated.
type: string
matchContainers:
description: MatchContainers specifies one or more expressions
which are evaluated to see if a container should be assigned
into balloon instances from this definition.
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
maxBalloons:
description: MaxBalloons is the maximum number of balloon instances
that is allowed to co-exist. If reached, new balloons cannot
Expand Down
45 changes: 45 additions & 0 deletions deployment/helm/balloons/crds/config.nri_balloonspolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,51 @@ spec:
description: CpuClass controls how CPUs of a balloon are (re)configured
whenever a balloon is created, inflated or deflated.
type: string
matchContainers:
description: MatchContainers specifies one or more expressions
which are evaluated to see if a container should be assigned
into balloon instances from this definition.
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
maxBalloons:
description: MaxBalloons is the maximum number of balloon instances
that is allowed to co-exist. If reached, new balloons cannot
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/config/v1alpha1/resmgr/policy/balloons/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (

policy "github.com/containers/nri-plugins/pkg/apis/config/v1alpha1/resmgr/policy"
"github.com/containers/nri-plugins/pkg/cpuallocator"

resmgr "github.com/containers/nri-plugins/pkg/apis/resmgr/v1alpha1"
)

type (
Expand Down Expand Up @@ -124,6 +126,10 @@ type BalloonDef struct {
// balloon instances from this definition. This is used by
// namespace assign methods.
Namespaces []string `json:"namespaces,omitempty"`
// MatchContainers specifies one or more expressions which are evaluated
// to see if a container should be assigned into balloon instances from
// this definition.
MatchContainers []resmgr.Expression `json:"matchContainers,omitempty"`
// MaxCpus specifies the maximum number of CPUs exclusively
// usable by containers in a balloon. Balloon size will not be
// inflated larger than MaxCpus.
Expand Down

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

0 comments on commit bb5b403

Please sign in to comment.