mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Allow specifying k8s context in configuration
This commit is contained in:
@@ -35,10 +35,21 @@ func (widget *Widget) getInstance() (*clientInstance, error) {
|
||||
|
||||
// getKubeClient returns a kubernetes clientset for the kubeconfig provided
|
||||
func (widget *Widget) getKubeClient() (kubernetes.Interface, error) {
|
||||
config, err := clientcmd.BuildConfigFromFlags("", widget.kubeconfig)
|
||||
var overrides *clientcmd.ConfigOverrides
|
||||
if widget.context != "" {
|
||||
overrides = &clientcmd.ConfigOverrides{
|
||||
CurrentContext: widget.context,
|
||||
}
|
||||
}
|
||||
|
||||
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
|
||||
&clientcmd.ClientConfigLoadingRules{ExplicitPath: widget.kubeconfig},
|
||||
overrides).ClientConfig()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
clientset, err := kubernetes.NewForConfig(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -18,6 +18,7 @@ type Settings struct {
|
||||
title string `help:"Override the title of widget."`
|
||||
kubeconfig string `help:"Location of a kubeconfig file."`
|
||||
namespaces []string `help:"List of namespaces to watch. If blank, defaults to all namespaces."`
|
||||
context string `help:"Kubernetes context to use. If blank, uses default context"`
|
||||
}
|
||||
|
||||
func NewSettingsFromYAML(name string, moduleConfig *config.Config, globalConfig *config.Config) *Settings {
|
||||
@@ -29,6 +30,7 @@ func NewSettingsFromYAML(name string, moduleConfig *config.Config, globalConfig
|
||||
title: moduleConfig.UString("title"),
|
||||
kubeconfig: moduleConfig.UString("kubeconfig"),
|
||||
namespaces: utils.ToStrs(moduleConfig.UList("namespaces")),
|
||||
context: moduleConfig.UString("context"),
|
||||
}
|
||||
|
||||
return &settings
|
||||
|
||||
@@ -18,6 +18,7 @@ type Widget struct {
|
||||
title string
|
||||
kubeconfig string
|
||||
namespaces []string
|
||||
context string
|
||||
settings *Settings
|
||||
}
|
||||
|
||||
@@ -31,6 +32,7 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
kubeconfig: settings.kubeconfig,
|
||||
namespaces: settings.namespaces,
|
||||
settings: settings,
|
||||
context: settings.context,
|
||||
}
|
||||
|
||||
widget.View.SetWrap(true)
|
||||
@@ -101,6 +103,10 @@ func (widget *Widget) generateTitle() string {
|
||||
}
|
||||
title := "Kube"
|
||||
|
||||
if widget.context != "" {
|
||||
title = fmt.Sprintf("%s (%s)", title, widget.context)
|
||||
}
|
||||
|
||||
if len(widget.namespaces) == 1 {
|
||||
title += fmt.Sprintf(" - Namespace: %s", widget.namespaces[0])
|
||||
} else if len(widget.namespaces) > 1 {
|
||||
|
||||
@@ -10,6 +10,7 @@ func Test_generateTitle(t *testing.T) {
|
||||
type fields struct {
|
||||
title string
|
||||
namespaces []string
|
||||
context string
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
@@ -46,6 +47,14 @@ func Test_generateTitle(t *testing.T) {
|
||||
},
|
||||
want: "Test Explicit Title",
|
||||
},
|
||||
{
|
||||
name: "Context set",
|
||||
fields: fields{
|
||||
namespaces: []string{},
|
||||
context: "test-context",
|
||||
},
|
||||
want: "Kube (test-context)",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range testCases {
|
||||
@@ -53,6 +62,7 @@ func Test_generateTitle(t *testing.T) {
|
||||
widget := &Widget{
|
||||
title: tt.fields.title,
|
||||
namespaces: tt.fields.namespaces,
|
||||
context: tt.fields.context,
|
||||
}
|
||||
assert.Equal(t, tt.want, widget.generateTitle())
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user