Git Product home page Git Product logo

k8sh's Introduction

k8sh

k8sh Screenshot

A shell wrapper for bash including aliases for kubectl that makes it easy to navigate between and execute commands on different kubernetes clusters and namespaces.

This script is designed to be as short and simple as possible and also easily extensible so you can make it your own without too much deep knowledge of bash.

First time

Clone the repo and then make sure k8sh has execute permissions:

chmod +x k8sh

Now you can execute k8sh!

./k8sh

Add to a PATH directory to execute anywhere.

k8sh will automatically look at your current kubectl configuration to determine your current kubernetes context and namespace.

Context and Namespace Commands

k8sh automatically keeps track of the current context and namespace you are operating in. These are displayed when starting up k8sh and on the k8sh prompt.

To switch contexts:

ct <context_to_switch_to>

To list available contexts:

ct

To switch namespaces:

ns <namespace_to_switch_to>

To list available namespaces:

ns

NOTE: When changing the context, the change is made globally to kubectl as if you did a kubectl config use-context yourself. The namespace, however, is kept track of by k8sh. The standard kubectl command is aliased to always include the namespace that is currently selected within k8sh.

Aliases

As stated above, when inside of k8sh the standard kubectl command is aliased to always include the namespace that is currently selected. k8sh also includes many other aliases to make accessing commonly used kubectl commands a snap.

k

k is an easy shorthand for kubectl

Common Actions

Shorthands for common actions

  • describe -> k describe
  • get -> k get
  • create -> k create
  • apply -> k apply
  • delete -> k delete
  • scale -> k scale
  • rollout -> k rollout
  • logs -> k logs
  • explain -> k explain

Query for common resources (kubectl get)

Instead of typing out kubectl get pods/services/replicationcontrollers/etc you can simply type the following aliases to get a list of those resources:

  • pods
  • services
  • deployments / dep
  • replicasets
  • replicationcontrollers / rc
  • nodes
  • limitranges
  • limits
  • events
  • persistentvolumes / pv
  • persistentvolumeclaims / pvc
  • namespaces
  • ingresses / ing
  • configmaps
  • secrets

Tab Completion

The ct, ns, k (as well as kubectl) commands support tab completion. k completion requires bash-completion to be installed in order to function which in turn requires bash 4.1+.

Installing bash 4 / bash-completion on MacOSX

Upgrading to Bash 4: https://itnext.io/upgrading-bash-on-macos-7138bd1066ba

Installing Bash Completion: https://davidalger.com/posts/bash-completion-on-os-x-with-brew/

.k8sh_extensions

On startup k8sh looks for a .k8sh_extensions file in your home directory. If it is there, it loads it as an inline bash script so you can supply your own aliases and functions to execute within k8sh.

To force the extensions file to be reloaded while in a k8sh session you can run:

reloadExtensions

See examples/k8sh_extensions for some examples of what extensions can do.

Todo

  • Feel free to submit your ideas!

k8sh's People

Contributors

frank-weindel avatar grzesuav avatar gustavoantao avatar kristakhare avatar michalmatoga avatar mikebryant avatar stuart-warren 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

k8sh's Issues

Get the standard kubectl tab completion to work with aliases

Hi,

I have looked into this issue and tried to solve it with https://github.com/cykerway/complete-alias and attach via ~/.k8sh_extensions, but it does not work as I would like to.

My script:

#!/usr/bin/env bash

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"

echo "$DIR"

source /usr/share/bash-completion/bash_completion
# shellcheck source=scripts/complete-alias/bash_completion.sh
source "${DIR}/scripts/complete-alias/bash_completion.sh"

complete -F _complete_alias k
complete -F _complete_alias get
complete -F _complete_alias pods

What do not work ?
When I run k8sh with this extension and press k ge[tab] I get

 k gcomplete k
et complete k
complete k

(after pressing several tabs) - eventually it is able to complete it.
When I run k8sh and then source ../complete-alias/bash_completion.sh - autocomplete for
k ge[tab] works. I have too poor bash knowledge to know why, maybe someone can propose some solution for that ?

What is also missing ? Ability to iterate over all aliases defined in https://github.com/Comcast/k8sh/blob/master/k8sh#L83 and automatically execute complete -F _complete_alias $alias_name .
If someone with bigger bash knowledge could adjust my solution (or write its own) it would be v. helpful.

Cheers

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.