Skip to content

Commit

Permalink
feat!: Rename project from kube-switcher to kubectl-switch
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the binary has been renamed from `kube-switcher` to `kubectl-switch`
  • Loading branch information
mircea-pavel-anton committed Oct 12, 2024
1 parent 4aece45 commit 1d5650d
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "kube-switcher",
"name": "kubectl-switch",
"build": {
"dockerfile": "Dockerfile"
},
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/devcontainer-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
uses: mirceanton/reusable-workflows/.github/workflows/reusable-docker-build-push.yaml@1f67ebf2d8aa5a366dc1a2179911c7edaea8a5af # v3.4.18
secrets: inherit
with:
image: "ghcr.io/mirceanton/kube-switcher-devcontainer"
image: "ghcr.io/mirceanton/kubectl-switch-devcontainer"
platforms: linux/amd64
tags: test
context: ./.devcontainer
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ jobs:
- name: Checkout
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1

- name: Build `kube-switcher`
run: go build -o kube-switcher
- name: Build `kubectl-switch`
run: go build -o kubectl-switch

- name: Install `minikube`
run: |
Expand All @@ -39,7 +39,7 @@ jobs:
wait
- name: Switch context to test-cluster-1
run: ./kube-switcher context test-cluster-1
run: ./kubectl-switch context test-cluster-1

- name: Validate the cluster has switched to test-cluster-1 by listing nodes
run: |
Expand All @@ -50,21 +50,21 @@ jobs:
kubectl get pods --namespace=default 2>&1 | grep "No resources found" || { echo "Error: Pods found in default namespace!" >&2; exit 1; }
- name: Switch to the kube-system namespace
run: ./kube-switcher namespace kube-system
run: ./kubectl-switch namespace kube-system

- name: Validate that kube-system namespace is selected and kube-apiserver is running
run: |
kubectl get pods --namespace=kube-system | grep "kube-apiserver" || { echo "Error: kube-apiserver not found in kube-system!" >&2; exit 1; }
- name: Switch back to the default namespace
run: ./kube-switcher namespace default
run: ./kubectl-switch namespace default

- name: Check that no pods are found again in the default namespace
run: |
kubectl get pods --namespace=default 2>&1 | grep "No resources found" || { echo "Error: Pods found in default namespace!" >&2; exit 1; }
- name: Switch context to test-cluster-2
run: ./kube-switcher context test-cluster-2
run: ./kubectl-switch context test-cluster-2

- name: Validate the cluster has switched to test-cluster-2 by listing nodes
run: |
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
kube-switcher
kubectl-switch
dist/
4 changes: 2 additions & 2 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
version: 2
project_name: kube-switcher
project_name: kubectl-switch

before:
hooks:
Expand Down Expand Up @@ -52,7 +52,7 @@ nfpms:
formats: [deb, apk, rpm]

brews:
- name: kube-switcher
- name: kubectl-switch
description: A simple tool to switch between Kubernetes contexts.
license: MIT
homepage: https://github.com/mirceanton/{{.ProjectName}}
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine:3.20.3@sha256:beefdbd8a1da6d2915566fde36db9db0b524eb737fc57cd1367effd16dc0d06d
USER 8675:8675
COPY kube-switcher /
ENTRYPOINT ["/kube-switcher"]
COPY kubectl-switch /
ENTRYPOINT ["/kubectl-switch"]
74 changes: 36 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,50 @@
# kube-switcher
# kubectl-switch

A simple tool to manage and switch between multiple Kubernetes configuration files. This tool allows you to dump all your kubeconfig files in a single directory and easily switch between them as well as configure the current namespace.

Available both as a standalone CLI or a `kubectl` plugin.

## Features

- Manage multiple kubeconfig files in a single directory.
- Easily switch between different Kubernetes contexts.
- Easily switch between different namespaces.
- Persistent context/namespace switching across different shells.
- Interactive prompts support fuzzy search.
- Non-destructive operations on your original kubeconfig files. `kube-switcher` will never edit those. It will only work with copies of them.
- Non-destructive operations on your original kubeconfig files. `kubectl-switch` will never edit those. It will only work with copies of them.

## Why `kube-switcher`?
## Why `kubectl-switch`?

`kube-switcher` is an alternative to tools like `kubectx`, `kubens` and `kubie`. It has been created because I feel all of those fall short in certain regards:
`kubectl-switch` is an alternative to tools like `kubectx`, `kubens` and `kubie`. It has been created because I feel all of those fall short in certain regards:

