Skip to content

Commit fd83635

Browse files
committed
Aging plugin example, minor fixes
1 parent a8a30a4 commit fd83635

File tree

4 files changed

+89
-10
lines changed

4 files changed

+89
-10
lines changed

pkg/kubectl/cmd/cmd.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, errOut io.Writer) *
283283
{
284284
Message: "Advanced Commands:",
285285
Commands: []*cobra.Command{
286-
NewCmdApply(f, out, err),
286+
NewCmdApply(f, out, errOut),
287287
NewCmdPatch(f, out),
288288
NewCmdReplace(f, out),
289289
NewCmdConvert(f, out),
@@ -307,7 +307,9 @@ func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, errOut io.Writer) *
307307
if len(loadedPlugins) > 0 {
308308
pluginCmds := []*cobra.Command{}
309309

310-
for _, plugin := range loadedPlugins {
310+
for i := 0; i < len(loadedPlugins); i++ {
311+
plugin := loadedPlugins[i]
312+
311313
pluginCmds = append(pluginCmds, &cobra.Command{
312314
Use: plugin.Use,
313315
Short: plugin.Short,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'json'
4+
require 'date'
5+
6+
class Numeric
7+
def duration
8+
secs = self.to_int
9+
mins = secs / 60
10+
hours = mins / 60
11+
days = hours / 24
12+
if days > 0
13+
"#{days} day(s)"
14+
elsif hours > 0
15+
"#{hours} hour(s)"
16+
elsif mins > 0
17+
"#{mins} minute(s)"
18+
elsif secs >= 0
19+
"#{secs} second(s)"
20+
end
21+
end
22+
end
23+
24+
if ARGV[0] == "metadata"
25+
metadata = <<-META
26+
{
27+
"use" : "aging",
28+
"short" : "Aging shows pods by age",
29+
"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.",
30+
"tunnel" : true
31+
}
32+
META
33+
puts metadata
34+
else
35+
api_host = ENV['KUBECTL_PLUGIN_API_HOST']
36+
api_endpoint = "http://#{api_host}/api/v1/namespaces/myproject/pods"
37+
38+
pods_json = `curl -s #{api_endpoint}`
39+
pods_parsed = JSON.parse(pods_json)
40+
41+
puts "I'm the magnificent aging plugin."
42+
puts ""
43+
44+
data = Hash.new
45+
max_name_length = 0
46+
max_age = 0
47+
min_age = 0
48+
49+
pods_parsed['items'].each { |pod|
50+
name = pod['metadata']['name']
51+
creation = pod['metadata']['creationTimestamp']
52+
53+
age = Time.now - DateTime.parse(creation).to_time
54+
data[name] = age
55+
56+
if name.length > max_name_length
57+
max_name_length = name.length
58+
end
59+
if age > max_age
60+
max_age = age
61+
end
62+
if age < min_age
63+
min_age = age
64+
end
65+
66+
}
67+
68+
data = data.sort_by{ |name, age| age }
69+
70+
if data.length
71+
data.each { |name, age|
72+
output = ""
73+
output += name.rjust(max_name_length, ' ') + ": "
74+
bar_size = (age*80/max_age).ceil
75+
bar_size.times{ output += "▒" }
76+
output += " " + age.duration
77+
puts output
78+
puts ""
79+
}
80+
else
81+
puts "No pods"
82+
end
83+
end

pkg/kubectl/plugins/examples/kubectl-foo

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@ if ARGV[0] == "metadata"
66
"use" : "foo",
77
"short" : "Foo plugin short description",
88
"long" : "Foo really LONG description\\n\\nCan take multi lines.",
9-
"tunnel" : true
9+
"tunnel" : false
1010
}
1111
META
1212
puts metadata
1313
else
14-
api_host = ENV['KUBECTL_PLUGIN_API_HOST']
15-
api_endpoint = "http://#{api_host}/api"
16-
1714
puts "I'm the magnificent foo plugin."
18-
puts ""
19-
puts "I can use the API from #{api_host}:"
20-
puts `curl -s #{api_endpoint}`
2115
end

pkg/kubectl/plugins/proxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func ServePluginAPIProxy(clientConfig *restclient.Config) (net.Listener, error)
3333
AcceptHosts: kubectl.MakeRegexpArrayOrDie(kubectl.DefaultHostAcceptRE),
3434
}
3535

36-
server, err := kubectl.NewProxyServer("", "/api", "", filter, clientConfig)
36+
server, err := kubectl.NewProxyServer("", "/", "", filter, clientConfig)
3737
if err != nil {
3838
return nil, err
3939
}

0 commit comments

Comments
 (0)