Repository: FairwindsOps/pluto
Pull request #419 (2022-11-25 23:07:44)

fix : rules to load client configuration to connect to a kubernetes cluster
fix : rules to load client configuration to connect to a kubernetes cluster

This PR : 
Fixes https://github.com/FairwindsOps/pluto/issues/418

## Checklist
* [x] I have signed the CLA
* [x] I have updated/added any relevant documentation

## Description
### What's the goal of this PR?

The same rules to load client configuration for all commands that need to connect to a kubernetes cluster

### What changes did you make?

Use the same way to load client configuration to connect to a kubernetes cluster as the detect-helm command for all commands

### What alternative solution should we consider, if any?

None

CLAassistant: [![CLA assistant check](https://cla-assistant.io/pull/badge/not_signed)](https://cla-assistant.io/FairwindsOps/pluto?pullRequest=419) <br/>Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our [Contributor License Agreement](https://cla-assistant.io/FairwindsOps/pluto?pullRequest=419) before we can accept your contribution.<br/><sub>You have signed the CLA already but the status is still pending? Let us [recheck](https://cla-assistant.io/check/FairwindsOps/pluto?pullRequest=419) it.</sub>
smehboub: Hello @sudermanjr @transient1 

Any news please ?

Thanks in advance.
Rgs,

Sophian
smehboub: Hello @sudermanjr @transient1

Any news please ?

Thanks in advance.
Rgs,

Sophian
aslafy-z: @smehboub can you please rebase your PR?
smehboub: > @smehboub can you please rebase your PR?

Hello @aslafy-z

Thanks you for feedbacks, It's done.

Rgs,
smehboub: > Looks like this needs a rebase now.

Hello @sudermanjr 

Thanks you for feedbacks, It's done.

Rgs,
transient1: Hi, @smehboub . Just wanted to let you know I will be reviewing this PR over the next day or so. 
smehboub: > Hi, @smehboub . Just wanted to let you know I will be reviewing this PR over the next day or so.

Thank you :-)
smehboub: > LGTM. Thanks for the contribution!

Thank you too :-)

Patch:
diff --git a/pkg/discovery-api/discovery_api.go b/pkg/discovery-api/discovery_api.go
index aaf4c53..097d7ba 100644
--- a/pkg/discovery-api/discovery_api.go
+++ b/pkg/discovery-api/discovery_api.go
@@ -41,7 +41,7 @@ import (
 	"k8s.io/klog/v2"
 
 	"github.com/fairwindsops/pluto/v5/pkg/api"
-	kube "github.com/fairwindsops/pluto/v5/pkg/kube"
+	"github.com/fairwindsops/pluto/v5/pkg/kube"
 )
 
 // DiscoveryClient is the declaration to hold objects needed for client-go/discovery.
@@ -60,7 +60,7 @@ func NewDiscoveryClient(namespace string, kubeContext string, instance *api.Inst
 	}
 
 	var err error
-	cl.ClientSet, cl.restConfig, err = kube.GetKubeDynamicClient(kubeContext)
+	cl.restConfig, err = kube.GetConfig(kubeContext)
 	if err != nil {
 		return nil, err
 	}
@@ -71,6 +71,10 @@ func NewDiscoveryClient(namespace string, kubeContext string, instance *api.Inst
 
 	cl.namespace = namespace
 
+	cl.ClientSet, err = dynamic.NewForConfig(cl.restConfig)
+	if err != nil {
+		return nil, err
+	}
 	return cl, nil
 }
 
diff --git a/pkg/helm/helm.go b/pkg/helm/helm.go
index ef10d8a..c044417 100644
--- a/pkg/helm/helm.go
+++ b/pkg/helm/helm.go
@@ -43,7 +43,7 @@ import (
 	"k8s.io/klog/v2"
 
 	"github.com/fairwindsops/pluto/v5/pkg/api"
-	kube "github.com/fairwindsops/pluto/v5/pkg/kube"
+	"github.com/fairwindsops/pluto/v5/pkg/kube"
 )
 
 // Helm represents all current releases that we can find in the cluster
diff --git a/pkg/kube/kube.go b/pkg/kube/kube.go
index df2c4bb..e184bf8 100644
--- a/pkg/kube/kube.go
+++ b/pkg/kube/kube.go
@@ -26,12 +26,11 @@
 // See the License for the specific language governing permissions and
 // limitations under the License
 
