-
Notifications
You must be signed in to change notification settings - Fork 1
feat: port forwarding for pods, services, and workloads (#1) #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
apps/desktop/src/renderer/detail/PortForwardSection.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| import { ArrowRightLeft, Check, Copy, LoaderCircle, Square } from "lucide-react"; | ||
| import { useState } from "react"; | ||
|
|
||
| import { Button } from "../components/ui/button"; | ||
| import { forwardKey, usePortForwards } from "../hooks/usePortForwards"; | ||
| import type { ForwardPort } from "./port-forward-ports"; | ||
|
|
||
| export interface PortForwardSectionProps { | ||
| contextId: string; | ||
| namespace: string; | ||
| name: string; | ||
| kind: string; | ||
| ports: ForwardPort[]; | ||
| } | ||
|
|
||
| /** | ||
| * Forwardable TCP ports for one resource. Each declared port row starts or | ||
| * stops a forward; a manual input covers pods listening on undeclared ports. | ||
| * Forwards live in a module-scoped store, so they survive navigation. | ||
| */ | ||
| export function PortForwardSection({ contextId, namespace, name, kind, ports }: PortForwardSectionProps) { | ||
| const { entries, start, stop, byKey } = usePortForwards(contextId); | ||
| const [manualPort, setManualPort] = useState(""); | ||
| const [manualLocalPort, setManualLocalPort] = useState(""); | ||
| const [localPorts, setLocalPorts] = useState<Record<number, string>>({}); | ||
| const visiblePorts = [...new Map(ports.map((port) => [port.port, port])).values()]; | ||
| for (const entry of entries) { | ||
| if (entry.kind === kind && entry.namespace === namespace && entry.name === name && | ||
| !visiblePorts.some((port) => port.port === entry.podPort)) { | ||
| visiblePorts.push({ label: "Other port", port: entry.podPort, protocol: "TCP" }); | ||
| } | ||
| } | ||
| function startForward(podPort: number, localValue = localPorts[podPort] ?? "") { | ||
| if (!validLocalPort(localValue)) return; | ||
| const localPort = localValue === "" ? 0 : Number(localValue); | ||
| void start({ | ||
| contextId, | ||
| namespace, | ||
| name, | ||
| podPort, | ||
| kind, | ||
| localPort, | ||
| }); | ||
| } | ||
|
|
||
| const manualValue = Number(manualPort); | ||
| const manualValid = Number.isInteger(manualValue) && manualValue >= 1 && manualValue <= 65_535 && validLocalPort(manualLocalPort); | ||
|
|
||
| return ( | ||
| <section className="resource-detail-section port-forward-section" data-testid="port-forward-section" aria-label="Port forwarding"> | ||
| <div className="resource-section-heading"> | ||
| <div> | ||
| <h2>Port forwarding</h2> | ||
| <p>Connect through a local TCP port. Leave the local port empty to assign one automatically.</p> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div className="port-forward-columns" aria-hidden="true"> | ||
| <span>Container / port</span><span>Remote port</span><span>Local port</span><span /> | ||
| </div> | ||
| <div className="port-forward-rows"> | ||
| {visiblePorts.map((port) => { | ||
| const key = forwardKey(kind, namespace, name, port.port); | ||
| const entry = byKey(key); | ||
| return ( | ||
| <div className="port-forward-row" key={key} data-testid="port-forward-row"> | ||
| <span className="port-forward-label" title={port.label}>{port.label}</span> | ||
| <span className="port-forward-port">{port.port}/{port.protocol}</span> | ||
| {entry?.localPort ? ( | ||
| <> | ||
| <div className="port-forward-address"> | ||
| <div className="port-forward-address-line"> | ||
| <span className="port-forward-local" data-testid="port-forward-local">localhost:{entry.localPort}</span> | ||
| <CopyLocalButton port={entry.localPort} /> | ||
| </div> | ||
| {entry.pod ? <span className="port-forward-pod" title={entry.pod}>via {entry.pod}</span> : null} | ||
| </div> | ||
| <Button | ||
| variant="outline" | ||
| data-testid="port-forward-stop" | ||
| disabled={entry.busy} | ||
| onClick={() => void stop(key)} | ||
| > | ||
| <Square aria-hidden="true" /> | ||
| Stop | ||
| </Button> | ||
| </> | ||
| ) : ( | ||
| <> | ||
| <input | ||
| className="port-forward-input" | ||
| inputMode="numeric" | ||
| placeholder="Auto" | ||
| value={localPorts[port.port] ?? ""} | ||
| aria-label={`Local port for ${port.label} ${port.port}`} | ||
| aria-invalid={!validLocalPort(localPorts[port.port] ?? "")} | ||
| title="Local port: 1–65535, or empty for a random port" | ||
| onChange={(event) => | ||
| setLocalPorts((current) => ({ ...current, [port.port]: event.target.value.replace(/[^0-9]/g, "").slice(0, 5) })) | ||
| } | ||
| /> | ||
| <Button | ||
| variant="outline" | ||
| data-testid="port-forward-start" | ||
| disabled={entry?.busy || !validLocalPort(localPorts[port.port] ?? "")} | ||
| onClick={() => startForward(port.port)} | ||
| > | ||
| {entry?.busy ? <LoaderCircle className="spin" aria-hidden="true" /> : <ArrowRightLeft aria-hidden="true" />} | ||
| Forward | ||
| </Button> | ||
| </> | ||
| )} | ||
| </div> | ||
| ); | ||
| })} | ||
|
|
||
| <form | ||
| className="port-forward-row port-forward-manual" | ||
| onSubmit={(event) => { | ||
| event.preventDefault(); | ||
| if (!manualValid) return; | ||
| startForward(manualValue, manualLocalPort); | ||
| setManualPort(""); | ||
| }} | ||
| > | ||
| <span className="port-forward-label">Other port</span> | ||
| <input | ||
| className="port-forward-input" | ||
| inputMode="numeric" | ||
| placeholder="8080" | ||
| value={manualPort} | ||
| aria-label="Pod port" | ||
| onChange={(event) => setManualPort(event.target.value.replace(/[^0-9]/g, ""))} | ||
| /> | ||
| <input | ||
| className="port-forward-input" | ||
| inputMode="numeric" | ||
| placeholder="Auto" | ||
| value={manualLocalPort} | ||
| aria-label="Local port for other port" | ||
| aria-invalid={!validLocalPort(manualLocalPort)} | ||
| title="Local port: 1–65535, or empty for a random port" | ||
| onChange={(event) => setManualLocalPort(event.target.value.replace(/[^0-9]/g, "").slice(0, 5))} | ||
| /> | ||
| <Button variant="outline" type="submit" disabled={!manualValid} data-testid="port-forward-manual-start"> | ||
| <ArrowRightLeft aria-hidden="true" /> | ||
| Forward | ||
| </Button> | ||
| </form> | ||
| </div> | ||
|
|
||
| <p className="port-forward-status" role="status" aria-live="polite"> | ||
| {[...new Set(visiblePorts.map((port) => byKey(forwardKey(kind, namespace, name, port.port))?.error).filter(Boolean))].join(" · ")} | ||
| </p> | ||
| </section> | ||
| ); | ||
| } | ||
|
|
||
| function validLocalPort(value: string): boolean { | ||
| return value === "" || (Number.isInteger(Number(value)) && Number(value) >= 1 && Number(value) <= 65_535); | ||
| } | ||
|
|
||
| function CopyLocalButton({ port }: { port: number }) { | ||
| const [copied, setCopied] = useState(false); | ||
| return ( | ||
| <Button | ||
| type="button" | ||
| variant="ghost" | ||
| size="icon-sm" | ||
| aria-label={`Copy localhost:${port}`} | ||
| title={copied ? "Copied" : "Copy local address"} | ||
| onClick={() => { | ||
| void navigator.clipboard?.writeText(`localhost:${port}`).then(() => { | ||
| setCopied(true); | ||
| setTimeout(() => setCopied(false), 1200); | ||
| }).catch(() => undefined); | ||
| }} | ||
| > | ||
| {copied ? <Check aria-hidden="true" /> : <Copy aria-hidden="true" />} | ||
| </Button> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P3] Unused imports.
PortForwardSectionandForwardPortare imported here but never used in this file — dead wiring (or an unfinished refactor). Remove them.