kubecontrol 0.1.1 → 0.1.2

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: 65dc046ec4edab7fe2699ece898e666aa7102bf6c4038a633bee75c7c4920b46
4
- data.tar.gz: 3ef2d900864e590b16c0dcf573bea8ef00bc0327e1b7a2ca435a3d1f1448c86b
3
+ metadata.gz: 78e24714ab4e03f425c86d2391a2952e34acacda235c312dcd66e49632c9b9a9
4
+ data.tar.gz: b3e7aac86f9053f4bb266f8701a1f1b756a98011ba43f1b3e398a99f342982ea
5
5
  SHA512:
6
- metadata.gz: 367b418c7e904dfc42960ec3ae3fa4bc8584dc3997e0ebbd45b1c4f05650d8c3e1529b2c0c832a484e9cd1571332a9e831f7de0c6c3e0a20894b0c220d651352
7
- data.tar.gz: dc448d8c8dc75dc0a3d38c17a45ed856a2a89f3f64daf55160fef1e38b506d89a6ff84cf5593936a626e0202484372427b931d9c27502a7375d421c7f2a6e1ba
6
+ metadata.gz: b3fb70722607a2c54b322f13c49e5960fcc428f34e9682af831f4a006ae3863196b648c174fe92ab09317ba9013dd0204de523b35cf85cf3fe9006fa647e36da
7
+ data.tar.gz: c741103c7f8f6797706f6156368b8bbef588fe8c1876f4fd44ec639b830e44d655256d3fa67a410c2ebd9374588560f86bb0c1efc21d8a3961a9e02814ac21a4
@@ -1,3 +1,6 @@
1
+ # v0.1.2 / 12-24-2019
2
+ - [FEATURE] Kubecontrol::Pod#exec
3
+
1
4
  # v0.1.0 / 12-20-2019
2
5
  - [FEATURE] Initial gem creation
