konf is a lightweight kubeconfig manager, that does not rely on subshells

Konf - Lightweight kubeconfig Manager

Why konf?

  • konf allows you to quickly switch between different kubeconfig files
  • konf allows you to simultaneously use different kubeconfigs in different shells
  • konf executes directly in your current shell and does not start any subshell (unlike kubie). As a result it works extremely fast

// TODO add little screencast

Installation

Run

go install github.com/simontheleg/konf-go@latest

Aferwards, add the following to your .zshrc and restart your shell or re-source the .zshrc afterwards:

# mandatory konf settings
konf() {
  res=$(konf-go $@)
  export KUBECONFIG=$res
}
konf_cleanup() {
  konf-go cleanup
}
add-zsh-hook zshexit konf_cleanup


# optional konf settings
alias kctx="konf set"
# Open last konf on new session
export KUBECONFIG=$(konf-go set -)

Usage

Before any kubeconfig can be used with konf you have to import it:

konf import <path-to-your-kubeconf>

This is required, because konf maintains its own store of kubeconfigs to be able to work its "no-additional-shell-required"-magic.

Afterwards you can quickly switch between konfs using either:

konf set      # will open a picker dialogue
konf set -    # will open the last konf
konf set <id> # will set a specific konf. <id> is usually <context>_<cluster>

Additional commands and flags can be seen by calling konf --help

How does it work?

kubeconfig management across shells

Essentially konf maintains its state via two directories:

  • <konfDir>/store -> contains all of your imported kubeconfigs, where each context is split into its own file
  • <konfDir>/active -> contains all currently active konfs. The filename refers to the PID of the shell. Konf will automatically clean unused files after you close the session

We need these two extra directories because:

  • each konf file must only contain one context. This is because konf can only use the $KUBECONFIG variable to point to one kubeconfig file. If there are multiple contexts in that file, kubernetes looks for a current-context key and sets the config to that, thus introducing some ambiguity. To avoid this, konf import splits all the contexts into separate files
  • in order to allow for different shells to have different kubeconfigs we need to maintain a single one per shell. Otherwise when you run modifications like changing the namespace, these would affect all shells, which is not what we want

zsh-func-magic

One of the largest difficulties in this project lies in the core design of the shell. Essentially a child process cannot make modifications to its parents. This includes setting an environment variable, which affects us because we want to set $KUBECONFIG. The way we work around this "limitation" is by using a zsh function that executes our binary and then sets $KUBECONFIG to the output of konf-go. With this trick we are actually able to set $KUBECONFIG and can make this project work. Since only the result of stdout will be captured by the zsh-func, we can still communicate normally with the user by using stderr.

As a result of this trick, konf currently only works with zsh. I did not have the time to see if I can also make it work for bash or fish. Maybe this becomes a community contribution? :)

Contributing

When developing for konf, it is important to keep in mind that you can never print anything to stdout. You always have to use stderr. This is because anything printed to stdout will automatically be added to the $KUBECONFIG variable by the surrounding zsh-func. Usually this should not be too much of an issue, because components like the default logger or promptui can both use stderr.

Ideas for Future Improvements

  • Make it work with other shells like bash or fish
  • Maybe you can print zsh_func directly from a command?
  • Allow usage of other fuzzy finders like fzf
  • Add CI
  • Add License
  • Double check the root.go and command descriptions
  • --silent option for set command on which it does not log anything. This can be useful for things like konf set - when running it in every new session
  • konf manage so you can rename contexts and clusters
  • konf delete option so you can delete konfs you don't need anymore
  • figure out auto-completion. This might be a bit tricky due to the konf zsh func wrapper
  • File column could be improved by either using '...' abreviation or filtering out the konfDir
