Skip to content

Commit

Permalink
[kubernetes] use k8s client per widget
Browse files Browse the repository at this point in the history
References issue wtfutil#1081
  • Loading branch information
hypnoglow committed Oct 31, 2021
1 parent f204653 commit 7d0941a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
24 changes: 7 additions & 17 deletions modules/kubernetes/client.go
@@ -1,36 +1,26 @@
package kubernetes

import (
"sync"

"k8s.io/client-go/kubernetes"
// Includes authentication modules for various Kubernetes providers
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/clientcmd"
)

var kubeClient *clientInstance
var kubeError error
var clientOnce sync.Once

type clientInstance struct {
Client kubernetes.Interface
}

// getInstance returns a Kubernetes interface for a clientset
func (widget *Widget) getInstance() (*clientInstance, error) {
clientOnce.Do(func() {
if kubeClient == nil {
client, err := widget.getKubeClient()
if err != nil {
kubeError = err
}
kubeClient = &clientInstance{
Client: client,
}
}
var err error

widget.clientOnce.Do(func() {
widget.client = &clientInstance{}
widget.client.Client, err = widget.getKubeClient()
})
return kubeClient, kubeError

return widget.client, err
}

// getKubeClient returns a kubernetes clientset for the kubeconfig provided
Expand Down
4 changes: 4 additions & 0 deletions modules/kubernetes/widget.go
Expand Up @@ -2,6 +2,7 @@ package kubernetes

import (
"fmt"
"sync"

"github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
Expand All @@ -14,6 +15,9 @@ import (
type Widget struct {
view.TextWidget

client *clientInstance
clientOnce sync.Once

objects []string
title string
kubeconfig string
Expand Down

0 comments on commit 7d0941a

Please sign in to comment.