mirror of
https://github.com/ahmetb/kubectx.git
synced 2026-02-25 09:32:25 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d3295e5b7a | ||
|
|
3369d42e2d | ||
|
|
f48c4198e7 | ||
|
|
26d3422917 | ||
|
|
56e30d2b43 | ||
|
|
dcb43fdf1b | ||
|
|
e2f7dc0de2 | ||
|
|
9645e5c62c | ||
|
|
00a1e12bfb | ||
|
|
c3dd1e5deb |
29
.github/workflows/release.yml
vendored
Normal file
29
.github/workflows/release.yml
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
name: release
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*.*.*'
|
||||
jobs:
|
||||
release:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@master
|
||||
- name: Create Release
|
||||
id: create_release
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
tag_name: ${{ github.ref }}
|
||||
release_name: Release ${{ github.ref }}
|
||||
- name: Update new version for plugin 'ctx' in krew-index
|
||||
uses: rajatjindal/krew-release-bot@v0.0.31
|
||||
with:
|
||||
krew_template_file: .krew/ctx.yaml
|
||||
- name: Update new version for plugin 'ns' in krew-index
|
||||
uses: rajatjindal/krew-release-bot@v0.0.31
|
||||
with:
|
||||
krew_template_file: .krew/ns.yaml
|
||||
|
||||
|
||||
31
.krew/ctx.yaml
Normal file
31
.krew/ctx.yaml
Normal file
@@ -0,0 +1,31 @@
|
||||
apiVersion: krew.googlecontainertools.github.com/v1alpha2
|
||||
kind: Plugin
|
||||
metadata:
|
||||
name: ctx
|
||||
spec:
|
||||
homepage: https://github.com/ahmetb/kubectx
|
||||
shortDescription: Switch between contexts in your kubeconfig
|
||||
version: {{ .TagName }}
|
||||
description: |
|
||||
Also known as "kubectx", a utility to switch between context entries in
|
||||
your kubeconfig file efficiently.
|
||||
caveats: |
|
||||
If fzf is installed on your machine, you can interactively choose
|
||||
between the entries using the arrow keys, or by fuzzy searching
|
||||
as you type.
|
||||
See https://github.com/ahmetb/kubectx for customization and details.
|
||||
platforms:
|
||||
- selector:
|
||||
matchExpressions:
|
||||
- key: os
|
||||
operator: In
|
||||
values:
|
||||
- darwin
|
||||
- linux
|
||||
{{addURIAndSha "https://github.com/ahmetb/kubectx/archive/{{ .TagName }}.tar.gz" .TagName }}
|
||||
bin: kubectx
|
||||
files:
|
||||
- from: kubectx-*/kubectx
|
||||
to: .
|
||||
- from: kubectx-*/LICENSE
|
||||
to: .
|
||||
31
.krew/ns.yaml
Normal file
31
.krew/ns.yaml
Normal file
@@ -0,0 +1,31 @@
|
||||
apiVersion: krew.googlecontainertools.github.com/v1alpha2
|
||||
kind: Plugin
|
||||
metadata:
|
||||
name: ns
|
||||
spec:
|
||||
homepage: https://github.com/ahmetb/kubectx
|
||||
shortDescription: Switch between Kubernetes namespaces
|
||||
version: {{ .TagName }}
|
||||
description: |
|
||||
Also known as "kubens", a utility to set your current namespace and switch
|
||||
between them.
|
||||
caveats: |
|
||||
If fzf is installed on your machine, you can interactively choose
|
||||
between the entries using the arrow keys, or by fuzzy searching
|
||||
as you type.
|
||||
See https://github.com/ahmetb/kubectx for customization and details.
|
||||
platforms:
|
||||
- selector:
|
||||
matchExpressions:
|
||||
- key: os
|
||||
operator: In
|
||||
values:
|
||||
- darwin
|
||||
- linux
|
||||
{{addURIAndSha "https://github.com/ahmetb/kubectx/archive/{{ .TagName }}.tar.gz" .TagName }}
|
||||
bin: kubens
|
||||
files:
|
||||
- from: kubectx-*/kubens
|
||||
to: .
|
||||
- from: kubectx-*/LICENSE
|
||||
to: .
|
||||
@@ -5,6 +5,7 @@ before_install:
|
||||
- sudo curl -fsSL -o /usr/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/v1.13.1/bin/linux/amd64/kubectl
|
||||
- sudo chmod +x /usr/bin/kubectl
|
||||
script:
|
||||
- basename /usr/bin
|
||||
- bats test/kubectx.bats
|
||||
- bats test/kubens.bats
|
||||
- shellcheck kubectx
|
||||
|
||||
31
README.md
31
README.md
@@ -7,6 +7,7 @@
|
||||
|
||||
|
||||
This repository provides both `kubectx` and `kubens` tools.
|
||||
[Install →](#installation)
|
||||
|
||||
|
||||
**`kubectx`** helps you switch between clusters back and forth:
|
||||
@@ -30,6 +31,7 @@ USAGE:
|
||||
kubectx -d <NAME> : delete context <NAME> ('.' for current-context)
|
||||
(this command won't delete the user/cluster entry
|
||||
that is used by the context)
|
||||
kubectx -u, --unset : unset the current context
|
||||
```
|
||||
|
||||
### Usage
|
||||
@@ -85,6 +87,31 @@ Active namespace is "default".
|
||||
|
||||
## Installation
|
||||
|
||||
There are several installation options:
|
||||
|
||||
- As kubectl plugins (macOS/Linux)
|
||||
- macOS
|
||||
- Homebrew (recommended)
|
||||
- MacPorts
|
||||
- Linux
|
||||
- manual installation/upgrades
|
||||
- Arch Linux
|
||||
- Debian/Ubuntu
|
||||
|
||||
### Kubectl Plugins (macOS and Linux)
|
||||
|
||||
You can install and use [Krew](https://github.com/kubernetes-sigs/krew/) kubectl
|
||||
plugin manager to get `kubectx` and `kubens`. **NOTE:** This will not install
|
||||
shell completion scripts, if you want those, choose another installation method
|
||||
below.
|
||||
|
||||
```sh
|
||||
kubectl krew install ctx
|
||||
kubectl krew install ns
|
||||
```
|
||||
|
||||
After installing, the tools will be available as `kubectl ctx` and `kubectl ns`.
|
||||
|
||||
### macOS
|
||||
|
||||
#### Homebrew
|
||||
@@ -125,8 +152,8 @@ them to any POSIX environment that has Bash installed.
|
||||
ln -s /opt/kubectx/completion/kubectx.zsh ~/.oh-my-zsh/completions/_kubectx.zsh
|
||||
ln -s /opt/kubectx/completion/kubens.zsh ~/.oh-my-zsh/completions/_kubens.zsh
|
||||
```
|
||||
Note that the leading underscore seems to be a convention.
|
||||
If not using oh-my-zsh, you could link to `/usr/share/zsh/functions/Completion` (might require sudo), depending on the `$fpath` of your zsh installation.
|
||||
Note that the leading underscore seems to be a convention. If completion doesn't work, add `autoload -U compinit && compinit` to your `.zshrc` (similar to [`zsh-completions`](https://github.com/zsh-users/zsh-completions/blob/master/README.md#oh-my-zsh)).
|
||||
If not using oh-my-zsh, you could link to `/usr/share/zsh/functions/Completion` (might require sudo), depending on the `$fpath` of your zsh installation.
|
||||
In case of error, calling `compaudit` might help.
|
||||
- For bash:
|
||||
```bash
|
||||
|
||||
@@ -2,11 +2,17 @@
|
||||
|
||||
local KUBECTX="${HOME}/.kube/kubectx"
|
||||
PREV=""
|
||||
|
||||
local all_contexts="$(kubectl config get-contexts --output='name')"
|
||||
if [ -f "$KUBECTX" ]; then
|
||||
# show '-' only if there's a saved previous context
|
||||
local PREV=$(cat "${KUBECTX}")
|
||||
_arguments "1: :(-
|
||||
$(kubectl config get-contexts --output='name'))"
|
||||
|
||||
_arguments \
|
||||
"-d:*: :(${all_contexts})" \
|
||||
"(- *): :(- ${all_contexts})"
|
||||
else
|
||||
_arguments "1: :($(kubectl config get-contexts --output='name'))"
|
||||
_arguments \
|
||||
"-d:*: :(${all_contexts})" \
|
||||
"(- *): :(${all_contexts})"
|
||||
fi
|
||||
|
||||
35
kubectx
35
kubectx
@@ -22,22 +22,30 @@ set -eou pipefail
|
||||
IFS=$'\n\t'
|
||||
|
||||
SELF_CMD="$0"
|
||||
|
||||
KUBECTX="${XDG_CACHE_HOME:-$HOME/.kube}/kubectx"
|
||||
|
||||
usage() {
|
||||
cat <<"EOF"
|
||||
local SELF
|
||||
SELF="kubectx"
|
||||
if [[ "$(basename "$0")" == kubectl-* ]]; then # invoked as plugin
|
||||
SELF="kubectl ctx"
|
||||
fi
|
||||
|
||||
cat <<EOF
|
||||
USAGE:
|
||||
kubectx : list the contexts
|
||||
kubectx <NAME> : switch to context <NAME>
|
||||
kubectx - : switch to the previous context
|
||||
kubectx -c, --current : show the current context name
|
||||
kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
|
||||
kubectx <NEW_NAME>=. : rename current-context to <NEW_NAME>
|
||||
kubectx -d <NAME> [<NAME...>] : delete context <NAME> ('.' for current-context)
|
||||
$SELF : list the contexts
|
||||
$SELF <NAME> : switch to context <NAME>
|
||||
$SELF - : switch to the previous context
|
||||
$SELF -c, --current : show the current context name
|
||||
$SELF <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
|
||||
$SELF <NEW_NAME>=. : rename current-context to <NEW_NAME>
|
||||
$SELF -d <NAME> [<NAME...>] : delete context <NAME> ('.' for current-context)
|
||||
(this command won't delete the user/cluster entry
|
||||
that is used by the context)
|
||||
$SELF -u, --unset : unset the current context
|
||||
|
||||
kubectx -h,--help : show this message
|
||||
$SELF -h,--help : show this message
|
||||
EOF
|
||||
}
|
||||
|
||||
@@ -107,7 +115,7 @@ choose_context_interactive() {
|
||||
local choice
|
||||
choice="$(_KUBECTX_FORCE_COLOR=1 \
|
||||
FZF_DEFAULT_COMMAND="${SELF_CMD}" \
|
||||
fzf --ansi || true)"
|
||||
fzf --ansi --no-preview || true)"
|
||||
if [[ -z "${choice}" ]]; then
|
||||
echo 2>&1 "error: you did not choose any of the options"
|
||||
exit 1
|
||||
@@ -178,6 +186,11 @@ delete_context() {
|
||||
$KUBECTL config delete-context "${ctx}"
|
||||
}
|
||||
|
||||
unset_context() {
|
||||
echo "Unsetting current context." >&2
|
||||
$KUBECTL config unset current-context
|
||||
}
|
||||
|
||||
main() {
|
||||
if hash kubectl 2>/dev/null; then
|
||||
KUBECTL=kubectl
|
||||
@@ -213,6 +226,8 @@ main() {
|
||||
# - it does not fail when current-context property is not set
|
||||
# - it does not return a trailing newline
|
||||
kubectl config current-context
|
||||
elif [[ "${1}" == '-u' || "${1}" == '--unset' ]]; then
|
||||
unset_context
|
||||
elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
|
||||
usage
|
||||
elif [[ "${1}" =~ ^-(.*) ]]; then
|
||||
|
||||
21
kubens
21
kubens
@@ -22,16 +22,23 @@ set -eou pipefail
|
||||
IFS=$'\n\t'
|
||||
|
||||
SELF_CMD="$0"
|
||||
|
||||
KUBENS_DIR="${XDG_CACHE_HOME:-$HOME/.kube}/kubens"
|
||||
|
||||
usage() {
|
||||
cat <<"EOF"
|
||||
local SELF
|
||||
SELF="kubens"
|
||||
if [[ "$(basename "$0")" == kubectl-* ]]; then # invoked as plugin
|
||||
SELF="kubectl ns"
|
||||
fi
|
||||
|
||||
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 -c, --current : show the current namespace
|
||||
kubens -h,--help : show this message
|
||||
$SELF : list the namespaces in the current context
|
||||
$SELF <NAME> : change the active namespace of current context
|
||||
$SELF - : switch to the previous namespace in this context
|
||||
$SELF -c, --current : show the current namespace
|
||||
$SELF -h,--help : show this message
|
||||
EOF
|
||||
}
|
||||
|
||||
@@ -109,7 +116,7 @@ choose_namespace_interactive() {
|
||||
local choice
|
||||
choice="$(_KUBECTX_FORCE_COLOR=1 \
|
||||
FZF_DEFAULT_COMMAND="${SELF_CMD}" \
|
||||
fzf --ansi || true)"
|
||||
fzf --ansi --no-preview || true)"
|
||||
if [[ -z "${choice}" ]]; then
|
||||
echo 2>&1 "error: you did not choose any of the options"
|
||||
exit 1
|
||||
|
||||
@@ -239,3 +239,16 @@ load common
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" = "user2@cluster1" ]]
|
||||
}
|
||||
|
||||
@test "unset selected context" {
|
||||
use_config config2
|
||||
|
||||
run ${COMMAND} user1@cluster1
|
||||
[ "$status" -eq 0 ]
|
||||
|
||||
run ${COMMAND} -u
|
||||
[ "$status" -eq 0 ]
|
||||
|
||||
run ${COMMAND} -c
|
||||
[ "$status" -ne 0 ]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user