Skip to content
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

chore/ingress-additions #315

Merged
merged 19 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions ftplugin/k8s_ingresses.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local api = vim.api
local ResourceBuilder = require("kubectl.resourcebuilder")
local ingresses_view = require("kubectl.views.ingresses")
local loop = require("kubectl.utils.loop")
local overview_view = require("kubectl.views.overview")
Expand All @@ -13,6 +14,58 @@ local function set_keymap(bufnr)
overview_view.View()
end,
})
api.nvim_buf_set_keymap(bufnr, "n", "gx", "", {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this!

Copy link
Owner

@Ramilito Ramilito Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add a plug function like this:

api.nvim_buf_set_keymap(bufnr, "n",  "<Plug>(kubectl.browse)", "", {

noremap = true,
silent = true,
desc = "Open host in browser",
callback = function()
local name, ns = ingresses_view.getCurrentSelection()
ResourceBuilder:new("ingress_host")
:setCmd({ "get", "ingress", name, "-n", ns, "-o", "json" }, "kubectl")
:fetchAsync(function(self)
self:decodeJson()
local data = self.data

-- determine port
local port = ""
if
data.spec.rules
and data.spec.rules[1]
and data.spec.rules[1].http
and data.spec.rules[1].http.paths
and data.spec.rules[1].http.paths[1]
and data.spec.rules[1].http.paths[1].backend
then
local backend = data.spec.rules[1].http.paths[1].backend
port = backend.service.port.number or backend.servicePort or "80"
end

-- determine host
local host = ""
if data.spec.rules and data.spec.rules[1] and data.spec.rules[1].host then
host = data.spec.rules[1].host
else
if data.status and data.status.loadBalancer and data.status.loadBalancer.ingress then
local ingress = data.status.loadBalancer.ingress[1]
if ingress.hostname then
host = ingress.hostname
elseif ingress.ip then
host = ingress.ip
end
else
return
end
end
local proto = port == "443" and "https" or "http"
if port ~= "443" and port ~= "80" then
port = ":" .. port
end
local final_url = string.format("%s://%s%s", proto, host, port)
vim.notify("Opening " .. final_url)
vim.ui.open(final_url)
end)
end,
})
end

--- Initialize the module
Expand Down
14 changes: 13 additions & 1 deletion lua/kubectl/views/ingresses/definition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ local M = {
display_name = "Ingresses",
ft = "k8s_ingresses",
url = { "{{BASE}}/apis/networking.k8s.io/v1/{{NAMESPACE}}ingresses?pretty=false" },
hints = {
{ key = "<Plug>(kubectl.browse)", desc = "browse", long_desc = "Open host in browser" },
Copy link
Owner

@Ramilito Ramilito Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and also add a hint here ☝️ (did an edit on the file since I couldn't comment without any changes)

},
}

local function get_ports(row)
Expand All @@ -27,6 +30,9 @@ local function get_ports(row)
end

local function get_hosts(row)
if not row.spec or not row.spec.rules then
return ""
end
local hosts = {}

for _, rule in ipairs(row.spec.rules) do
Expand Down Expand Up @@ -55,6 +61,12 @@ local function get_address(row)
return table.concat(addresses, ", ")
end

local function get_class(row)
return row.spec.ingressClassName
or row.metadata.annotations and row.metadata.annotations["kubernetes.io/ingress.class"]
or ""
end

function M.processRow(rows)
local data = {}

Expand All @@ -69,7 +81,7 @@ function M.processRow(rows)
data[i] = {
namespace = row.metadata.namespace,
name = row.metadata.name,
class = row.spec.ingressClassName,
class = get_class(row),
hosts = get_hosts(row),
address = get_address(row),
ports = get_ports(row),
Expand Down
Loading