-package helm
+package kube
 
 import (
 	"sync"
 
-	"k8s.io/client-go/dynamic"
 	"k8s.io/client-go/kubernetes"
 	"k8s.io/client-go/rest"
 	"k8s.io/klog/v2"
@@ -52,10 +51,16 @@ var once sync.Once
 func GetConfigInstance(kubeContext string) (*Kube, error) {
 	var err error
 	var client kubernetes.Interface
+	var kubeConfig *rest.Config
+
+	kubeConfig, err = GetConfig(kubeContext)
+        if err != nil {
+                return nil, err
+        }
 
 	once.Do(func() {
 		if kubeClient == nil {
-			client, err = GetKubeClient(kubeContext)
+			client, err = GetKubeClient(kubeConfig)
 
 			kubeClient = &Kube{
 				Client: client,
@@ -68,38 +73,24 @@ func GetConfigInstance(kubeContext string) (*Kube, error) {
 	return kubeClient, nil
 }
 
-// GetKubeClient returns a Kubernetes.Interface based on the current configuration
-func GetKubeClient(kubeContext string) (kubernetes.Interface, error) {
+// GetConfig returns the current kube config with a specific context
+func GetConfig(kubeContext string) (*rest.Config, error) {
 	if kubeContext != "" {
 		klog.V(3).Infof("using kube context: %s", kubeContext)
 	}
 
-	config, err := config.GetConfigWithContext(kubeContext)
+	kubeConfig, err := config.GetConfigWithContext(kubeContext)
 	if err != nil {
 		return nil, err
 	}
+	return kubeConfig, nil
+}
 
-	clientset, err := kubernetes.NewForConfig(config)
+// GetKubeClient returns a Kubernetes.Interface based on the current configuration
+func GetKubeClient(kubeConfig *rest.Config) (kubernetes.Interface, error) {
+	clientset, err := kubernetes.NewForConfig(kubeConfig)
 	if err != nil {
 		return nil, err
 	}
 	return clientset, nil
 }
-
-// GetKubeDynamicClient returns a dynamic.Interface, rest.Config based on the current configuration
-func GetKubeDynamicClient(kubeContext string) (dynamic.Interface, *rest.Config, error) {
-	if kubeContext != "" {
-		klog.V(3).Infof("using kube context: %s", kubeContext)
-	}
-
-	config, err := config.GetConfigWithContext(kubeContext)
-	if err != nil {
-		return nil, nil, err
-	}
-
-	clientset, err := dynamic.NewForConfig(config)
-	if err != nil {
-		return nil, nil, err
-	}
-	return clientset, config, nil
-}


Test patch:
diff --git a/pkg/helm/helm_test.go b/pkg/helm/helm_test.go
index 81c6df0..e388c79 100644
--- a/pkg/helm/helm_test.go
+++ b/pkg/helm/helm_test.go
@@ -33,16 +33,24 @@ import (
 	"testing"
 
 	"github.com/fairwindsops/pluto/v5/pkg/api"
-	kube "github.com/fairwindsops/pluto/v5/pkg/kube"
+        "github.com/fairwindsops/pluto/v5/pkg/kube"
 	"github.com/stretchr/testify/assert"
 	v1 "k8s.io/api/core/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/client-go/kubernetes"
 	testclient "k8s.io/client-go/kubernetes/fake"
-	_ "k8s.io/client-go/plugin/pkg/client/auth"
 	"k8s.io/client-go/rest"
 )
 
+var kubeClient *kube.Kube
+
+func getMockConfigInstance() *kube.Kube {
+        kubeClient = &kube.Kube{
+                Client: testclient.NewSimpleClientset(),
+        }
+        return kubeClient
+}
+
 var (
 	helmSecret = v1.Secret{
 		ObjectMeta: metav1.ObjectMeta{
@@ -153,13 +161,6 @@ var (
 	}
 )
 
-func getMockConfigInstance() *kube.Kube {
-	kubeClient := &kube.Kube{
-		Client: testclient.NewSimpleClientset(),
-	}
-	return kubeClient
-}
-
 func newMockHelm(namespace string) *Helm {
 	return &Helm{
 		Namespace: namespace,
diff --git a/pkg/kube/kube_test.go b/pkg/kube/kube_test.go
index 9fb166f..7acc514 100644
--- a/pkg/kube/kube_test.go
+++ b/pkg/kube/kube_test.go
@@ -12,13 +12,14 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package helm
+package kube
 
 import (
 	"os"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
+	_ "k8s.io/client-go/plugin/pkg/client/auth"
 )
 
 func Test_getKubeClient(t *testing.T) {
@@ -50,7 +51,7 @@ func Test_getKubeClient(t *testing.T) {
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
 			os.Setenv("KUBECONFIG", tt.kubeConfig)
-			_, err := GetKubeClient(tt.kubeContext)
+                        _, err := GetConfig(tt.kubeContext)
 			if tt.wantErr {
 				assert.Error(t, err)
 			} else {