Skip to content

Commit a8363be

Browse files
authored
Merge pull request kubernetes-client#45 from brendandburns/watch2
Add support for watch @ resourceVersion, and improve tests.
2 parents ec67474 + f0309ec commit a8363be

File tree

10 files changed

+81
-11
lines changed

10 files changed

+81
-11
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ gemfile: kubernetes/Gemfile
22
rvm:
33
- 2.6
44

5+
before_install:
6+
- gem update --system
7+
- gem install bundler
8+
59
script:
610
- cd kubernetes
711
- bundle exec rubocop ./src

kubernetes/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ gemspec
55
group :development, :test do
66
gem 'rake', '~> 12.0.0'
77
gem 'rubocop', '~> 0.65.0'
8+
gem 'webmock', '~> 3.5.1'
89
end

kubernetes/Gemfile.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ GEM
2424
ethon (0.12.0)
2525
ffi (>= 1.3.0)
2626
ffi (1.10.0)
27-
hashdiff (0.3.8)
27+
hashdiff (0.3.9)
2828
jaro_winkler (1.5.2)
2929
json (2.1.0)
3030
parallel (1.14.0)
@@ -58,14 +58,14 @@ GEM
5858
ruby-progressbar (~> 1.7)
5959
unicode-display_width (~> 1.4.0)
6060
ruby-progressbar (1.10.0)
61-
safe_yaml (1.0.4)
61+
safe_yaml (1.0.5)
6262
sys-uname (1.0.4)
6363
ffi (>= 1.0.0)
6464
typhoeus (1.3.1)
6565
ethon (>= 0.9.0)
6666
unicode-display_width (1.4.1)
6767
vcr (3.0.3)
68-
webmock (1.24.6)
68+
webmock (3.5.1)
6969
addressable (>= 2.3.6)
7070
crack (>= 0.3.2)
7171
hashdiff
@@ -83,7 +83,7 @@ DEPENDENCIES
8383
rspec (~> 3.6, >= 3.6.0)
8484
rubocop (~> 0.65.0)
8585
vcr (~> 3.0, >= 3.0.1)
86-
webmock (~> 1.24, >= 1.24.3)
86+
webmock (~> 3.5.1)
8787

8888
BUNDLED WITH
89-
1.16.1
89+
2.0.1

kubernetes/spec/.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Metrics/BlockLength:
2+
Max: 99

kubernetes/spec/spec_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
require 'kubernetes'
1313
require 'helpers/file_fixtures'
1414

15+
require 'webmock/rspec'
16+
WebMock.disable_net_connect!(allow_localhost: true)
17+
1518
# The following was generated by the `rspec --init` command. Conventionally,
1619
# all specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
1720
# The generated `.rspec` file contains `--require spec_helper` which will cause

kubernetes/spec/utils_spec.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
require 'helpers/file_fixtures'
1919

2020
require 'kubernetes/utils'
21-
22-
# rubocop:disable BlockLength
2321
describe Kubernetes do
2422
describe '.load_incluster_config' do
2523
let(:incluster_config) do
@@ -115,4 +113,3 @@
115113
end
116114
end
117115
end
118-
# rubocop:enable BlockLength

kubernetes/spec/watch_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,60 @@
2020
Kubernetes::Watch.new(nil)
2121
end
2222

23+
it 'should connect correctly with resource version' do
24+
config = Kubernetes::Configuration.new
25+
config.scheme = 'http'
26+
config.host = 'k8s.io:8080'
27+
client = Kubernetes::ApiClient.new(config)
28+
url = 'http://k8s.io:8080/some/path?watch=true&resourceVersion=foo'
29+
30+
WebMock.stub_request(:get, url)
31+
.with(
32+
headers: {
33+
'Authorization' => '',
34+
'Content-Type' => 'application/json',
35+
'Expect' => '',
36+
'User-Agent' => 'Swagger-Codegen/1.0.0-alpha2/ruby'
37+
}
38+
)
39+
.to_return(status: 200, body: "{}\n", headers: {})
40+
41+
watch = Kubernetes::Watch.new(client)
42+
result = []
43+
watch.connect('/some/path', 'foo') do |obj|
44+
result << obj
45+
end
46+
end
47+
48+
it 'should connect correctly' do
49+
config = Kubernetes::Configuration.new
50+
config.scheme = 'http'
51+
config.host = 'k8s.io:8080'
52+
client = Kubernetes::ApiClient.new(config)
53+
body = "{ \"foo\": \"bar\" }\n{ \"baz\": \"blah\" }\n{}\n"
54+
55+
WebMock.stub_request(:get, 'http://k8s.io:8080/some/path?watch=true')
56+
.with(
57+
headers: {
58+
'Authorization' => '',
59+
'Content-Type' => 'application/json',
60+
'Expect' => '',
61+
'User-Agent' => 'Swagger-Codegen/1.0.0-alpha2/ruby'
62+
}
63+
)
64+
.to_return(status: 200, body: body, headers: {})
65+
66+
watch = Kubernetes::Watch.new(client)
67+
result = []
68+
watch.connect('/some/path', nil) do |obj|
69+
result << obj
70+
end
71+
72+
expect(result.length).to eq(3)
73+
expect(result[0]['foo']).to eq('bar')
74+
expect(result[1]['baz']).to eq('blah')
75+
end
76+
2377
it 'should parse chunks correctly' do
2478
client = Kubernetes::Watch.new(nil)
2579
last = ''
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Metrics/MethodLength:
2+
Max: 50

kubernetes/src/kubernetes/config/kube_config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def list_context_names(config_file = KUBE_CONFIG_DEFAULT_LOCATION)
4040
attr_accessor :path
4141
attr_writer :config
4242

43-
def initialize(path, config_hash = nil)
43+
def initialize(path = nil, config_hash = nil)
4444
@path = path
4545
@config = config_hash
4646
end

kubernetes/src/kubernetes/watch.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@ def initialize(client)
2323
@client = client
2424
end
2525

26-
def connect(path, &_block)
26+
def make_url(path, resource_version)
27+
query = '?watch=true'
28+
query += "&resourceVersion=#{resource_version}" if resource_version
29+
path + query
30+
end
31+
32+
def connect(path, resource_version = nil, &_block)
2733
opts = { auth_names: ['BearerToken'] }
28-
request = @client.build_request('GET', path + '?watch=true', opts)
34+
url = make_url(path, resource_version)
35+
request = @client.build_request('GET', url, opts)
2936
last = ''
3037
request.on_body do |chunk|
3138
last, pieces = split_lines(last, chunk)

0 commit comments

Comments
 (0)