Category Archives: tools

Adding a function to your .bashrc for resource switching

Shout out to my friend Drew for this one – I had something similar (but nowhere near as cool) previously!

In my work environment, we have several different kubernetes clusters that my team manages. It’s relatively common to have to switch between them several times a day because of various work items that need completed. Aside from that, there are different namespaces within each environment as well, also which need to be specified. This usually comes in the form of one or two commands:

kubectl config use-context dev-env
kubectl config set-context --current --namespace mynamespace

(You can condense these down into one command, but I’m leaving it as two for simplicity.)

In any event, these commands need to be executed every time you switch from test to dev, from dev to prod, or whatever your environments are, along with the namespaces as well. Each cluster requires a yaml file downloaded from the cluster that contains all of the information kubectl needs to know what cluster to connect to, along with your credentials. This .bashrc function is a very elegant and simple way to be able to switch environments and/or namespaces with a single command:

clus() {
    if [ $# != 2 ]; then
    echo "usage: clus <environment> <namespace>" 1>&2
    return 1
fi
environment=$1
namespace=$2
if ! [[ "${environment}" =~ ^(dev(1|2)|test(1|2)|prod(1|2))$ ]]; then
    echo "error: invalid environment \"${environment}\"" 1>&2
    return 1
fi
if ! [[ "${namespace}" =~ ^(name1|name2|name3) ]]; then  
    echo "error: invalid namespace \"${namespace}\"" 1>&2
    return 1
fi
export KUBECONFIG=${HOME}/workspace/kubeconfigs/${environment}.yaml
kubectl config use-context "${environment}"-fqdn
kubectl config set-context --current --namespace "${namespace}"
}
export -f clus

So needless to say, I’ve obscured some of the company-specific details about our namespace and cluster names, but you get the idea. So now any time I’ve got an active terminal, all I have to do is type:

clus dev2 name3

And I’m configured for the dev2 environment and the name3 namespace. Messages are displayed on the screen to indicate success.

Just remember! You need to have downloaded your cluster yaml files into a directory (here mine is /home/username/workspace/kubeconfigs) for this to work!

Resolving a kubectl error in WSL2

For work, I often have to connect to a Kubernetes cluster to manage resources, and anyone who’s done that via CLI before knows about the kubectl command. To use it locally, you must first download a yaml configuration file to identify the cluster, namespace, etc., then the commands should work. Notice I said “should” work.

So enter the following error message when attempting to run kubectl get pods:

Unable to connect to the server: dial tcp 127.0.0.1:8080: connectex: No connection could be made because the target machine actively refused it.

Obviously I wasn’t wanting to connect to 127.0.0.1 (aka localhost), I was trying to connect to an enterprise Kubernetes cluster. Then later on after re-downloading the target cluster yaml file, I received this error while running kubectl commands:

Unable to connect to the server: EOF

Searching for this error online led me down a multitude of rabbit holes, each as unhelpful as the last, until I found a reference to Docker Desktop. I know that we (the company I work for) used to use it, but we don’t anymore. (At least I don’t in my current role.)

I raised my eyebrow at that one — I had a relatively new laptop, but one of the corporate-loaded tools on it for someone in my role was Docker Desktop. I checked the running services to see if it was running, and it was not, which is expected. I don’t need to run it.

I forgot to mention that I am using WSL2 (Fedora remix) as my VS Code terminal shell, and so far I’m nothing but happy with it. Sensing something off with my local installation of kubectl I ran which kubectl, which gives me the location of the binary currently in my path. (For the record if it appears more than once in your path, it only returns the first one it comes across, in order of your PATH paths.)

Sure enough, it pointed to /usr/local/bin/kubectl, which was unexpected. I wouldn’t think that an enterprise tool would be installed to /usr/local, and I was right. Performing a long listing of that directory showed me the following:

lrwxrwxrwx  1 root root   55 Jul 21 09:43 kubectl -> /mnt/wsl/docker-desktop/cli-tools/usr/local/bin/kubectl

So sure enough, I had been running the Docker desktop version of kubectl and not the one I had installed with yum officially (which existed in /usr/bin, but was after /usr/local in my PATH.)

So I removed the link, and now which kubectl immediately started showing the correct one, installed via the package manager, and it starting working, connecting to the correct cluster, and everything. While this may have been a simple fix for some, not being fully aware of what may be pre-installed on a work laptop did give me some surprises.

Going Down the VS Code Rabbithole

A while back I was introduced to VS Code (Visual Studio Code), a free code editor by Microsoft. It seemed pretty cool, but at the time I was determined to be a die-hard Emacs user. Unfortunately (or perhaps fortunately depending on your perspective) I started migrating away from Emacs/org-mode as a note taking medium, and using Microsoft OneNote instead. I was able to keep my notes in a central location, have easy access no matter where I was because of the mobile app, and I loved the ability to copy and paste images, screen captures, and files into a OneNote page. As my usage of Emacs started to wane, I heard more and more ravings from my peers about VS Code, so I decided to dive in for a while and try it out.

Man, what a ride!

So it’s easy to be overwhelmed at first, but the built-in integrated SCM (git by default) was enough to raise an eyebrow, but the extensive (and I mean EXTENSIVE) list of extensions available for everything under the Sun was just amazing. I started installing everything I thought I’d ever need…. (mistake #1) Then after I got a jazillion pop up messages about this or that not being available, or needed another package, or whatever, I went through and paired it down quite a bit. I ended up with some basic linters, OAS specification problem identification extension, kubernetes cluster info, and a few others. That coupled with WSL on my Windows 10 installation made a nice terminal emulator as well. Once I had all the packages installed that I needed, I was able to give up Cmder and ConEMU as well. (Cmder sits on top of ComEMU so I admit having both was a bit redundant.) The git integration released me from using GitHub Desktop as well.

Suffice to say, using VS Code has allowed me to let go of several other applications on my system, just giving me an even tighter realm of control and ease of use for my work.

Note: if you’re going to install extensions, be aware of the inherit risk — you could be installing something malicious… so if you’re in doubt, try to stick only to the “blue star” or approved/official extensions for whatever you’re working with.

Oh, and one last thing — even though it’s not actually VS Code, if you type a period (.) while browsing a GitHub repository, a VS Code-like editor will load in your browser that mimics what the application does, allowing you to interact with that repository right there on the web. Now that’s cool!

Happy coding!