Giter Site home page Giter Site logo

kubeprompt's Introduction

kubeprompt

Built with Nix

Isolates KUBECONFIG in each shell and shows the current Kubernetes context/namespace in your prompt

prompt

Installation

Manual install

Pre-built binaries are available for linux and MacOS on the releases page.

Homebrew (macOS and Linux)

brew tap jlesquembre/kubeprompt https://github.com/jlesquembre/kubeprompt/
brew install kubeprompt

NixOS

kubeprompt is available in the Nix Packages collection.

To install it globally, add it to your systemPackages. If you just want to try it, you can do it in a Nix shell:

nix-shell -p kubeprompt

Usage

kubeprompt is shell-agnostic and thus works on any shell, but every shell has a different way to customize its prompt. Some examples:

fish shell:

function fish_prompt
  echo (kubeprompt -f default) '>'
end

Zsh:

PROMPT='$(kubeprompt -f default)'$PROMPT

Bash:

PS1="[\u@\h \W \$(kubeprompt -f default)]\$ "

kubeprompt will print to stdout information about the current cluster, but first it needs to be enabled. It is considered enabled if the environment variable KUBECONFIG is set to a file in the $TMP/kubeprompt directory. To enable it, just execute kubeprompt. This will spawn a new shell (based on your SHELL environment variable) and will copy your current kubeconfig to a new temporary file (it uses k8s cli-runtime)

kubeprompt command will print the current K8S context and namespace, if kubeprompt is enabled. If not, it will start a sub shell with kubeprompt enabled. The sub shell to launch depends on the value of the environment variable SHELL, starting a bash shell if is not defined.

To disable kubeprompt you just need to got back to the previous shell. You can do that with the exit command or with the CTRL+d shortcut.

Valid flags:

  • -f, --format custom format string
  • -t, --temp-config copies the current KUBECONFIG to a temporary file, used with direnv
  • -c, --check print information about kubeprompt status
  • -h, --help help for kubeprompt
  • -v, --version print the version

Usage with direnv

From version 0.4, kubeprompt can be integrated with direnv. In this case, there is no need to start a sub shell.

.envrc e.g.:

export KUBECONFIG=$(kubeprompt -t)

# Only required if you want to set the K8S context
kubectx minikube

If you use direnv with nix (you should!), use this instead:

shell.nix:

with import <nixpkgs> { };
pkgs.mkShell {
  buildInputs = [
    # ...
  ];
  shellHook = ''
    export KUBECONFIG=$(kubeprompt -t)
    kubectx minikube
  '';
}

See direnv Wiki: Nix integration for more info.

direnv limitations: It is not possible to clean up the temporary KUBECONFIG files created when you exit the shell. But those temporary files are usually deleted when you shutdown your system. If you don't use direnv and spawn a new subshell with kubeprompt, the temporary files are deleted after you exit the subshell.

Integration with other tools

kubeprompt tries to leverage and complement existing tools.

To read and create a copy of your KUBECONFIG, it uses k8s cli-runtime. You can expect the same behavior as with the kubectl command.

It plays well with kubectx, since it uses kubectl under the hood.

For the same reason, it also plays well with k9s.

Prompt customization

It is possible to customize the prompt using go templates. The templates have access to 3 variables, Ctx, Ns and Enabled, and to the color functions provided by Aurora, plus the Bold, Faint, Italic and Underline functions.

The default format is

{{if .Enabled}}(K8S {{.Ctx | Yellow | Bold}}|{{.Ns | Magenta | Bold}}){{end}}

You have 2 options to provide your format string, with -f / --format option, or setting the environment variable KUBEPROMPT_FORMAT. If both are provided, -f is used.

Some examples:

  • No colors:
'(⎈ {{.Ctx}}|{{.Ns}})'
  • Print only if kubeprompt is enabled:
'{{if .Enabled}}{{"⎈" | Black | BgWhite }} {{.Ctx}}|{{.Ns}}{{end}}'
  • Print k8s string with a different color if kubeprompt is enabled:
'{{if .Enabled}}{{"k8s"|Green|Bold}}{{else}}{{"k8s"|Red}}{{end}} {{.Ctx}}|{{.Ns}}'

Workflows

Usually you'll call kubeprompt -f default in your dot files. In the terminal, you usually want to call kubeprompt to enable it, because after it, you can be confident about the information in your terminal, since every terminal will have its own isolated kubeconfig.

You can decide if you want to show the kubernetes information always or only when kubeprompt is enabled. If you want to show the information always, I recommend to use different colors to know if kubeprompt is enabled or not. I consider the disabled color as a warning, because in this case, you cannot be sure if the information is accurate. Since your kubeconfig is global, other applications (or yourself in other terminal) can change the global kubernetes context.

F.A.Q.

Why to copy kubeconfig and start a sub shell?

If you are working with multiple contexts/namespaces in multiple terminals, all of them will modify the same kubeconfig file when you update the context/namespace. For more context, see kubernetes PR #60044

Let's suppose that you are working in 2 terminals with kubectl in context foo. Since you are using kubeprompt, your prompt shows that the context is foo. Now, you change the context in one terminal to bar, and after it you change the focus to the other terminal. Since the prompt information is static, in that terminal the prompt still says that the context is foo, but that information is old, and you could execute a command in the wrong context.

To avoid it, kubeprompt creates a copy of your current kubeconfig per terminal and sets the KUBECONFIG environment variable to that copy. If now you change the context or the namespace, only that terminal will be affected. If you want to disable kubeprompt on one terminal, you just need to press CTRL+d

