Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Being able to match multiple items in jsonpath #202

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/cmd/spoke/operator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package spoke

import (
"context"

"github.com/openshift/library-go/pkg/controller/controllercmd"
"github.com/spf13/cobra"

Expand All @@ -14,7 +16,7 @@ func NewKlusterletOperatorCmd() *cobra.Command {
options := klusterlet.Options{}
cmdConfig := controllercmd.
NewControllerCommandConfig("klusterlet", version.Get(), options.RunKlusterletOperator)
cmd := cmdConfig.NewCommand()
cmd := cmdConfig.NewCommandWithContext(context.TODO())
cmd.Use = "klusterlet"
cmd.Short = "Start the klusterlet operator"

Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/spoke/registration.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package spoke

import (
"context"

"github.com/openshift/library-go/pkg/controller/controllercmd"
"github.com/spf13/cobra"

Expand All @@ -13,7 +15,7 @@ func NewRegistrationAgent() *cobra.Command {
cmdConfig := controllercmd.
NewControllerCommandConfig("registration-agent", version.Get(), agentOptions.RunSpokeAgent)

cmd := cmdConfig.NewCommand()
cmd := cmdConfig.NewCommandWithContext(context.TODO())
cmd.Use = "agent"
cmd.Short = "Start the Cluster Registration Agent"

Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/spoke/work.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package spoke

import (
"context"

"github.com/openshift/library-go/pkg/controller/controllercmd"
"github.com/spf13/cobra"

Expand All @@ -13,7 +15,7 @@ func NewWorkAgent() *cobra.Command {
o := spoke.NewWorkloadAgentOptions()
cmdConfig := controllercmd.
NewControllerCommandConfig("work-agent", version.Get(), o.RunWorkloadAgent)
cmd := cmdConfig.NewCommand()
cmd := cmdConfig.NewCommandWithContext(context.TODO())
cmd.Use = "agent"
cmd.Short = "Start the Work Agent"

Expand Down
2 changes: 1 addition & 1 deletion pkg/placement/controllers/scheduling/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
clusterapiv1beta2 "open-cluster-management.io/api/cluster/v1beta2"
"reflect"
"sort"
"testing"
Expand All @@ -16,6 +15,7 @@ import (
clusterapiv1 "open-cluster-management.io/api/cluster/v1"
clusterapiv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
clusterlisterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
clusterapiv1beta2 "open-cluster-management.io/api/cluster/v1beta2"

"open-cluster-management.io/ocm/pkg/placement/controllers/framework"
testinghelpers "open-cluster-management.io/ocm/pkg/placement/helpers/testing"
Expand Down
16 changes: 14 additions & 2 deletions pkg/work/spoke/statusfeedback/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,20 @@ func getValueByJsonPath(name, path string, obj *unstructured.Unstructured) (*wor
return nil, nil
}

// as we only support simple JSON path, we can assume to have only one result (or none, filtered out above)
value := results[0][0].Interface()
var value any
switch {
case len(results) == 0 || len(results[0]) == 0:
return nil, nil
case len(results) == 1 && len(results[0]) == 1:
value = results[0][0].Interface()
default:
var resultList []any
// only take care the first item in the results list.
for _, r := range results[0] {
resultList = append(resultList, r.Interface())
}
value = resultList
}

if value == nil {
// ignore the result if it is nil
Expand Down
51 changes: 51 additions & 0 deletions pkg/work/spoke/statusfeedback/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,33 @@ const (
}
}
`
deploymentJsonMultiCondition = `
{
"apiVersion":"apps/v1",
"kind":"Deployment",
"metadata":{
"name":"test"
},
"status":{
"readyReplicas":1,
"replicas":2,
"conditions":[
{
"type":"Cond1",
"status":"true"
},
{
"type":"Cond2",
"status":"false"
},
{
"type":"Cond3",
"status":"true"
}
]
}
}
`
deploymentJsonUknownGroup = `
{
"apiVersion":"extensions/v1",
Expand Down Expand Up @@ -282,6 +309,30 @@ func TestStatusReader(t *testing.T) {
},
},
},
{
name: "filtered rawjson value format",
object: unstrctureObject(deploymentJsonMultiCondition),
enableRaw: true,
rule: workapiv1.FeedbackRule{
Type: workapiv1.JSONPathsType,
JsonPaths: []workapiv1.JsonPath{
{
Name: "conditions",
Path: ".status.conditions[?(@.status==\"true\")].type",
},
},
},
expectError: false,
expectedValue: []workapiv1.FeedbackValue{
{
Name: "conditions",
Value: workapiv1.FieldValue{
Type: workapiv1.JsonRaw,
JsonRaw: pointer.String(`["Cond1","Cond3"]`),
},
},
},
},
}

reader := NewStatusReader()
Expand Down