Class: K8sInternalLb::Services::HTTP

Inherits:
K8sInternalLb::Service show all
Defined in:
lib/k8s_internal_lb/services/http.rb

Instance Attribute Summary collapse

Attributes inherited from K8sInternalLb::Service

#endpoints, #interval, #last_update, #name, #namespace

Instance Method Summary collapse

Methods inherited from K8sInternalLb::Service

create, #logger, #to_subsets

Constructor Details

#initialize(addresses:, method: :head, expects: :success, timeout: 5, http_opts: {}, **params) ⇒ HTTP

Returns a new instance of HTTP.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/k8s_internal_lb/services/http.rb', line 11

def initialize(addresses:, method: :head, expects: :success, timeout: 5, http_opts: {}, **params)
  params[:ports] ||= []
  super

  self.method = method
  self.expects = expects
  self.addresses = addresses

  @timeout = timeout
  @http_opts = http_opts

  @address_hash = nil
  @port_hash = nil
end

Instance Attribute Details

#addressesObject

Returns the value of attribute addresses.



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

def addresses
  @addresses
end

#expectsObject

Returns the value of attribute expects.



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

def expects
  @expects
end

#http_optsObject

Returns the value of attribute http_opts.



8
9
10
# File 'lib/k8s_internal_lb/services/http.rb', line 8

def http_opts
  @http_opts
end

#methodObject

Returns the value of attribute method.



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

def method
  @method
end

#timeoutObject

Returns the value of attribute timeout.



8
9
10
# File 'lib/k8s_internal_lb/services/http.rb', line 8

def timeout
  @timeout
end

Instance Method Details

#portsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/k8s_internal_lb/services/http.rb', line 26

def ports
  # Ensure data is recalculated if addresses or ports change
  address_hash = @addresses.hash
  port_hash = super.hash
  @http_ports = nil if @address_hash != address_hash
  @http_ports = nil if @port_hash != port_hash
  @address_hash = address_hash
  @port_hash = port_hash

  @http_ports ||= begin
    http_ports = @addresses.map { |addr| Port.new(port: addr.port) }.uniq
  
    # Copy port names over where appropriate
    super.each do |port|
      http_port = http_ports.find { |hp| hp.port == port.port }
      next unless http_port

      http_port.name = port.name
    end

    http_ports
  end
end

#updateObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/k8s_internal_lb/services/http.rb', line 74

def update
  @endpoints = addresses.map do |addr|
    available = false

    begin
      ssl = addr.scheme == 'https'

      Net::HTTP.start(addr.host, addr.port, use_ssl: ssl, read_timeout: timeout, **http_opts) do |h|
        resp = h.send(@method, addr.path)
        logger.debug "#{addr} - #{resp.inspect}"

        available = if @expects == :success
                      resp.is_a? Net::HTTPSuccess
                    elsif @expects.is_a? Numeric
                      resp.code == @expects
                    elsif @expects.is_a? Proc
                      @expects.call(resp)
                    end
      end
    rescue StandardError => e
      logger.debug "#{addr} - #{e.class}: #{e.message}\n#{e.backtrace[0, 20].join("\n")}"
      available = false # Assume failures to mean inaccessibility
    end

    e_addr = Address.new fqdn: addr.host
    Endpoint.new address: e_addr, port: ports.find { |p| p.port == addr.port }, status: available
  end

  true
end