Skip to content

Commit

Permalink
🐛 only read the first item when RawFeedbackJsonString is disabled (op…
Browse files Browse the repository at this point in the history
…en-cluster-management-io#613) (#122)

* only read the first item when RawFeedbackJsonString is disabled

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



* Add a test for backward compatible



---------

Signed-off-by: Jian Qiu <jqiu@redhat.com>
Co-authored-by: Jian Qiu <jqiu@redhat.com>
  • Loading branch information
zhujian7 and qiujian16 authored Sep 10, 2024
1 parent 9f4ae61 commit 64dbe44
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
9 changes: 4 additions & 5 deletions pkg/work/spoke/statusfeedback/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,11 @@ func getValueByJsonPath(name, path string, obj *unstructured.Unstructured) (*wor
}

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] {
Expand Down
31 changes: 30 additions & 1 deletion pkg/work/spoke/statusfeedback/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ const (
{
"type":"Available",
"status":"true"
},
{
"type":"Ready",
"status":"true"
}
]
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 64dbe44

Please sign in to comment.