From f3c7a859a7d44bc8dc8fef733d55593ed41d0fdf Mon Sep 17 00:00:00 2001 From: Stepan Stipl Date: Thu, 18 Nov 2021 19:04:46 +0000 Subject: [PATCH] fix: Fix unpopulated discovery client and add test for same --- pkg/collector/kube.go | 2 +- pkg/collector/kube_test.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/collector/kube.go b/pkg/collector/kube.go index fe42c26b..2d7d2982 100644 --- a/pkg/collector/kube.go +++ b/pkg/collector/kube.go @@ -26,7 +26,7 @@ func newKubeCollector(kubeconfig string, kubecontext string, discoveryClient dis return nil, fmt.Errorf("failed to assemble client config: %w", err) } - if discoveryClient, err = discovery.NewDiscoveryClientForConfig(col.restConfig); err != nil { + if col.discoveryClient, err = discovery.NewDiscoveryClientForConfig(col.restConfig); err != nil { return nil, fmt.Errorf("failed to create client: %w", err) } } diff --git a/pkg/collector/kube_test.go b/pkg/collector/kube_test.go index 2246e3bc..33d183ec 100644 --- a/pkg/collector/kube_test.go +++ b/pkg/collector/kube_test.go @@ -26,6 +26,23 @@ func TestNewKubeCollector(t *testing.T) { } } +func TestNewKubeCollectorWithKubeconfigPath(t *testing.T) { + col, err := newKubeCollector(filepath.Join(FIXTURES_DIR, "kube.config.basic"), "", nil) + + if err != nil { + t.Fatalf("Failed to create kubeCollector from fake discovery client") + } + if col == nil { + t.Fatalf("Should return collector, got nil instead") + } + if col.discoveryClient == nil { + t.Fatalf("Collector should have discovery client, got nil instead") + } + if col.restConfig == nil { + t.Fatalf("Collector should have rest config, got nil instead") + } +} + func TestNewKubeCollectorError(t *testing.T) { _, err := newKubeCollector("does-not-exist", "", nil)