Skip to content

Commit

Permalink
[k8s] Basic kubectl completion script (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmusfaber authored and vladimir-kotikov committed Nov 28, 2019
1 parent 50954bc commit aa0541f
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions kubectl.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
local w = require('tables').wrap
local parser = clink.arg.new_parser

local function exec_kubectl(arguments, template)
local f = io.popen("kubectl "..arguments.." -o template --template=\""..template.."\"")
if not f then return w({}) end
local output = f:read('*all')
f:close()
local res = w({})
for element in output:gmatch("%S+") do table.insert(res, element) end
return res
end

local function get_config(config)
return exec_kubectl("config view", "{{ range ."..config.." }}{{ .name }} {{ end }}")
end

local function get_config_func(config)
return function()
return get_config(config)
end
end

local function get_resources(noun)
return exec_kubectl("get "..noun, "{{ range .items }}{{ .metadata.name }} {{ end }}")
end

local function get_resources_func(noun)
return function()
return get_resources(noun)
end
end

local resource_parser = parser(
{
"all" .. parser({get_resources_func("all")}),
"node" .. parser({get_resources_func("node")}),
"service" .. parser({get_resources_func("service")}),
"pod" .. parser({get_resources_func("pod")}),
"deployment" .. parser({get_resources_func("deployment")})
}
)

local scale_parser = parser(
{
"deployment" .. parser({get_resources_func("deployment")}, parser({"--replicas"}))
}
)

local config_parser = parser(
{
"current-context",
"delete-cluster",
"delete-context",
"get-clusters",
"get-contexts",
"rename-context",
"set",
"set-cluster",
"set-context",
"set-credentials",
"unset",
"use-context" .. parser({get_config_func("contexts")}),
"view"
}
)

local kubectl_parser = parser(
{
"apply",
"exec" .. parser({get_resources_func("pod")}, parser({ "-it"})),
"get" .. resource_parser,
"describe" .. resource_parser,
"logs" .. parser({get_resources_func("pod")}),
"port-forward" .. parser({get_resources_func("pod")}),
"scale" .. scale_parser,
"config" .. config_parser
}
)

clink.arg.register_parser("kubectl", kubectl_parser)

0 comments on commit aa0541f

Please sign in to comment.