Skip to content

Commit

Permalink
Aging plugin example, minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianofranz committed Dec 16, 2016
1 parent a8a30a4 commit fd83635
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 10 deletions.
6 changes: 4 additions & 2 deletions pkg/kubectl/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, errOut io.Writer) *
{
Message: "Advanced Commands:",
Commands: []*cobra.Command{
NewCmdApply(f, out, err),
NewCmdApply(f, out, errOut),
NewCmdPatch(f, out),
NewCmdReplace(f, out),
NewCmdConvert(f, out),
Expand All @@ -307,7 +307,9 @@ func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, errOut io.Writer) *
if len(loadedPlugins) > 0 {
pluginCmds := []*cobra.Command{}

for _, plugin := range loadedPlugins {
for i := 0; i < len(loadedPlugins); i++ {
plugin := loadedPlugins[i]

pluginCmds = append(pluginCmds, &cobra.Command{
Use: plugin.Use,
Short: plugin.Short,
Expand Down
83 changes: 83 additions & 0 deletions pkg/kubectl/plugins/examples/kubectl-aging
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env ruby

require 'json'
require 'date'

class Numeric
def duration
secs = self.to_int
mins = secs / 60
hours = mins / 60
days = hours / 24
if days > 0
"#{days} day(s)"
elsif hours > 0
"#{hours} hour(s)"
elsif mins > 0
"#{mins} minute(s)"
elsif secs >= 0
"#{secs} second(s)"
end
end
end

if ARGV[0] == "metadata"
metadata = <<-META
{
"use" : "aging",
"short" : "Aging shows pods by age",
"long" : "Aging shows pods by age\\n\\nWhich shows pods by age which shows pods by age which shows pods by age which shows pods by age which shows pods by age which shows pods by age which shows pods by age which shows pods by age which shows pods by age which shows pods by age which shows pods by age which shows pods by age which shows pods by age which shows pods by age.",
"tunnel" : true
}
META
puts metadata
else
api_host = ENV['KUBECTL_PLUGIN_API_HOST']
api_endpoint = "http://#{api_host}/api/v1/namespaces/myproject/pods"

pods_json = `curl -s #{api_endpoint}`
pods_parsed = JSON.parse(pods_json)

puts "I'm the magnificent aging plugin."
puts ""

data = Hash.new
max_name_length = 0
max_age = 0
min_age = 0

pods_parsed['items'].each { |pod|
name = pod['metadata']['name']
creation = pod['metadata']['creationTimestamp']

age = Time.now - DateTime.parse(creation).to_time
data[name] = age

if name.length > max_name_length
max_name_length = name.length
end
if age > max_age
max_age = age
end
if age < min_age
min_age = age
end

}

data = data.sort_by{ |name, age| age }

if data.length
data.each { |name, age|
output = ""
output += name.rjust(max_name_length, ' ') + ": "
bar_size = (age*80/max_age).ceil
bar_size.times{ output += "▒" }
output += " " + age.duration
puts output
puts ""
}
else
puts "No pods"
end
end
8 changes: 1 addition & 7 deletions pkg/kubectl/plugins/examples/kubectl-foo
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@ if ARGV[0] == "metadata"
"use" : "foo",
"short" : "Foo plugin short description",
"long" : "Foo really LONG description\\n\\nCan take multi lines.",
"tunnel" : true
"tunnel" : false
}
META
puts metadata
else
api_host = ENV['KUBECTL_PLUGIN_API_HOST']
api_endpoint = "http://#{api_host}/api"

puts "I'm the magnificent foo plugin."
puts ""
puts "I can use the API from #{api_host}:"
puts `curl -s #{api_endpoint}`
end
2 changes: 1 addition & 1 deletion pkg/kubectl/plugins/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func ServePluginAPIProxy(clientConfig *restclient.Config) (net.Listener, error)
AcceptHosts: kubectl.MakeRegexpArrayOrDie(kubectl.DefaultHostAcceptRE),
}

server, err := kubectl.NewProxyServer("", "/api", "", filter, clientConfig)
server, err := kubectl.NewProxyServer("", "/", "", filter, clientConfig)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit fd83635

Please sign in to comment.