Documentation
¶
Overview ¶
Package resource_properties provides several helpers to work with kubernetes resource requests and limits, as well as other quantities that would be specific to a given kubernetes v1.ResourceName
Unlike kube's APIs, requests and limits are programmatically the same, as well as other quantities, which greatly reduces tedium when doing arithmetic on those.
It departs from v1 resource handling by leaning heavily into floats, with the round-trip issues that come with it, even though some mitigations are provided.
Index ¶
- Constants
- type ResourceKind
- type ResourceProperties
- func (rp *ResourceProperties) Add(operand *ResourceProperties)
- func (rp *ResourceProperties) AddResourceRequirements(reqs *corev1.ResourceRequirements)
- func (rp *ResourceProperties) All() iter.Seq[*ResourcePropertyBinding]
- func (rp *ResourceProperties) Bind(bind ResourcePropertyBinding)
- func (rp *ResourceProperties) BindPropertyFloat(kind ResourceKind, prop ResourceProperty, res corev1.ResourceName, ...)
- func (rp *ResourceProperties) BindPropertyString(kind ResourceKind, prop ResourceProperty, res corev1.ResourceName, ...) error
- func (rp *ResourceProperties) ClampRequestsAndLimits(userSettings *ResourceProperties)
- func (rp *ResourceProperties) Div(operand *ResourceProperties) *ResourceProperties
- func (rp *ResourceProperties) ForceLimitAboveRequest()
- func (rp *ResourceProperties) GetValue(prop ResourceProperty, res corev1.ResourceName) (float64, bool)
- func (rp *ResourceProperties) Mul(operand *ResourceProperties) *ResourceProperties
- func (rp *ResourceProperties) String() string
- type ResourceProperty
- type ResourcePropertyBinding
- func (rpb *ResourcePropertyBinding) HumanValue() string
- func (rpb *ResourcePropertyBinding) Property() ResourceProperty
- func (rpb *ResourcePropertyBinding) PropertyJsonPath(containerIndex int) string
- func (rpb *ResourcePropertyBinding) ResourceName() corev1.ResourceName
- func (rpb *ResourcePropertyBinding) SetValue(v float64)
- func (rpb *ResourcePropertyBinding) String() string
- func (rpb *ResourcePropertyBinding) Value() float64
Constants ¶
const ( ResourceInvalid ResourceProperty = "invalid" ResourceRequests ResourceProperty = "requests" ResourceLimits ResourceProperty = "limits" ResourcePodMinimum ResourceProperty = "pod-minimum" ResourcePodMaximum ResourceProperty = "pod-maximum" ResourceFraction ResourceKind = "fraction" ResourceQuantity ResourceKind = "quantity" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ResourceKind ¶
type ResourceKind string
type ResourceProperties ¶
type ResourceProperties struct {
// contains filtered or unexported fields
}
func New ¶
func New() *ResourceProperties
func NewFromAnnotations ¶
func NewFromAnnotations(annotations map[string]string) (error, *ResourceProperties)
func (*ResourceProperties) Add ¶
func (rp *ResourceProperties) Add(operand *ResourceProperties)
Add merges two sets of properties into the receiver, by adding properties values from the added props, creating new bindings if needed. NB: Unlike the Div and Mul operators, the Add operators works in-place.
func (*ResourceProperties) AddResourceRequirements ¶
func (rp *ResourceProperties) AddResourceRequirements(reqs *corev1.ResourceRequirements)
AddResourceRequirements merge a Kubernetes ResourceRequirements to the props
func (*ResourceProperties) All ¶
func (rp *ResourceProperties) All() iter.Seq[*ResourcePropertyBinding]
All iterates over all bindings
func (*ResourceProperties) Bind ¶
func (rp *ResourceProperties) Bind(bind ResourcePropertyBinding)
Bind registers a new binding, potentially removing a pre-existing binding. The pass-by-value is intentional.
func (*ResourceProperties) BindPropertyFloat ¶
func (rp *ResourceProperties) BindPropertyFloat(kind ResourceKind, prop ResourceProperty, res corev1.ResourceName, value float64)
BindPropertyFloat binds a given resource property to a float value
func (*ResourceProperties) BindPropertyString ¶
func (rp *ResourceProperties) BindPropertyString(kind ResourceKind, prop ResourceProperty, res corev1.ResourceName, value string) error
BindPropertyString binds a given resource property to a float value by parsing it from a string. The parsing is different whether the kind is a fraction or a quantity:
- For fractions, a floating point number between 0 and 1 (excluded) is expected. I'm ~into the idea of support N/M rationals, but that might be purely a curiosity thing.
- For quantities, any number that Kubernetes would accept will do. That includes many quantities with SI suffixes, like 100m or 2G
func (*ResourceProperties) ClampRequestsAndLimits ¶
func (rp *ResourceProperties) ClampRequestsAndLimits(userSettings *ResourceProperties)
ClampRequestsAndLimits goes over every bound property. If, for any given resourceName, a limit or a requests needs to be clamped according to the matching minimum or maximum from userSettings, it will be.
func (*ResourceProperties) Div ¶
func (rp *ResourceProperties) Div(operand *ResourceProperties) *ResourceProperties
Div produces new resource properties by dividing the receiver values by the operand values
Only props defined on the receiver will be used. If no matching prop is defined on the operand, this operation will panic, like a division by zero would.
If some props are defined on the operand but not on the receiver, then these props will be absent from the result.
The ResourceKind algebra is as follows: - quantity / quantity => fraction - fraction / quantity => quantity (weird way to put things, consider using Mul instead) - quantity / fraction => quantity (weird way to put things, consider using Mul instead) - fraction / fraction => fraction
func (*ResourceProperties) ForceLimitAboveRequest ¶
func (rp *ResourceProperties) ForceLimitAboveRequest()
ForceLimitAboveRequest goes over every bound property. If, for any given resourceName, a limit would be below the request, it is mutated to be equal to the request instead.
This is - not great - but it's a necessary evil when working with floats and their ever-perplexing rounding oddities. We could rework our whole package to be able to work with rational numbers expressed as fractions to mitigate most of it, but at some point, node resources will have to be divided.
func (*ResourceProperties) GetValue ¶
func (rp *ResourceProperties) GetValue(prop ResourceProperty, res corev1.ResourceName) (float64, bool)
GetValue returns (value, true) of an existing binding, or (0, false) for an unbound prop
func (*ResourceProperties) Mul ¶
func (rp *ResourceProperties) Mul(operand *ResourceProperties) *ResourceProperties
Mul produces new resource properties by multiplying the receiver values by the operand values Props unset on either side of the operation are unset on the result rather than set to zero.
The output kind depends on the input kind : multiplying two fractions produces another fraction, while any other combination produces a quantity.
func (*ResourceProperties) String ¶
func (rp *ResourceProperties) String() string
type ResourceProperty ¶
type ResourceProperty string
type ResourcePropertyBinding ¶
type ResourcePropertyBinding struct {
// contains filtered or unexported fields
}
func NewBinding ¶
func NewBinding(resourceKind ResourceKind, resourceProp ResourceProperty, resourceName corev1.ResourceName, value float64) *ResourcePropertyBinding
func (*ResourcePropertyBinding) HumanValue ¶
func (rpb *ResourcePropertyBinding) HumanValue() string
HumanValue converts from the internal float to a string that looks like the usual suffixed representation, i.e. 2G or 200m
func (*ResourcePropertyBinding) Property ¶
func (rpb *ResourcePropertyBinding) Property() ResourceProperty
func (*ResourcePropertyBinding) PropertyJsonPath ¶
func (rpb *ResourcePropertyBinding) PropertyJsonPath(containerIndex int) string
func (*ResourcePropertyBinding) ResourceName ¶
func (rpb *ResourcePropertyBinding) ResourceName() corev1.ResourceName
func (*ResourcePropertyBinding) SetValue ¶
func (rpb *ResourcePropertyBinding) SetValue(v float64)
func (*ResourcePropertyBinding) String ¶
func (rpb *ResourcePropertyBinding) String() string
func (*ResourcePropertyBinding) Value ¶
func (rpb *ResourcePropertyBinding) Value() float64