- `kubectx` assumes all your contexts are defined in a single config file. Yes, there is some hackery you can do to your `KUBECONFIG` environment variable to make it work with multiple files, but it is a sub-par workflow in my opinion and I never really liked it
- `kubectx` and `kubens` assume all your contexts are defined in a single config file. Yes, there is some hackery you can do to your `KUBECONFIG` environment variable to make it work with multiple files, but it is (in my opinion) a sub-par workflow and I never really liked it
- `kubie` spawns a new shell when you use it to change contexts, making it practically impossible to integrate into scripts or taskfile automation. Also I consider it to be too complicated of a solution for what is fundamentally a really simple problem

What I wanted was something much simpler conceptually: I just want to dump all my `kubeconfig` files in a single directory and then, have my tool parse them and "physically" move over the config file to `.kube/config` (or whatever is configured in my `KUBECONFIG` env var) such that it is also persistent between different shells.
What I wanted was something very simple conceptually: I just want to dump all my `kubeconfig` files in a single directory and then have my tool parse them and "physically" move over the config file to `.kube/config` (or whatever is configured in my `KUBECONFIG` env var) such that it is also persistent between different shells. Here is where `kubectl-switch` comes in!

## Installation

> [!TIP]
> When installing `kube-switcher`, you can use it as a `kubectl` plugin by just renaming the binary to `kubectl-switch`.
> This way, you can run it via `kubectl switch ...`
### Download Precompiled Binaries