Owner
Simon Bein
Passionate Runner and Socks Collector 🤩
Simon Bein
Comments
  • document Nix-based installation

    document Nix-based installation

    Since this year's Kubernetes Community Days in Berlin, where I was introduced to this tool, I have been using konf almost every day and would like to thank you for it!

    Now that versioning and a release cycle are provided, the packaging of the application in Nix is straightforward. I took this as an opportunity to create a Nix package so that Nix-based systems can install the software more easily and elegantly.

    In this pull request, I have a suggestion for the documentation of the Nix-based installation.

  • fix: handling of hidden files

    fix: handling of hidden files

    This commit improves the handling of hidden files in the store. Usually it should be impossible for konf import to create such hidden files. However operating systems like MacOs, may place hidden files in the folder on their own. Before this fix, files like .DS_Store were breaking all konf set functionality

  • feat: add tab completion for bash

    feat: add tab completion for bash

    This PR adds the basic tab completion for bash.

    Additionally under the hood, it fixes and issue that bash shells had with multiline output from the wrapper.

  • add tab completion for zsh

    add tab completion for zsh

    This PR includes adding the completion framework to konf for zsh.

    To make this work, this PR has some magnificent changes under the hood 🌟 :

    • create more flexible konf-wrapper, so commands can now use stdIn and not have to use stdErr for everything
    • create an integration test to test the konf-wrapper against different shells

    It is not a breaking change, but it is recommended for alpha users to update their .zshrc with the settings recommended in the new Readme

  • refactor: extract testhelper into separate package

    refactor: extract testhelper into separate package

    This makes sense as the package utils is used by our commands and therefore should have a high test-coverage. The opposite is true for the mock-filesystems we use, which now are extracted into their own, untested package

  • refactor: write own config package + remove viper

    refactor: write own config package + remove viper

    This comes with a number of advantages:

    • we can provide defaults more easily
    • we can get rid of the pesky InitTestViper() in a lot of tests

    Additionally viper as a depency can be removed

  • fix: changelog generation

    fix: changelog generation

    With this commit the changelog will be generated correctly if multiple commits are between two versions. Refer to https://goreleaser.com/errors/no-history/

  • doc fix

    doc fix

    Removes the future ideas section, as these have been created as GH issues.

    In addition this slightly extends the nix code block, to fix GH's syntax highlighting. Taken from nixpkgs documentation

    before image

    after image

  • feat: add version command

    feat: add version command

    Add version command which gives details about the build.

    In addition this PR also adds the required ldflags for goreleaser, so new binaries have this info embedded.

    fixes: #38

  • Add ‘--move’ Flag To Import Cmd

    Add ‘--move’ Flag To Import Cmd

    It would be good to have a --move flag for the import cmd. This would then import the konf and after a successful import automatically delete the original file

  • konf ns should highlight currently selected namespace by default

    konf ns should highlight currently selected namespace by default

    When running konf ns the first namespace is highlighted even though that one is not necessarily the currently set namespace. It should have the currently selected namespace pre-selected. For example, see how kns works.

  • Allow aliasing when importing kubeconfigs

    Allow aliasing when importing kubeconfigs

    When importing kubeconfigs it should be possible to use aliases for context and cluster. This makes it easier for users to use their own names to identify konfs, without the need to rename them first.

    Handling in combination with the multi-kubeconfig feature from #43 tbd.

  • Better handling of namespace prompt when connection timeout

    Better handling of namespace prompt when connection timeout

    When you have no connection to a cluster, and run konf ns a blocking, but empty dialogue is displayed which only

    image

    Only after a while the error message appears:

    Error: Get "<sanitized>/api/v1/namespaces": dial tcp <sanitized>: i/o timeout
    Usage:
      konf namespace [flags]
    
    Aliases:
      namespace, ns
    
    Flags:
      -h, --help   help for namespace
    
    Global Flags:
          --konf-dir string   konfs directory for kubeconfigs and tracking active konfs (default is $HOME/.kube/konfs)
          --silent            suppress log output if set to true (default is false)
    
    Error: Get "<sanitized>/api/v1/namespaces": dial tcp <sanitized>: i/o timeout
    

    Steps to reproduces:

    1. Select a cluster which you cannot connect to
    2. run konf ns

    Expected behaviour: Should display some hint, that we are attempting to connect to the cluster, before we display the empty dialogue.

Kcs - KubeConfig Context Switcher
Kcs - KubeConfig Context Switcher

KubeConfig Context Switch Simple cli tool for kube config context switching with

Feb 24, 2022
Go-reconcile - Super tiny go package which does reconcile planning

go-reconcile Super tiny go package which does reconcile planning: taking desired

