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.
23 24 25 |
# File 'lib/kubernetes/watch.rb', line 23 def initialize(client) @client = client end |
Instance Method Details
#connect(path, resource_version = nil, &_block) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/kubernetes/watch.rb', line 36 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
27 28 29 30 31 32 33 34 |
# File 'lib/kubernetes/watch.rb', line 27 def make_url(path, resource_version) uri = URI.parse(path) query = URI.decode_www_form(uri.query || '').to_h query['watch'] = 'true' query['resourceVersion'] = resource_version if resource_version query_string = query.map { |k, v| "#{URI.encode_www_form_component(k).gsub('+', '%20')}=#{URI.encode_www_form_component(v).gsub('+', '%20')}" }.join('&') "#{uri.path}?#{query_string}" end |
#split_lines(last, chunk) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/kubernetes/watch.rb', line 57 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 |