Skip to content

Commit 667a065

Browse files
committed
Add a data type for dynamic data gatherer
Signed-off-by: Richard Wall <[email protected]>
1 parent f76274a commit 667a065

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

pkg/datagatherer/k8s/dynamic.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,17 @@ func (g *DataGathererDynamic) WaitForCacheSync(ctx context.Context) error {
307307
return nil
308308
}
309309

310+
type DynamicData struct {
311+
Items []*api.GatheredResource `json:"items"`
312+
}
313+
310314
// Fetch will fetch the requested data from the apiserver, or return an error
311315
// if fetching the data fails.
312316
func (g *DataGathererDynamic) Fetch() (interface{}, int, error) {
313317
if g.groupVersionResource.String() == "" {
314318
return nil, -1, fmt.Errorf("resource type must be specified")
315319
}
316320

317-
var list = map[string]interface{}{}
318321
var items = []*api.GatheredResource{}
319322

320323
fetchNamespaces := g.namespaces
@@ -344,10 +347,9 @@ func (g *DataGathererDynamic) Fetch() (interface{}, int, error) {
344347
return nil, -1, err
345348
}
346349

347-
// add gathered resources to items
348-
list["items"] = items
349-
350-
return list, len(items), nil
350+
return &DynamicData{
351+
Items: items,
352+
}, len(items), nil
351353
}
352354

353355
func redactList(list []*api.GatheredResource, excludeAnnotKeys, excludeLabelKeys []*regexp.Regexp) error {

pkg/datagatherer/k8s/dynamic_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -730,15 +730,12 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
730730
}
731731

732732
if tc.expected != nil {
733-
items, ok := res.(map[string]interface{})
733+
data, ok := res.(*DynamicData)
734734
if !ok {
735-
t.Errorf("expected result be an map[string]interface{} but wasn't")
735+
t.Errorf("expected result be *DynamicData but wasn't")
736736
}
737737

738-
list, ok := items["items"].([]*api.GatheredResource)
739-
if !ok {
740-
t.Errorf("expected result be an []*api.GatheredResource but wasn't")
741-
}
738+
list := data.Items
742739
// sorting list of results by name
743740
sortGatheredResources(list)
744741
// sorting list of expected results by name
@@ -1045,10 +1042,9 @@ func TestDynamicGathererNativeResources_Fetch(t *testing.T) {
10451042
}
10461043

10471044
if tc.expected != nil {
1048-
res, ok := rawRes.(map[string]interface{})
1049-
require.Truef(t, ok, "expected result be an map[string]interface{} but wasn't")
1050-
actual := res["items"].([]*api.GatheredResource)
1051-
require.Truef(t, ok, "expected result be an []*api.GatheredResource but wasn't")
1045+
res, ok := rawRes.(*DynamicData)
1046+
require.Truef(t, ok, "expected result be an *DynamicData but wasn't")
1047+
actual := res.Items
10521048

10531049
// sorting list of results by name
10541050
sortGatheredResources(actual)

0 commit comments

Comments
 (0)