Class: K8sInternalLb::Client
- Inherits:
-
Object
- Object
- K8sInternalLb::Client
- Defined in:
- lib/k8s_internal_lb/client.rb
Constant Summary collapse
- TIMESTAMP_ANNOTATION =
'com.github.ananace.k8s-internal-lb/timestamp'
Instance Attribute Summary collapse
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#auth_options ⇒ Object
Returns the value of attribute auth_options.
-
#kubeclient_options ⇒ Object
Returns the value of attribute kubeclient_options.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#server ⇒ Object
Returns the value of attribute server.
-
#services ⇒ Object
readonly
Returns the value of attribute services.
-
#sleep_duration ⇒ Object
Returns the value of attribute sleep_duration.
-
#ssl_options ⇒ Object
Returns the value of attribute ssl_options.
Class Method Summary collapse
Instance Method Summary collapse
- #add_service(name, **data) ⇒ Object
- #in_cluster? ⇒ Boolean
- #remove_service(name) ⇒ Object
- #run ⇒ Object
Instance Attribute Details
#api_version ⇒ Object
Returns the value of attribute api_version.
9 10 11 |
# File 'lib/k8s_internal_lb/client.rb', line 9 def api_version @api_version end |
#auth_options ⇒ Object
Returns the value of attribute auth_options.
9 10 11 |
# File 'lib/k8s_internal_lb/client.rb', line 9 def end |
#kubeclient_options ⇒ Object
Returns the value of attribute kubeclient_options.
9 10 11 |
# File 'lib/k8s_internal_lb/client.rb', line 9 def end |
#namespace ⇒ Object
Returns the value of attribute namespace.
9 10 11 |
# File 'lib/k8s_internal_lb/client.rb', line 9 def namespace @namespace end |
#server ⇒ Object
Returns the value of attribute server.
9 10 11 |
# File 'lib/k8s_internal_lb/client.rb', line 9 def server @server end |
#services ⇒ Object (readonly)
Returns the value of attribute services.
11 12 13 |
# File 'lib/k8s_internal_lb/client.rb', line 11 def services @services end |
#sleep_duration ⇒ Object
Returns the value of attribute sleep_duration.
10 11 12 |
# File 'lib/k8s_internal_lb/client.rb', line 10 def sleep_duration @sleep_duration end |
#ssl_options ⇒ Object
Returns the value of attribute ssl_options.
9 10 11 |
# File 'lib/k8s_internal_lb/client.rb', line 9 def end |
Class Method Details
.instance ⇒ Object
13 14 15 |
# File 'lib/k8s_internal_lb/client.rb', line 13 def self.instance @instance ||= Client.new end |
Instance Method Details
#add_service(name, **data) ⇒ Object
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 |
# File 'lib/k8s_internal_lb/client.rb', line 22 def add_service(name, **data) service = nil if name.is_a? Service service = name name = service.name else data[:name] ||= name service = Service.create(**data) end k8s_service = get_endpoint(service) raise 'Unable to find service' if k8s_service.nil? if k8s_service.&.annotations&.to_hash&.key? TIMESTAMP_ANNOTATION ts = k8s_service.annotations[TIMESTAMP_ANNOTATION] if ts =~ /\A\d+\z/ service.last_update = Time.at(ts.to_i) else service.last_update = Time.parse(ts) end end @services[name] = service end |
#in_cluster? ⇒ Boolean
17 18 19 20 |
# File 'lib/k8s_internal_lb/client.rb', line 17 def in_cluster? # FIXME: Better detection, actually look for the necessary cluster components Dir.exist? '/var/run/secrets/kubernetes.io' end |
#remove_service(name) ⇒ Object
48 49 50 |
# File 'lib/k8s_internal_lb/client.rb', line 48 def remove_service(name) @services.delete name end |
#run ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/k8s_internal_lb/client.rb', line 52 def run loop do sleep_duration = @sleep_duration @services.each do |name, service| logger.debug "Checking #{name} for interval" diff = (Time.now - service.last_update) until_next = service.interval - diff sleep_duration = until_next if until_next.positive? && until_next < sleep_duration next unless diff >= service.interval logger.debug "Interval reached on #{name}, running update" update(service) end sleep sleep_duration end end |