Testing
To facilitate testing the results of functions and rules, we provide an API endpoint. This endpoint allows you to send the data collected by a Fitness Signal and simulate the score it would produce after being processed by the Engine.
Example
Let's illustrate how to use this API with a sample configuration that measures the number of replicas for each deployment.
kind: FitnessSensor
apiVersion: fitness.orcasio.com/v1alpha3
metadata:
name: kube-deploy
namespace: sample
spec:
sensor: sensor://fitness.orcasio.net/kubernetes
source: deployment
configmap: kubernetes
secret: kubernetes
trigger:
name: kubewatch
kubewatch:
apiVersion: "apps/v1"
kind: "Deployment"
namespace: sample
enabled: true
tags:
env: dev
org: it
clustername: fin
kind: FitnessFunction
apiVersion: fitness.orcasio.com/v1alpha3
metadata:
name: avail-deploy
namespace: sample
spec:
sensor: sensor://fitness.orcasio.net/kubernetes
source: deployment
filters:
.data.kind: Deployment
aspect: Availability
weight: 8
rule: avail-deploy
enabled: true
dryrun: false
tags:
app: ".data.metadata.name"
name: ".data..metadata.name"
kind: OrcasRule
apiVersion: orcasio.com/v1alpha3
metadata:
name: avail-deploy
namespace: sample
spec:
version: v1.0.0
rule:
spec:
replicas:
orcas:enabled: true
orcas:measure: "replicas"
orcas:value: |
# golang
v, _ := toInt(request.Value)
response.Measure = "replicas"
switch v {
case 0:
response.Message = "No active replicas"
response.FitnessPercent = 0
response.MutatedValue = 3
case 1:
response.Message = "Does not meet the minimum number of replicas"
response.FitnessPercent = 25
case 2:
response.Message = "Does not meet the minimum number of replicas"
response.FitnessPercent = 50
default:
response.Message = "Meets the minimum number of replicas"
response.FitnessPercent = 100
}
# webhook
# url: http://localhost:8090/webhook
When the sensor generates a signal, you can observe a log similar to this:
15:28:09 DBG orcas fitness signal group=deployment-namesample name=orcas-sample-deployment-name-1694273288942 sensor=sensor://fitness.orcasio.net/kubernetes sensor_id=aa664067-cc19-4e21-b47f-d0cbdf8647ae signal={"apiVersion": "apps/v1","kind": "Deployment","metadata": {"creationTimestamp": "2023-06-01T14:58:57Z","generation": 4,"managedFields": null,"name": "deployment-name","namespace": "sample","resourceVersion": "169798","uid": "97a2e70f-68ad-42df-89c5-3c1bd3ad441c"},"spec": { "replicas": 0 },"status": {}} source=deployment
To use the API, extract the JSON under signal and send it as signalData in the request, like this:
curl --location --request POST '<API-URL>/api/inventory/functions/avail-deploy/test' \
--header 'Content-Type: application/json' \
--header 'orcas-token: <ORCAS-TOKEN>' \
--data-raw '{
"data": {
"signalData": {
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"creationTimestamp": "2023-06-01T14:58:57Z",
"generation": 4,
"managedFields": null,
"name": "deployment-name",
"namespace": "sample",
"resourceVersion": "169798",
"uid": "97a2e70f-68ad-42df-89c5-3c1bd3ad441c"
},
"spec": {
"replicas": 0
},
"status": {}
}
}
}'
The response will look similar to this:
{
"data": {
"function": "avail-deploy",
"ruleName": "avail-deploy",
"tags": {
"app": "deployment-name",
"name": "deployment-name",
"namespace": "sample"
},
"scores": [
{
"measure": "replicas",
"measurePath": ".spec.replicas",
"weight": 8,
"fitnessPercent": 0,
"message": "No active replicas",
"description": "<nil>",
"tags": null
}
]
}
}
Swagger specification
For detailed API documentation, please refer to the Swagger specification below: