Compare commits

..

1 Commits

Author SHA1 Message Date
Ahmet Alp Balkan
a444364ed3 fix: improve error handling and resource management in kubeconfig
This change addresses eight key improvements to the kubectx/kubens codebase:

Resource Management Fixes:
- Fix use-after-close bugs where Kubeconfig was accessed after Close()
- Fix resource leaks on error paths by ensuring defer kc.Close() is called
- Fix YAML encoder not being closed after Encode(), causing buffered data loss

API Design Improvements:
- Change ContextNames() to return ([]string, error) instead of silently returning
  nil on error, making parse failures distinguishable from empty results
- Change GetCurrentContext() to return (string, error) instead of returning ""
  for both "not set" and parse error cases
- Update all 16 call sites across cmd/kubectx and cmd/kubens packages to handle
  the new error returns while preserving backward-compatible behavior

Error Handling:
- Add explicit error handling for printer.Success() calls in 5+ locations
  by prefixing unchecked calls with _ =

Performance:
- Add slice pre-allocation in namespace list pagination using slices.Grow()
  before append loops, reducing allocations when fetching 500+ item batches

All changes maintain backward compatibility for missing kubeconfig keys while
improving error transparency and resource safety.
2026-03-08 17:42:34 -07:00
14 changed files with 210 additions and 129 deletions

View File

@@ -24,7 +24,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@master
- run: git fetch --tags
- name: Setup Go
uses: actions/setup-go@v6

View File

@@ -15,7 +15,7 @@
# limitations under the License.
# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at https://goreleaser.com
# Make sure to check the documentation at http://goreleaser.com
version: 2
before:
@@ -69,11 +69,11 @@ archives:
{{- else -}}v{{- . -}}
{{- end -}}
{{- end -}}
ids:
builds:
- kubectx
format_overrides:
- goos: windows
formats: [zip]
format: zip
files: ["LICENSE"]
- id: kubens-archive
name_template: |-
@@ -89,11 +89,11 @@ archives:
{{- else -}}v{{- . -}}
{{- end -}}
{{- end -}}
ids:
builds:
- kubens
format_overrides:
- goos: windows
formats: [zip]
format: zip
files: ["LICENSE"]
checksum:
name_template: "checksums.txt"
@@ -111,7 +111,7 @@ snapcrafts:
kubens is a tool to switch between Kubernetes namespaces (and configure them for kubectl) easily.
grade: stable
confinement: classic
base: core24
base: core20
apps:
kubectx:
command: kubectx

172
README.md
View File