3
6
  - [FEATURE] Kubecontrol::Client
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kubecontrol (0.1.1)
4
+ kubecontrol (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -38,6 +38,10 @@ pod.ready
38
38
  pod.status
39
39
  pod.restarts
40
40
  pod.age
41
+ pod.namespace
42
+
43
+ #exec commands on a pod
44
+ std_out, std_err, exit_code = pod.exec('ls')
41
45
  ```
42
46
 
43
47
  ## Development
@@ -1,3 +1,4 @@
1
+ require 'open3'
1
2
  require_relative 'pod'
2
3
 
3
4
  module Kubecontrol
@@ -11,13 +12,13 @@ module Kubecontrol
11
12
  end
12
13
 
13
14
  def pods
14
- get_pods_result = kubectl_command('get pods')
15
+ get_pods_result, _stderr, _exit_code = kubectl_command('get pods')
15
16
  return [] if get_pods_result.empty?
16
17
 
17
18
  pods_array = get_pods_result.split
18
19
  pods_array.shift 5 # remove output table headers
19
20
  pods_array.each_slice(5).map do |pod_data|
20
- Pod.new(*pod_data)
21
+ Pod.new(*pod_data, namespace: namespace, client: self)
21
22
  end
22
23
  end
23
24
 
@@ -25,10 +26,11 @@ module Kubecontrol
25
26
  pods.find { |pod| pod.name.match?(name_regex) }
26
27
  end
27
28
 
28
- private
29
-
30
29
  def kubectl_command(command)
31
- `kubectl -n #{namespace} #{command}`
30
+ stdout_data, stderr_data, status = Open3.capture3("kubectl -n #{namespace} #{command}")
31
+ exit_code = status.exitstatus
32
+
33
+ [stdout_data, stderr_data, exit_code]
32
34
  end
33
35
  end
34
36
  end
@@ -2,14 +2,16 @@ module Kubecontrol
2
2
  class Pod
3
3
  RUNNING = 'Running'.freeze
4
4
 
5
- attr_reader :name, :ready, :status, :restarts, :age
5
+ attr_reader :name, :ready, :status, :restarts, :age, :namespace, :client
6
6
 
7
- def initialize(name, ready, status, restarts, age)
7
+ def initialize(name, ready, status, restarts, age, namespace:, client:)
8
8
  @name = name
9
9
  @ready = ready
10
10
  @status = status
11
11
  @restarts = restarts
12
12
  @age = age
13
+ @namespace = namespace
14
+ @client = client
13
15
  end
14
16
 
15
17
  def stopped?
@@ -19,5 +21,10 @@ module Kubecontrol
19
21
  def running?
20
22
  @status == RUNNING
21
23
  end
24
+
25
+ def exec(command)
26
+ stdout_data, stderr_data, exit_code = @client.kubectl_command("exec -i #{name} -- sh -c \"#{command.gsub('"', '\"')}\"")
27
+ [stdout_data, stderr_data, exit_code]
28
+ end
22
29
  end
23
30
  end
@@ -1,3 +1,3 @@
1
1
  module Kubecontrol
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.1.2'.freeze
3
3
  end
@@ -7,12 +7,19 @@ RSpec.describe Kubecontrol::Client do
7
7
  let(:pod_status) { 'Running' }
8
8
  let(:pod_restarts) { '0' }
9
9
  let(:pod_age) { '20d' }
10
- let(:get_pods_response) do
10
+
11
+ let(:get_pods_std_out) do
11
12
  <<~RUBY
12
13
  NAME READY STATUS RESTARTS AGE
13
14
  #{pod_name} #{pod_ready} #{pod_status} #{pod_restarts} #{pod_age}
14
15
  RUBY
15
16
  end
17
+ let(:get_pods_std_err) { '' }
18
+ let(:process_status) do
19
+ fork { exit }
20
+ $CHILD_STATUS
21
+ end
22
+ let(:get_pods_response) { [get_pods_std_out, get_pods_std_err, process_status] }
16
23
 
17
24
  describe '#initialize' do
18
25
  subject { Kubecontrol::Client }
@@ -47,12 +54,12 @@ RSpec.describe Kubecontrol::Client do
47
54
  subject { Kubecontrol::Client.new.pods }
48
55
 
49
56
  it 'send a kubectl request to the command line' do
50
- expect_any_instance_of(Kubecontrol::Client).to receive(:`).with('kubectl -n default get pods').and_return ''
57
+ expect(Open3).to receive(:capture3).with('kubectl -n default get pods').and_return get_pods_response
51
58
  subject
52
59
  end
53
60
 
54
61
  it 'returns an array of Kubecontrol::Pods' do
55
- allow_any_instance_of(Kubecontrol::Client).to receive(:`).and_return get_pods_response
62
+ allow(Open3).to receive(:capture3).and_return get_pods_response
56
63
  result = subject
57
64
  expect(result).to be_an_instance_of Array
58
65
  expect(result.length).to eq 1
@@ -60,8 +67,10 @@ RSpec.describe Kubecontrol::Client do
60
67
  end
61
68
 
62
69
  context 'no pods found' do
70
+ let(:get_pods_std_out) { '' }
71
+
63
72
  before do
64
- allow_any_instance_of(Kubecontrol::Client).to receive(:`).and_return ''
73
+ allow(Open3).to receive(:capture3).and_return get_pods_response
65
74
  end
66
75
 
67
76
  it { is_expected.to be_empty }
@@ -72,7 +81,7 @@ RSpec.describe Kubecontrol::Client do
72
81
  subject { Kubecontrol::Client.new.find_pod_by_name(pod_name) }
73
82
 
74
83
  before do
75
- allow_any_instance_of(Kubecontrol::Client).to receive(:`).and_return get_pods_response
84
+ allow(Open3).to receive(:capture3).and_return get_pods_response
76
85
  end
77
86
 
78
87
  it { is_expected.to be_an_instance_of Kubecontrol::Pod }
@@ -82,9 +91,7 @@ RSpec.describe Kubecontrol::Client do
82
91
  end
83
92
 
84
93
  context 'pod does not exist' do
85
- before do
86
- allow_any_instance_of(Kubecontrol::Client).to receive(:`).and_return ''
87
- end
94
+ let(:get_pods_std_out) { '' }
88
95
 
89
96
  it { is_expected.to be_nil }
90
97
  end
@@ -6,9 +6,11 @@ RSpec.describe Kubecontrol::Pod do
6
6
  let(:pod_status) { 'Running' }
7
7
  let(:pod_restarts) { '0' }
8
8
  let(:pod_age) { '20d' }
9
+ let(:namespace) { 'default' }
10
+ let(:client) { Kubecontrol::Client.new }
9
11
 
10
12
  describe '#initialize' do
11
- subject { Kubecontrol::Pod.new(pod_name, pod_ready, pod_status, pod_restarts, pod_age) }
13
+ subject { Kubecontrol::Pod.new(pod_name, pod_ready, pod_status, pod_restarts, pod_age, namespace: namespace, client: client) }
12
14
 
13
15
  it 'sets the pod name field' do
14
16
  expect(subject.name).to eq pod_name
@@ -29,6 +31,14 @@ RSpec.describe Kubecontrol::Pod do
29
31
  it 'sets the pod age field' do
30
32
  expect(subject.age).to eq pod_age
31
33
  end
34
+
35
+ it 'sets the pod namespace' do
36
+ expect(subject.namespace).to eq namespace
37
+ end
38
+
39
+ it 'sets the client' do
40
+ expect(subject.client).to eq client
41
+ end
32
42
  end
33
43
 
34
44
  describe '#running?' do
@@ -58,4 +68,29 @@ RSpec.describe Kubecontrol::Pod do
58
68
  it { is_expected.to eq true }
59
69
  end
60
70
  end
71
+
72
+ describe '#exec' do
73
+ let(:command) { 'ls' }
74
+ let(:kubectl_command) { "exec -i #{pod.name} -- sh -c \"#{command.gsub('"', '\"')}\"" }
75
+ let(:std_out) { "bin\ndev\netc\nhome\nlib" }
76
+ let(:std_err) { '' }
77
+ let(:status_code) { 0 }
78
+ let(:kubectl_command_response) { [std_out, std_err, status_code] }
79
+ let(:pod) { Kubecontrol::Pod.new(pod_name, pod_ready, pod_status, pod_restarts, pod_age, namespace: namespace, client: client) }
80
+
81
+ subject { pod.exec(command) }
82
+
83
+ it 'sends the exec command via Kubecontrol::Client#kubectl_command' do
84
+ expect(pod.client).to receive(:kubectl_command).with(kubectl_command).and_return kubectl_command_response
85
+ subject
86
+ end
87
+
88
+ it 'returns an array of std_out, std_err, and status code' do
89
+ allow(pod.client).to receive(:kubectl_command).with(kubectl_command).and_return kubectl_command_response
90
+ std_out_response, std_err_response, status_code_response = subject
91
+ expect(std_out_response).to eq std_out
92
+ expect(std_err_response).to eq std_err
93
+ expect(status_code_response).to eq status_code
94
+ end
95
+ end
61
96
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kubecontrol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Adkins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-23 00:00:00.000000000 Z
11
+ date: 2019-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler