Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to increase productivity with kubectl, using multiple namespaces per context #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

solsson
Copy link
Owner

@solsson solsson commented Oct 31, 2017

There's quite some activity in this area, but I found nothing with the proper level of respect for what it means that context is per-user, not per shell. You normally have dev, qa and prod clusters.

Not even if the context name is displayed in your shell prompt will you be properly warned from doing something dev-ish in prod: the prompt may be old - you might be hopping between terminal tabs.

@solsson
Copy link
Owner Author

solsson commented Oct 31, 2017

I ended up doing, in my ~/.bash_profile:

# TODO how do I get bash completion to work for kubectl aliases?
alias k="kubectl"

kcfg() {
  KCFG="${1}"
  # keep config names short and you won't need tab completion :)
  [ ! -f "$KCFG" ] && [ -f "$HOME/.kube/$KCFG" ] && KCFG="$HOME/.kube/$KCFG"
  export KUBECONFIG="$KCFG"
  namespaces=$(kubectl --request-timeout 3 get namespace -o jsonpath="{.items[*].metadata.name}")
  [ $? -eq 0 ] && {
    echo -n "Aliases for $(basename $KUBECONFIG):"
    for namespace in $namespaces
    do
      # https://github.com/kubernetes/kubernetes/pull/23262
      alias k-$namespace="kubectl --namespace $namespace"
      echo -n ", k-$namespace"
    done
    echo ""
  }
}

Works nicely with a prompt:

export PS1="\u@\h\[\033[34m\]\$(basename \"\$KUBECONFIG\")\[\033[00m\] \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

Assumption: default config (~/.kube/config) is never a production cluster -- you must explicitly select those per shell.

@solsson
Copy link
Owner Author

solsson commented Nov 7, 2017

These aliases have really helped. I should take another look at https://github.com/ahmetb/kubectl-aliases.

@solsson
Copy link
Owner Author

solsson commented Nov 7, 2017

Yolean/kubernetes-kafka#84 (comment) is worth an alias too

@solsson
Copy link
Owner Author

solsson commented Feb 23, 2018

I'd like to merge this with atamon/kube-cluster-alias#1

@atamon
Copy link

atamon commented Feb 23, 2018

Please let me know which parts. Maybe we should transfer the result to the Yolean organization too I guess?

I'm quite happy with atamon/kube-cluster-alias#1 after running it a while now. So I'm merging it now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants