blob: ef676660d5396fc2cfb72959155eec2e65de14f5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
require 'json'
require 'http'
module Kubeclient
module Common
# HTTP Stream used to watch changes on entities
class WatchStream
def initialize(uri, http_options, formatter:)
@uri = uri
@http_client = nil
@http_options = http_options
@http_options[:http_max_redirects] ||= Kubeclient::Client::DEFAULT_HTTP_MAX_REDIRECTS
@formatter = formatter
end
def each
@finished = false
@http_client = build_client
response = @http_client.request(:get, @uri, build_client_options)
unless response.code < 300
raise Kubeclient::HttpError.new(response.code, response.reason, response)
end
buffer = ''
response.body.each do |chunk|
buffer << chunk
while (line = buffer.slice!(/.+\n/))
yield @formatter.call(line.chomp)
end
end
rescue StandardError
raise unless @finished
end
def finish
@finished = true
@http_client.close unless @http_client.nil?
end
private
def max_hops
@http_options[:http_max_redirects] + 1
end
def follow_option
if max_hops > 1
{ max_hops: max_hops }
else
# i.e. Do not follow redirects as we have set http_max_redirects to 0
# Setting `{ max_hops: 1 }` does not work FWIW
false
end
end
def build_client
client = HTTP::Client.new(follow: follow_option)
if @http_options[:basic_auth_user] && @http_options[:basic_auth_password]
client = client.basic_auth(
user: @http_options[:basic_auth_user],
pass: @http_options[:basic_auth_password]
)
end
client
end
def using_proxy
proxy = @http_options[:http_proxy_uri]
return nil unless proxy
p_uri = URI.parse(proxy)
{
proxy_address: p_uri.hostname,
proxy_port: p_uri.port,
proxy_username: p_uri.user,
proxy_password: p_uri.password
}
end
def build_client_options
client_options = {
headers: @http_options[:headers],
proxy: using_proxy
}
if @http_options[:ssl]
client_options[:ssl] = @http_options[:ssl]
socket_option = :ssl_socket_class
else
socket_option = :socket_class
end
client_options[socket_option] = @http_options[socket_option] if @http_options[socket_option]
client_options
end
end
end
end
|