Skip to content

Commit

Permalink
feat(metrics-operator): introduce range operators in AnalysisDefiniti…
Browse files Browse the repository at this point in the history
…on (#1976)

Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT authored Aug 30, 2023
1 parent c7697bf commit 7fb8952
Show file tree
Hide file tree
Showing 11 changed files with 523 additions and 2 deletions.
100 changes: 100 additions & 0 deletions .github/scripts/.helm-tests/default/result.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,56 @@ spec:
required:
- fixedValue
type: object
inRange:
description: InRange represents operator checking the
value is inclusively in the defined range, e.g. 2
<= x <= 5
properties:
highBound:
anyOf:
- type: integer
- type: string
description: HighBound defines the higher bound of
the range
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
lowBound:
anyOf:
- type: integer
- type: string
description: LowBound defines the lower bound of
the range
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
required:
- highBound
- lowBound
type: object
notInRange:
description: NotInRange represents operator checking
the value is exclusively out of the defined range,
e.g. x < 2 AND x > 5
properties:
highBound:
anyOf:
- type: integer
- type: string
description: HighBound defines the higher bound of
the range
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
lowBound:
anyOf:
- type: integer
- type: string
description: LowBound defines the lower bound of
the range
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
required:
- highBound
- lowBound
type: object
type: object
warning:
description: Warning defines limits where the result does
Expand Down Expand Up @@ -443,6 +493,56 @@ spec:
required:
- fixedValue
type: object
inRange:
description: InRange represents operator checking the
value is inclusively in the defined range, e.g. 2
<= x <= 5
properties:
highBound:
anyOf:
- type: integer
- type: string
description: HighBound defines the higher bound of
the range
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
lowBound:
anyOf:
- type: integer
- type: string
description: LowBound defines the lower bound of
the range
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
required:
- highBound
- lowBound
type: object
notInRange:
description: NotInRange represents operator checking
the value is exclusively out of the defined range,
e.g. x < 2 AND x > 5
properties:
highBound:
anyOf:
- type: integer
- type: string
description: HighBound defines the higher bound of
the range
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
lowBound:
anyOf:
- type: integer
- type: string
description: LowBound defines the lower bound of
the range
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
required:
- highBound
- lowBound
type: object
type: object
type: object
weight:
Expand Down
17 changes: 17 additions & 0 deletions docs/content/en/docs/crd-ref/metrics/v1alpha3/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ _Appears in:_
| `greaterThan` _[OperatorValue](#operatorvalue)_ | GreaterThan represents '>' operator |
| `greaterThanOrEqual` _[OperatorValue](#operatorvalue)_ | GreaterThanOrEqual represents '>=' operator |
| `equalTo` _[OperatorValue](#operatorvalue)_ | EqualTo represents '==' operator |
| `inRange` _[RangeValue](#rangevalue)_ | InRange represents operator checking the value is inclusively in the defined range, e.g. 2 <= x <= 5 |
| `notInRange` _[RangeValue](#rangevalue)_ | NotInRange represents operator checking the value is exclusively out of the defined range, e.g. x < 2 AND x > 5 |


#### OperatorValue
Expand Down Expand Up @@ -388,6 +390,21 @@ _Appears in:_
| `aggregation` _string_ | Aggregation defines the type of aggregation function to be applied on the data. Accepted values: p90, p95, p99, max, min, avg, median |


#### RangeValue



RangeValue represents a range which the value should fit

_Appears in:_
- [Operator](#operator)

| Field | Description |
| --- | --- |
| `lowBound` _Quantity_ | LowBound defines the lower bound of the range |
| `highBound` _Quantity_ | HighBound defines the higher bound of the range |


#### Target


Expand Down
100 changes: 100 additions & 0 deletions helm/chart/templates/analysisdefinition-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,56 @@ spec:
required:
- fixedValue
type: object
inRange:
description: InRange represents operator checking the
value is inclusively in the defined range, e.g. 2
<= x <= 5
properties:
highBound:
anyOf:
- type: integer
- type: string
description: HighBound defines the higher bound of
the range
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
lowBound:
anyOf:
- type: integer
- type: string
description: LowBound defines the lower bound of
the range
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
required:
- highBound
- lowBound
type: object
notInRange:
description: NotInRange represents operator checking
the value is exclusively out of the defined range,
e.g. x < 2 AND x > 5
properties:
highBound:
anyOf:
- type: integer
- type: string
description: HighBound defines the higher bound of
the range
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
lowBound:
anyOf:
- type: integer
- type: string
description: LowBound defines the lower bound of
the range
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
required:
- highBound
- lowBound
type: object
type: object
warning:
description: Warning defines limits where the result does
Expand Down Expand Up @@ -204,6 +254,56 @@ spec:
required:
- fixedValue
type: object
inRange:
description: InRange represents operator checking the
value is inclusively in the defined range, e.g. 2
<= x <= 5
properties:
highBound:
anyOf:
- type: integer
- type: string
description: HighBound defines the higher bound of
the range
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
lowBound:
anyOf:
- type: integer
- type: string
description: LowBound defines the lower bound of
the range
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
required:
- highBound
- lowBound
type: object
notInRange:
description: NotInRange represents operator checking
the value is exclusively out of the defined range,
e.g. x < 2 AND x > 5
properties:
highBound:
anyOf:
- type: integer
- type: string
description: HighBound defines the higher bound of
the range
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
lowBound:
anyOf:
- type: integer
- type: string
description: LowBound defines the lower bound of
the range
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
required:
- highBound
- lowBound
type: object
type: object
type: object
weight:
Expand Down
12 changes: 12 additions & 0 deletions metrics-operator/api/v1alpha3/analysisdefinition_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ type OperatorValue struct {
FixedValue resource.Quantity `json:"fixedValue" yaml:"fixedValue"`
}

// RangeValue represents a range which the value should fit
type RangeValue struct {
// LowBound defines the lower bound of the range
LowBound resource.Quantity `json:"lowBound"`
// HighBound defines the higher bound of the range
HighBound resource.Quantity `json:"highBound"`
}

// Operator specifies the supported operators for value comparisons
type Operator struct {
// LessThanOrEqual represents '<=' operator
Expand All @@ -81,6 +89,10 @@ type Operator struct {
GreaterThanOrEqual *OperatorValue `json:"greaterThanOrEqual,omitempty" yaml:"greaterThanOrEqual,omitempty"`
// EqualTo represents '==' operator
EqualTo *OperatorValue `json:"equalTo,omitempty" yaml:"equalTo,omitempty"`
// InRange represents operator checking the value is inclusively in the defined range, e.g. 2 <= x <= 5
InRange *RangeValue `json:"inRange,omitempty" yaml:"inRange,omitempty"`
// NotInRange represents operator checking the value is exclusively out of the defined range, e.g. x < 2 AND x > 5
NotInRange *RangeValue `json:"notInRange,omitempty" yaml:"notInRange,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
20 changes: 20 additions & 0 deletions metrics-operator/api/v1alpha3/analysisdefinition_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func (t *Target) validate() error {
return nil
}

//nolint:gocyclo
func (o *Operator) validate() error {
counter := 0
if o.LessThan != nil {
Expand All @@ -117,11 +118,30 @@ func (o *Operator) validate() error {
if o.EqualTo != nil {
counter++
}
if o.InRange != nil {
counter++
}
if o.NotInRange != nil {
counter++
}
if counter > 1 {
return fmt.Errorf("Operator: multiple operators can not be set")
}
if counter == 0 {
return fmt.Errorf("Operator: no operator set")
}
if o.InRange != nil {
return o.InRange.validate()
}
if o.NotInRange != nil {
return o.NotInRange.validate()
}
return nil
}

func (r *RangeValue) validate() error {
if r.LowBound.AsApproximateFloat64() >= r.HighBound.AsApproximateFloat64() {
return fmt.Errorf("RangeValue: lower bound of the range needs to be smaller than higher bound")
}
return nil
}
Loading

0 comments on commit 7fb8952

Please sign in to comment.