diff --git a/cmd/plugins/balloons/policy/balloons-policy.go b/cmd/plugins/balloons/policy/balloons-policy.go index 698c589e1..c6f369ce5 100644 --- a/cmd/plugins/balloons/policy/balloons-policy.go +++ b/cmd/plugins/balloons/policy/balloons-policy.go @@ -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)) { diff --git a/config/crd/bases/config.nri_balloonspolicies.yaml b/config/crd/bases/config.nri_balloonspolicies.yaml index e39fd0adc..9cc3cc950 100644 --- a/config/crd/bases/config.nri_balloonspolicies.yaml +++ b/config/crd/bases/config.nri_balloonspolicies.yaml @@ -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 diff --git a/deployment/helm/balloons/crds/config.nri_balloonspolicies.yaml b/deployment/helm/balloons/crds/config.nri_balloonspolicies.yaml index e39fd0adc..9cc3cc950 100644 --- a/deployment/helm/balloons/crds/config.nri_balloonspolicies.yaml +++ b/deployment/helm/balloons/crds/config.nri_balloonspolicies.yaml @@ -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 diff --git a/pkg/apis/config/v1alpha1/resmgr/policy/balloons/config.go b/pkg/apis/config/v1alpha1/resmgr/policy/balloons/config.go index 42af407ca..322182ccf 100644 --- a/pkg/apis/config/v1alpha1/resmgr/policy/balloons/config.go +++ b/pkg/apis/config/v1alpha1/resmgr/policy/balloons/config.go @@ -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 ( @@ -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. diff --git a/pkg/apis/config/v1alpha1/resmgr/policy/balloons/zz_generated.deepcopy.go b/pkg/apis/config/v1alpha1/resmgr/policy/balloons/zz_generated.deepcopy.go index be332eaae..1f0ceacf8 100644 --- a/pkg/apis/config/v1alpha1/resmgr/policy/balloons/zz_generated.deepcopy.go +++ b/pkg/apis/config/v1alpha1/resmgr/policy/balloons/zz_generated.deepcopy.go @@ -21,6 +21,7 @@ package balloons import ( "github.com/containers/nri-plugins/pkg/apis/config/v1alpha1/resmgr/policy" + v1alpha1 "github.com/containers/nri-plugins/pkg/apis/resmgr/v1alpha1" ) // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. @@ -31,6 +32,13 @@ func (in *BalloonDef) DeepCopyInto(out *BalloonDef) { *out = make([]string, len(*in)) copy(*out, *in) } + if in.MatchContainers != nil { + in, out := &in.MatchContainers, &out.MatchContainers + *out = make([]v1alpha1.Expression, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } if in.PreferSpreadOnPhysicalCores != nil { in, out := &in.PreferSpreadOnPhysicalCores, &out.PreferSpreadOnPhysicalCores *out = new(bool)