Skip to content

Commit 2a9e827

Browse files
ideliceRamilito
andauthored
feat: port-forward pod (#59)
Co-authored-by: Delice0 <[email protected]> Co-authored-by: Ramilito <[email protected]>
1 parent c02d1b7 commit 2a9e827

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

ftplugin/k8s_pods.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local api = vim.api
2+
local actions = require("kubectl.actions.actions")
23
local commands = require("kubectl.actions.commands")
34
local container_view = require("kubectl.views.containers")
45
local deployment_view = require("kubectl.views.deployments")
@@ -17,6 +18,7 @@ api.nvim_buf_set_keymap(0, "n", "g?", "", {
1718
hints = hints .. tables.generateHintLine("<d>", "Describe selected pod \n")
1819
hints = hints .. tables.generateHintLine("<t>", "Show resources used \n")
1920
hints = hints .. tables.generateHintLine("<enter>", "Opens container view \n")
21+
hints = hints .. tables.generateHintLine("<shift-f>", "Port forward \n")
2022
hints = hints .. tables.generateHintLine("<C-k>", "Kill pod \n")
2123
view.Hints(hints)
2224
end,
@@ -104,6 +106,46 @@ api.nvim_buf_set_keymap(0, "n", "<C-k>", "", {
104106
end,
105107
})
106108

109+
api.nvim_buf_set_keymap(0, "n", "<S-f>", "", {
110+
noremap = true,
111+
silent = true,
112+
callback = function()
113+
local namespace, pod_name = tables.getCurrentSelection(unpack(col_indices))
114+
if pod_name and namespace then
115+
local current_port_query = "get pod " .. pod_name .. ' -o jsonpath="{.spec.containers[*].ports[*].containerPort}"'
116+
117+
local current_port_result = commands.execute_shell_command("kubectl", current_port_query)
118+
119+
vim.ui.input({ prompt = "Port forward " .. current_port_result }, function(input)
120+
if input ~= nil then
121+
actions.confirmation_buffer(
122+
"Are you sure that you want to port forward to " .. input .. "?",
123+
nil,
124+
function(confirm)
125+
if confirm then
126+
local port_forward_query = "port-forward pods/"
127+
.. pod_name
128+
.. " "
129+
.. input
130+
.. ":"
131+
.. current_port_result
132+
133+
commands.shell_command_async("kubectl", port_forward_query, function(response)
134+
vim.schedule(function()
135+
vim.notify(response)
136+
end)
137+
end)
138+
end
139+
end
140+
)
141+
end
142+
end)
143+
else
144+
api.nvim_err_writeln("Failed to select pod.")
145+
end
146+
end,
147+
})
148+
107149
if not loop.is_running() then
108150
loop.start_loop(pod_view.Pods)
109151
end

lua/kubectl/views/pods/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function M.Pods(cancellationToken)
1515
{ key = "<d>", desc = "describe" },
1616
{ key = "<t>", desc = "top" },
1717
{ key = "<enter>", desc = "containers" },
18+
{ key = "<shift-f>", desc = "port forward" },
1819
{ key = "<C-k>", desc = "kill pod" },
1920
}, true, true)
2021
:display("k8s_pods", "Pods", cancellationToken)

0 commit comments

Comments
 (0)