class Hetzner::LoadBalancer

Attributes

cluster_name[R]
hetzner_client[R]
load_balancer[R]
location[R]
network_id[R]

Public Class Methods

new(hetzner_client:, cluster_name:) click to toggle source
# File lib/hetzner/infra/load_balancer.rb, line 3
def initialize(hetzner_client:, cluster_name:)
  @hetzner_client = hetzner_client
  @cluster_name = cluster_name
end

Public Instance Methods

create(location:, network_id:) click to toggle source
# File lib/hetzner/infra/load_balancer.rb, line 8
def create(location:, network_id:)
  @location = location
  @network_id = network_id

  puts

  if load_balancer = find_load_balancer
    puts "API load balancer already exists, skipping."
    puts
    return load_balancer["id"]
  end

  puts "Creating API load_balancer..."

  response = hetzner_client.post("/load_balancers", create_load_balancer_config).body
  puts "...API load balancer created."
  puts

  JSON.parse(response)["load_balancer"]["id"]
end
delete(ha:) click to toggle source
# File lib/hetzner/infra/load_balancer.rb, line 29
def delete(ha:)
  if load_balancer = find_load_balancer
    puts "Deleting API load balancer..." unless ha

    hetzner_client.post("/load_balancers/#{load_balancer["id"]}/actions/remove_target", remove_targets_config)

    hetzner_client.delete("/load_balancers", load_balancer["id"])
    puts "...API load balancer deleted." unless ha
  elsif ha
    puts "API load balancer no longer exists, skipping."
  end

  puts
end

Private Instance Methods

create_load_balancer_config() click to toggle source
# File lib/hetzner/infra/load_balancer.rb, line 52
def create_load_balancer_config
  {
    "algorithm": {
      "type": "round_robin"
    },
    "load_balancer_type": "lb11",
    "location": location,
    "name": load_balancer_name,
    "network": network_id,
    "public_interface": true,
    "services": [
      {
        "destination_port": 6443,
        "listen_port": 6443,
        "protocol": "tcp",
        "proxyprotocol": false
      }
    ],
    "targets": [
      {
        "label_selector": {
          "selector": "cluster=#{cluster_name},role=master"
        },
        "type": "label_selector",
        "use_private_ip": true
      }
    ]
  }
end
find_load_balancer() click to toggle source
# File lib/hetzner/infra/load_balancer.rb, line 91
def find_load_balancer
  hetzner_client.get("/load_balancers")["load_balancers"].detect{ |load_balancer| load_balancer["name"] == load_balancer_name }
end
load_balancer_name() click to toggle source
# File lib/hetzner/infra/load_balancer.rb, line 48
def load_balancer_name
  "#{cluster_name}-api"
end
remove_targets_config() click to toggle source
# File lib/hetzner/infra/load_balancer.rb, line 82
def remove_targets_config
  {
    "label_selector": {
      "selector": "cluster=#{cluster_name},role=master"
    },
    "type": "label_selector"
  }
end