1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Merge pull request #630 from Seanstoppable/removepanicfromkube

Remove panic from kubernetes client
This commit is contained in:
Chris Cummer 2019-09-13 03:51:09 -07:00 committed by GitHub
commit 4c3abf5aca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 8 deletions

View File

@ -10,6 +10,7 @@ import (
) )
var kubeClient *clientInstance var kubeClient *clientInstance
var kubeError error
var clientOnce sync.Once var clientOnce sync.Once
type clientInstance struct { type clientInstance struct {
@ -17,26 +18,30 @@ type clientInstance struct {
} }
// getInstance returns a Kubernetes interface for a clientset // getInstance returns a Kubernetes interface for a clientset
func (widget *Widget) getInstance() *clientInstance { func (widget *Widget) getInstance() (*clientInstance, error) {
clientOnce.Do(func() { clientOnce.Do(func() {
if kubeClient == nil { if kubeClient == nil {
client, err := widget.getKubeClient()
if err != nil {
kubeError = err
}
kubeClient = &clientInstance{ kubeClient = &clientInstance{
Client: widget.getKubeClient(), Client: client,
} }
} }
}) })
return kubeClient return kubeClient, kubeError
} }
// getKubeClient returns a kubernetes clientset for the kubeconfig provided // getKubeClient returns a kubernetes clientset for the kubeconfig provided
func (widget *Widget) getKubeClient() kubernetes.Interface { func (widget *Widget) getKubeClient() (kubernetes.Interface, error) {
config, err := clientcmd.BuildConfigFromFlags("", widget.kubeconfig) config, err := clientcmd.BuildConfigFromFlags("", widget.kubeconfig)
if err != nil { if err != nil {
panic(err) return nil, err
} }
clientset, err := kubernetes.NewForConfig(config) clientset, err := kubernetes.NewForConfig(config)
if err != nil { if err != nil {
panic(err) return nil, err
} }
return clientset return clientset, nil
} }

View File

@ -41,7 +41,12 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget {
// Refresh executes the command and updates the view with the results // Refresh executes the command and updates the view with the results
func (widget *Widget) Refresh() { func (widget *Widget) Refresh() {
title := widget.generateTitle() title := widget.generateTitle()
client := widget.getInstance() client, err := widget.getInstance()
if err != nil {
widget.Redraw(func() (string, string, bool) { return title, err.Error(), true })
return
}
var content string var content string