From 1a0e22036864519c49087b6dfbce1bdd3cabc1e8 Mon Sep 17 00:00:00 2001 From: Jose Donizetti Date: Mon, 30 May 2022 17:53:37 -0300 Subject: [PATCH 1/4] fix: if current namespace empty, returns default Signed-off-by: Jose Donizetti --- pkg/k8s/k8s.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/k8s/k8s.go b/pkg/k8s/k8s.go index 1dbe597..d030658 100644 --- a/pkg/k8s/k8s.go +++ b/pkg/k8s/k8s.go @@ -88,6 +88,10 @@ func GetCluster(context string) (Cluster, error) { namespace = context.Namespace } + if len(namespace) == 0 { + namespace = "default" + } + restMapper, err := cf.ToRESTMapper() if err != nil { return nil, err From 6a446e7bb06452fa6052771e0e0f2be57ef46b1c Mon Sep 17 00:00:00 2001 From: Jose Donizetti Date: Mon, 30 May 2022 20:29:27 -0300 Subject: [PATCH 2/4] test: add GetCurrentNamespace test Signed-off-by: Jose Donizetti --- pkg/k8s/k8s.go | 25 ++++++++++++++--------- pkg/k8s/k8s_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/pkg/k8s/k8s.go b/pkg/k8s/k8s.go index d030658..8470742 100644 --- a/pkg/k8s/k8s.go +++ b/pkg/k8s/k8s.go @@ -6,6 +6,7 @@ import ( "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/client-go/dynamic" "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" ) const ( @@ -65,20 +66,31 @@ func GetCluster(context string) (Cluster, error) { cf.Context = &context - kubeConfig, err := cf.ToRESTConfig() + // disable warnings + rest.SetDefaultWarningHandler(rest.NoWarnings{}) + + clientConfig := cf.ToRawKubeConfigLoader() + + restMapper, err := cf.ToRESTMapper() if err != nil { return nil, err } - // disable warnings - rest.SetDefaultWarningHandler(rest.NoWarnings{}) + return getCluster(clientConfig, restMapper) +} + +func getCluster(clientConfig clientcmd.ClientConfig, restMapper meta.RESTMapper) (*cluster, error) { + kubeConfig, err := clientConfig.ClientConfig() + if err != nil { + return nil, err + } k8sDynamicClient, err := dynamic.NewForConfig(kubeConfig) if err != nil { return nil, err } - rawCfg, err := cf.ToRawKubeConfigLoader().RawConfig() + rawCfg, err := clientConfig.RawConfig() if err != nil { return nil, err } @@ -92,11 +104,6 @@ func GetCluster(context string) (Cluster, error) { namespace = "default" } - restMapper, err := cf.ToRESTMapper() - if err != nil { - return nil, err - } - return &cluster{ currentContext: rawCfg.CurrentContext, currentNamespace: namespace, diff --git a/pkg/k8s/k8s_test.go b/pkg/k8s/k8s_test.go index 1e5ad04..361f733 100644 --- a/pkg/k8s/k8s_test.go +++ b/pkg/k8s/k8s_test.go @@ -5,8 +5,37 @@ import ( "github.com/stretchr/testify/assert" "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/tools/clientcmd" + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" ) +func Test_getCluster(t *testing.T) { + tests := []struct { + Name string + Namespace string + ExpectedNamespace string + }{ + { + Name: "empty context and empty namespace", + ExpectedNamespace: "default", + }, + { + Name: "non empty namespace", + Namespace: "namespace", + ExpectedNamespace: "namespace", + }, + } + + for _, test := range tests { + t.Run(test.Name, func(t *testing.T) { + fakeConfig := createValidTestConfig(test.Namespace) + cluster, err := getCluster(fakeConfig, nil) + assert.NoError(t, err) + assert.Equal(t, test.ExpectedNamespace, cluster.GetCurrentNamespace()) + }) + } +} + func TestIsClusterResource(t *testing.T) { for _, r := range getClusterResources() { assert.True(t, IsClusterResource(newGVR(r)), r) @@ -20,3 +49,23 @@ func TestIsClusterResource(t *testing.T) { func newGVR(resource string) schema.GroupVersionResource { return schema.GroupVersionResource{Resource: resource} } + +func createValidTestConfig(namespace string) clientcmd.ClientConfig { + const ( + server = "https://anything.com:8080" + token = "the-token" + ) + + config := clientcmdapi.NewConfig() + + config.CurrentContext = "cluster1" + config.Clusters["cluster1"] = &clientcmdapi.Cluster{Server: server} + config.AuthInfos["cluster1"] = &clientcmdapi.AuthInfo{Token: token} + config.Contexts["cluster1"] = &clientcmdapi.Context{ + Cluster: "cluster1", + AuthInfo: "cluster1", + Namespace: namespace, + } + + return clientcmd.NewNonInteractiveClientConfig(*config, "cluster1", &clientcmd.ConfigOverrides{}, nil) +} From 9b402cc0cb083965d6a0d1c59baec63140cc5e59 Mon Sep 17 00:00:00 2001 From: Jose Donizetti Date: Mon, 30 May 2022 20:59:42 -0300 Subject: [PATCH 3/4] test: add GetGVR test Signed-off-by: Jose Donizetti --- pkg/k8s/k8s_test.go | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/pkg/k8s/k8s_test.go b/pkg/k8s/k8s_test.go index 361f733..b5d3d7d 100644 --- a/pkg/k8s/k8s_test.go +++ b/pkg/k8s/k8s_test.go @@ -4,12 +4,13 @@ import ( "testing" "github.com/stretchr/testify/assert" + "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/tools/clientcmd" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" ) -func Test_getCluster(t *testing.T) { +func TestGetCurrentNamespace(t *testing.T) { tests := []struct { Name string Namespace string @@ -36,6 +37,44 @@ func Test_getCluster(t *testing.T) { } } +func TestGetGVR(t *testing.T) { + tests := []struct { + Resource string + GroupVersionKind schema.GroupVersionKind + ExpectedResource schema.GroupVersionResource + Err bool + }{ + { + Resource: "nonexisting", + GroupVersionKind: schema.GroupVersionKind{Group: "testapi", Version: "test", Kind: "MyObject"}, + Err: true, + }, + { + Resource: "MyObject", + GroupVersionKind: schema.GroupVersionKind{Group: "testapi", Version: "test", Kind: "MyObject"}, + ExpectedResource: schema.GroupVersionResource{Resource: "myobjects", Group: "testapi", Version: "test"}, + }, + } + + for _, test := range tests { + mapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{test.GroupVersionKind.GroupVersion()}) + mapper.Add(test.GroupVersionKind, meta.RESTScopeNamespace) + + fakeConfig := createValidTestConfig("") + + cluster, err := getCluster(fakeConfig, mapper) + assert.NoError(t, err) + + gvr, err := cluster.GetGVR(test.Resource) + if test.Err { + assert.Error(t, err) + continue + } + + assert.Equal(t, test.ExpectedResource, gvr) + } +} + func TestIsClusterResource(t *testing.T) { for _, r := range getClusterResources() { assert.True(t, IsClusterResource(newGVR(r)), r) From 178b2052d2e307911678d61b27ea15deb52819f4 Mon Sep 17 00:00:00 2001 From: Jose Donizetti Date: Mon, 30 May 2022 21:13:27 -0300 Subject: [PATCH 4/4] refactor: fix test name Signed-off-by: Jose Donizetti --- pkg/k8s/k8s_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/k8s/k8s_test.go b/pkg/k8s/k8s_test.go index b5d3d7d..e6b0cb9 100644 --- a/pkg/k8s/k8s_test.go +++ b/pkg/k8s/k8s_test.go @@ -17,7 +17,7 @@ func TestGetCurrentNamespace(t *testing.T) { ExpectedNamespace string }{ { - Name: "empty context and empty namespace", + Name: "empty namespace", ExpectedNamespace: "default", }, {