From 786d8840f6b28c1d878175e96fede2c288583ab0 Mon Sep 17 00:00:00 2001 From: Digvijay Gunjal Date: Tue, 24 Nov 2026 00:37:56 +0100 Subject: [PATCH 1/2] fix(ssh): fall back to single-pod read when list omits runtime (#408) `ssh info` (and the deprecated `ssh connect` alias) derived pod readiness solely from GetPods(), the /pods list endpoint. That endpoint can omit runtime/ssh fields for a pod that is already running and reachable, while the single-pod GET (/pods/{id}) already reports them -- the exact gap Client.GetPodRuntime's own doc comment describes, and the one internal/waitfor/pod.go already re-reads around for `pod create --wait`. ssh info never got that fallback, so a fully-up pod could be reported as "no container reported yet (image pull, container create or boot)" for as long as the list read lagged behind the single-pod one -- 25+ minutes in the field per #408, despite container logs showing sshd already up. Add the same fallback `pod create --wait` already has: when the listed pod has no runtime, re-read it via GetPodRuntime before deriving state. Added a regression test (TestSSHInfo_FallsBackToSinglePodReadWhenListLagsRuntime) exercising a stubbed list/get divergence, and extended the ssh test stub to serve /pods/ so it can simulate that divergence. Not yet e2e-tested against the live API per AGENTS.md's non-negotiable rule -- opening as a draft for that reason; happy to run that pass (costs a live pod) before this is marked ready, or if a maintainer can confirm the fix from the unit test alone. Fixes #408 --- cmd/ssh.go | 6 ++++++ cmd/ssh_test.go | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/cmd/ssh.go b/cmd/ssh.go index 88333cab..b3cddad6 100644 --- a/cmd/ssh.go +++ b/cmd/ssh.go @@ -230,6 +230,12 @@ func runSSHInfoWithArgs(cmd *cobra.Command, args []string, allowAll bool) error nameOrID := args[0] pod, conn := sshconnect.FindPodConnection(pods, nameOrID, keyInfo) if pod != nil { + if pod.Runtime == nil { + if fresh, err := client.GetPodRuntime(pod.ID); err == nil && fresh != nil { + pod = fresh + conn = sshconnect.BuildConnection(pod, keyInfo) + } + } // the same derivation `pod get` uses, so both commands explain a // not-ready pod identically instead of saying only "pod not ready". state := sshconnect.PodState(pod) diff --git a/cmd/ssh_test.go b/cmd/ssh_test.go index 968a8dbe..d85db2c6 100644 --- a/cmd/ssh_test.go +++ b/cmd/ssh_test.go @@ -119,14 +119,36 @@ func TestSSHRemoveKey_RequiresIdentifier(t *testing.T) { // an agent and an ssh command that can never connect. func sshStub(t *testing.T, pods string) { + sshStubWithPodGet(t, pods, nil) +} + +func sshStubWithPodGet(t *testing.T, pods string, podGetOverrides map[string]string) { t.Helper() + var parsedPods []json.RawMessage + if err := json.Unmarshal([]byte(pods), &parsedPods); err != nil { + t.Fatalf("invalid pods fixture: %v", err) + } server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - switch r.URL.Path { - case "/pods": + switch { + case r.URL.Path == "/pods": _, _ = w.Write([]byte(`{"pods":` + pods + `,"pagination":{"hasNextPage":false,"nextCursor":null}}`)) - case "/account/ssh-keys": + case r.URL.Path == "/account/ssh-keys": _, _ = w.Write([]byte(`{"keys":[]}`)) + case strings.HasPrefix(r.URL.Path, "/pods/"): + id := strings.TrimPrefix(r.URL.Path, "/pods/") + if override, ok := podGetOverrides[id]; ok { + _, _ = w.Write([]byte(override)) + return + } + for _, raw := range parsedPods { + var entry map[string]interface{} + if err := json.Unmarshal(raw, &entry); err == nil && entry["id"] == id { + _, _ = w.Write(raw) + return + } + } + w.WriteHeader(http.StatusNotFound) default: t.Errorf("unexpected request %s", r.URL.Path) w.WriteHeader(http.StatusNotFound) @@ -234,6 +256,21 @@ func TestSSHInfo_RuntimeState(t *testing.T) { } } +func TestSSHInfo_FallsBackToSinglePodReadWhenListLagsRuntime(t *testing.T) { + listPods := `[{"id":"p","name":"p","status":"RUNNING","ports":["22/tcp"],"runtime":null}]` + freshGet := `{"id":"p","name":"p","status":"RUNNING","ports":["22/tcp"],"runtime":{"uptime":30,"ports":[` + sshPortJSON + `]}}` + sshStubWithPodGet(t, listPods, map[string]string{"p": freshGet}) + + got := sshCaptureJSON(t, []string{"p"}, false) + + if gotErr, ok := got["error"]; ok { + t.Errorf("expected a ready connection once the single-pod read is consulted, got error %v (%v)", gotErr, got) + } + if _, ok := got["ssh_command"]; !ok { + t.Errorf("expected an ssh_command once the single-pod read shows the pod is up, got %v", got) + } +} + // TestSSHConnect_ListSkipsDeadPods covers the no-arg legacy path, which builds // connections through ListConnections rather than FindPodConnection. func TestSSHConnect_ListSkipsDeadPods(t *testing.T) { From ba1b1dac3e34ff9c407a2424f0f05438fbbd616d Mon Sep 17 00:00:00 2001 From: Digvijay Gunjal Date: Tue, 24 Nov 2026 01:04:31 +0100 Subject: [PATCH 2/2] fix(ssh): guard the single-pod fallback read against id mismatch Mirror internal/waitfor/pod.go's fresh.ID == podID check on the same fallback read in cmd/ssh.go -- the first version trusted any non-error, non-nil GetPodRuntime response without checking it actually describes the pod being queried. Added two tests: the fallback read failing (confirms the command still falls back to the pre-fix not-ready behavior instead of erroring), and the fallback read returning a different pod's id (confirms it's ignored). --- cmd/ssh.go | 2 +- cmd/ssh_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/cmd/ssh.go b/cmd/ssh.go index b3cddad6..29e92d23 100644 --- a/cmd/ssh.go +++ b/cmd/ssh.go @@ -231,7 +231,7 @@ func runSSHInfoWithArgs(cmd *cobra.Command, args []string, allowAll bool) error pod, conn := sshconnect.FindPodConnection(pods, nameOrID, keyInfo) if pod != nil { if pod.Runtime == nil { - if fresh, err := client.GetPodRuntime(pod.ID); err == nil && fresh != nil { + if fresh, err := client.GetPodRuntime(pod.ID); err == nil && fresh != nil && fresh.ID == pod.ID { pod = fresh conn = sshconnect.BuildConnection(pod, keyInfo) } diff --git a/cmd/ssh_test.go b/cmd/ssh_test.go index d85db2c6..c926800f 100644 --- a/cmd/ssh_test.go +++ b/cmd/ssh_test.go @@ -271,6 +271,44 @@ func TestSSHInfo_FallsBackToSinglePodReadWhenListLagsRuntime(t *testing.T) { } } +// TestSSHInfo_SinglePodReadFailureKeepsListedView covers the fallback's error +// path: if GetPodRuntime fails (the single-pod endpoint 404s or errors), the +// command must fall back to the listed (nil-runtime) view instead of failing +// the whole command, and report the pod as not ready exactly as it did before +// this fallback existed. +func TestSSHInfo_SinglePodReadFailureKeepsListedView(t *testing.T) { + listPods := `[{"id":"p","name":"p","status":"RUNNING","ports":["22/tcp"],"runtime":null}]` + sshStubWithPodGet(t, listPods, nil) // no override -> /pods/p 404s + + got := sshCaptureJSON(t, []string{"p"}, false) + + if _, ok := got["error"]; !ok { + t.Errorf("expected a not-ready error when the single-pod read fails, got %v", got) + } + if _, ok := got["ssh_command"]; ok { + t.Errorf("did not expect an ssh_command from a failed single-pod read, got %v", got) + } +} + +// TestSSHInfo_SinglePodReadIgnoredOnIDMismatch mirrors the defensive ID check +// internal/waitfor/pod.go already applies to the same fallback read: a +// response whose id doesn't match the pod being queried must never be +// trusted, even if the http call itself succeeded. +func TestSSHInfo_SinglePodReadIgnoredOnIDMismatch(t *testing.T) { + listPods := `[{"id":"p","name":"p","status":"RUNNING","ports":["22/tcp"],"runtime":null}]` + wrongIDGet := `{"id":"other","name":"other","status":"RUNNING","ports":["22/tcp"],"runtime":{"uptime":30,"ports":[` + sshPortJSON + `]}}` + sshStubWithPodGet(t, listPods, map[string]string{"p": wrongIDGet}) + + got := sshCaptureJSON(t, []string{"p"}, false) + + if _, ok := got["error"]; !ok { + t.Errorf("expected a not-ready error when the single-pod read returns a mismatched id, got %v", got) + } + if id, _ := got["id"].(string); id != "p" { + t.Errorf("expected the originally listed pod id 'p' to be reported, got %v", got) + } +} + // TestSSHConnect_ListSkipsDeadPods covers the no-arg legacy path, which builds // connections through ListConnections rather than FindPodConnection. func TestSSHConnect_ListSkipsDeadPods(t *testing.T) {