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

feat(inspect-api): return empty list of 'items' instead of 'null' #3765

Merged
merged 2 commits into from
Feb 7, 2022
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
10 changes: 4 additions & 6 deletions pkg/api-server/inspect_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ func inspectDataplane(cfg *kuma_cp.Config, builder xds_context.MeshContextBuilde
return
}

entries := newDataplaneInspectResponse(matchedPolicies, dp)
result := &api_server_types.DataplaneInspectEntryList{
Items: entries,
Total: uint32(len(entries)),
}
result := api_server_types.NewDataplaneInspectEntryList()
result.Items = append(result.Items, newDataplaneInspectResponse(matchedPolicies, dp)...)
result.Total = uint32(len(result.Items))

if err := response.WriteAsJson(result); err != nil {
rest_errors.HandleError(response, err, "Could not write response")
Expand All @@ -111,7 +109,7 @@ func inspectPolicies(
return
}

result := &api_server_types.PolicyInspectEntryList{}
result := api_server_types.NewPolicyInspectEntryList()

for _, dp := range meshContext.Resources.Dataplanes().Items {
dpKey := core_model.MetaToResourceKey(dp.GetMeta())
Expand Down
29 changes: 29 additions & 0 deletions pkg/api-server/inspect_endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,21 @@ var _ = Describe("Inspect WS", func() {
},
},
}),
Entry("inspect dataplane, empty response", testCase{
path: "/meshes/default/dataplanes/backend-1/policies",
goldenFile: "inspect_dataplane_empty-response.json",
resources: []core_model.Resource{
newMesh("default"),
newDataplane().
meta("backend-1", "default").
inbound("backend", "192.168.0.1", 80, 81).
outbound("redis", "192.168.0.2", 8080).
outbound("gateway", "192.168.0.3", 8080).
outbound("postgres", "192.168.0.4", 8080).
outbound("web", "192.168.0.2", 8080).
build(),
},
}),
Entry("inspect traffic permission", testCase{
path: "/meshes/default/traffic-permissions/tp-1/dataplanes",
goldenFile: "inspect_traffic-permission.json",
Expand Down Expand Up @@ -561,6 +576,20 @@ var _ = Describe("Inspect WS", func() {
build(),
},
}),
Entry("inspect traffic trace, empty response", testCase{
path: "/meshes/mesh-1/traffic-traces/tt-1/dataplanes",
goldenFile: "inspect_traffic-trace_empty-response.json",
resources: []core_model.Resource{
newMesh("mesh-1"),
&core_mesh.TrafficTraceResource{
Meta: &test_model.ResourceMeta{Name: "tt-1", Mesh: "mesh-1"},
Spec: &mesh_proto.TrafficTrace{
Selectors: anyService(),
Conf: samples.TrafficTrace.Conf,
},
},
},
}),
)

It("should change response if state changed", func() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"total": 0,
"items": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"total": 0,
"items": []
}
14 changes: 14 additions & 0 deletions pkg/api-server/types/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ type PolicyInspectEntryList struct {
Items []*PolicyInspectEntry `json:"items"`
}

func NewPolicyInspectEntryList() *PolicyInspectEntryList {
return &PolicyInspectEntryList{
Total: 0,
Items: []*PolicyInspectEntry{},
}
}

type DataplaneInspectEntry struct {
AttachmentEntry
MatchedPolicies map[core_model.ResourceType][]*rest.Resource `json:"matchedPolicies"`
Expand All @@ -40,6 +47,13 @@ type DataplaneInspectEntryList struct {
Items []*DataplaneInspectEntry `json:"items"`
}

func NewDataplaneInspectEntryList() *DataplaneInspectEntryList {
return &DataplaneInspectEntryList{
Total: 0,
Items: []*DataplaneInspectEntry{},
}
}

type DataplaneInspectEntryReceiver struct {
DataplaneInspectEntry
NewResource func(resourceType core_model.ResourceType) (core_model.Resource, error)
Expand Down