JanDeDobbeleer / oh-my-posh

The most customisable and low-latency cross platform/shell prompt renderer
https://ohmyposh.dev
MIT License
16.7k stars 2.34k forks source link

Add ability to display current kubernetes namespace #392

Closed andrewsav-bt closed 3 years ago

andrewsav-bt commented 3 years ago

Description

I would like to be able to include the name of the current kubernetes namespace into a segment.

See https://stackoverflow.com/a/55854690 for a kubectl command to get the namespace, but I'm sure that go client api also can do that.

tradiff commented 3 years ago

I can work on this today after work. A few questions:

  1. @andrewsav-bt If the user has not set a namespace, should the return value be "default"?
  2. @JanDeDobbeleer I'm thinking this would be part of the kubectl segment, and it would work similar to the azure segment. Do you agree with this approach?
    1. Add DisplayContext (boolean) defaults to true
    2. Add DisplayNamespace (boolean) defaults to false
    3. Add InfoSeparator (string)
JanDeDobbeleer commented 3 years ago

@TravisTX I would opt to go for templates rather than the InfoSeparator, I should finish up the aws segment (#387) in that case as that contains a generic way to use templates in segments. All I need to do is add the tests, functionality is validated.

JanDeDobbeleer commented 3 years ago

@TravisTX merged #387, will extend the tests later, basic functionality is there.

andrewsav-bt commented 3 years ago

@andrewsav-bt If the user has not set a namespace, should the return value be "default"?

As far as understand when you run any kubectl namespaced command, such as get pod it will always return data from a single namespace when you do not specify any switches that affect this behaviour. This is the namespace I would expect to be returned. Many thanks.

andrewsav-bt commented 3 years ago

Thank you everyone who helped making it happen!

alecor191 commented 3 years ago

I can't seem to get namespaces to be shown, so I'm wondering if I'm missing something:

❯ get-module oh-my-posh

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     3.87.0     beta       oh-my-posh                          {Get-PoshInfoForV2Users, Get-PoshThemes, Set-PoshPrompt, Write-PoshTheme…}
{
    "type": "kubectl",
    "style": "diamond",
    "foreground": "#FFFFFF",
    "background": "#2A67E5",
    "leading_diamond": " \uE0B6",
    "trailing_diamond": "\uE0B4 ",
    "properties": {
        "prefix": "\uFD31 ",
        "template": "{{.Context}} :: {{if .Namespace}}{{.Namespace}}{{else}}default{{end}}",
        "display_error": true
    }
},
❯ kubectl config view --minify --output "jsonpath={..current-context},{..namespace}"
test-cluster,my-test-namespace

I also tried to remove the template property from my kubectl segment as I understand that by default it should now also show the namespace, but it didn't help. I always just get the name of the context printed by oh-my-posh.

I'm using PowerShell 7.1.2 inside Windows Terminal 1.5.10411.0.

Am I missing something?

JanDeDobbeleer commented 3 years ago

@TravisTX care to take a look?

tradiff commented 3 years ago

@alecor191 everything looks correct to me. Just to be sure, I have copied your config as well as your context and namespace names and confirmed, I do indeed see "my-test-namespace".

The behavior sounds like OMP is actually running on an older version of OMP. Maybe go to your modules folder and delete any older versions to confirm that's not what's going on. @JanDeDobbeleer you have any other ideas about that? I typically don't run from the public powershell module (although I did just now install it, to confirm the kubectl segment works as expected with that method)

JanDeDobbeleer commented 3 years ago

No, same thoughts. Couldn't see an issue either.

alecor191 commented 3 years ago

Thanks a lot @TravisTX and @JanDeDobbeleer for your super-fast replies. That was it: While I did uninstall all previous versions, even reboot PC before posting my message, it turned out that I still had an old OMP executable around that got picked up when calling oh-my-posh --init -shell pwsh ....

Deleting that old file and using Set-PoshPrompt instead of the command above, resolved the issue.

Thanks for your help!

github-actions[bot] commented 4 months ago

This issue has been automatically locked since there has not been any recent activity (i.e. last half year) after it was closed. It helps our maintainers focus on the active issues. If you have found a problem that seems similar, please open a discussion first, complete the body with all the details necessary to reproduce, and mention this issue as reference.