Files
cozystack/pkg/reservation/environment.go
T
Mattia Eleuteri 89aba6e104 feat(api): add the reservation evaluator for tenant quotas
Tenant quotas are declared in instance-type units but enforced against
ResourceQuota.status.used, which counts virt-launcher pods with their
additive virtualization overhead and drifts without recomputation. The
accepted tenant-quota-reservation design makes the declared reservation
the accounting authority instead; this is its first phase, the
evaluator, with no consumer yet.

A kind describes its consumption as CEL over its values, so the
evaluator carries no per-kind knowledge and an out-of-tree kind
participates without a change here. Instance types resolve to scalars
keyed by class, read from the type's spec rather than its name, because
a pinned core, a hugepage and an overcommitted page are not the same
capacity as a shared vCPU or ordinary memory. KubeVirt pins the
emulator thread's parity core per type, from the annotations the type
hands to its VMs, so the classifier reads it there too. Anything that
does not resolve is an error, never a smaller figure.

Stored values carry only the keys a tenant wrote, so the evaluator
defaults them first. The defaulting code moves out of the application
registry into its own package so both read the same defaults. Defaulting
replaces a value of the wrong type with the schema's default, which
would turn a malformed spec into a smaller reservation, so values are
type-checked against the schema before it runs.

Assisted-by: LLM
Signed-off-by: Mattia Eleuteri <mattia@hidora.io>
2026-11-09 11:35:53 +01:00

85 lines
2.6 KiB
Go

/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package reservation
import (
"errors"
"fmt"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// ErrNotFound is wrapped by an Environment when a named instance type or
// preset does not exist. The evaluator fails closed on it rather than
// reserving nothing.
var ErrNotFound = errors.New("not found")
// Environment resolves the cluster objects a declaration may reference by
// name. Implementations must be safe for concurrent use and return a list
// the caller may keep: the evaluator does not copy it.
type Environment interface {
Instancetype(name string) (ResourceList, error)
Preset(name string) (ResourceList, error)
}
// StaticEnvironment is an Environment over fixed tables.
type StaticEnvironment struct {
Instancetypes map[string]ResourceList
Presets map[string]ResourceList
}
var _ Environment = (*StaticEnvironment)(nil)
// NewStaticEnvironment classifies every VirtualMachineClusterInstancetype in
// instancetypes and pairs the result with presets. One unclassifiable type
// fails the whole environment, so that a broken catalog is noticed when it is
// loaded rather than when a tenant first orders that type.
func NewStaticEnvironment(instancetypes []map[string]any, presets map[string]ResourceList) (*StaticEnvironment, error) {
env := &StaticEnvironment{
Instancetypes: make(map[string]ResourceList, len(instancetypes)),
Presets: presets,
}
for _, obj := range instancetypes {
rl, err := ClassifyInstancetype(obj)
if err != nil {
return nil, err
}
name, _, _ := unstructured.NestedString(obj, "metadata", "name")
if name == "" {
return nil, fmt.Errorf("instance type without metadata.name")
}
env.Instancetypes[name] = rl
}
return env, nil
}
func (e *StaticEnvironment) Instancetype(name string) (ResourceList, error) {
rl, ok := e.Instancetypes[name]
if !ok {
return nil, fmt.Errorf("instance type %q: %w", name, ErrNotFound)
}
return rl, nil
}
func (e *StaticEnvironment) Preset(name string) (ResourceList, error) {
rl, ok := e.Presets[name]
if !ok {
return nil, fmt.Errorf("resource preset %q: %w", name, ErrNotFound)
}
return rl, nil
}