@@ -72,23 +72,139 @@ names anymore.
## Installation
| Package manager | Command |
|---|---|
| [Homebrew](https://brew.sh/) (macOS & Linux) | `brew install kubectx` |
| [MacPorts](https://www.macports.org) (macOS) | `sudo port install kubectx` |
| apt (Debian/Ubuntu) | `sudo apt install kubectx` |
| pacman (Arch Linux) | `sudo pacman -S kubectx` |
| [Chocolatey](https://chocolatey.org/) (Windows) | `choco install kubens kubectx` |
| [Scoop](https://scoop.sh/) (Windows) | `scoop bucket add main && scoop install main/kubens main/kubectx` |
| [winget](https://learn.microsoft.com/en-us/windows/package-manager/) (Windows) | `winget install --id ahmetb.kubectx && winget install --id ahmetb.kubens` |
| [Krew](https://github.com/kubernetes-sigs/krew/) (kubectl plugin) | `kubectl krew install ctx && kubectl krew install ns` |
Stable versions of `kubectx` and `kubens` are small bash scripts that you
can find in this repository.
Alternatively, download binaries from the [**Releases page →**](https://github.com/ahmetb/kubectx/releases) and add them to somewhere in your `PATH`.
Starting with v0.9.0, `kubectx` and `kubens` **are now rewritten in Go**. They
should work the same way (and we'll keep the bash-based implementations around)
but the new features will be added to the new Go programs. Please help us test
this new Go implementation by downloading the binaries from the [**Releases page
→**](https://github.com/ahmetb/kubectx/releases)
<details>
<summary>Shell completion scripts</summary>
**Installation options:**
#### zsh (with [antibody](https://getantibody.github.io))
- [as kubectl plugins (macOS & Linux)](#kubectl-plugins-macos-and-linux)
- [with Homebrew (macOS & Linux)](#homebrew-macos-and-linux)
- [with MacPorts (macOS)](#macports-macos)
- [with apt (Debian)](#apt-debian)
- [with pacman (Arch Linux)](#pacman-arch-linux)
- [with Chocolatey (Windows)](#windows-installation-using-chocolatey)
- [Windows Installation (using Scoop)](#windows-installation-using-scoop)
- [with winget (Windows)](#windows-installation-using-winget)
- [manually (macOS & Linux)](#manual-installation-macos-and-linux)
If you like to add context/namespace information to your shell prompt (`$PS1`),
you can try out [kube-ps1].
[kube-ps1]: https://github.com/jonmosco/kube-ps1
### Kubectl Plugins (macOS and Linux)
You can install and use the [Krew](https://github.com/kubernetes-sigs/krew/) kubectl
plugin manager to get `kubectx` and `kubens`.
**Note:** This will not install the shell completion scripts. If you want them,
*choose another installation method
or install the scripts [manually](#manual-installation-macos-and-linux).
```sh
kubectl krew install ctx
kubectl krew install ns
```
After installing, the tools will be available as `kubectl ctx` and `kubectl ns`.
### Homebrew (macOS and Linux)
If you use [Homebrew](https://brew.sh/) you can install like this:
```sh
brew install kubectx
```
This command will set up bash/zsh/fish completion scripts automatically. Make sure you [configure your shell](https://docs.brew.sh/Shell-Completion) to load completions for installed Homebrew formulas.
### MacPorts (macOS)
If you use [MacPorts](https://www.macports.org) you can install like this:
```sh
sudo port install kubectx
```
### apt (Debian)
``` bash
sudo apt install kubectx
```
Newer versions might be available on repos like
[Debian Buster (testing)](https://packages.debian.org/buster/kubectx),
[Sid (unstable)](https://packages.debian.org/sid/kubectx)
(_if you are unfamiliar with the Debian release process and how to enable
testing/unstable repos, check out the
[Debian Wiki](https://wiki.debian.org/DebianReleases)_):
### pacman (Arch Linux)
Available as official Arch Linux package. Install it via:
```bash
sudo pacman -S kubectx
```
### Windows Installation (using Chocolatey)
Available as packages on [Chocolatey](https://chocolatey.org/why-chocolatey)
```pwsh
choco install kubens kubectx
```
### Windows Installation (using Scoop)
Available as packages on [Scoop](https://scoop.sh/)
```pwsh
scoop bucket add main
scoop install main/kubens main/kubectx
```
### Windows Installation (using winget)
Available as packages on [winget](https://learn.microsoft.com/en-us/windows/package-manager/)
```pwsh
winget install --id ahmetb.kubectx
winget install --id ahmetb.kubens
```
### Manual Installation (macOS and Linux)
Since `kubectx` and `kubens` are written in Bash, you should be able to install
them to any POSIX environment that has Bash installed.
- Download the `kubectx`, and `kubens` scripts.
- Either:
- save them all to somewhere in your `PATH`,
- or save them to a directory, then create symlinks to `kubectx`/`kubens` from
somewhere in your `PATH`, like `/usr/local/bin`
- Make `kubectx` and `kubens` executable (`chmod +x ...`)
Example installation steps:
``` bash
sudo git clone https://github.com/ahmetb/kubectx /opt/kubectx
sudo ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx
sudo ln -s /opt/kubectx/kubens /usr/local/bin/kubens
```
If you also want to have shell completions, pick an installation method for the
[completion scripts](completion/) that fits your system best: [`zsh` with
`antibody`](#completion-scripts-for-zsh-with-antibody), [plain
`zsh`](#completion-scripts-for-plain-zsh),
[`bash`](#completion-scripts-for-bash) or
[`fish`](#completion-scripts-for-fish).
#### Completion scripts for `zsh` with [antibody](https://getantibody.github.io)
Add this line to your [Plugins File](https://getantibody.github.io/usage/) (e.g.
`~/.zsh_plugins.txt`):
@@ -101,9 +217,9 @@ Depending on your setup, you might or might not need to call `compinit` or
`autoload -U compinit && compinit` in your `~/.zshrc` after you load the Plugins
file. If you use [oh-my-zsh](https://github.com/ohmyzsh/ohmyzsh), load the
completions before you load `oh-my-zsh` because `oh-my-zsh` will call
`compinit`.
`compinit`.
#### zsh (plain)
#### Completion scripts for plain `zsh`
The completion scripts have to be in a path that belongs to `$fpath`. Either
link or copy them to an existing folder.
@@ -128,7 +244,7 @@ depending on the `$fpath` of your zsh installation.
In case of errors, calling `compaudit` might help.
#### bash
#### Completion scripts for `bash`
```bash
git clone https://github.com/ahmetb/kubectx.git ~/.kubectx
@@ -143,7 +259,7 @@ export PATH=~/.kubectx:\$PATH
EOF
```
#### fish
#### Completion scripts for `fish`
```fish
mkdir -p ~/.config/fish/completions
@@ -151,12 +267,6 @@ ln -s /opt/kubectx/completion/kubectx.fish ~/.config/fish/completions/
ln -s /opt/kubectx/completion/kubens.fish ~/.config/fish/completions/
```
</details>
> [!NOTE]
> Tip: Show context/namespace in your shell prompt with [oh-my-posh](https://ohmyposh.dev/) or
> simply with [kube-ps1](https://github.com/jonmosco/kube-ps1).
-----
### Interactive mode
@@ -167,12 +277,12 @@ with fuzzy searching, you just need to [install
![kubectx interactive search with fzf](img/kubectx-interactive.gif)
Caveats:
- If you have `fzf` installed, but want to opt out of using this feature, set the
environment variable `KUBECTX_IGNORE_FZF=1`.
- If you want to keep `fzf` interactive mode but need the default behavior of the
command, you can do it by piping the output to another command (e.g. `kubectx |
cat `).
If you have `fzf` installed, but want to opt out of using this feature, set the
environment variable `KUBECTX_IGNORE_FZF=1`.
If you want to keep `fzf` interactive mode but need the default behavior of the
command, you can do it by piping the output to another command (e.g. `kubectx |
cat `).
-----
@@ -196,7 +306,7 @@ Colors in the output can be disabled by setting the
If you liked `kubectx`, you may like my
[`kubectl-aliases`](https://github.com/ahmetb/kubectl-aliases) project, too. I
recommend pairing kubectx and kubens with [fzf](#interactive-mode) and
[kube-ps1](https://github.com/jonmosco/kube-ps1).
[kube-ps1].
#### Stargazers over time

View File

@@ -52,14 +52,6 @@ func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error {
return fmt.Errorf("kubeconfig error: %w", err)
}
ctxNames, err := kc.ContextNames()
if err != nil {
return fmt.Errorf("failed to get context names: %w", err)
}
if len(ctxNames) == 0 {
return errors.New("no contexts found in the kubeconfig file")
}
cmd := exec.Command("fzf", "--ansi", "--no-preview")
var out bytes.Buffer
cmd.Stdin = os.Stdin

View File

@@ -24,11 +24,11 @@ import (
)
func kubectxPrevCtxFile() (string, error) {
dir := cmdutil.CacheDir()
if dir == "" {
home := cmdutil.HomeDir()
if home == "" {
return "", errors.New("HOME or USERPROFILE environment variable not set")
}
return filepath.Join(dir, "kubectx"), nil
return filepath.Join(home, ".kube", "kubectx"), nil
}
// readLastContext returns the saved previous context

View File

@@ -73,7 +73,6 @@ func Test_writeLastContext(t *testing.T) {
func Test_kubectxFilePath(t *testing.T) {
t.Setenv("HOME", filepath.FromSlash("/foo/bar"))
t.Setenv("XDG_CACHE_HOME", "")
expected := filepath.Join(filepath.FromSlash("/foo/bar"), ".kube", "kubectx")
v, err := kubectxPrevCtxFile()
@@ -85,19 +84,6 @@ func Test_kubectxFilePath(t *testing.T) {
}
}
func Test_kubectxFilePath_xdgCacheHome(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", filepath.FromSlash("/tmp/xdg-cache"))
expected := filepath.Join(filepath.FromSlash("/tmp/xdg-cache"), "kubectx")
v, err := kubectxPrevCtxFile()
if err != nil {
t.Fatal(err)
}
if v != expected {
t.Fatalf("expected=\"%s\" got=\"%s\"", expected, v)
}
}
func Test_kubectxFilePath_error(t *testing.T) {
t.Setenv("HOME", "")
t.Setenv("USERPROFILE", "")

View File

@@ -46,14 +46,6 @@ func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error {
return fmt.Errorf("kubeconfig error: %w", err)
}
ctxNames, err := kc.ContextNames()
if err != nil {
return fmt.Errorf("failed to get context names: %w", err)
}
if len(ctxNames) == 0 {
return errors.New("no contexts found in the kubeconfig file")
}
cmd := exec.Command("fzf", "--ansi", "--no-preview")
var out bytes.Buffer
cmd.Stdin = os.Stdin

View File

@@ -24,7 +24,7 @@ import (
"github.com/ahmetb/kubectx/internal/cmdutil"
)
var defaultDir = filepath.Join(cmdutil.CacheDir(), "kubens")
var defaultDir = filepath.Join(cmdutil.HomeDir(), ".kube", "kubens")
type NSFile struct {
dir string

View File

@@ -18,6 +18,8 @@ import (
"runtime"
"strings"
"testing"
"github.com/ahmetb/kubectx/internal/testutil"
)
func TestNSFile(t *testing.T) {
@@ -48,7 +50,7 @@ func TestNSFile(t *testing.T) {
}
func TestNSFile_path_windows(t *testing.T) {
t.Setenv("_FORCE_GOOS", "windows")
defer testutil.WithEnvVar("_FORCE_GOOS", "windows")()
fp := NewNSFile("a:b:c").path()
if expected := "a__b__c"; !strings.HasSuffix(fp, expected) {
@@ -66,7 +68,7 @@ func Test_isWindows(t *testing.T) {
t.Fatalf("isWindows() returned true for %s", runtime.GOOS)
}
t.Setenv("_FORCE_GOOS", "windows")
defer testutil.WithEnvVar("_FORCE_GOOS", "windows")()
if !isWindows() {
t.Fatalf("isWindows() failed to detect windows with env override.")
}

View File

@@ -17,7 +17,6 @@ package cmdutil
import (
"errors"
"os"
"path/filepath"
)
func HomeDir() string {
@@ -28,19 +27,6 @@ func HomeDir() string {
return home
}
// CacheDir returns XDG_CACHE_HOME if set, otherwise $HOME/.kube,
// matching the bash scripts' behavior: ${XDG_CACHE_HOME:-$HOME/.kube}.
func CacheDir() string {
if xdg := os.Getenv("XDG_CACHE_HOME"); xdg != "" {
return xdg
}
home := HomeDir()
if home == "" {
return ""
}
return filepath.Join(home, ".kube")
}
// IsNotFoundErr determines if the underlying error is os.IsNotExist.
func IsNotFoundErr(err error) bool {
for e := err; e != nil; e = errors.Unwrap(e) {

View File

@@ -15,8 +15,9 @@
package cmdutil
import (
"path/filepath"
"testing"
"github.com/ahmetb/kubectx/internal/testutil"
)
func Test_homeDir(t *testing.T) {
@@ -62,40 +63,18 @@ func Test_homeDir(t *testing.T) {
for _, c := range cases {
t.Run(c.name, func(tt *testing.T) {
var unsets []func()
for _, e := range c.envs {
tt.Setenv(e.k, e.v)
unsets = append(unsets, testutil.WithEnvVar(e.k, e.v))
}
got := HomeDir()
if got != c.want {
t.Errorf("expected:%q got:%q", c.want, got)
}
for _, u := range unsets {
u()
}
})
}
}
func TestCacheDir(t *testing.T) {
t.Run("XDG_CACHE_HOME set", func(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", "/tmp/xdg-cache")
t.Setenv("HOME", "/home/user")
if got := CacheDir(); got != "/tmp/xdg-cache" {
t.Errorf("expected:%q got:%q", "/tmp/xdg-cache", got)
}
})
t.Run("XDG_CACHE_HOME unset, falls back to HOME/.kube", func(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", "")
t.Setenv("HOME", "/home/user")
want := filepath.Join("/home/user", ".kube")
if got := CacheDir(); got != want {
t.Errorf("expected:%q got:%q", want, got)
}
})
t.Run("neither set", func(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", "")
t.Setenv("HOME", "")
t.Setenv("USERPROFILE", "")
if got := CacheDir(); got != "" {
t.Errorf("expected:%q got:%q", "", got)
}
})
}

View File

@@ -15,16 +15,17 @@
package kubeconfig
import (
"github.com/ahmetb/kubectx/internal/cmdutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/ahmetb/kubectx/internal/cmdutil"
"github.com/ahmetb/kubectx/internal/testutil"
)
func Test_kubeconfigPath(t *testing.T) {
t.Setenv("HOME", "/x/y/z")
defer testutil.WithEnvVar("HOME", "/x/y/z")()
expected := filepath.FromSlash("/x/y/z/.kube/config")
got, err := kubeconfigPath()
@@ -37,9 +38,9 @@ func Test_kubeconfigPath(t *testing.T) {
}
func Test_kubeconfigPath_noEnvVars(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", "")
t.Setenv("HOME", "")
t.Setenv("USERPROFILE", "")
defer testutil.WithEnvVar("XDG_CACHE_HOME", "")()
defer testutil.WithEnvVar("HOME", "")()
defer testutil.WithEnvVar("USERPROFILE", "")()
_, err := kubeconfigPath()
if err == nil {
@@ -48,7 +49,7 @@ func Test_kubeconfigPath_noEnvVars(t *testing.T) {
}
func Test_kubeconfigPath_envOvveride(t *testing.T) {
t.Setenv("KUBECONFIG", "foo")
defer testutil.WithEnvVar("KUBECONFIG", "foo")()
v, err := kubeconfigPath()
if err != nil {
@@ -61,7 +62,7 @@ func Test_kubeconfigPath_envOvveride(t *testing.T) {
func Test_kubeconfigPath_envOvverideDoesNotSupportPathSeparator(t *testing.T) {
path := strings.Join([]string{"file1", "file2"}, string(os.PathListSeparator))
t.Setenv("KUBECONFIG", path)
defer testutil.WithEnvVar("KUBECONFIG", path)()
_, err := kubeconfigPath()
if err == nil {
@@ -70,7 +71,7 @@ func Test_kubeconfigPath_envOvverideDoesNotSupportPathSeparator(t *testing.T) {
}
func TestStandardKubeconfigLoader_returnsNotFoundErr(t *testing.T) {
t.Setenv("KUBECONFIG", "foo")
defer testutil.WithEnvVar("KUBECONFIG", "foo")()
kc := new(Kubeconfig).WithLoader(DefaultLoader)
err := kc.Parse()
if err == nil {

View File

@@ -18,6 +18,8 @@ import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/ahmetb/kubectx/internal/testutil"
)
var (
@@ -25,8 +27,8 @@ var (
)
func Test_useColors_forceColors(t *testing.T) {
t.Setenv("_KUBECTX_FORCE_COLOR", "1")
t.Setenv("NO_COLOR", "1")
defer testutil.WithEnvVar("_KUBECTX_FORCE_COLOR", "1")()
defer testutil.WithEnvVar("NO_COLOR", "1")()
if v := useColors(); !cmp.Equal(v, &tr) {
t.Fatalf("expected useColors() = true; got = %v", v)
@@ -34,7 +36,7 @@ func Test_useColors_forceColors(t *testing.T) {
}
func Test_useColors_disableColors(t *testing.T) {
t.Setenv("NO_COLOR", "1")
defer testutil.WithEnvVar("NO_COLOR", "1")()
if v := useColors(); !cmp.Equal(v, &fa) {
t.Fatalf("expected useColors() = false; got = %v", v)
@@ -42,8 +44,8 @@ func Test_useColors_disableColors(t *testing.T) {
}
func Test_useColors_default(t *testing.T) {
t.Setenv("NO_COLOR", "")
t.Setenv("_KUBECTX_FORCE_COLOR", "")
defer testutil.WithEnvVar("NO_COLOR", "")()
defer testutil.WithEnvVar("_KUBECTX_FORCE_COLOR", "")()
if v := useColors(); v != nil {
t.Fatalf("expected useColors() = nil; got=%v", *v)

View File

@@ -0,0 +1,31 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testutil
import "os"
// WithEnvVar sets an env var temporarily. Call its return value
// in defer to restore original value in env (if exists).
func WithEnvVar(key, value string) func() {
orig, ok := os.LookupEnv(key)
os.Setenv(key, value)
return func() {
if ok {
os.Setenv(key, orig)
} else {
os.Unsetenv(key)
}
}
}