Class: K8sInternalLb::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/k8s_internal_lb/client.rb

Constant Summary collapse

TIMESTAMP_ANNOTATION =
'com.github.ananace.k8s-internal-lb/timestamp'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_versionObject

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_optionsObject

Returns the value of attribute auth_options.



9
10
11
# File 'lib/k8s_internal_lb/client.rb', line 9

def auth_options
  @auth_options
end

#kubeclient_optionsObject

Returns the value of attribute kubeclient_options.



9
10
11
# File 'lib/k8s_internal_lb/client.rb', line 9

def kubeclient_options
  @kubeclient_options
end

#namespaceObject

Returns the value of attribute namespace.



9
10
11
# File 'lib/k8s_internal_lb/client.rb', line 9

def namespace
  @namespace
end

#serverObject

Returns the value of attribute server.



9
10
11
# File 'lib/k8s_internal_lb/client.rb', line 9

def server
  @server
end

#servicesObject (readonly)

Returns the value of attribute services.



11
12
13
# File 'lib/k8s_internal_lb/client.rb', line 11

def services
  @services
end

#sleep_durationObject

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_optionsObject

Returns the value of attribute ssl_options.



9
10
11
# File 'lib/k8s_internal_lb/client.rb', line 9

def ssl_options
  @ssl_options
end

Class Method Details

.instanceObject



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

Returns:

  • (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

#runObject



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