kbe 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b9932bb5cf99190078cc9246a55236b0cf41b58b1a904688d2b5e644098f460
4
- data.tar.gz: ffb627ff3b746edd9368958d54e9eec3f99b6936667e1c9bb249cdf8f73c4656
3
+ metadata.gz: 6305d833ab3cf7f17e9e62a7a60c9cb767e19220f01cca2e2c24e5bd08a60cab
4
+ data.tar.gz: 21132300f9c022d1db52d8432aec27898ac4fd452bbd53a4b857b7abf78efd75
5
5
  SHA512:
6
- metadata.gz: d68c0f1fcf977575f9c013ec91f72f736818b9bca4af6dd0b4f74b3744b6094c2fb5ed724436b32791c566b37553552a1f5c039a889d19d05b2630fa324bc047
7
- data.tar.gz: c46a5629630f7341f45fba5d3cd1825304cbd3941ce882ddfaa729b700f1dde3178471757137272e007eabb3822f9d43541a51966d70f94647d65ab5bbcb6507
6
+ metadata.gz: c0b9ac3700f873bec682f6d2d4111837b9242c55cb96b9dd505673673aaf45b0cc1de111ff783e90cdc8623977891f3e4e216a07d7dbd53aec701cf1dd645d19
7
+ data.tar.gz: 8db51d7e138940dfe30e75ff11d681621bddc25c02af7956e67c5905b36f3311ede3bd710284803c18a60d0cc32f27e87853e5049be4173848abc117e8ad4b6d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kbe (0.1.4)
4
+ kbe (0.2.0)
5
5
  clamp
6
6
  colorize (~> 0.8, >= 0.8.1)
7
7
 
data/lib/kbe.rb CHANGED
@@ -12,27 +12,48 @@ module KBE
12
12
  exec cmd_string
13
13
  end
14
14
 
15
- def self.pod_by(selector)
16
- ["app", "job-name"].each do |selector_prefix|
17
- magic_selector = if selector.match? "="
18
- selector
19
- else
20
- "#{selector_prefix}=#{selector}"
21
- end
15
+ def self.pod_by(name_or_selector, only_running: true)
16
+ pods = if name_or_selector.match? "="
17
+ get_pods selector: name_or_selector, only_running: only_running
18
+ else
19
+ get_pods name: name_or_selector, only_running: only_running
20
+ end
22
21
 
23
- pods = `kubectl get pods --no-headers -o custom-columns=":metadata.name" --field-selector=status.phase=Running --selector=#{magic_selector}`.split("\n")
24
- return pods.first if pods.size == 1
22
+ if pods.empty?
23
+ ["app", "job-name"].each do |selector_prefix|
24
+ magic_selector = "#{selector_prefix}=#{name_or_selector}"
25
25
 
26
- break if magic_selector == selector
27
- next if pods.empty?
26
+ pods = get_pods selector: magic_selector, only_running: only_running
27
+ break if pods.any?
28
+ end
29
+ end
28
30
 
29
- STDERR.puts "too many pods"
30
- exit 1
31
+ if pods.size == 1
32
+ return pods.first
33
+ elsif pods.size > 1
34
+ STDERR.puts "too many pods found:"
35
+ STDERR.p pods
36
+ else
37
+ STDERR.puts "no pods found."
31
38
  end
32
39
 
33
- STDERR.puts "no pod found"
34
40
  exit 1
35
41
  end
42
+
43
+ def self.get_pods(selector:nil, name:nil, only_running: true)
44
+ parts = []
45
+ parts << "kubectl get pods --no-headers -o custom-columns=':metadata.name'"
46
+ if selector
47
+ parts << "--field-selector=status.phase=Running" if only_running
48
+ parts << "--selector=#{selector}"
49
+ else
50
+ parts << name
51
+ end
52
+
53
+ cmd = parts.join " "
54
+ # dev/null supresses the "pod not found" by name
55
+ `#{cmd} 2>/dev/null`.split("\n")
56
+ end
36
57
  end
37
58
 
38
59
  require_relative 'kbe/version'
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+ require "json"
3
+
4
+ module KBE
5
+ module CLI
6
+ class DeleteCommand < Clamp::Command
7
+ option ['-h', '--help'], :flag, "help" do
8
+ puts "kbe delete selector"
9
+ exit 0
10
+ end
11
+
12
+ parameter "SELECTOR_OR_POD", "selector or pod name"
13
+
14
+ def execute
15
+ pod = KBE.pod_by(selector_or_pod, only_running: false)
16
+ KBE.kubectl "delete pod #{pod}"
17
+ end
18
+ end
19
+ end
20
+ end
21
+
@@ -13,12 +13,12 @@ module KBE
13
13
 
14
14
  option ['-c', '--container'], 'CONTAINER', 'container', default: :first
15
15
 
16
- parameter "SELECTOR", "k8s selector, like app=nginx"
16
+ parameter "SELECTOR_OR_POD", "selector or pod name"
17
17
  parameter "[CMD] ...", "cmd"
18
18
 
19
19
  def execute
20
20
  cmd_list = ["sh"] unless cmd_list
21
- pod = KBE.pod_by selector
21
+ pod = KBE.pod_by selector_or_pod
22
22
 
23
23
  args = []
24
24
  args << "exec -it #{pod}"
@@ -10,10 +10,11 @@ module KBE
10
10
  exit 0
11
11
  end
12
12
 
13
- parameter "[POD]", "pod"
13
+ parameter "[POD]", "list containers from pod", attribute_name: :selector
14
14
 
15
15
  def execute
16
- if pod
16
+ if selector
17
+ pod = KBE.pod_by(selector, only_running: false)
17
18
  json = JSON.parse(`kubectl get pod #{pod} -o json`)
18
19
  for container in json["spec"]["containers"]
19
20
  puts container["name"]
@@ -21,6 +22,7 @@ module KBE
21
22
  else
22
23
  KBE.kubectl ["get pod"]
23
24
  end
25
+ rescue JSON::ParserError
24
26
  end
25
27
  end
26
28
  end
@@ -3,6 +3,7 @@ require_relative "version_command"
3
3
  require_relative "help_command"
4
4
  require_relative "enter_command"
5
5
  require_relative "list_command"
6
+ require_relative "delete_command"
6
7
 
7
8
  module KBE
8
9
  module CLI
@@ -15,6 +16,8 @@ module KBE
15
16
  end
16
17
  subcommand ["list"], "List pods", ListCommand
17
18
  subcommand ["enter"], "Enter a container", EnterCommand
19
+ subcommand ["delete"], "Delete a pod", DeleteCommand
20
+
18
21
  subcommand ["version"], "Show version information", VersionCommand
19
22
  subcommand ["help"], "Show help", HelpCommand
20
23
 
data/lib/kbe/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module KBE
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kbe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matti Paksula
@@ -110,6 +110,7 @@ files:
110
110
  - kbe.gemspec
111
111
  - lib/kbe.rb
112
112
  - lib/kbe/cli.rb
113
+ - lib/kbe/cli/delete_command.rb
113
114
  - lib/kbe/cli/enter_command.rb
114
115
  - lib/kbe/cli/help_command.rb
115
116
  - lib/kbe/cli/list_command.rb