jonmosco / kube-ps1 Goto Github PK
View Code? Open in Web Editor NEWKubernetes prompt info for bash and zsh
License: Apache License 2.0
Kubernetes prompt info for bash and zsh
License: Apache License 2.0
seeing the following in osx with debug on, the prompt never displays any information.
[⎈ |:]]
+++ stat -f %m /Users/user/.kube/config
stat: cannot read file system information for '%m': No such file or directory
++ mtime=' File: "/Users/user/.kube/config"
ID: 100000500000018 Namelen: ? Type: apfs
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 488475719 Free: 420205592 Available: 418951658
Inodes: Total: 9223372036854775807 Free: 9223372036852263089'
++ [[ File: "/Users/user/.kube/config"
ID: 100000500000018 Namelen: ? Type: apfs
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 488475719 Free: 420205592 Available: 418951658
Inodes: Total: 9223372036854775807 Free: 9223372036852263089 -gt 0 ]]
-bash: [[: File: "/Users/user/.kube/config"
ID: 100000500000018 Namelen: ? Type: apfs
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 488475719 Free: 420205592 Available: 418951658
Inodes: Total: 9223372036854775807 Free: 9223372036852263089: syntax error in expression (error token is ": "/Users/user/.kube/config"
ID: 100000500000018 Namelen: ? Type: apfs
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 488475719 Free: 420205592 Available: 418951658
Inodes: Total: 9223372036854775807 Free: 9223372036852263089")
When using kube-ps1 the prompt can be very long as seen in this example:
It would be great if one could create a shorter alias for the context (and possibly namespace as well).
For example:
KUBE_PS1_CONTEXT_ALIASES='gke_xxxxxxxxxxxxxxxxxxxxxxx_europe-west1-c_prod=prod, another_context=another'
thus rendering only prod
instead of gke_xxxxxxxxxxxxxxxxxxxxxxx_europe-west1-c_prod
and another
instead of another_context
as context name in the prompt.
Ubuntu: 16.04
Issue
The latest pull has changed the icon to odd characters.
Example:
[(�⎈ |minikube:default)�]
When using the KUBECONFIG
environment variable to change the path to the kubeconfig file it results in the prompt not being updated.
~ (⎈ |minikube:default) export KUBECONFIG=~/.kube/config-n10
~ (⎈ |minikube:default) kctx
kubernetes-admin@kubernetes
I would have expected my prompt to have changed to
~ (⎈ |kubernetes-admin@kubernetes:default)
kubectl config view --minify --output 'jsonpath={..namespace}'
can return empty if you haven't explicitly set the namespace.
I'm seeing (⎈ |minikube:)
in my prompt, for instance. Setting a namespace fixes it. So maybe default to "default"
if value is missing.
(⎈ |minikube:) ➜ ~ git:(master) kubens default
Context "minikube" modified.
Active namespace is "default".
(⎈ |minikube:default) ➜ ~ git:(master)
not particularly an issue, but I think calling kube_ps1
function takes about 200ms. this is because kube-ps1 calls kubectl, twice, for each shell prompt.
this is visibly making my shell slower.
ls
(enter), ls
(enter), ls
(enter) ,ls
(enter) and get a feeling for itfunction kube_ps1() {}
ls
(enter), ls
(enter), ls
(enter) ,ls
(enter)to measure the delay added, paste this to your shell:
while :; do
ts=$(date +%s%N) ; kube_ps1 ; tt=$((($(date +%s%N) - $ts)/1000000)) ; echo "Time taken: $tt milliseconds"
done
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 229 milliseconds
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 201 milliseconds
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 238 milliseconds
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 219 milliseconds
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 204 milliseconds
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 210 milliseconds
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 207 milliseconds
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 203 milliseconds
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 208 milliseconds
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 209 milliseconds
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 198 milliseconds
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 208 milliseconds
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 219 milliseconds
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 242 milliseconds
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 218 milliseconds
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 208 milliseconds
(%f%F{blue}⎈ %f|%F{red}minikube%f:%F{cyan}default%f)
Time taken: 220 milliseconds
I'll talk about some potential improvements in the next isssue.
Is this working with oh-my-zsh?
I am on a CentOS 7 machine and I have kubectl installed. I am using oh-my-zsh with agnoster
as a theme.
I have added the following lines in my .zshrc
file but nothing is happening when I source
/.zshrc`:
source /path/to/kube-ps1.sh
PROMPT='$(kube-ps1)'$PROMPT
As the README, config ~/.zshrc
source /path/to/kube-ps1.sh
PROMPT='$(kube_ps1) $PROMPT'
Get this prompt:
(⎈ |kubernetes:default)$(kube_ps1) $PROMPT
Change to
PROMPT='$(kube_ps1)'$PROMPT
Get this:
(⎈ |kubernetes:dev)➜ ~
I'd like to change the background color of prompt so it would be nice if this would be available as a configuration option. Currently I do like this to get it to work the way I want:
PROMPT=%{$bg[white]%}'$(kube_ps1)'$PROMPT
Hi, there, after I ran upgrade_oh_my_zsh
and restarted shell, I get this:
.oh-my-zsh/plugins/kube-ps1/kube-ps1.plugin.zsh:24: command not found: add-zsh-hook
Best regards
As seen in #4, kube_ps1
shell function is slow (adds about 200 ms to every shell prompt users get).
This is because kubectl is not a fast program as one would think and it has a startup latency.
I have a few ideas to make this faster. Here's my top idea that I think can work really fast:
yq
(https://github.com/kislyuk/yq) (or another yaml parser) a mandatory dependency for this scriptcat $f | yq (..path-query..)
.this should be muuuchhhh faster, I bet it can complete within 10 ms, which is an acceptable delay.
I just installed to another machine on zsh and sourcing the script blocks the shell from starting:
It should work fine even if there’s no current-context set.
export KUBE_PS1_SEPARATOR=''
when I specify that I still get the |
character:
{⎈ |test:default}
any other non-empty value works fine, however.
Would it be possible and make sense to add an environment variable that hides the namespace? In my case I virtually never change the namespace so I might as well not show it.
If current-context is not set on kubectl, starting a new shell gives error:
error: current-context is not set
kubectl context is not set
Hey,
thanks for this nice tool. I have just one problem: Everytime i hit tab to complete paths in zsh, it appends a whitespace and thus destroys further navigation with tab.
Any idea?
Here is my config (.zshrc):
source /usr/local/Cellar/kube-ps1/0.6.0/share/kube-ps1.sh
KUBE_PS1_SYMBOL_USE_IMG=true
KUBE_PS1_SYMBOL_ENABLE=true
KUBE_PS1_NS_ENABLE=true
PROMPT=$(kube_ps1)$PROMPT
KUBE_PS1_BG_COLOR=black
Hello,
I'm trying to specify a different color for the Kubernetes context according to the documentation for bash but this doesn't seem to work.
Would you be able to assist me with this ?
I basically want to modify the context color from red to purple.
Thanks.
Regards,
Dieter.
I see it's on the file, but would be nice to have in repo root as a file as well. GitHub detects that and shows in the UI.
Hello,
I've sourced the kube-ps1.sh file in my bash configuration and modified my bash prompt to include the kubernetes context information.
However, it seems that when I open a new shell for the first time, the kubernetes context is not loaded. Only after hitting 'Enter' on the command line, is the context loaded.
See my prompt output below.
Last login: Sun Jan 21 19:25:42 on ttys002
(⎈ |:) » ~
(⎈ |docker-for-desktop:default) » ~
Any idea what could be wrong in my configuration ?
Thanks.
Would be good to have the fixes in homebrew.
(⎈ |test:default) ➜ ~ export KUBE_PS1_PREFIX=''
⎈ |test:default) ➜ ~ export KUBE_PS1_SEPARATOR=''
⎈ |test:default) ➜ ~ export KUBE_PS1_SEPARATOR='x'
⎈ |test:default) ➜ ~
In bash following lines are not working. Removing parts in brackets is work arounding it.
https://github.com/jonmosco/kube-ps1/blob/master/kube-ps1.sh#L31-L34
github.com/jonmosco/kube-ps1/kube-ps1.sh: line 31: conditional binary operator expected
github.com/jonmosco/kube-ps1/kube-ps1.sh: line 31: syntax error near KUBE_PS1_PREFIX' github.com/jonmosco/kube-ps1/kube-ps1.sh: line 31:
[[ -v KUBE_PS1_PREFIX ]] || KUBE_PS1_PREFIX="("'
It looks like my zsh completion doesn't work as expected when the kube-ps1 prompt is enabled. This is the relavant parts of my ~/.zshrc
file:
KUBE_PS1_SYMBOL_USE_IMG=true
KUBE_PS1_SYMBOL_ENABLE=true
KUBE_PS1_SEPARATOR=' '
KUBE_PS1_PREFIX=''
KUBE_PS1_SUFFIX=''
KUBE_PS1_NS_ENABLE=false
source ~/.zsh/scripts/kube-ps1.sh
PROMPT='$(kube_ps1)'$PROMPT
# Disable kube prompt by default, enable with "kubeon"
kubeoff
The prompt now looks like this when I start a new window:
If I enter kubec
and press tab it's expanded to:
But when I run kubeon
and enter kubec
and pressing tab I get this:
Notice the extra "k" that is inserted.
Lines 61 to 62 in 7d06e2d
This caused me at least 1 hour of debugging.
I was trying to figure out what the hell is loading zsh stat module, because it prevented me from calling the actual stat
binary on my machine.
Please remove these lines. It's unexpected for a simple tool like kube-ps1 to have substantial side-effects like this.
Starting state: kubeoff -g
, run 3 shell windows
When I run kubeon -g
on the current shell, I expect:
Current behavior:
Starting state: kubeon -g
, run 3 shell windows
When I run kubeoff -g
on the current shell, I expect:
Current behavior:
Are my expectations not accurate? I kind of expected kubeon/kubeoff to work this way.
stat
function does not check target file's changed time but symlink files' changed time. Therefore, _kube_ps1_file_newer_than
function always return false if kube config file is symlink
_kube_ps1_file_newer_than() {
local mtime
local file=$1
local check_time=$2
if [[ "${KUBE_PS1_SHELL}" == "zsh" ]]; then
mtime=$(stat +mtime "${file}")
elif stat -c "%s" /dev/null &> /dev/null; then
# GNU stat
mtime=$(stat -c %Y "${file}")
else
# BSD stat
mtime=$(stat -f %m "$file")
fi
[[ "${mtime}" -gt "${check_time}" ]]
}
Could you possibly add the ability to display the kube_ps1 part of the prompt on the right side of the terminal (refer to screenshots).
This is how kube-ps1 looks in my terminal window:
And this is how zsh-kubectl-prompt looks in my terminal window:
I think a custom option that would implement this functionality would also be extremely useful for people who use bullet train/powerline9k type themes where the prompt tends to get pretty long.
If there is already a way through which I can accomplish this by modifying my zsh config then do let me know, I'd highly appreciate that and would love to switch to over to kube-ps1.
I just downloaded from master and sourced, but I got:
_kube_ps1_shell_settings:4: command not found: add-zsh-hook
I'm using oh-my-zsh on Mac.
I think I saw Jon demo this but I didn't see any information about it in the documentation.
Thanks
Showing name of the user from the current context would be super useful for workflows where one needs to interact with the cluster using varying set of credentials (like "cluster admin" vs "app deployer").
kubeoff -g
→ hides kube-ps1 string (expected)kubeon
→ ps1 string still does not show up (NOT EXPECTED)the following doesn't work for two reasons:
# .zshrc file:
export KUBE_PS1_PREFIX='['
export KUBE_PS1_SUFFIX=']'
source "$HOME/workspace/dotfiles/kube-ps1.sh"
(
and )
.(⎈ |docker-for-desktop:default) ➜ ~ env | grep KUBE
KUBE_PS1_PREFIX=(
KUBE_PS1_SUFFIX=)
ideally the order of export
and source
shouldn't matter in this case I think.
If I move export after sourcing, it works.
Hello, I'm using a font that didn't support the unicode symbol \u2388
, so I try to customize the symbol in ~/.bashrc
, then I found the symbol is hard coded in the bash block of _kube_ps1_symbol()
, not using the KUBE_PS1_SYMBOL_DEFAULT
.
I think this is the reason why the customization of the symbol not taking effect, tried to fix it and tested on bash 4.x
.
Due to the different encode way , I don't know how to fix it on bash version lower than 4.x
, because I don't have the envirment to test it, does anyone have a good idea?
I see similar issue was reported but closed here.
In my case (Linux Mint 18.3 Sylvia) it looks like this:
I fixed it by using:
_KUBE_PS1_OPEN_ESC=$'' _KUBE_PS1_CLOSE_ESC=$''
instead of:
_KUBE_PS1_OPEN_ESC=$'\001' _KUBE_PS1_CLOSE_ESC=$'\002'
kubeon/kubeoff no longer seems to work with comments indicating deprecation in the source code.
Was there a discussion about this? I’m lately a little frustrated in number of the ways the project is getting in my way of doing things.
I have developed kubectx/kubens so people set it up once and forget about it. So far I tend to think I achieved that. I have ideas to add there but I’ve been holding off on making major changes, because a lot of people tell me that it’s very much central to their day-to-day workflow.
I want to see the same from kube-ps1. However, so far my experience is reflecting it's been the case that things keep breaking, too many configurable knobs mostly untested, kubeon/kubeoff stops working either because of badly implemented state machine or some arbitrary deprecation.
I am no longer able to keep up with these changes. If you are seeking help or guidance on this project, I can provide support such as reviewing every single commit. But I can no longer stand the fact that this project is being pulled to different directions in many directions. I just want it to work.
Installed bash-git-prompt
using brew
before on my MacOS.
Here is how my ~/.bash_profile look like:
if [ -f $(brew --prefix)/etc/bash_completion ]; then
source $(brew --prefix)/etc/bash_completion
fi
if [ -f $(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh ]; then
__GIT_PROMPT_DIR=$(brew --prefix)/opt/bash-git-prompt/share
source $(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh
fi
if [ -f $(brew --prefix)/opt/kube-ps1/share/kube-ps1.sh ]; then
source $(brew --prefix)/opt/kube-ps1/share/kube-ps1.sh
PS1='$(kube_ps1) $(__git_ps1)'
fi
However the new terminal window only shows git prompt.
I also tried putting the new lines in ~/.bashrc as said in the installation setup, but no luck.
I also tried putting the new lines before git-prompt setup, but no luck.
The only way to get kube_ps1 to work is to remove git-prompt.
For long cluster names & namespaces it takes a lot of space in a prompt:
(kubernetes.us-west-2.dev.subenv.company.domain:application.namepsace)~prompt$
Two options can be introduced:
setting both to 10 will end to:
(kube.us-we..:applicatio..)~prompt$
default: -1 (no trim)
E.g. prod, production, etc.
Thoughts? If idea accepted, I can send a PR. 😇
I propose we introduce these commands kubeon/kubeoff to indicate whether we want Kubernetes status shown on our ps1 or not.
kubeoff
would create a sentinel file, say, ~/.kube/ps1/off
. When this exists, the kube_ps1
would return empty string/noop, so no Kubernetes shown in PS1.kubeon
would delete that file.I have 3 different contexts, all set in KUBECONFIG.
kube-prompt fails when I try to get a certain pod or service
>>> get pods panic: stat /Users/tarekmehrez/.kube/config: no such file or directory
goroutine 36 [running]:
github.com/c-bata/kube-prompt/kube.NewClient(0x0)
/Users/a14737/go/src/github.com/c-bata/kube-prompt/kube/client.go:29 +0x140
github.com/c-bata/kube-prompt/kube.getClient(0x0)
/Users/a14737/go/src/github.com/c-bata/kube-prompt/kube/client.go:14 +0x2e
github.com/c-bata/kube-prompt/kube.fetchPods()
/Users/a14737/go/src/github.com/c-bata/kube-prompt/kube/resource.go:177 +0x72
created by github.com/c-bata/kube-prompt/kube.getPodSuggestions
/Users/a14737/go/src/github.com/c-bata/kube-prompt/kube/resource.go:183 +0x39
Is there a way that this might work just by checking my current context?
First off, awesome prompt! I enabled it and noticed that it appears to significantly slow down my prompt. The simple test is hitting enter a few times. If I run kubeoff the response is immediate, but if I run kubeon then hitting enter multiple times results in a rather significant delay. Unfortunately, I have not had much time to look into why this might be the case, but figured I would file an issue.
Kubeon/off currently applies to all shells based off a file check.
I think these functions could be more useful if they apply to the current session only. This can be achieved by setting and unsetting a variable:
KUBE_PS1_DISABLE=true
unset KUBE_PS1_DISABLE
then test for this on kube_ps1
Add a feature to disable the k8s image from the prompt.
It looks like it's outdated.
It can now use kubeon
/kubeoff
, along with the usual kubectx/kubens jazz!
Hi!
I've installed kube-ps1
but it messed my bash completion :(
It completes commands properly but not resources names (for instance pod names).
It still won't work, even after I've uninstalled kube-ps1.
Some help would be really appreciated, thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.