mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
feat(api): carry a consumption declaration on ApplicationDefinition
Tenant quotas are to be enforced against what an application declares rather than against the pods it renders, and the apiserver must hold no per-kind knowledge of how to compute that, so that a kind shipped from an out-of-tree catalog can take part. The declaration therefore lives on the kind's definition, next to the schema it is evaluated against. The evaluator's Declaration becomes an alias of the API type, so a definition is compiled exactly as it was read, with no conversion that could drift from it. The CRD refuses an exempt declaration that still carries expressions and a non-exempt one that names no resource, so an omission cannot pass for a zero reservation. Nothing reads the field yet. Assisted-by: LLM Signed-off-by: Mattia Eleuteri <mattia@hidora.io>
This commit is contained in:
6 files changed
+219
-18
No files matched your search
@@ -61,6 +61,68 @@ type ApplicationDefinitionSpec struct {
|
||||
|
||||
// Dashboard configuration for this resource
|
||||
Dashboard *ApplicationDefinitionDashboard `json:"dashboard,omitempty"`
|
||||
|
||||
// Consumption declares what an application of this kind reserves from
|
||||
// its tenant's quota budget, computed from its values. Unset means the
|
||||
// kind has not declared it, which is not the same as consuming nothing:
|
||||
// a kind that consumes nothing sets exempt.
|
||||
// +optional
|
||||
Consumption *ApplicationDefinitionConsumption `json:"consumption,omitempty"`
|
||||
}
|
||||
|
||||
// ApplicationDefinitionConsumption is how an application's reservation is
|
||||
// computed from its values, as CEL over the values defaulted against the
|
||||
// kind's openAPISchema.
|
||||
//
|
||||
// An expression sees the variable `values`, every binding in let, and four
|
||||
// functions: instanceType(name) and preset(name), which return a map of
|
||||
// quantities keyed by quota key, quantity(x), which takes an int or a
|
||||
// quantity string, and fail(reason), which raises an evaluation error with
|
||||
// that reason, for a spec the kind's chart would refuse to render. CEL's
|
||||
// logical operators absorb an error when the other operand decides the
|
||||
// result, so fail() refuses the spec only from a branch of a conditional. A
|
||||
// quantity is scaled with q.mul(n). An expression may
|
||||
// return an int, a double, a quantity string or a quantity; a key that
|
||||
// evaluates to zero reserves nothing.
|
||||
// Its fields are owned together: expressions refer to bindings by name, so a
|
||||
// partial update could leave an expression whose binding is gone.
|
||||
// +structType=atomic
|
||||
// +kubebuilder:validation:XValidation:rule="!(has(self.exempt) && self.exempt) || (!has(self.__let__) && !has(self.resources))",message="an exempt declaration carries neither let nor resources"
|
||||
// +kubebuilder:validation:XValidation:rule="(has(self.exempt) && self.exempt) || (has(self.resources) && size(self.resources) > 0)",message="a declaration either is exempt or names at least one resource"
|
||||
type ApplicationDefinitionConsumption struct {
|
||||
// Exempt declares that the kind consumes nothing from a tenant's budget.
|
||||
// +optional
|
||||
Exempt bool `json:"exempt,omitempty"`
|
||||
|
||||
// Let binds intermediate expressions, evaluated in order. A binding
|
||||
// reads only the bindings before it; a resources expression reads them
|
||||
// all. The list is replaced as a whole, since a binding left behind by a
|
||||
// partial update could refer to one that is gone.
|
||||
// +optional
|
||||
// +listType=atomic
|
||||
// +kubebuilder:validation:MaxItems=64
|
||||
// +kubebuilder:validation:XValidation:rule="self.all(b, self.exists_one(c, c.name == b.name))",message="binding names must be unique"
|
||||
Let []ApplicationDefinitionConsumptionBinding `json:"let,omitempty"`
|
||||
|
||||
// Resources maps a quota key, as a Tenant's resourceQuotas spells it,
|
||||
// to the expression computing how much of it the application reserves.
|
||||
// +optional
|
||||
// +kubebuilder:validation:MaxProperties=64
|
||||
// +kubebuilder:validation:XValidation:rule="self.all(k, size(k) > 0)",message="a quota key must not be empty"
|
||||
Resources map[string]string `json:"resources,omitempty"`
|
||||
}
|
||||
|
||||
// ApplicationDefinitionConsumptionBinding names one intermediate expression.
|
||||
type ApplicationDefinitionConsumptionBinding struct {
|
||||
// Name is how later expressions refer to the binding. `values` is taken
|
||||
// by the application's values, and CEL's reserved words cannot name a
|
||||
// variable.
|
||||
// +kubebuilder:validation:Pattern=`^[a-zA-Z_][a-zA-Z0-9_]*$`
|
||||
// +kubebuilder:validation:MaxLength=63
|
||||
// +kubebuilder:validation:XValidation:rule="!(self in ['values', 'true', 'false', 'null', 'in', 'as', 'break', 'const', 'continue', 'else', 'for', 'function', 'if', 'import', 'let', 'loop', 'package', 'namespace', 'return', 'var', 'void', 'while'])",message="the name is reserved"
|
||||
Name string `json:"name"`
|
||||
// +kubebuilder:validation:MinLength=1
|
||||
Expression string `json:"expression"`
|
||||
}
|
||||
|
||||
type ApplicationDefinitionApplication struct {
|
||||
|
||||
Generated
+47
@@ -70,6 +70,48 @@ func (in *ApplicationDefinitionApplication) DeepCopy() *ApplicationDefinitionApp
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *ApplicationDefinitionConsumption) DeepCopyInto(out *ApplicationDefinitionConsumption) {
|
||||
*out = *in
|
||||
if in.Let != nil {
|
||||
in, out := &in.Let, &out.Let
|
||||
*out = make([]ApplicationDefinitionConsumptionBinding, len(*in))
|
||||
copy(*out, *in)
|
||||
}
|
||||
if in.Resources != nil {
|
||||
in, out := &in.Resources, &out.Resources
|
||||
*out = make(map[string]string, len(*in))
|
||||
for key, val := range *in {
|
||||
(*out)[key] = val
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ApplicationDefinitionConsumption.
|
||||
func (in *ApplicationDefinitionConsumption) DeepCopy() *ApplicationDefinitionConsumption {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(ApplicationDefinitionConsumption)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *ApplicationDefinitionConsumptionBinding) DeepCopyInto(out *ApplicationDefinitionConsumptionBinding) {
|
||||
*out = *in
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ApplicationDefinitionConsumptionBinding.
|
||||
func (in *ApplicationDefinitionConsumptionBinding) DeepCopy() *ApplicationDefinitionConsumptionBinding {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(ApplicationDefinitionConsumptionBinding)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *ApplicationDefinitionDashboard) DeepCopyInto(out *ApplicationDefinitionDashboard) {
|
||||
*out = *in
|
||||
@@ -241,6 +283,11 @@ func (in *ApplicationDefinitionSpec) DeepCopyInto(out *ApplicationDefinitionSpec
|
||||
*out = new(ApplicationDefinitionDashboard)
|
||||
(*in).DeepCopyInto(*out)
|
||||
}
|
||||
if in.Consumption != nil {
|
||||
in, out := &in.Consumption, &out.Consumption
|
||||
*out = new(ApplicationDefinitionConsumption)
|
||||
(*in).DeepCopyInto(*out)
|
||||
}
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ApplicationDefinitionSpec.
|
||||
|
||||
+69
@@ -62,6 +62,75 @@ spec:
|
||||
- plural
|
||||
- singular
|
||||
type: object
|
||||
consumption:
|
||||
description: |-
|
||||
Consumption declares what an application of this kind reserves from
|
||||
its tenant's quota budget, computed from its values. Unset means the
|
||||
kind has not declared it, which is not the same as consuming nothing:
|
||||
a kind that consumes nothing sets exempt.
|
||||
properties:
|
||||
exempt:
|
||||
description: Exempt declares that the kind consumes nothing from
|
||||
a tenant's budget.
|
||||
type: boolean
|
||||
let:
|
||||
description: |-
|
||||
Let binds intermediate expressions, evaluated in order. A binding
|
||||
reads only the bindings before it; a resources expression reads them
|
||||
all. The list is replaced as a whole, since a binding left behind by a
|
||||
partial update could refer to one that is gone.
|
||||
items:
|
||||
description: ApplicationDefinitionConsumptionBinding names one
|
||||
intermediate expression.
|
||||
properties:
|
||||
expression:
|
||||
minLength: 1
|
||||
type: string
|
||||
name:
|
||||
description: |-
|
||||
Name is how later expressions refer to the binding. `values` is taken
|
||||
by the application's values, and CEL's reserved words cannot name a
|
||||
variable.
|
||||
maxLength: 63
|
||||
pattern: ^[a-zA-Z_][a-zA-Z0-9_]*$
|
||||
type: string
|
||||
x-kubernetes-validations:
|
||||
- message: the name is reserved
|
||||
rule: '!(self in [''values'', ''true'', ''false'', ''null'',
|
||||
''in'', ''as'', ''break'', ''const'', ''continue'',
|
||||
''else'', ''for'', ''function'', ''if'', ''import'',
|
||||
''let'', ''loop'', ''package'', ''namespace'', ''return'',
|
||||
''var'', ''void'', ''while''])'
|
||||
required:
|
||||
- expression
|
||||
- name
|
||||
type: object
|
||||
maxItems: 64
|
||||
type: array
|
||||
x-kubernetes-list-type: atomic
|
||||
x-kubernetes-validations:
|
||||
- message: binding names must be unique
|
||||
rule: self.all(b, self.exists_one(c, c.name == b.name))
|
||||
resources:
|
||||
additionalProperties:
|
||||
type: string
|
||||
description: |-
|
||||
Resources maps a quota key, as a Tenant's resourceQuotas spells it,
|
||||
to the expression computing how much of it the application reserves.
|
||||
maxProperties: 64
|
||||
type: object
|
||||
x-kubernetes-validations:
|
||||
- message: a quota key must not be empty
|
||||
rule: self.all(k, size(k) > 0)
|
||||
type: object
|
||||
x-kubernetes-map-type: atomic
|
||||
x-kubernetes-validations:
|
||||
- message: an exempt declaration carries neither let nor resources
|
||||
rule: '!(has(self.exempt) && self.exempt) || (!has(self.__let__)
|
||||
&& !has(self.resources))'
|
||||
- message: a declaration either is exempt or names at least one resource
|
||||
rule: (has(self.exempt) && self.exempt) || (has(self.resources)
|
||||
&& size(self.resources) > 0)
|
||||
dashboard:
|
||||
description: Dashboard configuration for this resource
|
||||
properties:
|
||||
|
||||
@@ -99,6 +99,13 @@ func Compile(decl *Declaration, schema *structuralschema.Structural) (*Evaluator
|
||||
cel.Function(presetFunc,
|
||||
cel.Overload("preset_string", []*cel.Type{cel.StringType}, cel.MapType(cel.StringType, cel.DynType),
|
||||
cel.LateFunctionBinding())),
|
||||
// fail refuses a spec the kind's chart would refuse to render, with
|
||||
// the declaration's reason, rather than reserving for it.
|
||||
cel.Function("fail",
|
||||
cel.Overload("fail_string", []*cel.Type{cel.StringType}, cel.DynType,
|
||||
cel.UnaryBinding(func(msg ref.Val) ref.Val {
|
||||
return types.NewErr("%s", msg.(types.String))
|
||||
}))),
|
||||
cel.Function("quantity",
|
||||
cel.Overload("quantity_int", []*cel.Type{cel.IntType}, apiservercel.QuantityType,
|
||||
cel.UnaryBinding(func(v ref.Val) ref.Val {
|
||||
|
||||
@@ -19,6 +19,7 @@ package reservation
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
@@ -495,6 +496,32 @@ func TestEvaluateMulKeepsExactProducts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A declaration states a spec its chart refuses to render with fail(), so
|
||||
// that the refusal carries the declaration's own reason.
|
||||
func TestEvaluateFail(t *testing.T) {
|
||||
env := testEnvironment(t)
|
||||
// Keys evaluate in sorted order, so cpu succeeds before memory fails.
|
||||
ev, err := Compile(&Declaration{Resources: map[string]string{
|
||||
"cpu": `quantity('1')`,
|
||||
"memory": `has(values.memory) ? quantity(values.memory) : fail('memory is required')`,
|
||||
}}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := ev.Evaluate(map[string]any{}, env)
|
||||
if err == nil || !strings.Contains(err.Error(), "memory is required") {
|
||||
t.Errorf("got %v, want an error carrying the declared reason", err)
|
||||
}
|
||||
if got != nil {
|
||||
t.Errorf("a failed evaluation returned a partial reservation %v", got)
|
||||
}
|
||||
got, err = ev.Evaluate(map[string]any{"memory": "1Gi"}, env)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertList(t, "branch not taken", got, rl("cpu", "1", "memory", "1Gi"))
|
||||
}
|
||||
|
||||
// A null is absent to Helm, which deletes the key it nulls, so it must be
|
||||
// absent to an expression too, including where the schema has no default to
|
||||
// put in its place.
|
||||
|
||||
@@ -30,6 +30,8 @@ import (
|
||||
"sort"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
|
||||
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
|
||||
)
|
||||
|
||||
// Quota keys for KubeVirt instance-type classes. A shared vCPU and a pinned
|
||||
@@ -45,31 +47,18 @@ const (
|
||||
KeyHugepagesPrefix = "hugepages-"
|
||||
)
|
||||
|
||||
// Declaration is how a kind's reservation is computed from its values.
|
||||
// Declaration is how a kind's reservation is computed from its values. It is
|
||||
// the type an ApplicationDefinition carries, so a definition is compiled
|
||||
// as read, with no conversion in between.
|
||||
//
|
||||
// Exactly one of Exempt or Resources is meaningful: a kind that consumes
|
||||
// nothing says so with Exempt rather than with an empty Resources map, so
|
||||
// that a zero reservation is always a reviewed statement and never an
|
||||
// omission.
|
||||
type Declaration struct {
|
||||
// Exempt declares that the kind consumes nothing from a tenant's budget.
|
||||
Exempt bool `json:"exempt,omitempty"`
|
||||
|
||||
// Let binds intermediate CEL expressions, evaluated in order, each one
|
||||
// visible to the later bindings and to every Resources expression.
|
||||
Let []Binding `json:"let,omitempty"`
|
||||
|
||||
// Resources maps a quota key to the CEL expression computing the
|
||||
// amount of it the application reserves. An expression may return an
|
||||
// int, a double, a quantity string or a quantity.
|
||||
Resources map[string]string `json:"resources,omitempty"`
|
||||
}
|
||||
type Declaration = cozyv1alpha1.ApplicationDefinitionConsumption
|
||||
|
||||
// Binding names one intermediate CEL expression of a Declaration.
|
||||
type Binding struct {
|
||||
Name string `json:"name"`
|
||||
Expression string `json:"expression"`
|
||||
}
|
||||
type Binding = cozyv1alpha1.ApplicationDefinitionConsumptionBinding
|
||||
|
||||
// ResourceList is a reservation, keyed by quota key.
|
||||
type ResourceList map[string]resource.Quantity
|
||||
|
||||
Reference in new issue
Block a user