Class: Kubernetes::Watch
- Inherits:
-
Object
- Object
- Kubernetes::Watch
- Defined in:
- lib/kubernetes/watch.rb
Overview
The Watch class provides the ability to watch a specific resource for updates.
Instance Method Summary collapse
- #connect(path, resource_version = nil, &_block) ⇒ Object
-
#initialize(client) ⇒ Watch
constructor
A new instance of Watch.
- #make_url(path, resource_version) ⇒ Object
- #split_lines(last, chunk) ⇒ Object
Constructor Details
#initialize(client) ⇒ Watch
Returns a new instance of Watch.
22 23 24 |
# File 'lib/kubernetes/watch.rb', line 22 def initialize(client) @client = client end |
Instance Method Details
#connect(path, resource_version = nil, &_block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/kubernetes/watch.rb', line 32 def connect(path, resource_version = nil, &_block) opts = { auth_names: ['BearerToken'] } url = make_url(path, resource_version) request = @client.build_request('GET', url, opts) last = '' request.on_body do |chunk| last, pieces = split_lines(last, chunk) pieces.each do |part| begin event = JSON.parse(part) rescue JSON::ParserError => e warn "Failed to parse watch event: #{e.}. Raw event: #{part.inspect}" next end yield event end end request.run end |
#make_url(path, resource_version) ⇒ Object
26 27 28 29 30 |
# File 'lib/kubernetes/watch.rb', line 26 def make_url(path, resource_version) query = '?watch=true' query += "&resourceVersion=#{resource_version}" if resource_version path + query end |
#split_lines(last, chunk) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/kubernetes/watch.rb', line 53 def split_lines(last, chunk) data = chunk data = last + '' + data ix = data.rindex("\n") return [data, []] unless ix complete = data[0..ix] last = data[(ix + 1)..data.length] [last, complete.split(/\n/)] end |