mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
feat(kubectl): specify context aliases
This commit is contained in:
parent
2bfd08ec57
commit
b27608dd73
|
@ -10,7 +10,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Whether to use kubectl or read kubeconfig ourselves
|
// 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 {
|
type Kubectl struct {
|
||||||
props properties.Properties
|
props properties.Properties
|
||||||
|
@ -88,9 +91,13 @@ func (k *Kubectl) doParseKubeConfig() bool {
|
||||||
if !exists {
|
if !exists {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if context != nil {
|
if context != nil {
|
||||||
k.KubeContext = *context
|
k.KubeContext = *context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
k.SetContextAlias()
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,6 +130,7 @@ func (k *Kubectl) doCallKubectl() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
k.Context = config.CurrentContext
|
k.Context = config.CurrentContext
|
||||||
|
k.SetContextAlias()
|
||||||
if len(config.Contexts) > 0 {
|
if len(config.Contexts) > 0 {
|
||||||
k.KubeContext = *config.Contexts[0].Context
|
k.KubeContext = *config.Contexts[0].Context
|
||||||
}
|
}
|
||||||
|
@ -137,3 +145,10 @@ func (k *Kubectl) setError(message string) {
|
||||||
k.User = message
|
k.User = message
|
||||||
k.Cluster = 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -34,6 +34,7 @@ func TestKubectlSegment(t *testing.T) {
|
||||||
ExpectedEnabled bool
|
ExpectedEnabled bool
|
||||||
ExpectedString string
|
ExpectedString string
|
||||||
Files map[string]string
|
Files map[string]string
|
||||||
|
ContextAliases map[string]string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
Case: "kubeconfig incomplete",
|
Case: "kubeconfig incomplete",
|
||||||
|
@ -57,6 +58,16 @@ func TestKubectlSegment(t *testing.T) {
|
||||||
ExpectedEnabled: true,
|
ExpectedEnabled: true,
|
||||||
},
|
},
|
||||||
{Case: "no namespace", Template: standardTemplate, KubectlExists: true, Context: "aaa", ExpectedString: "aaa", 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",
|
Case: "kubectl error",
|
||||||
Template: standardTemplate,
|
Template: standardTemplate,
|
||||||
|
@ -77,6 +88,15 @@ func TestKubectlSegment(t *testing.T) {
|
||||||
ExpectedString: "aaa :: bbb :: ccc :: ddd",
|
ExpectedString: "aaa :: bbb :: ccc :: ddd",
|
||||||
ExpectedEnabled: true,
|
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",
|
Case: "kubeconfig multiple current marker first",
|
||||||
Template: testKubectlAllInfoTemplate,
|
Template: testKubectlAllInfoTemplate,
|
||||||
|
@ -135,6 +155,7 @@ func TestKubectlSegment(t *testing.T) {
|
||||||
props: properties.Map{
|
props: properties.Map{
|
||||||
properties.DisplayError: tc.DisplayError,
|
properties.DisplayError: tc.DisplayError,
|
||||||
ParseKubeConfig: tc.ParseKubeConfig,
|
ParseKubeConfig: tc.ParseKubeConfig,
|
||||||
|
ContextAliases: tc.ContextAliases,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
assert.Equal(t, tc.ExpectedEnabled, k.Enabled(), tc.Case)
|
assert.Equal(t, tc.ExpectedEnabled, k.Enabled(), tc.Case)
|
||||||
|
|
|
@ -1416,6 +1416,12 @@
|
||||||
"title": "Parse kubeconfig",
|
"title": "Parse kubeconfig",
|
||||||
"description": "Parse kubeconfig files instead of calling out to kubectl to improve performance.",
|
"description": "Parse kubeconfig files instead of calling out to kubectl to improve performance.",
|
||||||
"default": false
|
"default": false
|
||||||
|
},
|
||||||
|
"context_aliases": {
|
||||||
|
"type": "object",
|
||||||
|
"title": "Context aliases",
|
||||||
|
"description": "Custom context names.",
|
||||||
|
"default": {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,16 +10,23 @@ Display the currently active Kubernetes context name and namespace name.
|
||||||
|
|
||||||
## Sample Configuration
|
## Sample Configuration
|
||||||
|
|
||||||
import Config from '@site/src/components/Config.js';
|
import Config from "@site/src/components/Config.js";
|
||||||
|
|
||||||
<Config data={{
|
<Config
|
||||||
"type": "kubectl",
|
data={{
|
||||||
"style": "powerline",
|
type: "kubectl",
|
||||||
"powerline_symbol": "\uE0B0",
|
style: "powerline",
|
||||||
"foreground": "#000000",
|
powerline_symbol: "\uE0B0",
|
||||||
"background": "#ebcc34",
|
foreground: "#000000",
|
||||||
"template": " \uFD31 {{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}} "
|
background: "#ebcc34",
|
||||||
}}/>
|
template: " \uFD31 {{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}} ",
|
||||||
|
properties: {
|
||||||
|
context_aliases: {
|
||||||
|
"arn:aws:eks:eu-west-1:1234567890:cluster/posh": "posh",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
|
||||||
## Properties
|
## 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` |
|
| `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` |
|
| `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])
|
## Template ([info][templates])
|
||||||
|
|
||||||
|
@ -47,11 +55,14 @@ import Config from '@site/src/components/Config.js';
|
||||||
| `.User` | `string` | the current kubectl context user |
|
| `.User` | `string` | the current kubectl context user |
|
||||||
| `.Cluster` | `string` | the current kubectl context cluster |
|
| `.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
|
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:
|
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
|
[templates]: /docs/configuration/templates
|
||||||
|
|
Loading…
Reference in a new issue