Skip to content

Commit

Permalink
feat(metrics-operator): add query to the analysis result (#2188)
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT authored Sep 28, 2023
1 parent bab605e commit 233aac4
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .github/scripts/.helm-tests/default/result.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ spec:
required:
- name
type: object
query:
description: Query represents the executed query
type: string
value:
description: Value is the value the provider returned
type: string
Expand Down
1 change: 1 addition & 0 deletions docs/content/en/docs/crd-ref/metrics/v1alpha3/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ _Appears in:_
| Field | Description |
| --- | --- |
| `objectiveReference` _[ObjectReference](#objectreference)_ | Objective store reference to corresponding objective template |
| `query` _string_ | Query represents the executed query |
| `value` _string_ | Value is the value the provider returned |
| `errMsg` _string_ | ErrMsg stores any possible error at retrieval time |

Expand Down
3 changes: 3 additions & 0 deletions helm/chart/templates/analysis-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ spec:
required:
- name
type: object
query:
description: Query represents the executed query
type: string
value:
description: Value is the value the provider returned
type: string
Expand Down
2 changes: 2 additions & 0 deletions metrics-operator/api/v1alpha3/analysis_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type AnalysisSpec struct {
type ProviderResult struct {
// Objective store reference to corresponding objective template
Objective ObjectReference `json:"objectiveReference,omitempty"`
// Query represents the executed query
Query string `json:"query,omitempty"`
// Value is the value the provider returned
Value string `json:"value,omitempty"`
// ErrMsg stores any possible error at retrieval time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ spec:
required:
- name
type: object
query:
description: Query represents the executed query
type: string
value:
description: Value is the value the provider returned
type: string
Expand Down
2 changes: 2 additions & 0 deletions metrics-operator/controllers/analysis/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ func Test_extractMissingObjectives(t *testing.T) {
existingValues := map[string]metricsapi.ProviderResult{
common.ComputeKey(ad.Spec.Objectives[1].AnalysisValueTemplateRef): {
Value: "1.0",
Query: "query",
},
common.ComputeKey(ad.Spec.Objectives[2].AnalysisValueTemplateRef): {
ErrMsg: "error",
Expand All @@ -551,6 +552,7 @@ func Test_extractMissingObjectives(t *testing.T) {
require.Equal(t, needToRetry, todo[1].AnalysisValueTemplateRef)
require.Len(t, existing, 1)
require.Equal(t, "1.0", existing[common.ComputeKey(done)].Value)
require.Equal(t, "query", existing[common.ComputeKey(done)].Query)

// verify that the analysisDefinition has not been changed
require.Len(t, ad.Spec.Objectives, 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (oe ObjectivesEvaluator) Evaluate(ctx context.Context, providerType string,
}
result := metricsapi.ProviderResult{
Objective: o.Objective.AnalysisValueTemplateRef,
Query: o.Query,
Value: value,
ErrMsg: strErr,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestEvaluate(t *testing.T) {
Name: "mytemp",
Namespace: "default",
},
Query: "query_fake_metric",
Value: "10",
ErrMsg: "",
},
Expand Down Expand Up @@ -78,6 +79,7 @@ func TestEvaluate(t *testing.T) {
Namespace: "default",
},
Value: "",
Query: "query_fake_metric",
ErrMsg: "something bad",
},
expectedError: "something bad",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestAnalysisEvaluator_Evaluate(t *testing.T) {
{
Result: types.TargetResult{},
Value: 5.0,
Query: "query",
Score: 10.0,
Error: nil,
},
Expand All @@ -68,6 +69,7 @@ func TestAnalysisEvaluator_Evaluate(t *testing.T) {
EvaluateFunc: func(values map[string]v1alpha3.ProviderResult, objective *v1alpha3.Objective) types.ObjectiveResult {
return types.ObjectiveResult{
Result: types.TargetResult{},
Query: "query",
Value: 5.0,
Score: 10.0,
Error: nil,
Expand Down Expand Up @@ -103,12 +105,14 @@ func TestAnalysisEvaluator_Evaluate(t *testing.T) {
{
Result: types.TargetResult{},
Value: 5.0,
Query: "query",
Score: 10.0,
Error: nil,
},
{
Result: types.TargetResult{},
Value: 5.0,
Query: "query",
Score: 10.0,
Error: nil,
},
Expand All @@ -119,6 +123,7 @@ func TestAnalysisEvaluator_Evaluate(t *testing.T) {
return types.ObjectiveResult{
Result: types.TargetResult{},
Value: 5.0,
Query: "query",
Score: 10.0,
Error: nil,
}
Expand Down Expand Up @@ -149,6 +154,7 @@ func TestAnalysisEvaluator_Evaluate(t *testing.T) {
ObjectiveResults: []types.ObjectiveResult{
{
Result: types.TargetResult{},
Query: "query",
Value: 5.0,
Score: 5.0,
Error: nil,
Expand All @@ -159,6 +165,7 @@ func TestAnalysisEvaluator_Evaluate(t *testing.T) {
EvaluateFunc: func(values map[string]v1alpha3.ProviderResult, objective *v1alpha3.Objective) types.ObjectiveResult {
return types.ObjectiveResult{
Result: types.TargetResult{},
Query: "query",
Value: 5.0,
Score: 5.0,
Error: nil,
Expand Down Expand Up @@ -191,6 +198,7 @@ func TestAnalysisEvaluator_Evaluate(t *testing.T) {
{
Result: types.TargetResult{},
Value: 5.0,
Query: "query",
Score: 0.0,
Error: nil,
},
Expand All @@ -201,6 +209,7 @@ func TestAnalysisEvaluator_Evaluate(t *testing.T) {
return types.ObjectiveResult{
Result: types.TargetResult{},
Value: 5.0,
Query: "query",
Score: 0.0,
Error: nil,
}
Expand Down Expand Up @@ -237,12 +246,14 @@ func TestAnalysisEvaluator_Evaluate(t *testing.T) {
{
Result: types.TargetResult{},
Value: 5.0,
Query: "query",
Score: 10.0,
Error: nil,
},
{
Result: types.TargetResult{},
Value: 5.0,
Query: "query",
Score: 0.0,
Error: nil,
},
Expand All @@ -254,13 +265,15 @@ func TestAnalysisEvaluator_Evaluate(t *testing.T) {
return types.ObjectiveResult{
Result: types.TargetResult{},
Value: 5.0,
Query: "query",
Score: 0.0,
Error: nil,
}
}
return types.ObjectiveResult{
Result: types.TargetResult{},
Value: 5.0,
Query: "query",
Score: 10.0,
Error: nil,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func (oe *ObjectiveEvaluator) Evaluate(values map[string]v1alpha3.ProviderResult
}

// get the value
floatVal, err := getValueFromMap(values, ComputeKey(obj.AnalysisValueTemplateRef))
floatVal, query, err := getResultFromMap(values, ComputeKey(obj.AnalysisValueTemplateRef))
result.Query = query
if err != nil {
result.Error = err
return result
Expand All @@ -50,17 +51,17 @@ func (oe *ObjectiveEvaluator) Evaluate(values map[string]v1alpha3.ProviderResult
return result
}

func getValueFromMap(values map[string]v1alpha3.ProviderResult, key string) (float64, error) {
func getResultFromMap(values map[string]v1alpha3.ProviderResult, key string) (float64, string, error) {
val, ok := values[key]
if !ok {
return 0.0, fmt.Errorf("required value '%s' not available", key)
return 0.0, "", fmt.Errorf("required value '%s' not available", key)
}
floatVal, err := strconv.ParseFloat(val.Value, 64)
if err != nil {
return 0.0, err
return 0.0, val.Query, err
}

return floatVal, nil
return floatVal, val.Query, nil
}

func ComputeKey(obj v1alpha3.ObjectReference) string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestObjectiveEvaluator_Evaluate(t *testing.T) {
{
name: "evaluation passed",
values: map[string]v1alpha3.ProviderResult{
"name": {Value: "20"},
"name": {Value: "20", Query: "qqqqqqqq"},
},
o: v1alpha3.Objective{
AnalysisValueTemplateRef: v1alpha3.ObjectReference{
Expand All @@ -59,6 +59,7 @@ func TestObjectiveEvaluator_Evaluate(t *testing.T) {
Score: 2.0,
Error: nil,
Value: 20.0,
Query: "qqqqqqqq",
Result: types.TargetResult{
Pass: true,
},
Expand All @@ -73,7 +74,7 @@ func TestObjectiveEvaluator_Evaluate(t *testing.T) {
{
name: "evaluation finished with warning",
values: map[string]v1alpha3.ProviderResult{
"name": {Value: "20"},
"name": {Value: "20", Query: "qqqqqqqq"},
},
o: v1alpha3.Objective{
AnalysisValueTemplateRef: v1alpha3.ObjectReference{
Expand All @@ -93,6 +94,7 @@ func TestObjectiveEvaluator_Evaluate(t *testing.T) {
Score: 1.0,
Error: nil,
Value: 20.0,
Query: "qqqqqqqq",
Result: types.TargetResult{
Pass: false,
Warning: true,
Expand All @@ -108,7 +110,7 @@ func TestObjectiveEvaluator_Evaluate(t *testing.T) {
{
name: "evaluation failed",
values: map[string]v1alpha3.ProviderResult{
"name": {Value: "20"},
"name": {Value: "20", Query: "qqqqqqqq"},
},
o: v1alpha3.Objective{
AnalysisValueTemplateRef: v1alpha3.ObjectReference{
Expand All @@ -128,6 +130,7 @@ func TestObjectiveEvaluator_Evaluate(t *testing.T) {
Score: 0.0,
Error: nil,
Value: 20.0,
Query: "qqqqqqqq",
Result: types.TargetResult{
Pass: false,
Warning: false,
Expand Down Expand Up @@ -155,44 +158,49 @@ func TestGetValueFromMap(t *testing.T) {
values map[string]v1alpha3.ProviderResult
in string
val float64
query string
wantErr bool
}{
{
name: "happy path",
values: map[string]v1alpha3.ProviderResult{
"key1": {Value: "7"},
"key1": {Value: "7", Query: "qqqqqqqq"},
},
in: "key1",
val: 7.0,
query: "qqqqqqqq",
wantErr: false,
},
{
name: "key not found",
values: map[string]v1alpha3.ProviderResult{
"key1": {Value: "7"},
"key1": {Value: "7", Query: "qqqqqqqq"},
},
in: "key",
val: 0.0,
query: "",
wantErr: true,
},
{
name: "value not float",
values: map[string]v1alpha3.ProviderResult{
"key": {},
"key1": {Value: "", Query: "qqqqqqqq"},
},
in: "key1",
val: 0.0,
query: "qqqqqqqq",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res, err := getValueFromMap(tt.values, tt.in)
res, query, err := getResultFromMap(tt.values, tt.in)
if (err != nil) != tt.wantErr {
t.Errorf("error = %v, wantErr %v", err, tt.wantErr)
}
require.Equal(t, tt.val, res)
require.Equal(t, tt.query, query)
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type ObjectiveResult struct {
Result TargetResult `json:"result"`
Objective v1alpha3.Objective `json:"objective"`
Value float64 `json:"value"`
Query string `json:"query"`
Score float64 `json:"score"`
Error error `json:"error,omitempty"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ spec:
status:
pass: true
# yamllint disable-line rule:line-length
raw: '{"objectiveResults":[{"result":{"failResult":{"operator":{"lessThan":{"fixedValue":"2"}},"fulfilled":false},"warnResult":{"operator":{"lessThan":{"fixedValue":"3"}},"fulfilled":false},"warning":false,"pass":true},"objective":{"analysisValueTemplateRef":{"name":"ready","namespace":"testy"},"target":{"failure":{"lessThan":{"fixedValue":"2"}},"warning":{"lessThan":{"fixedValue":"3"}}},"weight":1},"value":4,"score":1}],"totalScore":1,"maximumScore":1,"pass":true,"warning":false}'
raw: '{"objectiveResults":[{"result":{"failResult":{"operator":{"lessThan":{"fixedValue":"2"}},"fulfilled":false},"warnResult":{"operator":{"lessThan":{"fixedValue":"3"}},"fulfilled":false},"warning":false,"pass":true},"objective":{"analysisValueTemplateRef":{"name":"ready","namespace":"testy"},"target":{"failure":{"lessThan":{"fixedValue":"2"}},"warning":{"lessThan":{"fixedValue":"3"}}},"weight":1},"value":4,"query":"sum(kube_pod_container_status_ready{namespace=\"keptn-lifecycle-toolkit-system\"})","score":1}],"totalScore":1,"maximumScore":1,"pass":true,"warning":false}'
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ spec:
status:
pass: true
# yamllint disable-line rule:line-length
raw: '{"objectiveResults":[{"result":{"failResult":{"operator":{"lessThan":{"fixedValue":"5"}},"fulfilled":false},"warnResult":{"operator":{"lessThan":{"fixedValue":"4"}},"fulfilled":false},"warning":false,"pass":true},"objective":{"analysisValueTemplateRef":{"name":"value-1"},"target":{"failure":{"lessThan":{"fixedValue":"5"}},"warning":{"lessThan":{"fixedValue":"4"}}},"weight":2},"value":11,"score":2},{"result":{"failResult":{"operator":{"greaterThan":{"fixedValue":"20"}},"fulfilled":false},"warnResult":{"operator":{"greaterThan":{"fixedValue":"15"}},"fulfilled":true},"warning":true,"pass":false},"objective":{"analysisValueTemplateRef":{"name":"value-2"},"target":{"failure":{"greaterThan":{"fixedValue":"20"}},"warning":{"greaterThan":{"fixedValue":"15"}}},"weight":1},"value":20,"score":0.5},{"result":{"failResult":{"operator":{"notInRange":{"lowBound":"25","highBound":"35"}},"fulfilled":false},"warnResult":{"operator":{},"fulfilled":false},"warning":false,"pass":true},"objective":{"analysisValueTemplateRef":{"name":"value-3"},"target":{"failure":{"notInRange":{"lowBound":"25","highBound":"35"}}},"weight":1},"value":30,"score":1}],"totalScore":3.5,"maximumScore":4,"pass":true,"warning":false}'
raw: '{"objectiveResults":[{"result":{"failResult":{"operator":{"lessThan":{"fixedValue":"5"}},"fulfilled":false},"warnResult":{"operator":{"lessThan":{"fixedValue":"4"}},"fulfilled":false},"warning":false,"pass":true},"objective":{"analysisValueTemplateRef":{"name":"value-1"},"target":{"failure":{"lessThan":{"fixedValue":"5"}},"warning":{"lessThan":{"fixedValue":"4"}}},"weight":2},"value":11,"query":"query-1","score":2},{"result":{"failResult":{"operator":{"greaterThan":{"fixedValue":"20"}},"fulfilled":false},"warnResult":{"operator":{"greaterThan":{"fixedValue":"15"}},"fulfilled":true},"warning":true,"pass":false},"objective":{"analysisValueTemplateRef":{"name":"value-2"},"target":{"failure":{"greaterThan":{"fixedValue":"20"}},"warning":{"greaterThan":{"fixedValue":"15"}}},"weight":1},"value":20,"query":"query-2","score":0.5},{"result":{"failResult":{"operator":{"notInRange":{"lowBound":"25","highBound":"35"}},"fulfilled":false},"warnResult":{"operator":{},"fulfilled":false},"warning":false,"pass":true},"objective":{"analysisValueTemplateRef":{"name":"value-3"},"target":{"failure":{"notInRange":{"lowBound":"25","highBound":"35"}}},"weight":1},"value":30,"query":"query-3","score":1}],"totalScore":3.5,"maximumScore":4,"pass":true,"warning":false}'
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ status:
pass: true
state: Completed
# yamllint disable-line rule:line-length
raw: '{"objectiveResults":[{"result":{"failResult":{"operator":{"lessThan":{"fixedValue":"2"}},"fulfilled":false},"warnResult":{"operator":{"lessThan":{"fixedValue":"3"}},"fulfilled":false},"warning":false,"pass":true},"objective":{"analysisValueTemplateRef":{"name":"ready"},"target":{"failure":{"lessThan":{"fixedValue":"2"}},"warning":{"lessThan":{"fixedValue":"3"}}},"weight":1},"value":4,"score":1}],"totalScore":1,"maximumScore":1,"pass":true,"warning":false}'
raw: '{"objectiveResults":[{"result":{"failResult":{"operator":{"lessThan":{"fixedValue":"2"}},"fulfilled":false},"warnResult":{"operator":{"lessThan":{"fixedValue":"3"}},"fulfilled":false},"warning":false,"pass":true},"objective":{"analysisValueTemplateRef":{"name":"ready"},"target":{"failure":{"lessThan":{"fixedValue":"2"}},"warning":{"lessThan":{"fixedValue":"3"}}},"weight":1},"value":4,"query":"sum(kube_pod_container_status_ready{namespace=\"keptn-lifecycle-toolkit-system\"})","score":1}],"totalScore":1,"maximumScore":1,"pass":true,"warning":false}'
2 changes: 1 addition & 1 deletion test/testanalysis/analysis-controller/01-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ status:
pass: true
state: Completed
# yamllint disable-line rule:line-length
raw: '{"objectiveResults":[{"result":{"failResult":{"operator":{"lessThan":{"fixedValue":"2"}},"fulfilled":false},"warnResult":{"operator":{"lessThan":{"fixedValue":"3"}},"fulfilled":false},"warning":false,"pass":true},"objective":{"analysisValueTemplateRef":{"name":"ready"},"target":{"failure":{"lessThan":{"fixedValue":"2"}},"warning":{"lessThan":{"fixedValue":"3"}}},"weight":1},"value":4,"score":1}],"totalScore":1,"maximumScore":1,"pass":true,"warning":false}'
raw: '{"objectiveResults":[{"result":{"failResult":{"operator":{"lessThan":{"fixedValue":"2"}},"fulfilled":false},"warnResult":{"operator":{"lessThan":{"fixedValue":"3"}},"fulfilled":false},"warning":false,"pass":true},"objective":{"analysisValueTemplateRef":{"name":"ready"},"target":{"failure":{"lessThan":{"fixedValue":"2"}},"warning":{"lessThan":{"fixedValue":"3"}}},"weight":1},"value":4,"query":"sum(kube_pod_container_status_ready{namespace=\"keptn-lifecycle-toolkit-system\"})","score":1}],"totalScore":1,"maximumScore":1,"pass":true,"warning":false}'

0 comments on commit 233aac4

Please sign in to comment.