-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[k8s] Basic kubectl completion script (#113)
- Loading branch information
1 parent
50954bc
commit aa0541f
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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) |