Precompiled binaries are available for various platforms. You can download the latest release from the [GitHub Releases page](https://github.com/mirceanton/kube-switcher/releases/latest).
Precompiled binaries are available for various platforms. You can download the latest release from the [GitHub Releases page](https://github.com/mirceanton/kubectl-switch/releases/latest).

1. Download the appropriate binary for your system and extract the archive.
2. Make the extracted binary executable:

```bash
chmod +x kube-switcher
chmod +x kubectl-switch
```

3. Move the binary to a directory in your PATH:

```bash
mv kube-switcher /usr/local/bin/kube-switcher
mv kubectl-switch /usr/local/bin/kubectl-switch
```

### Running via Docker

`kube-switcher` is also available as a Docker container:
`kubectl-switch` is also available as a Docker container:

```bash
docker pull ghcr.io/mirceanton/kube-switcher
docker pull ghcr.io/mirceanton/kubectl-switch
```

### Install via homebrew
Expand All @@ -61,19 +55,19 @@ docker pull ghcr.io/mirceanton/kube-switcher
brew tap mirceanton/taps
```

2. Install `kube-switcher`
2. Install `kubectl-switch`

```bash
brew install kube-switcher
brew install kubectl-switch
```

### Build from Source

1. Clone the repository:

```bash
git clone https://github.com/mirceanton/kube-switcher
cd kube-switcher
git clone https://github.com/mirceanton/kubectl-switch
cd kubectl-switch
```

2. Build the tool:
Expand All @@ -87,50 +81,52 @@ docker pull ghcr.io/mirceanton/kube-switcher
Otherwise, simply run the `go build` command:

```bash
go build -o talswitcher
go build -o kubectl-switch
```

## Usage

1. Place all of your `kubeconfig` files in a single directory. I personally prefer `~/.kube/configs/`
1. Place all of your `kubeconfig` files in a single directory. I personally prefer `~/.kube/configs/` but you can do whatever you fancy.

2. Set the environment variable `KUBECONFIG_DIR`

```sh
export KUBECONFIG_DIR="~/.kube/configs/"
export KUBECONFIG_DIR="~/.kube/configs/" # <- put here whatever folder you decided on at step 1
```

3. Run `kube-swticher context`/`kube-switcher ctx` or `kubectl switch context`/`kubectl switch ctx` to interactively select your context from a list
3. Run `kubectl switch ctx` or `kubectl switch context` to interactively select your context from a list

```sh
vscode ➜ /workspaces/kube-switcher $ kubectl switch context
vscode ➜ /workspaces/kubectl-switch $ kubectl switch context
? Choose a context: [Use arrows to move, type to filter]
cluster1
cluster1kubectl-switch
cluster2
> cluster3
> cluster3kubectl-switch
INFO[0102] Switched to context 'cluster3'
INFO[0102] Switched tkubectl-switchuster3'
vscode ➜ /workspaces/kube-switcher $ kubectl get nodes
vscode ➜ /workspaces/kubectl-switch $ kubectl get nodes
NAME STATUS ROLES AGE VERSION
cluster3 Ready control-plane 21h v1.30.0
```
Alternatively, pass in a context name to the command to non-interactively switch to it:
```sh
vscode ➜ /workspaces/kube-switcher $ kubectl switch ctx cluster3
vscode ➜ /workspaces/kubectl-switch $ kubectl switch ctx cluster3
INFO[0000] Switched to context 'cluster3'
vscode ➜ /workspaces/kube-switcher $ kubectl get nodes
vscode ➜ /workspaces/kubectl-switch $ kubectl get nodes
NAME STATUS ROLES AGE VERSION
cluster3 Ready control-plane 21h v1.30.0
```
4. Run the `kube-switcher namespace`/`kube-switcher ns` or `kubectl switch namespace`/`kubectl switch ns` to interactively select your current namespace from a list
This will literally detect the file in which that context is defined and copy-paste it to the path defined in your `KUBECONFIG`.
4. Run the `kubectl switch ns` or `kubectl switch namespace` to interactively select your current namespace from a list
```sh
vscode ➜ /workspaces/kube-switcher $ kubectl switch namespace
vscode ➜ /workspaces/kubectl-switch $ kubectl switch namespace
? Choose a namespace: [Use arrows to move, type to filter]
> default
kube-node-lease
Expand All @@ -139,7 +135,7 @@ docker pull ghcr.io/mirceanton/kube-switcher
INFO[0012] Switched to namespace 'kube-system'
vscode ➜ /workspaces/kube-switcher $ kubectl get pods
vscode ➜ /workspaces/kubectl-switch $ kubectl get pods
NAME READY STATUS RESTARTS AGE
coredns-7db6d8ff4d-nqmlf 1/1 Running 1 (69m ago) 21h
etcd-cluster1 1/1 Running 1 (69m ago) 21h
Expand All @@ -153,14 +149,16 @@ docker pull ghcr.io/mirceanton/kube-switcher
Alternatively, pass in a namespace name to the command to non-interactively switch to it:
```sh
vscode ➜ /workspaces/kube-switcher $ kubectl get pods
vscode ➜ /workspaces/kubectl-switch $ kubectl get pods
No resources found in default namespace.
vscode ➜ /workspaces/kube-switcher $ kubectl switch ns kube-public
vscode ➜ /workspaces/kubectl-switch $ kubectl switch ns kube-public
INFO[0000] Switched to namespace 'kube-public'
vscode ➜ /workspaces/kube-switcher $ kubectl get pods
vscode ➜ /workspaces/kubectl-switch $ kubectl get pods
No resources found in kube-public namespace.
```
This will modify your currently active kubeconfig file (not the original one from `KUBECONFIGS_DIR`, but rather the copy from `KUBECONFIG`) to set the current namespace.
## License
This project is licensed under the MIT License. See the `LICENSE` file for details.
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ var (
)

var rootCmd = &cobra.Command{
Use: "kube-switcher",
Use: "kubectl-switch",
Short: "A tool to switch Kubernetes contexts",
Long: `kube-switcher is a CLI tool to switch Kubernetes contexts from multiple kubeconfig files.`,
Long: `kubectl-switch is a CLI tool to switch Kubernetes contexts from multiple kubeconfig files.`,
Version: version,
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/mirceanton/kube-switcher
module github.com/mirceanton/kubectl-switch

go 1.22.5

Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,10 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/api v0.31.0 h1:b9LiSjR2ym/SzTOlfMHm1tr7/21aD7fSkqgD/CVJBCo=
k8s.io/api v0.31.0/go.mod h1:0YiFF+JfFxMM6+1hQei8FY8M7s1Mth+z/q7eF1aJkTE=
k8s.io/api v0.31.1 h1:Xe1hX/fPW3PXYYv8BlozYqw63ytA92snr96zMW9gWTU=
k8s.io/api v0.31.1/go.mod h1:sbN1g6eY6XVLeqNsZGLnI5FwVseTrZX7Fv3O26rhAaI=
k8s.io/apimachinery v0.31.0 h1:m9jOiSr3FoSSL5WO9bjm1n6B9KROYYgNZOb4tyZ1lBc=
k8s.io/apimachinery v0.31.0/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo=
k8s.io/apimachinery v0.31.1 h1:mhcUBbj7KUjaVhyXILglcVjuS4nYXiwC+KKFBgIVy7U=
k8s.io/apimachinery v0.31.1/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo=
k8s.io/client-go v0.31.0 h1:QqEJzNjbN2Yv1H79SsS+SWnXkBgVu4Pj3CJQgbx0gI8=
k8s.io/client-go v0.31.0/go.mod h1:Y9wvC76g4fLjmU0BA+rV+h2cncoadjvjjkkIGoTLcGU=
k8s.io/client-go v0.31.1 h1:f0ugtWSbWpxHR7sjVpQwuvw9a3ZKLXX0u0itkFXufb0=
k8s.io/client-go v0.31.1/go.mod h1:sKI8871MJN2OyeqRlmA4W4KM9KBdBUpDLu/43eGemCg=
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/mirceanton/kube-switcher/cmd"
"github.com/mirceanton/kubectl-switch/cmd"
)

func main() {
Expand Down

0 comments on commit 1d5650d

Please sign in to comment.