kubens: use context to cancel slow-query warning timer after API returns

Replace done channel + defer with context.WithCancel so the warning
goroutine is cancelled immediately when queryNamespaces returns, not
when Run returns (which could be minutes later while user browses fzf).
Also thread the context into queryNamespaces so the k8s List call
respects it.

https://claude.ai/code/session_01XJXHq8WG22iqX8KaDb9RZz
This commit is contained in:
Claude
2026-03-09 16:33:05 +00:00
parent f1283e7ebc
commit 2b0e4de615
2 changed files with 11 additions and 10 deletions

View File

@@ -16,6 +16,7 @@ package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
@@ -54,17 +55,17 @@ func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error {
return fmt.Errorf("cannot read current namespace: %w", err)
}
done := make(chan struct{})
defer close(done)
ctx, cancel := context.WithCancel(context.Background())
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:
case <-ctx.Done():
}
}()
ns, err := queryNamespaces(kc)
ns, err := queryNamespaces(ctx, kc)
cancel()
if err != nil {
return fmt.Errorf("could not list namespaces (is the cluster accessible?): %w", err)
}

View File

@@ -53,17 +53,17 @@ 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)
ctx, cancel := context.WithCancel(context.Background())
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:
case <-ctx.Done():
}
}()
ns, err := queryNamespaces(kc)
ns, err := queryNamespaces(ctx, kc)
cancel()
if err != nil {
return fmt.Errorf("could not list namespaces (is the cluster accessible?): %w", err)
}
@@ -78,7 +78,7 @@ func (op ListOp) Run(stdout, stderr io.Writer) error {
return nil
}
func queryNamespaces(kc *kubeconfig.Kubeconfig) ([]string, error) {
func queryNamespaces(ctx context.Context, kc *kubeconfig.Kubeconfig) ([]string, error) {
if os.Getenv("_MOCK_NAMESPACES") != "" {
return []string{"ns1", "ns2"}, nil
}
@@ -92,7 +92,7 @@ func queryNamespaces(kc *kubeconfig.Kubeconfig) ([]string, error) {
var next string
for {
list, err := clientset.CoreV1().Namespaces().List(
context.Background(),
ctx,
metav1.ListOptions{
Limit: 500,
Continue: next,