🐛 only read the first item when RawFeedbackJsonString is disabled (#613)

* only read the first item when RawFeedbackJsonString is disabled

This is to ensure the backward compatible when the feature gate
is disabled

Signed-off-by: Jian Qiu <jqiu@redhat.com>

* Add a test for backward compatible

Signed-off-by: Jian Qiu <jqiu@redhat.com>

---------

Signed-off-by: Jian Qiu <jqiu@redhat.com>
This commit is contained in:
Jian Qiu
2024-09-09 21:34:01 +08:00
committed by GitHub
parent b6763a13c0
commit 8678ede813
2 changed files with 34 additions and 6 deletions

View File

@@ -100,12 +100,11 @@ func (s *StatusReader) getValueByJsonPath(name, path string, obj *unstructured.U
}
var value any
switch {
case len(results) == 0 || len(results[0]) == 0:
return nil, nil
case len(results) == 1 && len(results[0]) == 1:
// if the RawFeedbackJsonString is disabled, we always get the first item
if (len(results) == 1 && len(results[0]) == 1) ||
!features.SpokeMutableFeatureGate.Enabled(ocmfeature.RawFeedbackJsonString) {
value = results[0][0].Interface()
default:
} else {
var resultList []any
// only take care the first item in the results list.
for _, r := range results[0] {

View File

@@ -76,6 +76,10 @@ const (
{
"type":"Available",
"status":"true"
},
{
"type":"Ready",
"status":"true"
}
]
}
@@ -216,7 +220,7 @@ func TestStatusReader(t *testing.T) {
expectedValue: []workapiv1.FeedbackValue{},
},
{
name: "wrog version set for jsonpaths",
name: "wrong version set for jsonpaths",
object: unstrctureObject(deploymentJson),
rule: workapiv1.FeedbackRule{
Type: workapiv1.JSONPathsType,
@@ -287,6 +291,31 @@ func TestStatusReader(t *testing.T) {
},
},
},
{
// this is for a backward compatible test, when rawjson is disabled, and there are multiple match on
// json path, it should return the first item.
name: "Return 1st item with multiple patch",
object: unstrctureObject(deploymentJson),
rule: workapiv1.FeedbackRule{
Type: workapiv1.JSONPathsType,
JsonPaths: []workapiv1.JsonPath{
{
Name: "type",
Path: ".status.conditions[?(@.status==\"true\")].type ",
},
},
},
expectError: false,
expectedValue: []workapiv1.FeedbackValue{
{
Name: "type",
Value: workapiv1.FieldValue{
Type: workapiv1.String,
String: pointer.String("Available"),
},
},
},
},
{
name: "rawjson value format",
object: unstrctureObject(podJson),