Mar 1, 2022
Terraform Provider for Azure (Resource Manager)Terraform Provider for Azure (Resource Manager)
Terraform Provider for Azure (Resource Manager)Terraform Provider for Azure (Resource Manager)

Terraform Provider for Azure (Resource Manager) Version 2.x of the AzureRM Provider requires Terraform 0.12.x and later, but 1.0 is recommended. Terra

Oct 16, 2021
General Pod Autoscaler(GPA) is a extension for K8s HPA, which can be used not only for serving, also for game.
General Pod Autoscaler(GPA) is a extension for K8s HPA, which can be used not only for serving, also for game.

Introduction General Pod Autoscaler(GPA) is a extension for K8s HPA, which can be used not only for serving, also for game. Features Compatible with a

Aug 19, 2022
Not another markup language. Framework for replacing Kubernetes YAML with Go.

Not another markup language. Replace Kubernetes YAML with raw Go! Say so long ?? to YAML and start using the Go ?? programming language to represent a

Jan 3, 2023
Write controller-runtime based k8s controllers that read/write to git, not k8s

Git Backed Controller The basic idea is to write a k8s controller that runs against git and not k8s apiserver. So the controller is reading and writin

Dec 10, 2021
Pulumi provider for Vultr (based on the Terraform one), not official

Vultr Resource Provider The Vultr Resource Provider lets you manage Vultr resources. Installing This package is currently not available for most langu

Apr 23, 2022
An operator which complements grafana-operator for custom features which are not feasible to be merged into core operator

Grafana Complementary Operator A grafana which complements grafana-operator for custom features which are not feasible to be merged into core operator

Aug 16, 2022
This is for managing Slack App Manifests, it is no use if you are not developing an App for Slack.

Terraform Provider Slack App This is for managing Slack App Manifests, it is no use if you are not developing an App for Slack. Requirements Terraform

May 23, 2022
Go Version Manager

gvm By Josh Bussdieker (jbuss, jaja, jbussdieker) while working at Moovweb Currently lovingly maintained by Benjamin Knigge Pull requests and other an

Jan 2, 2023
A simple and powerful SSH keys manager
A simple and powerful SSH keys manager

SKM is a simple and powerful SSH Keys Manager. It helps you to manage your multiple SSH keys easily! Features Create, List, Delete your SSH key(s) Man

Dec 17, 2022
Go version manager. Super simple tool to install and manage Go versions. Install go without root. Gobrew doesn't require shell rehash.

gobrew Go version manager Install or update With curl $ curl -sLk https://git.io/gobrew | sh - or with go $ go get -u github.com/kevincobain2000/gobre

Jan 5, 2023
Kubernetes Lazy User Manager

klum - Kubernetes Lazy User Manager klum does the following basic tasks: Create/Delete/Modify users Easily manage roles associated with users Issues k

Dec 6, 2022
network-node-manager is a kubernetes controller that controls the network configuration of a node to resolve network issues of kubernetes.
network-node-manager is a kubernetes controller that controls the network configuration of a node to resolve network issues of kubernetes.

Network Node Manager network-node-manager is a kubernetes controller that controls the network configuration of a node to resolve network issues of ku

Dec 18, 2022
The smart virtual machines manager. A modern CLI for Vagrant Boxes.
The smart virtual machines manager.  A modern CLI for Vagrant Boxes.

The smart virtual machines manager Table of Contents: What is Vermin Install Vermin Usage Contributors TODO What is Vermin Vermin is a smart, simple a

Dec 22, 2022
Grafana Dashboard Manager

Grafana dash-n-grab Grafana Dash-n-Grab (GDG) -- Dashboard/DataSource Manager. The purpose of this project is to provide an easy to use CLI to interac

Dec 31, 2022
operator to install cluster manager and klusterlet.

registration-operator Minimum cluster registration and work Community, discussion, contribution, and support Check the CONTRIBUTING Doc for how to con

Dec 14, 2022
The Scylla Manager.

Scylla Manager Welcome to Scylla Manager repository! Scylla Manager user docs can be found here. Scylla Manager consists of tree components: a server

Jan 4, 2023
This manager helps handle the life cycle of your eBPF programs

eBPF Manager This repository implements a manager on top of Cilium's eBPF library. This declarative manager simplifies attaching and detaching eBPF pr

Dec 1, 2022