{"id":13821452,"url":"https://github.com/Ramilito/kubesess","last_synced_at":"2025-05-16T12:33:25.323Z","repository":{"id":50490246,"uuid":"517858560","full_name":"Ramilito/kubesess","owner":"Ramilito","description":"Kubectl plugin managing sessions","archived":false,"fork":false,"pushed_at":"2025-05-15T08:26:43.000Z","size":4553,"stargazers_count":268,"open_issues_count":6,"forks_count":5,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-05-15T08:38:48.047Z","etag":null,"topics":["bash","cli","kubectl","kubectl-plugins","kubernetes","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ramilito.png","metadata":{"files":{"readme":".github/README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-07-26T00:15:55.000Z","updated_at":"2025-05-15T08:23:12.000Z","dependencies_parsed_at":"2023-10-16T18:24:52.915Z","dependency_job_id":"10070a09-9bf2-4e65-a5ff-4680931a4b7a","html_url":"https://github.com/Ramilito/kubesess","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ramilito%2Fkubesess","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ramilito%2Fkubesess/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ramilito%2Fkubesess/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ramilito%2Fkubesess/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ramilito","download_url":"https://codeload.github.com/Ramilito/kubesess/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254530679,"owners_count":22086661,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","cli","kubectl","kubectl-plugins","kubernetes","rust"],"created_at":"2024-08-04T08:01:22.173Z","updated_at":"2025-05-16T12:33:25.316Z","avatar_url":"https://github.com/Ramilito.png","language":"Rust","funding_links":[],"categories":["rust","Rust"],"sub_categories":[],"readme":"# kubesess(ion)\n\n\u003cp align=\"center\"\u003e\n  \u003cimg width=\"512\" height=\"512\" src=\"https://github.com/Ramilito/kubesess/assets/8473233/c84dd0b1-0856-414b-98ff-7f3b4a9a301e\"\u003e\n\u003c/p\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eTable of Contents\u003c/summary\u003e\n\n- [kubesess(ion)](#kubesess-ion-)\n  * [Showcase](#showcase)\n  * [About The Project](#about-the-project)\n    + [What](#what)\n    + [Why](#why)\n    + [How](#how)\n    + [Benchmark](#benchmark)\n  * [Getting Started](#getting-started)\n    + [Prerequisite](#prerequisite)\n    + [Installation](#installation)\n  * [Usage](#usage)\n  * [Roadmap](#roadmap)\n  * [Troubleshooting](#troubleshooting))\n\n\u003c/details\u003e\n\n## Showcase\n\n![showcase](https://raw.github.com/Ramilito/kubesess/main/docs/images/kubesess.gif)\n\nThe showcase demonstrates the multiple sessions per shell feature, selecting items and fuzzy searching for them.\nThe same features apply for handling namespace as well\n\n\n## About The Project\n\n### What\n\nThis BLAZINGLY FAST plugin written in Rust makes it possible to have one context per shell active.\n\n### Why\n\nWhy isolated context?\nTypically when you switch context in kubectl (with ex. kubectx), the change happens on all terminal instances. \nThat is because the change is saved in your $HOME/.kube/config file and is read on every interaction with kubectl.\n\nThis will lead to the inevitable scenario of working on a local cluster, and needing to do something quickly in production. \nYou open another terminal, switch context, do your work and then go right back to your old terminal. \nThe issue is that the prompt has not visually refreshed to the actual context. \nOften the following command you type will not be dangerous, and you will notice that you did it in production, but sometimes the damage is severe💥.\n\n### How\n\nWe will use the config merge capability of kubectl to override the current-context setting.\nBy creating a file with the correct context and prepend it to the KUBECONFIG environment variable \n\nThe program will output the SESSION_CONTEXT and the alias created in \u003ca href=\"#installation\"\u003eInstallation\u003c/a\u003e will do the prepending\n```\nexport KUBECONFIG=$SESSION_CONTEXT:$KUBECONFIG\n```\n\n### Benchmark\n\nTool: [custom script](./benches/benchmark.sh)\nCommand | [kubesess](https://github.com/Ramilito/kubesess) | [kubectx](https://github.com/ahmetb/kubectx/tree/master/cmd/kubectx)\n---- | ---- | ----\n20 runs with no ctx switch and no kubectl calls | .024931342 | 1.744966963\n20 runs with ctx switch and no kubectl calls | .049247181 | 3.775905777\n20 runs with ctx switch and calling kubectl get nodes | 11.167763585 | 15.265837926\n\n\u003csup\u003eI am using the input argument variant for both tools, using fzf or tab completion is harder to do.\u003c/sup\u003e\n\n\nTool: [hyperfine](./benches/hyperfine/markdown.md)\n| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |\n|:---|---:|---:|---:|---:|\n| `kubesess -v docker-desktop context` | 1.3 ± 0.2 | 1.0 | 2.2 | 1.00 |\n| `kubectx docker-desktop` | 91.8 ± 3.3 | 85.1 | 100.7 | 71.23 ± 13.64 |\n\n| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |\n|:---|---:|---:|---:|---:|\n| `kubesess -v monitoring namespace` | 0.8 ± 0.1 | 0.7 | 0.9 | 1.00 |\n| `kubens monitoring` | 215.3 ± 27.0 | 199.5 | 288.9 | 270.22 ± 38.34 |\n\n\\* Disclaimer *\nkubectx and kubens are a wonderful tools, been using it allot but my workflow is different and thus this tool was created.\nProbably most of the speed gains are because I am bypassing kubectl and just editing config files.\n\n## Getting Started\n\n### Prerequisite\n\n* No dependencies\n\n### Installation\n\n#### Binary\nDownload and extract the binary.\n```zsh\nKUBESESS_VERSION=2.0.3 \u0026\u0026 \\\nKUBESESS_OS=x86_64-unknown-linux-gnu \u0026\u0026 \\\nwget \"https://github.com/Ramilito/kubesess/releases/download/${KUBESESS_VERSION}/kubesess_${KUBESESS_VERSION}_${KUBESESS_OS}.tar.gz\" \u0026\u0026 \\\nmkdir -p $HOME/.kube/kubesess \u0026\u0026 tar zxpf kubesess_${KUBESESS_VERSION}_${KUBESESS_OS}.tar.gz -C $HOME/.kube/kubesess \u0026\u0026 \\\nsudo mv ~/.kube/kubesess/target/${KUBESESS_OS}/release/kubesess /usr/local/bin/kubesess\n```\n\nA script wrapper called kubesess.sh is provided for easier use, source the script wrapper in your .bashrc, .zshrc.\n```zsh\nsource ~/.kube/kubesess/scripts/sh/kubesess.sh\n```\n\nFor zsh users, source the completion script to your .zsh/.zshrc file\n```zsh\nsource ~/.kube/kubesess/scripts/sh/completion.sh\n```\n\nFor fish users, copy functions and completion scripts in your fish config.\n```shell\nrsync -a ~/.kube/kubesess/scripts/fish/ ~/.config/fish/\n```\n\n#### Brew\n```zsh\nbrew install kubesess\n```\n\nAdd the following to your .zshrc:\n```zsh\nsource ${HOMEBREW_PREFIX}/share/zsh/site-functions/kubesess.sh\nsource ${HOMEBREW_PREFIX}/opt/kubesess/etc/bash_completion.d/completion.sh\n\n```\n\nOr if you use fish:\n```fish\ncp /$HOMEBREW_PREFIX/share/fish/vendor_functions.d/{kcd.fish,kc.fish,knd.fish,kn.fish} ~/.config/fish/functions/\n```\n\n## Usage\n\nSee the available commands by running kubesess -h, output from the program needs to be added to $KUBECONFIG env variable.\n\n#### Aliases are provided for easier use, when sourced these aliases will be created.\n```zsh\nkc  #kube_context: Sets session context\n\nkcd #kube_context_default: Sets context across all shells\n\nkn  #kube_namespace: Sets namespace\n\nknd #kube_namespace_default: Sets namespace across all shells\n```\n\n#### Multiple config files\nWe have two ways of handling multiple config files, the first one is to use kubectl recommended way of adding multiconfig files found [here](https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/#create-a-second-configuration-file).\nExample:\n```\nexport KUBECONFIG=$HOME/.kube/config:$HOME/.kube/config-demo:$HOME/.kube/config-demo-2\n```\n\n\u003e [!Note]\n\u003e **The order is important*: the first file will be the master config!\n\nThe second way is to let Kubesess handle it by adding one or more config files under the $HOME/.kube folder and it will be automatically merged.\n\n\u003e [!Warning]\n\u003e Currently there is a bug that requires us to have this env variable: `export KUBECONFIG=$(find ~/.kube/ -maxdepth 1 -type f -printf \":%p\" | sed 's/^://')`\n\n#### Add information to prompt (there are other good tools for this, kube-ps1 and p10k)\n```\nprompt_context() {\n    KUBE_CTX=$(kubesess -c context)\n    KUBE_NS=$(kubesess -c namespace)\n\n    if [[ $KUBE_CTX == *\"dev\"* ]]; then\n      echo \"❗%{$fg[yellow]%}|$KUBE_CTX%{$reset_color%}:%F{6}$KUBE_NS%f\"\n    elif [[ $KUBE_CTX == *\"prod\"* ]]; then\n      echo \"⛔%{$fg[red]%}|$KUBE_CTX%{$reset_color%}:%F{6}$KUBE_NS%f\"\n    elif [[ $KUBE_CTX == *\"staging\"* ]]; then\n      echo \"⛔%{$fg[red]%}|$KUBE_CTX%{$reset_color%}:%F{6}$KUBE_NS%f\"\n    else\n      echo \"✅️%{$fg[green]%}|$KUBE_CTX%{$reset_color%}:%F{6}$KUBE_NS%f\"\n    fi\n}\n\nRPROMPT='$(prompt_context)'\n```\n\n![showcase](https://raw.github.com/Ramilito/kubesess/main/docs/images/prompt.png)\n\n\u003c!-- ROADMAP --\u003e\n## Roadmap\n\n- [x] List all contexts\n- [x] Present it with fzf\n- [x] Write selection to file\n- [x] Output link to file\n- [x] Add alias to handle output\n- [x] Cleanup after use\n    - [x] clean prepended env variable\n    - [x] output files to $HOME/.cache/kubesess\n- [x] Handle different namespaces per context\n- [x] Use rust tui instead of fzf\n- [x] Add option to make changes stick (default-context)\n- [x] Add option to make changes stick (default-namespace)\n- [x] Add tests \n- [x] Add tab completion - https://github.com/clap-rs/clap/issues/1232\n- [x] Add to brew\n- [x] Add support for multiple .kube/config files\n- [x] Add support for multiple namespace per session\n- [x] Add error handling\n\n## Troubleshooting\n\n### Just fix it\nA hard reset will fix most issues, to do that just remove the ```~/.kube/kubesess/cache``` folder.\n\n\\*The downside is that your last visited namespace per context will be lost.\n\n\n### Why is it happening?\nThere are only two places that can go wrong, either the ```$KUBECONFIG``` env is \nnot set correctly or the generated file is corrupt.\n\nThis is how the ```$KUBECONFIG``` should look like (replace ```${USER}``` with your user name):\n```zsh\n/home/${USER}/.kube/kubesess/cache/docker-desktop:/home/${USER}/.kube/config\n```\n\nThis is how the generated file should look like:\n```yaml\nclusters:\n- name: docker-desktop\n  cluster:\n    server: https://kubernetes.docker.internal:6443\n    certificate-authority-data: REDACTED\nusers:\n- name: docker-desktop\n  user:\n    client-certificate-data: REDACTED\n    client-key-data: REDACTED\ncontexts:\n- name: docker-desktop\n  context:\n    cluster: docker-desktop\n    user: docker-desktop\n    namespace: default\ncurrent-context: docker-desktop\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRamilito%2Fkubesess","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRamilito%2Fkubesess","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRamilito%2Fkubesess/lists"}