42 Commits

Author SHA1 Message Date
Ahmet Alp Balkan
80336137bd Merge pull request #32 from shiroyasha/installation-steps-linux
Example installation steps on Linux
2018-03-30 10:26:18 -07:00
Ahmet Alp Balkan
1c9b5c54e8 Merge pull request #33 from jonmosco/utils_bash
Incorporate utils.bash into each executable to help simplify packaging for Linux and other platforms.  This adds to PR #32
2018-03-15 10:17:48 -07:00
Jon Mosco
b188f4da88 Incorporate utils.bash into each executable to help simplify packaging
for Linux and other platforms.  This adds to PR #32

remove unused functions
2018-03-15 12:34:18 -04:00
Igor Šarčević
57893cb668 Example installation steps on Linux 2018-03-10 13:17:09 +01:00
Ahmet Alp Balkan
8da629d98e Update README.md 2018-03-01 10:27:55 -08:00
Ahmet Alp Balkan
b2992aa0df Update readme to add fish completion support 2018-01-18 10:18:50 -08:00
Ahmet Alp Balkan
2f33693466 Merge pull request #26 from ichekrygin/master
Add Fish Shell Completions
2018-01-18 10:18:04 -08:00
Illya Chekrygin
c813642dc3 Add fish completions for kubectx and kubens 2018-01-17 23:23:53 -08:00
Ahmet Alp Balkan
12575f8ce4 Update README.md 2018-01-12 15:30:38 -08:00
Ahmet Alp Balkan
46d4236f1a Update README.md 2018-01-12 15:29:57 -08:00
Ahmet Alp Balkan
7ae68ada83 Update README.md 2018-01-02 14:58:33 -08:00
Ahmet Alp Balkan
146b67d2b0 Merge pull request #20 from knil-sama/knil-sama-readme-typo
Update README.md
2017-12-20 09:04:05 -08:00
Clement Demonchy
e3ceb14db5 Update README.md
Correct typo soemwhere -> somewhere
2017-12-20 14:16:28 +01:00
Ahmet Alp Balkan
6e250aecb6 Remove Formula/*, it's now in homebrew-core
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2017-12-14 15:21:10 -08:00
Ahmet Alp Balkan
d613c37c16 Update README.md 2017-12-12 10:34:58 -08:00
Ahmet Alp Balkan
61f1aa8fd7 Update README.md 2017-12-12 10:34:44 -08:00
Ahmet Alp Balkan
8390860474 Update README.md 2017-11-17 10:15:31 -08:00
Ahmet Alp Balkan
394288edc0 Update README.md 2017-11-15 12:49:33 -08:00
Ahmet Alp Balkan
dc6d2e57b3 README.md: Add user testimonials 2017-11-15 12:48:05 -08:00
Ahmet Alp Balkan
aa215b9eeb Update installation instructions for non-macOS 2017-11-15 12:41:03 -08:00
Ahmet Alp Balkan
e82879a185 Update README.md 2017-11-09 14:15:52 -08:00
Ahmet Alp Balkan
8c17834c36 Merge pull request #15 from bnookala/use-rename-context
Use rename-context in place of set-context
2017-11-09 14:15:23 -08:00
Bhargav Nookala
9ed6690978 🔧 use rename-context in place of set-context 2017-11-03 12:49:03 -07:00
Ahmet Alp Balkan
ed2afa7572 Add GIFs to README.md
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2017-10-24 12:32:22 -07:00
Ahmet Alp Balkan
5d79a0663d Remove message for '-' in kubectx.zsh
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2017-10-23 23:56:21 -07:00
Ahmet Alp Balkan
e7131add7a Add Stargazers chart
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2017-10-22 19:22:23 -07:00
Ahmet Alp Balkan
fd91e53d1f 🎉 kubectx is now in homebrew-core 🎊
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2017-06-29 14:12:46 -07:00
Ahmet Alp Balkan
b06fcaf054 Update Homebrew Formula with v0.3.1
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2017-05-17 10:09:10 -07:00
Ahmet Alp Balkan
9a7358ce67 Merge pull request #9 from ahmetb/readlink
Fallback to readlink -f on macOS
2017-05-17 10:07:04 -07:00
Ahmet Alp Balkan
8e413fc8ac Fallback to readlink -f on macOS
BSD coreutils readlink doesn't have -f option, so
adding a Python one-liner feedback to eval symlinks/homedir.
Fixes #8.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2017-05-17 09:37:57 -07:00
Ahmet Alp Balkan
7e10232fdd Update Homebrew Formula with v0.3.0
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2017-05-16 22:19:32 -07:00
Ahmet Alp Balkan
572b3e83d9 Refactor completion descriptions on README
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2017-05-16 22:12:57 -07:00
Ahmet Alp Balkan
03f24f2625 Merge pull request #7 from ahmetb/short-names
Add --with-short-names option
2017-05-16 22:06:15 -07:00
Ahmet Alp Balkan
8dec9cf004 Document --with-short-names
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2017-05-16 22:05:52 -07:00
Ahmet Alp Balkan
a376b02e88 Add --with-short-names option
This allows linking as "kctx" and "kns" to prevent overlap with kubectl Tab
completion when user hits `ku<Tab>`. Fixes #3.

Also fixed variables KUBECTX and PREV leaking to zsh shell once the _kubectx
function is invoked.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2017-05-16 22:01:46 -07:00
Ahmet Alp Balkan
da3f814280 Merge pull request #6 from ahmetb/wip-kubens
Add kubens tool for namespace switching
2017-05-16 20:31:15 -07:00
Ahmet Alp Balkan
d441505348 Add kubens tool for namespace switching
Fixes #1. Now kubectx also ships with kubens.

Extracted utility functions to a utils.bash file, loaded
from ../include/utils.bash.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2017-05-16 19:06:58 -07:00
Ahmet Alp Balkan
c49f975e45 Merge pull request #4 from swestcott/master
Fix context discovery
2017-05-15 19:34:28 -07:00
Simon Westcott
8c6cf84801 Unified context discovery 2017-05-12 09:05:02 +01:00
Simon Westcott
d04543ffef Fix context discovery 2017-05-11 12:44:11 +01:00
Ahmet Alp Balkan
d1c04555f4 Update readmes
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2017-04-26 09:49:46 -07:00
Ahmet Alp Balkan
6e2f181518 Update brew formula for v0.2.0
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2017-04-26 09:42:08 -07:00
13 changed files with 280 additions and 93 deletions

View File

@@ -1,18 +0,0 @@
class Kubectx < Formula
desc "Tool that can switch between kubectl contexts easily and create aliases"
homepage "https://github.com/ahmetb/kubectx"
url "https://github.com/ahmetb/kubectx/archive/v0.2.0.tar.gz"
sha256 "9fb6557416e4be3ef7e9701527f89fa73a1a0545a3cafbe6bed7527061c6cfb7"
bottle :unneeded
def install
bin.install "kubectx"
bash_completion.install "completion/kubectx.bash" => "kubectx"
zsh_completion.install "completion/kubectx.zsh" => "_kubectx"
end
test do
system "which", "kubectx"
end
end

112
README.md
View File

@@ -1,3 +1,12 @@
This repository provides both `kubectx` and `kubens` tools.
**`kubectx`** help you switch between clusters back and forth:
![kubectx demo GIF](img/kubectx-demo.gif)
**`kubens`** help you switch between Kubernetes namespaces smoothly:
![kubens demo GIF](img/kubens-demo.gif)
# kubectx(1)
kubectx is an utility to manage and switch between kubectl(1) contexts.
@@ -5,16 +14,13 @@ kubectx is an utility to manage and switch between kubectl(1) contexts.
```
USAGE:
kubectx : list the contexts
kubectx <NAME> : switch to context
kubectx <NAME> : switch to context <NAME>
kubectx - : switch to the previous context
kubectx <NEW_NAME>=<NAME> : create alias for context
kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
kubectx -h,--help : show this message
```
Purpose of this project is to provide an utility and facilitate discussion
about how `kubectl` can manage contexts better.
## Example
### Usage
```sh
$ kubectx minikube
@@ -31,12 +37,100 @@ Context "dublin" set.
Aliased "gke_ahmetb_europe-west1-b_dublin" as "dublin".
```
[Set up `bash` and `zsh` completion &rarr;](completion/README.md)
`kubectx` supports <kbd>Tab</kbd> completion on bash/zsh/fish shells to help with
long context names. You don't have to remember full context names anymore.
### Help wanted
-----
- [ ] homebrew formula/tap that installs the script and completions
# kubens(1)
kubens is an utility to switch between Kubernetes namespaces.
```
USAGE:
kubens : list the namespaces
kubens <NAME> : change the active namespace
kubens - : switch to the previous namespace
kubens -h,--help : show this message
```
### Usage
```sh
$ kubens kube-system
Context "test" set.
Active namespace is "kube-system".
$ kubens -
Context "test" set.
Active namespace is "default".
```
`kubens` also supports <kbd>Tab</kbd> completion on bash/zsh/fish shells.
-----
## Installation
**macOS:**
:confetti_ball: Use the [Homebrew](https://brew.sh/) package manager:
brew install kubectx
This command will set up bash/zsh/fish completion scripts automatically.
- Running `brew install` with `--with-short-names` will install tools with names
`kctx` and `kns` to prevent prefix collision with `kubectl` name.
- If you like to add context/namespace info to your shell prompt (`$PS1`),
I recommend trying out [kube-ps1](https://github.com/jonmosco/kube-ps1).
**Linux:**
Since `kubectx`/`kubens` are written in Bash, you should be able to instal
them to any POSIX environment that has Bash installed.
- Download the `kubectx`, and `kubens` scripts.
- Either:
- save them all to somewhere in your `PATH`,
- or save them to a directory, then create symlinks to `kubectx`/`kubens` from
somewhere in your `PATH`, like `/usr/local/bin`
- Make `kubectx` and `kubens` executable (`chmod +x ...`)
- Figure out how to install bash/zsh/fish [completion scripts](completion/).
Example installation steps:
``` bash
sudo git clone https://github.com/ahmetb/kubectx /opt/kubectx
sudo ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx
sudo ln -s /opt/kubectx/kubens /usr/local/bin/kubens
```
-----
#### Users
| What are others saying about kubectx? |
| ---- |
| _“Thank you for kubectx & kubens - I use them all the time & have them in my k8s toolset to maintain happiness :) ”_ [@pbouwer](https://twitter.com/pbouwer/status/925896377929949184) |
| _“I can't imagine working without kubectx and especially kubens anymore. It's pure gold.”_ [@timoreimann](https://twitter.com/timoreimann/status/925801946757419008) |
| _“I'm liking kubectx from @ahmetb, makes it super-easy to switch #Kubernetes contexts [...]”_ &mdash; [@lizrice](https://twitter.com/lizrice/status/928556415517589505) |
| _“Also using it on a daily basis. This and my zsh config that shows me the current k8s context 😉”_ [@puja108](https://twitter.com/puja108/status/928742521139810305) |
| _“Lately I've found myself using the kubens command more than kubectx. Both very useful though :-)”_ [@stuartleeks](https://twitter.com/stuartleeks/status/928562850464907264) |
| _“yeah kubens rocks!”_ [@embano1](https://twitter.com/embano1/status/928698440732815360) |
| _“Special thanks to Ahmet Alp Balkan for creating kubectx, kubens, and kubectl aliases, as these tools made my life better.”_ [@strebeld](https://medium.com/@strebeld/5-ways-to-enhance-kubectl-ux-97c8893227a)
> If you liked `kubectx`, you may like my [`kubectl-aliases`](https://github.com/ahmetb/kubectl-aliases) project, too.
-----
Disclaimer: This is not an official Google product.
#### Stargazers over time
[![Stargazers over time](https://starcharts.herokuapp.com/ahmetb/kubectx.svg)](https://starcharts.herokuapp.com/ahmetb/kubectx)

View File

@@ -1,45 +0,0 @@
kubectx provides shell completion scripts to complete context names, making it
even faster to switch between contexts easily.
## Bash setup
Copy the `kubectx.bash` file to your HOME directory:
```sh
cp kubectx.bash ~/.kubectx.bash
```
And source it in your `~/.bashrc` file by adding the line:
```sh
[ -f ~/.kubectx.bash ] && source ~/.kubectx.bash
```
Start a new shell, type `kubectx`, then hit <kbd>Tab</kbd> to see the existing
contexts.
You can Add `TAB: menu-complete` to your `~/.inputrc` to cycle through the
options with <kbd>Tab</kbd>.
## Zsh setup
`zsh` can leverage the `bash` completion scripts. Copy the `kubectx.bash` file
to your HOME directory:
```sh
cp kubectx.bash ~/.kubectx.bash
```
And add the following to your `.zshrc`:
```sh
[ -f ~/.kubectx.bash ] && source ~/.kubectx.bash
```
Start a new shell, type `kubectx`, then hit <kbd>Tab</kbd> to see the existing
contexts. If it does not work, modify the line above to:
```sh
[ -f ~/.kubectx.bash ] && autoload bashcompinit && bashcompinit && \
source ~/.kubectx.bash
```

View File

@@ -2,7 +2,7 @@ _kube_contexts()
{
local curr_arg;
curr_arg=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "- $(kubectl config get-contexts | awk '{print $2}' | tail -n +2)" -- $curr_arg ) );
COMPREPLY=( $(compgen -W "- $(kubectl config get-contexts --output='name')" -- $curr_arg ) );
}
complete -F _kube_contexts kubectx
complete -F _kube_contexts kubectx kctx

3
completion/kubectx.fish Normal file
View File

@@ -0,0 +1,3 @@
# kubectx
complete -f -c kubectx -a "- (kubectl config get-contexts --output='name')"

View File

@@ -1,12 +1,12 @@
#compdef kubectx
#compdef kubectx kctx=kubectx
KUBECTX="${HOME}/.kube/kubectx"
local KUBECTX="${HOME}/.kube/kubectx"
PREV=""
if [ -f "$KUBECTX" ]; then
# show '-' only if there's a saved previous context
PREV=$(cat "${KUBECTX}")
_arguments "1: :((-\:Back\ to\ ${PREV} \
$(kubectl config get-contexts | awk '{print $2}' | tail -n +2)))"
local PREV=$(cat "${KUBECTX}")
_arguments "1: :((- \
$(kubectl config get-contexts --output='name')))"
else
_arguments "1: :($(kubectl config get-contexts | awk '{print $2}' | tail -n +2))"
_arguments "1: :($(kubectl config get-contexts --output='name'))"
fi

8
completion/kubens.bash Normal file
View File

@@ -0,0 +1,8 @@
_kube_namespaces()
{
local curr_arg;
curr_arg=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "- $(kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}')" -- $curr_arg ) );
}
complete -F _kube_namespaces kubens kns

3
completion/kubens.fish Normal file
View File

@@ -0,0 +1,3 @@
# kubens
complete -f -c kubens -a "(kubectl get ns -o=custom-columns=NAME:.metadata.name --no-headers)"

2
completion/kubens.zsh Normal file
View File

@@ -0,0 +1,2 @@
#compdef kubens kns=kubens
_arguments "1: :(- $(kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}'))"

BIN
img/kubectx-demo.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 163 KiB

BIN
img/kubens-demo.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 226 KiB

21
kubectx
View File

@@ -27,9 +27,9 @@ usage() {
cat <<"EOF"
USAGE:
kubectx : list the contexts
kubectx <NAME> : switch to context
kubectx <NAME> : switch to context <NAME>
kubectx - : switch to the previous context
kubectx <NEW_NAME>=<NAME> : create alias for context
kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
kubectx -h,--help : show this message
EOF
exit 1
@@ -40,8 +40,7 @@ current_context() {
}
get_contexts() {
kubectl config view \
-o=jsonpath='{range .contexts[*].name}{@}{"\n"}{end}'
kubectl config get-contexts -o=name | sort -n
}
list_contexts() {
@@ -89,7 +88,6 @@ set_context() {
}
swap_context() {
set -e
local ctx="$(read_context)"
if [[ -z "${ctx}" ]]; then
echo "error: No previous context found." >&2
@@ -108,7 +106,7 @@ cluster_of_context() {
-o=jsonpath="{.contexts[?(@.name==\"${1}\")].context.cluster}"
}
alias_context() {
rename_context() {
local old_name="${1}"
local new_name="${2}"
@@ -120,11 +118,7 @@ alias_context() {
exit 1
fi
kubectl config set-context "${new_name}" \
--cluster="${old_cluster}" \
--user="${old_user}" \
echo "Aliased \"${old_name}\" as \"${new_name}\"." >&2
kubectl config rename-context "${old_name}" "${new_name}"
}
main() {
@@ -139,9 +133,10 @@ main() {
elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
usage
elif [[ "${1}" =~ ^-(.*) ]]; then
echo "error: unrecognized flag" >&2; usage
echo "error: unrecognized flag \"${1}\"" >&2
usage
elif [[ "${1}" =~ (.+)=(.+) ]]; then
alias_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
rename_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
else
set_context "${1}"
fi

145
kubens Executable file
View File

@@ -0,0 +1,145 @@
#!/bin/bash
#
# kubenx(1) is a utility to switch between Kubernetes namespaces.
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
[[ -n $DEBUG ]] && set -x
set -eou pipefail
IFS=$'\n\t'
KUBENS_DIR="${HOME}/.kube/kubens"
usage() {
cat <<"EOF"
USAGE:
kubens : list the namespaces in the current context
kubens <NAME> : change the active namespace of current context
kubens - : switch to the previous namespace in this context
kubens -h,--help : show this message
EOF
exit 1
}
current_namespace() {
local cur_ctx=$(current_context)
ns="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${cur_ctx}\")].context.namespace}")"
if [[ -z "${ns}" ]]; then
echo "default"
else
echo "${ns}"
fi
}
current_context() {
kubectl config view -o=jsonpath='{.current-context}'
}
get_namespaces() {
kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}'
}
namespace_file() {
local ctx="${1}"
echo "${KUBENS_DIR}/${ctx}"
}
read_namespace() {
local f="$(namespace_file "${1}")"
[[ -f "${f}" ]] && cat "${f}"
}
save_namespace() {
mkdir -p "${KUBENS_DIR}"
local f="$(namespace_file "${1}")"
local saved="$(read_namespace "${1}")"
if [[ "${saved}" != "${2}" ]]; then
printf %s "${2}" > "${f}"
fi
}
switch_namespace() {
local ctx="${1}"
kubectl config set-context "${ctx}" --namespace="${2}"
echo "Active namespace is \"${2}\".">&2
}
set_namespace() {
local ctx="$(current_context)"
local prev="$(current_namespace)"
if grep -q ^"${1}"\$ <(get_namespaces); then
switch_namespace "${ctx}" "${1}"
if [[ "${prev}" != "${1}" ]]; then
save_namespace "${ctx}" "${prev}"
fi
else
echo "error: no namespace exists with name \"${1}\".">&2
exit 1
fi
}
list_namespaces() {
local cur="$(current_namespace)"
local yellow=$(tput setaf 3)
local darkbg=$(tput setab 0)
local normal=$(tput sgr0)
for c in $(get_namespaces); do
if [[ "${c}" = "${cur}" ]]; then
echo "${darkbg}${yellow}${c}${normal}"
else
echo "${c}"
fi
done
}
swap_namespace() {
local ctx="$(current_context)"
local ns="$(read_namespace "${ctx}")"
if [[ -z "${ns}" ]]; then
echo "error: No previous namespace found for current context." >&2
exit 1
fi
set_namespace "${ns}"
}
main() {
if [[ "$#" -eq 0 ]]; then
list_namespaces
elif [[ "$#" -eq 1 ]]; then
if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
usage
elif [[ "${1}" == "-" ]]; then
swap_namespace
elif [[ "${1}" =~ ^-(.*) ]]; then
echo "error: unrecognized flag \"${1}\"" >&2
usage
elif [[ "${1}" =~ (.+)=(.+) ]]; then
alias_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
else
set_namespace "${1}"
fi
else
echo "error: too many flags" >&2
usage
fi
}
main "$@"