mirror of
https://github.com/ahmetb/kubectx.git
synced 2026-03-16 02:42:15 +00:00
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:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user