Skip to content

Commit afd439a

Browse files
authored
feat: Add new feature formula for SO (kedacore#1198)
1 parent be9d58e commit afd439a

File tree

1 file changed

+86
-4
lines changed

1 file changed

+86
-4
lines changed

content/docs/2.12/concepts/scaling-deployments.md

+86-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The only constraint is that the target `Custom Resource` must define `/scale` [s
2929

3030
## ScaledObject spec
3131

32-
This specification describes the `ScaledObject` Custom Resource definition which is used to define how KEDA should scale your application and what the triggers are. The `.spec.ScaleTargetRef` section holds the reference to the target resource, ie. `Deployment`, `StatefulSet` or `Custom Resource`.
32+
This specification describes the `ScaledObject` Custom Resource definition which is used to define how KEDA should scale your application and what the triggers are. The `.spec.ScaleTargetRef` section holds the reference to the target resource, ie. `Deployment`, `StatefulSet` or `Custom Resource`.
3333

3434
[`scaledobject_types.go`](https://github.com/kedacore/keda/blob/main/apis/keda/v1alpha1/scaledobject_types.go)
3535

@@ -49,7 +49,7 @@ spec:
4949
envSourceContainerName: {container-name} # Optional. Default: .spec.template.spec.containers[0]
5050
pollingInterval: 30 # Optional. Default: 30 seconds
5151
cooldownPeriod: 300 # Optional. Default: 300 seconds
52-
idleReplicaCount: 0 # Optional. Default: ignored, must be less than minReplicaCount
52+
idleReplicaCount: 0 # Optional. Default: ignored, must be less than minReplicaCount
5353
minReplicaCount: 1 # Optional. Default: 0
5454
maxReplicaCount: 100 # Optional. Default: 100
5555
fallback: # Optional. Section to specify fallback options
@@ -113,7 +113,7 @@ The `cooldownPeriod` only applies after a trigger occurs; when you first create
113113
#### idleReplicaCount
114114

115115
```yaml
116-
idleReplicaCount: 0 # Optional. Default: ignored, must be less than minReplicaCount
116+
idleReplicaCount: 0 # Optional. Default: ignored, must be less than minReplicaCount
117117
```
118118

119119
> 💡 **NOTE:** Due to limitations in HPA controller the only supported value for this property is 0, it will not work correctly otherwise. See this [issue](https://github.com/kedacore/keda/issues/2314) for more details.
@@ -167,7 +167,7 @@ advanced:
167167
restoreToOriginalReplicaCount: true/false # Optional. Default: false
168168
```
169169

170-
This property specifies whether the target resource (`Deployment`, `StatefulSet`,...) should be scaled back to original replicas count, after the `ScaledObject` is deleted.
170+
This property specifies whether the target resource (`Deployment`, `StatefulSet`,...) should be scaled back to original replicas count, after the `ScaledObject` is deleted.
171171
Default behavior is to keep the replica count at the same number as it is in the moment of `ScaledObject's` deletion.
172172

173173
For example a `Deployment` with `3 replicas` is created, then `ScaledObject` is created and the `Deployment` is scaled by KEDA to `10 replicas`. Then `ScaledObject` is deleted:
@@ -201,6 +201,29 @@ Starting from Kubernetes v1.18 the autoscaling API allows scaling behavior to be
201201

202202
**Assumptions:** KEDA must be running on Kubernetes cluster v1.18+, in order to be able to benefit from this setting.
203203

204+
---
205+
206+
```yaml
207+
advanced:
208+
scalingModifiers: # Optional. Section to specify scaling modifiers
209+
target: {target-value-to-scale-on} # Mandatory. New target if metrics are anyhow composed together
210+
formula: {formula-for-fetched-metrics} # Mandatory. Formula for calculation
211+
```
212+
213+
**`scalingModifiers`**
214+
215+
The `scalingModifiers` is optional and **experimental**. If defined, both `target` and `formula` are mandatory. Using this structure creates `composite-metric` for the HPA that will replace all requests for external metrics and handle them internally. With `scalingModifiers` each trigger used in the `formula` **must** have a name defined.
216+
217+
**`scalingModifiers.target`**
218+
219+
`target` defines new target value to scale on for the composed metric. All scaler metrics must be of the same type in order for ScaledObject to be successfully validated.
220+
221+
**`scalingModifiers.formula`**
222+
223+
`formula` composes metrics together and allows them to be modified/manipulated. It accepts mathematical/conditional statements using [this external project](https://github.com/antonmedv/expr). If the `fallback` scaling feature is in effect, the `formula` will NOT modify its metrics (therefore it modifies metrics only when all of their triggers are healthy). Complete language definition of `expr` package can be found [here](https://expr.medv.io/docs/Language-Definition). Formula must return a single value (not boolean).
224+
225+
For examples of this feature see section [Scaling Modifiers](#scaling-modifiers-experimental) below.
226+
204227
---
205228
#### triggers
206229
```yaml
@@ -245,6 +268,65 @@ metadata:
245268

246269
The presensce of this annotation will pause autoscaling no matter what number of replicas is provided. The above annotation will scale your current workload to 0 replicas and pause autoscaling. You can set the value of replicas for an object to be paused at to any arbitary number. To enable autoscaling again, simply remove the annotation from the `ScaledObject` definition.
247270

271+
272+
### Scaling Modifiers (Experimental)
273+
274+
**Example: compose average value**
275+
276+
```yaml
277+
advanced:
278+
scalingModifiers:
279+
formula: "(trig_one + trig_two)/2"
280+
target: "2"
281+
...
282+
triggers:
283+
- type: kubernetes-workload
284+
name: trig_one
285+
metadata:
286+
podSelector: 'pod=workload-test'
287+
value: '1'
288+
- type: metrics-api
289+
name: trig_two
290+
metadata:
291+
targetValue: "2"
292+
url: "https://mockbin.org/bin/336a8d99-9e09-4f1f-979d-851a6d1b1423"
293+
valueLocation: "tasks"
294+
```
295+
296+
Formula composes 2 given metrics from 2 triggers `kubernetes-workload` named `trig_one` and `metrics-api` named `trig_two` together as an average value and returns one final metric which is used to make autoscaling decisions on.
297+
298+
**Example: ternary operator**
299+
300+
```yaml
301+
advanced:
302+
scalingModifiers:
303+
formula: "trig_one > 2 ? trig_one + trig_two : 1"
304+
```
305+
306+
If metric value of trigger `trig_one` is more than 2, then return `trig_one` + `trig_two` otherwise return 1.
307+
308+
**Example: count function**
309+
310+
```yaml
311+
advanced:
312+
scalingModifiers:
313+
formula: "count([trig_one,trig_two,trig_three],{#>1}) > 1 ? 5 : 0"
314+
```
315+
316+
If atleast 2 metrics (from the list `trig_one`,`trig_two`,`trig_three`) have value of more than 1, then return 5, otherwise return 0
317+
318+
**Example: nested conditions and operators**
319+
320+
```yaml
321+
advanced:
322+
scalingModifiers:
323+
formula: "trig_one < 2 ? trig_one+trig_two >= 2 ? 5 : 10 : 0"
324+
```
325+
326+
Conditions can be used within another condition as well.
327+
If value of `trig_one` is less than 2 AND `trig_one`+`trig_two` is atleast 2 then return 5, if only the first is true return 10, if the first condition is false then return 0.
328+
329+
Complete language definition of `expr` package can be found [here](https://expr.medv.io/docs/Language-Definition). Formula must return a single value (not boolean)
248330
### Activating and Scaling thresholds
249331

250332
To give a consistent solution to this problem, KEDA has 2 different phases during the autoscaling process.

0 commit comments

Comments
 (0)