---
date: 2024-04-21
title: Setting Up Kubernetes Tools
tags: 
  - homelab
  - linux
  - bash
  - development
  - kubernetes
---

At work, I somewhat recently transitioned from using the Kubernetes Dashboard to using [k9s](https://k9scli.io/)
as DigitalOcean deprecated their hosted Dashboard for DOKS. Today, I found that I needed to modify
a `Secret` and that `k9s` and `kubectl` have no native tools to do this. I also found that I didn't have bash 
completion set up since upgrading to a new dev machine, so I took the opportunity to set things up better by using my 
[Shell Customizations](https://blog.mcknight.tech/2024/03/27/Shell-Customizations/). Now I won't need to configure
bash completion for helm/kubectl every time I set up a new computer.

## Shell Customizations
Rather than adding shell completion to the system config as is often suggested in documentation, I chose to keep
everything in my `.bashrc` so its portable between environments.

I already had some k8s-related content:
```shell
export EDITOR=nano
alias k9=k9s
```

I added:
```shell
which kubectl 1> /dev/null && source <(kubectl completion bash)
which helm 1> /dev/null && source <(helm completion bash)
[ -d "${KREW_ROOT:-$HOME/.krew}/bin" ] && export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
```

This adds `kubectl` and `helm` completion iff ([not a typo](https://en.wikipedia.org/wiki/If_and_only_if)) 
those commands are available. It also appends the [`krew`](https://krew.sigs.k8s.io/) 
binary path to `$PATH` if its defined (more on `krew` later).


## `k9s` and `helm` Installation
`k9s` has a few installation methods, but I chose to grab the [latest release from GitHub](https://github.com/derailed/k9s/releases)
since I don't have `snap` installed and usually run Linux Mint. I ended up updating `k9s` because the configuration file specs have
changed some since I originally got k9s, and updating means the example configurations in docs and other resources should all work.
The currently installed version can be determined with `k9s info`.

`helm` installation is [documented on their website](https://helm.sh/docs/intro/install/). I prefer to install
via `apt` to simplify updates since (as noted earlier) I don't usually have `snap` installed.


## krew and the kubectl-modify-secret plugin
`krew` is a plugin for `kubectl` that makes it easy to install more plugins. They have an 
[installation guide](https://krew.sigs.k8s.io/docs/user-guide/setup/install/) that is very
straight-forward. Note that the change in my `.bashrc` earlier adds the same `KREW_ROOT`
to my `PATH` as they specify in the installation guide.

With `krew` installed, I'm finally back to what I started this all for: installing a plugin to
modify a secret in `k9s`. The [kubectl-modify-secret plugin](https://github.com/rajatjindal/kubectl-modify-secret?tab=readme-ov-file)
can be installed via:
```shell
kubectl krew update
kubectl krew install modify-secret
```

Now, `kubectl modify-secret <secret_name>` should open an editor where a secret can be modified in plaintext.

## k9s Plugin Configuration
With the plugin installed, I next configured `k9s` so I can use the plugin to edit secrets the same way I edit configurations and
other specs. The `k9s` plugin configuration is [documented here](https://k9scli.io/topics/plugins/). 
My config at `~/.config/k9s/plugins.yaml` looks like:

```yaml
plugins:
  edit-secret:
    shortCut: Ctrl-X
    confirm: false
    description: "Edit Secret"
    scopes:
      - secrets
    command: kubectl
    background: false
    args:
      - modify-secret
      - --namespace
      - $NAMESPACE
      - --context
      - $CONTEXT
      - $NAME
```

This adds a `ctrl`+`x` shortcut to edit a `Secret`, similar to how `e` would edit a `ConfigMap`; `ctrl`+`e` is already used to
show/hide the information at the top of `k9s`, otherwise I'd have used that.

## Future Plans
I'm satisfied now with my tools and workflow for interacting with Kubernetes. Ideally, I would like to come up with a method
for synchronizing configuration between my devices automatically; for now, I manually copy my `.bashrc`, `.ssh`, and `.config`
file/directories around. A better solution I think could be a git repository + update script, Nextcloud sync, or something else.
I still have some research to do, but for now the changes are infrequent enough I'm satisfied with manual processes.
