Skip to content

Commit

Permalink
refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramilito committed Oct 8, 2024
1 parent 262336c commit 802ac5f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 57 deletions.
3 changes: 0 additions & 3 deletions lua/kubectl/actions/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ function M.await_shell_command_async(cmds, callback)
local start_command
local on_command_done

vim.print("Total: ", #cmds)
-- Function to start a command
start_command = function(i)
local cmd = cmds[i]
Expand All @@ -120,8 +119,6 @@ function M.await_shell_command_async(cmds, callback)
done_count = done_count + 1
active_count = active_count - 1 -- Decrement the count of active commands

vim.print("Done: ", done_count)

-- Start the next command if any are left
if next_cmd_index <= #cmds then
start_command(next_cmd_index)
Expand Down
94 changes: 41 additions & 53 deletions lua/kubectl/views/lineage/definition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,89 +4,81 @@ local M = {
ft = "k8s_lineage",
}

-- Function to collect all resources from the data sample
local function get_kind(resource, default_kind)
return (resource.kind and resource.kind:lower()) or (default_kind and default_kind:lower()) or "unknownkind"
end

function M.collect_all_resources(data_sample)
local resources = {}
for kind_key, resource_group in pairs(data_sample) do
if resource_group.data then
-- Extract resource instances from the 'data' field
for _, resource in ipairs(resource_group.data) do
resource.kind = resource.kind and resource.kind:lower()
or resource_group.kind and resource_group.kind:lower()
or kind_key:lower()
resource.kind = get_kind(resource, resource_group.kind or kind_key)
table.insert(resources, resource)
end
end
end
return resources
end

-- Function to create a unique key for resources
function M.get_resource_key(resource)
local ns_part = resource.ns or "cluster"
local kind = resource.kind and resource.kind:lower() or "unknownkind"
return kind .. "/" .. ns_part .. "/" .. resource.name
local ns = resource.ns or "cluster"
local kind = get_kind(resource)
return string.format("%s/%s/%s", kind, ns, resource.name)
end

local function init_graph_node(graph, resource)
local key = M.get_resource_key(resource)
if not graph[key] then
graph[key] = { resource = resource, neighbors = {} }
end
return graph[key]
end

local function add_bidirectional_edge(node, neighbor)
table.insert(node.neighbors, neighbor)
table.insert(neighbor.neighbors, node)
end

-- Function to build a graph of resources
function M.build_graph(resources)
local graph = {}

-- First pass: map keys to resources and initialize graph nodes
-- First pass: initialize graph nodes
for _, resource in ipairs(resources) do
if resource.name then
resource.kind = resource.kind and resource.kind:lower() or "unknownkind"
local resource_key = M.get_resource_key(resource)
graph[resource_key] = { resource = resource, neighbors = {} }
init_graph_node(graph, resource)
end
end

-- Second pass: build ownership edges
for _, resource in ipairs(resources) do
if resource.name then
local resource_key = M.get_resource_key(resource)
local node = graph[resource_key]
if resource.owners then
for _, owner in ipairs(resource.owners) do
owner.kind = owner.kind and owner.kind:lower() or "unknownkind"
owner.ns = owner.ns or resource.ns -- Assume same namespace if not specified
local owner_key = M.get_resource_key(owner)
if not graph[owner_key] then
graph[owner_key] = { resource = owner, neighbors = {} }
end
-- Add bidirectional edge
table.insert(node.neighbors, graph[owner_key])
table.insert(graph[owner_key].neighbors, node)
end
if resource.owners then
local node = init_graph_node(graph, resource)
for _, owner in ipairs(resource.owners) do
owner.kind = get_kind(owner)
owner.ns = owner.ns or resource.ns
local owner_node = init_graph_node(graph, owner)
add_bidirectional_edge(node, owner_node)
end
end
end

-- Third pass: build label selector edges
for _, resource in ipairs(resources) do
if resource.name and resource.selectors then
local resource_key = M.get_resource_key(resource)
local node = graph[resource_key]

-- Match resources by label selectors
if resource.selectors then
local node = init_graph_node(graph, resource)
for _, potential_child in ipairs(resources) do
local match = true
if potential_child.labels then
local is_match = true
-- Check if labels match the selector
for key, value in pairs(resource.selectors) do
if potential_child.labels[key] ~= value then
is_match = false
match = false
break
end
end
if is_match then
local child_key = M.get_resource_key(potential_child)
if not graph[child_key] then
graph[child_key] = { resource = potential_child, neighbors = {} }
end
-- Add bidirectional edge (resource -> child and child -> resource)
table.insert(node.neighbors, graph[child_key])
table.insert(graph[child_key].neighbors, node)
if match then
local child_node = init_graph_node(graph, potential_child)
add_bidirectional_edge(node, child_node)
end
end
end
Expand All @@ -98,9 +90,7 @@ end

-- Function to find associated resources using BFS traversal
function M.find_associated_resources(graph, start_key)
local visited = {}
local queue = {}
local associated_resources = {}
local visited, queue, associated_resources = {}, {}, {}

if not graph[start_key] then
print("Selected resource not found in the graph.")
Expand All @@ -112,18 +102,16 @@ function M.find_associated_resources(graph, start_key)

while #queue > 0 do
local current = table.remove(queue, 1)
local current_key = current.key
local level = current.level
local node = graph[current_key]
local node = graph[current.key]

if node then
node.resource.level = level
node.resource.level = current.level
table.insert(associated_resources, node.resource)
for _, neighbor in ipairs(node.neighbors) do
local neighbor_key = M.get_resource_key(neighbor.resource)
if not visited[neighbor_key] then
visited[neighbor_key] = true
table.insert(queue, { key = neighbor_key, level = level + 1 })
table.insert(queue, { key = neighbor_key, level = current.level + 1 })
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lua/kubectl/views/lineage/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function M.View(name, ns, kind)
local associated_resources = definition.find_associated_resources(graph, selected_key)

for _, res in ipairs(associated_resources) do
table.insert(builder.data, string.rep(" ", res.level) .. "- " .. res.kind .. ": " .. res.ns .. "/" .. res.name)
table.insert(builder.data, string.rep(" ", res.level) .. "- " .. res.kind .. ": " .. (res.ns or "") .. "/" .. res.name)
end
end

Expand Down

0 comments on commit 802ac5f

Please sign in to comment.