kubens: show slow-listing warning after 3s during namespace list

When `kubens` is run with no args in non-interactive mode (ListOp),
start a background goroutine that prints a warning to stderr after
3 seconds if the Kubernetes API call is still in progress. The warning
advises users to switch directly with `kubens -f <ns>` to avoid the
slow list call. The goroutine is cancelled immediately if listing
completes before the timeout.

https://claude.ai/code/session_01XJXHq8WG22iqX8KaDb9RZz
This commit is contained in:
Claude
2026-03-09 08:15:23 +00:00
parent f500964e24
commit c57909e31a

View File

@@ -21,6 +21,7 @@ import (
"io"
"os"
"slices"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
@@ -52,6 +53,16 @@ func (op ListOp) Run(stdout, stderr io.Writer) error {
return fmt.Errorf("cannot read current namespace: %w", err)
}
done := make(chan struct{})
defer close(done)
go func() {
select {
case <-time.After(3 * time.Second):
printer.Warning(stderr, `listing namespaces is taking long, switch to a namespace directly with "kubens -f <ns>"`)
case <-done:
}
}()
ns, err := queryNamespaces(kc)
if err != nil {
return fmt.Errorf("could not list namespaces (is the cluster accessible?): %w", err)