feat(kubectl): specify context aliases

This commit is contained in:
Jan De Dobbeleer 2023-07-14 13:51:25 +02:00 committed by Jan De Dobbeleer
parent 2bfd08ec57
commit b27608dd73
4 changed files with 65 additions and 12 deletions

View file

@ -10,7 +10,10 @@ import (
)
// Whether to use kubectl or read kubeconfig ourselves
const ParseKubeConfig properties.Property = "parse_kubeconfig"
const (
ParseKubeConfig properties.Property = "parse_kubeconfig"
ContextAliases properties.Property = "context_aliases"
)
type Kubectl struct {
props properties.Properties
@ -88,9 +91,13 @@ func (k *Kubectl) doParseKubeConfig() bool {
if !exists {
continue
}
if context != nil {
k.KubeContext = *context
}
k.SetContextAlias()
return true
}
@ -123,6 +130,7 @@ func (k *Kubectl) doCallKubectl() bool {
return false
}
k.Context = config.CurrentContext
k.SetContextAlias()
if len(config.Contexts) > 0 {
k.KubeContext = *config.Contexts[0].Context
}
@ -137,3 +145,10 @@ func (k *Kubectl) setError(message string) {
k.User = message
k.Cluster = message
}
func (k *Kubectl) SetContextAlias() {
aliases := k.props.GetKeyValueMap(ContextAliases, map[string]string{})
if alias, exists := aliases[k.Context]; exists {
k.Context = alias
}
}

View file

@ -34,6 +34,7 @@ func TestKubectlSegment(t *testing.T) {
ExpectedEnabled bool
ExpectedString string
Files map[string]string
ContextAliases map[string]string
}{
{
Case: "kubeconfig incomplete",
@ -57,6 +58,16 @@ func TestKubectlSegment(t *testing.T) {
ExpectedEnabled: true,
},
{Case: "no namespace", Template: standardTemplate, KubectlExists: true, Context: "aaa", ExpectedString: "aaa", ExpectedEnabled: true},
{
Case: "kubectl context alias",
Template: standardTemplate,
KubectlExists: true,
Context: "aaa",
Namespace: "bbb",
ContextAliases: map[string]string{"aaa": "ccc"},
ExpectedString: "ccc :: bbb",
ExpectedEnabled: true,
},
{
Case: "kubectl error",
Template: standardTemplate,
@ -77,6 +88,15 @@ func TestKubectlSegment(t *testing.T) {
ExpectedString: "aaa :: bbb :: ccc :: ddd",
ExpectedEnabled: true,
},
{
Case: "kubeconfig context alias",
Template: standardTemplate,
ParseKubeConfig: true,
Files: testKubeConfigFiles,
ContextAliases: map[string]string{"aaa": "ccc"},
ExpectedString: "ccc :: bbb",
ExpectedEnabled: true,
},
{
Case: "kubeconfig multiple current marker first",
Template: testKubectlAllInfoTemplate,
@ -135,6 +155,7 @@ func TestKubectlSegment(t *testing.T) {
props: properties.Map{
properties.DisplayError: tc.DisplayError,
ParseKubeConfig: tc.ParseKubeConfig,
ContextAliases: tc.ContextAliases,
},
}
assert.Equal(t, tc.ExpectedEnabled, k.Enabled(), tc.Case)

View file

@ -1416,6 +1416,12 @@
"title": "Parse kubeconfig",
"description": "Parse kubeconfig files instead of calling out to kubectl to improve performance.",
"default": false
},
"context_aliases": {
"type": "object",
"title": "Context aliases",
"description": "Custom context names.",
"default": {}
}
}
}

View file

@ -10,16 +10,23 @@ Display the currently active Kubernetes context name and namespace name.
## Sample Configuration
import Config from '@site/src/components/Config.js';
import Config from "@site/src/components/Config.js";
<Config data={{
"type": "kubectl",
"style": "powerline",
"powerline_symbol": "\uE0B0",
"foreground": "#000000",
"background": "#ebcc34",
"template": " \uFD31 {{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}} "
}}/>
<Config
data={{
type: "kubectl",
style: "powerline",
powerline_symbol: "\uE0B0",
foreground: "#000000",
background: "#ebcc34",
template: " \uFD31 {{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}} ",
properties: {
context_aliases: {
"arn:aws:eks:eu-west-1:1234567890:cluster/posh": "posh",
},
},
}}
/>
## Properties
@ -27,6 +34,7 @@ import Config from '@site/src/components/Config.js';
| ------------------ | --------- | ----------------------------------------------------------------------------------------------------- |
| `display_error` | `boolean` | show the error context when failing to retrieve the kubectl information - defaults to `false` |
| `parse_kubeconfig` | `boolean` | parse kubeconfig files instead of calling out to kubectl to improve performance - defaults to `false` |
| `context_aliases` | `object` | custom context names |
## Template ([info][templates])
@ -47,11 +55,14 @@ import Config from '@site/src/components/Config.js';
| `.User` | `string` | the current kubectl context user |
| `.Cluster` | `string` | the current kubectl context cluster |
## Tips
:::tip
It is common for the Kubernetes "default" namespace to be used when no namespace is provided. If you want your prompt to
render an empty current namespace using the word "default", you can use something like this for the template:
`{{.Context}} :: {{if .Namespace}}{{.Namespace}}{{else}}default{{end}}`
```
{{.Context}} :: {{if .Namespace}}{{.Namespace}}{{else}}default{{end}}
```
:::
[templates]: /docs/configuration/templates