diff --git a/appliances/VRouter/HAProxy/execute.rb b/appliances/VRouter/HAProxy/execute.rb index a212a2ef..6d954763 100644 --- a/appliances/VRouter/HAProxy/execute.rb +++ b/appliances/VRouter/HAProxy/execute.rb @@ -8,6 +8,7 @@ module HAProxy extend self VROUTER_ID = env :VROUTER_ID, nil + SERVICE_ID = env :SERVICE_ID, nil def extract_backends(objects = {}) @ave ||= [detect_addrs, detect_vips].then do |a, v| @@ -16,8 +17,8 @@ def extract_backends(objects = {}) static = backends.from_env(prefix: 'ONEAPP_VNF_HAPROXY_LB') - dynamic = VROUTER_ID.nil? ? backends.from_vms(objects, prefix: 'ONEGATE_HAPROXY_LB') - : backends.from_vnets(objects, prefix: 'ONEGATE_HAPROXY_LB') + dynamic = VROUTER_ID.nil? ? backends.from_vms(objects, prefix: 'ONEGATE_HAPROXY_LB', id: SERVICE_ID) + : backends.from_vnets(objects, prefix: 'ONEGATE_HAPROXY_LB', id: VROUTER_ID) # Replace all "", "" and "" placeholders where possible. static = backends.resolve static, *@ave diff --git a/appliances/VRouter/HAProxy/tests.rb b/appliances/VRouter/HAProxy/tests.rb index 3603ae1a..e476c968 100644 --- a/appliances/VRouter/HAProxy/tests.rb +++ b/appliances/VRouter/HAProxy/tests.rb @@ -189,6 +189,7 @@ def clear_vars(object) "ONEGATE_HAPROXY_LB0_SERVER_PORT": "1234", "ONEGATE_HAPROXY_LB0_SERVER_WEIGHT": "1", + "ONEGATE_HAPROXY_LB1_ID": "NOT-86", "ONEGATE_HAPROXY_LB1_IP": "10.2.11.86", "ONEGATE_HAPROXY_LB1_PORT": "8686", "ONEGATE_HAPROXY_LB1_SERVER_HOST": "10.2.11.201", @@ -208,6 +209,7 @@ def clear_vars(object) "ONEGATE_HAPROXY_LB0_SERVER_PORT": "1234", "ONEGATE_HAPROXY_LB0_SERVER_WEIGHT": "1", + "ONEGATE_HAPROXY_LB1_ID": "86", "ONEGATE_HAPROXY_LB1_IP": "10.2.11.86", "ONEGATE_HAPROXY_LB1_PORT": "8686", "ONEGATE_HAPROXY_LB1_SERVER_HOST": "10.2.11.200", @@ -257,7 +259,6 @@ def clear_vars(object) mode tcp balance roundrobin option tcp-check - server lb1_10.2.11.201_4321 10.2.11.201:4321 check observe layer4 error-limit 50 on-error mark-down server lb1_10.2.11.200_4321 10.2.11.200:4321 check observe layer4 error-limit 50 on-error mark-down DYNAMIC @@ -280,6 +281,9 @@ def clear_vars(object) ENV['ONEAPP_VNF_HAPROXY_LB0_IP'] = '10.2.11.86' ENV['ONEAPP_VNF_HAPROXY_LB0_PORT'] = '5432' + ENV['ONEAPP_VNF_HAPROXY_LB1_IP'] = '10.2.11.86' + ENV['ONEAPP_VNF_HAPROXY_LB1_PORT'] = '4321' + (vms ||= []) << JSON.parse(<<~'VM0') { "VM": { @@ -295,10 +299,18 @@ def clear_vars(object) "LOGO": "images/logos/linux.png", "LXD_SECURITY_PRIVILEGED": "true", "MEMORY_UNIT_COST": "MB", + "ONEGATE_HAPROXY_LB0_IP": "10.2.11.86", "ONEGATE_HAPROXY_LB0_PORT": "5432", "ONEGATE_HAPROXY_LB0_SERVER_HOST": "10.2.11.202", "ONEGATE_HAPROXY_LB0_SERVER_PORT": "2345", + + "ONEGATE_HAPROXY_LB1_ID": "86", + "ONEGATE_HAPROXY_LB1_IP": "10.2.11.86", + "ONEGATE_HAPROXY_LB1_PORT": "4321", + "ONEGATE_HAPROXY_LB1_SERVER_HOST": "10.2.11.202", + "ONEGATE_HAPROXY_LB1_SERVER_PORT": "1234", + "ROLE_NAME": "server", "SERVICE_ID": "23" }, @@ -337,10 +349,18 @@ def clear_vars(object) "LOGO": "images/logos/linux.png", "LXD_SECURITY_PRIVILEGED": "true", "MEMORY_UNIT_COST": "MB", + "ONEGATE_HAPROXY_LB0_IP": "10.2.11.86", "ONEGATE_HAPROXY_LB0_PORT": "5432", "ONEGATE_HAPROXY_LB0_SERVER_HOST": "10.2.11.203", "ONEGATE_HAPROXY_LB0_SERVER_PORT": "2345", + + "ONEGATE_HAPROXY_LB1_ID": "123", + "ONEGATE_HAPROXY_LB1_IP": "10.2.11.86", + "ONEGATE_HAPROXY_LB1_PORT": "4321", + "ONEGATE_HAPROXY_LB1_SERVER_HOST": "10.2.11.203", + "ONEGATE_HAPROXY_LB1_SERVER_PORT": "1234", + "ROLE_NAME": "server", "SERVICE_ID": "23" }, @@ -368,6 +388,7 @@ def clear_vars(object) load './main.rb'; include Service::HAProxy Service::HAProxy.const_set :VROUTER_ID, nil + Service::HAProxy.const_set :SERVICE_ID, '123' allow(Service::HAProxy).to receive(:detect_nics).and_return(%w[eth0 eth1 eth2 eth3]) allow(Service::HAProxy).to receive(:addrs_to_nics).and_return({ @@ -388,6 +409,17 @@ def clear_vars(object) option tcp-check server lb0_10.2.11.202_2345 10.2.11.202:2345 check observe layer4 error-limit 50 on-error mark-down server lb0_10.2.11.203_2345 10.2.11.203:2345 check observe layer4 error-limit 50 on-error mark-down + + frontend lb1_4321 + mode tcp + bind 10.2.11.86:4321 + default_backend lb1_4321 + + backend lb1_4321 + mode tcp + balance roundrobin + option tcp-check + server lb1_10.2.11.203_1234 10.2.11.203:1234 check observe layer4 error-limit 50 on-error mark-down DYNAMIC Dir.mktmpdir do |dir| diff --git a/appliances/VRouter/LVS/execute.rb b/appliances/VRouter/LVS/execute.rb index 55123f13..999a1d0c 100644 --- a/appliances/VRouter/LVS/execute.rb +++ b/appliances/VRouter/LVS/execute.rb @@ -8,6 +8,7 @@ module LVS extend self VROUTER_ID = env :VROUTER_ID, nil + SERVICE_ID = env :SERVICE_ID, nil def extract_backends(objects = {}) @ave ||= [detect_addrs, detect_vips].then do |a, v| @@ -16,8 +17,8 @@ def extract_backends(objects = {}) static = backends.from_env(prefix: 'ONEAPP_VNF_LB', allow_nil_ports: true) - dynamic = VROUTER_ID.nil? ? backends.from_vms(objects, prefix: 'ONEGATE_LB') - : backends.from_vnets(objects, prefix: 'ONEGATE_LB') + dynamic = VROUTER_ID.nil? ? backends.from_vms(objects, prefix: 'ONEGATE_LB', id: SERVICE_ID) + : backends.from_vnets(objects, prefix: 'ONEGATE_LB', id: VROUTER_ID) # Replace all "", "" and "" placeholders where possible. static = backends.resolve static, *@ave diff --git a/appliances/VRouter/LVS/tests.rb b/appliances/VRouter/LVS/tests.rb index 858b5315..aa5fa653 100644 --- a/appliances/VRouter/LVS/tests.rb +++ b/appliances/VRouter/LVS/tests.rb @@ -353,6 +353,7 @@ def clear_vars(object) "ONEGATE_LB0_SERVER_PORT": "6969", "ONEGATE_LB0_SERVER_WEIGHT": "2", + "ONEGATE_LB1_ID": "NOT-86", "ONEGATE_LB1_IP": "10.2.11.86", "ONEGATE_LB1_PORT": "8686", @@ -374,6 +375,7 @@ def clear_vars(object) "ONEGATE_LB0_SERVER_PORT": "6969", "ONEGATE_LB0_SERVER_WEIGHT": "1", + "ONEGATE_LB1_ID": "86", "ONEGATE_LB1_IP": "10.2.11.86", "ONEGATE_LB1_PORT": "8686", @@ -436,13 +438,6 @@ def clear_vars(object) lb_kind DR protocol TCP - real_server 10.2.11.201 8686 { - weight 2 - TCP_CHECK { - connect_timeout 3 - connect_port 8686 - } - } real_server 10.2.11.200 8686 { weight 1 TCP_CHECK { @@ -476,6 +471,13 @@ def clear_vars(object) ENV['ONEAPP_VNF_LB0_TIMEOUT'] = '5' ENV['ONEAPP_VNF_LB0_SCHEDULER'] = 'rr' + ENV['ONEAPP_VNF_LB1_IP'] = '10.2.11.86' + ENV['ONEAPP_VNF_LB1_PORT'] = '4321' + ENV['ONEAPP_VNF_LB1_PROTOCOL'] = 'TCP' + ENV['ONEAPP_VNF_LB1_METHOD'] = 'DR' + ENV['ONEAPP_VNF_LB1_TIMEOUT'] = '5' + ENV['ONEAPP_VNF_LB1_SCHEDULER'] = 'rr' + (vms ||= []) << JSON.parse(<<~'VM0') { "VM": { @@ -491,11 +493,20 @@ def clear_vars(object) "LOGO": "images/logos/linux.png", "LXD_SECURITY_PRIVILEGED": "true", "MEMORY_UNIT_COST": "MB", + "ONEGATE_LB0_IP": "10.2.11.86", "ONEGATE_LB0_PORT": "5432", "ONEGATE_LB0_SERVER_HOST": "10.2.11.202", "ONEGATE_LB0_SERVER_PORT": "2345", "ONEGATE_LB0_SERVER_WEIGHT": "1", + + "ONEGATE_LB1_ID": "86", + "ONEGATE_LB1_IP": "10.2.11.86", + "ONEGATE_LB1_PORT": "4321", + "ONEGATE_LB1_SERVER_HOST": "10.2.11.202", + "ONEGATE_LB1_SERVER_PORT": "1234", + "ONEGATE_LB1_SERVER_WEIGHT": "1", + "ROLE_NAME": "server", "SERVICE_ID": "23" }, @@ -534,11 +545,20 @@ def clear_vars(object) "LOGO": "images/logos/linux.png", "LXD_SECURITY_PRIVILEGED": "true", "MEMORY_UNIT_COST": "MB", + "ONEGATE_LB0_IP": "10.2.11.86", "ONEGATE_LB0_PORT": "5432", "ONEGATE_LB0_SERVER_HOST": "10.2.11.203", "ONEGATE_LB0_SERVER_PORT": "2345", "ONEGATE_LB0_SERVER_WEIGHT": "2", + + "ONEGATE_LB1_ID": "123", + "ONEGATE_LB1_IP": "10.2.11.86", + "ONEGATE_LB1_PORT": "4321", + "ONEGATE_LB1_SERVER_HOST": "10.2.11.203", + "ONEGATE_LB1_SERVER_PORT": "1234", + "ONEGATE_LB1_SERVER_WEIGHT": "2", + "ROLE_NAME": "server", "SERVICE_ID": "23" }, @@ -566,6 +586,7 @@ def clear_vars(object) load './main.rb'; include Service::LVS Service::LVS.const_set :VROUTER_ID, nil + Service::LVS.const_set :SERVICE_ID, '123' allow(Service::LVS).to receive(:detect_nics).and_return(%w[eth0 eth1 eth2 eth3]) allow(Service::LVS).to receive(:addrs_to_nics).and_return({ @@ -596,6 +617,20 @@ def clear_vars(object) } } } + virtual_server 10.2.11.86 4321 { + delay_loop 6 + lb_algo rr + lb_kind DR + protocol TCP + + real_server 10.2.11.203 1234 { + weight 2 + TCP_CHECK { + connect_timeout 3 + connect_port 1234 + } + } + } DYNAMIC Dir.mktmpdir do |dir| diff --git a/appliances/VRouter/vrouter.rb b/appliances/VRouter/vrouter.rb index 5774ccfa..72076287 100644 --- a/appliances/VRouter/vrouter.rb +++ b/appliances/VRouter/vrouter.rb @@ -445,10 +445,10 @@ def parse_static(names, prefix, allow_nil_ports: false) end end - def parse_dynamic(objects, prefix) + def parse_dynamic(objects, prefix, id: nil) objects.each_with_object({}) do |(name, v), acc| case name - when /^#{prefix}(\d+)_(IP|PORT)$/ + when /^#{prefix}(\d+)_(ID|IP|PORT)$/ lb_idx, opt = $1.to_i, $2 key = lb_idx acc[:options] ||= {} @@ -461,6 +461,15 @@ def parse_dynamic(objects, prefix) acc[:by_index][key] ||= {} acc[:by_index][key][opt.downcase.to_sym] = v end + end.then do |doc| + if !id.to_s.empty? + included = doc[:options].each_with_object(Set.new) do |(lb_idx, v), acc| + acc << lb_idx if v[:id].to_s.empty? || v[:id] == id + end + doc[:options] = doc[:options].slice *included + doc[:by_index] = doc[:by_index].slice *included + end + doc end.then do |doc| doc[:by_index]&.each do |lb_idx, v| key1 = [lb_idx, doc[:options][lb_idx][:ip], doc[:options][lb_idx][:port]] @@ -484,7 +493,7 @@ def from_env(prefix: 'ONEAPP_VNF_LB', allow_nil_ports: false) # also 'ONEAPP_HAP parse_static(ENV.keys, prefix, allow_nil_ports: allow_nil_ports) end - def from_vnets(vnets, prefix: 'ONEGATE_LB') # also 'ONEGATE_HAPROXY_LB' + def from_vnets(vnets, prefix: 'ONEGATE_LB', id: nil) # also 'ONEGATE_HAPROXY_LB' vnets.each_with_object({}) do |vnet, acc| next if (ars = vnet.dig('VNET', 'AR_POOL', 'AR')).nil? @@ -494,17 +503,17 @@ def from_vnets(vnets, prefix: 'ONEGATE_LB') # also 'ONEGATE_HAPROXY_LB' leases.each do |lease| next if lease['BACKEND'] != 'YES' - hashmap.combine! acc, parse_dynamic(lease, prefix) + hashmap.combine! acc, parse_dynamic(lease, prefix, id: id) end end end end - def from_vms(vms, prefix: 'ONEGATE_LB') # also 'ONEGATE_HAPROXY_LB' + def from_vms(vms, prefix: 'ONEGATE_LB', id: nil) # also 'ONEGATE_HAPROXY_LB' vms.each_with_object({}) do |vm, acc| next if (user_template = vm.dig('VM', 'USER_TEMPLATE')).nil? - hashmap.combine! acc, parse_dynamic(user_template, prefix) + hashmap.combine! acc, parse_dynamic(user_template, prefix, id: id) end end