Related tools

kubeprompt's People

Contributors

butuzov avatar goreleaserbot avatar heydonovan avatar jlesquembre avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

kubeprompt's Issues

Just to say thank you!

After having integrated somewhat in our workflow, I realize this is a very valuable and little known piece of devops tooling! So thank you!

Request for advice: using kubeprompt inside a shell script

Is there a way to call kubeprompt inside a shell script? At least inside a bash script it seems to stop the execution of the script and request input from caller.

This might a bash thing also and not directly related to kubeprompt?

For example:

$ cat kubeprompt-test.bash
kubeprompt
kubeprompt -c

echo "do stuff"

exit # kubeprompt

echo "do other stuff still"

exit # script

Running the script

$ bash -x kubeprompt-test.bash 
+ kubeprompt
$ exit                   <<------- script execution stopped before this line; I typed 'exit' here manually
+ kubeprompt -c
kubeprompt is NOT active
+ echo 'do stuff'
do stuff
+ exit

Thanks!

Aurora greedily consumes chars in zsh prompt

It happens o that zsh seems to require special escapes so that color escape codes do not consume caracters in the tty:

https://github.com/jonmosco/kube-ps1/blob/486404e4b19b009ed0caaa55f642307ce8f3a506/kube-ps1.sh#L57-L73

image

Problem: I tried to work around working with the formatting context, but that doesn't work reliably since in direnv and shellHook the shell is always bash and not the end user shell. So I can't detect the end user shell in my setup.

I opened numtide/devshell#25 to track discussion on this user shell transparency.

direnv support

# .envrc
export KUBECONFIG=$(kubeprompt)

Just need to detect the go equivalent of:

        if [[ ''${DIRENV_IN_ENVRC:-} = 1 ]]; then
          return $TMPFILE
        fi

This is useful if a projects subfolder should scope to different environments.

A common use case is having kustomize overlays for:

├── bases
└── overlays
    ├── dev
    ├── prod
    └── staging
# ./overlays/dev/.envrc
kubectl config use-context k3d-local
export KUBECONFIG=$(kubeprompt)

Something is not working with the SHELL substitution

Is this an easy fix?

image

───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: /home/blaggacao/.oh-my-zsh/custom/plugins/nix-shell/scripts/buildShellShim.zsh
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ #! /usr/bin/env sh
   2   │
   3   │ if [ "$1" = "--rcfile" ]; then
   4   │   # the rcfile flag indicates that the --command option was used.
   5   │   # This means the shell should stay open after executing. So we remove the last line which contains 'exit'
   6   │   shift
   7   │   tmp="$(cat $1)"
   8   │   echo ${tmp%exit} > $1
   9   │   echo $NIX_EXECUTING_SHELL >> $1
  10   │   bash $1
  11   │ else
  12   │   bash "$@"
  13   │ fi
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Custom formated string

one more idea is actually to have custom formated string with two strings placeholders something %s%s

>kubeprompt -f --fmt "⎈ %s@%s"
⎈ minikube@default

Shell substitution.

There are small issues, like shell substitution now.

image

any usage of the kupeprompt, leave with no my helper functions.

Activation?

Should readme examples include part about pros/cons of -p and -f options?

And should it also include an example of actual activation with running kubeprompt with no options at all (as it spawns new shell).

Change context? (bash)

I wonder if it's actually possible to change $PS1 after switching context in bash.

image

# Added before declaration of PS1
export KUBECONFIG=~/.kube/config
kubeprompt
  • context not changing
  • shell losing all of the nice things, like auto-completion (because of kubeprompt switchover)

/ no bueno =(

Customize prompt with golang templates

I'm thinking about customizing the prompt using go templates.
The templates will have access to 3 variables, Ctx, Ns and Enabled, and to the color functions provided by Aurora. I think this solution gives to the users a lot of flexibility, and will allow to remove some of the flags. Some examples:

Default behaviour:

`(k8s {{.Ctx|Yellow}}|{{.Ns|Magenta}})`

Monochrome:

`(k8s {{.Ctx}}|{{.Ns}})`

Print only if enabled:

`{{if .Enabled}}k8s {{.Ctx}}|{{.Ns}}{{end}}`

Print k8s with a different color if kubepromt is enabled:

`{{if .Enabled}}{{"k8s"|Green|Bold}}{{else}}{{"k8s"|Red}}{{end}} {{.Ctx}}|{{.Ns}}`

ping @butuzov

Brew install doesn't work

Hi,

I tried

 brew tap jlesquembre/kubeprompt https://github.com/jlesquembre/kubeprompt/

==> Tapping jlesquembre/kubeprompt
Cloning into '/usr/local/Homebrew/Library/Taps/jlesquembre/homebrew-kubeprompt'...
ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

but it failed so i did

 git clone https://github.com/jlesquembre/kubeprompt.git /usr/local/Homebrew/Library/Taps/jlesquembre/homebrew-kubeprompt --origin=origin --template=
 and 
 brew install kubeprompt
 it works

homebrew installation fails

$ brew tap jlesquembre/kubeprompt
==> Tapping jlesquembre/kubeprompt
Cloning into '/usr/local/Homebrew/Library/Taps/jlesquembre/homebrew-kubeprompt'...
remote: Repository not found.
fatal: repository 'https://github.com/jlesquembre/homebrew-kubeprompt/' not found
Error: Failure while executing; `git clone https://github.com/jlesquembre/homebrew-kubeprompt /usr/local/Homebrew/Library/Taps/jlesquembre/homebrew-kubeprompt` exited with 128.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.