From 762fbffe3c6c6a55f729ba7e1410535f21e8ac17 Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Wed, 22 Feb 2023 18:33:35 +0100 Subject: [PATCH] refs #5101 Publish TCP backends --- index.js | 55 ++++++++++++++++++++++++++++++++--------------- rproxy.handlebars | 22 ++++++++++++------- test.json | 18 +++++++++++++++- 3 files changed, 69 insertions(+), 26 deletions(-) diff --git a/index.js b/index.js index f5fdba1..ca3d4ee 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,7 @@ const isProduction = process.env.NODE_ENV === 'production'; const tmpDir = isProduction ? `/tmp/${appName}` : `${__dirname}/tmp`; const hashFile = `${tmpDir}/config.hash`; const confDir = conf.rproxy.confDir; +const httpServices = new Set(); async function updateProxy(firstRun) { console.log('Updating reverse proxy configuration.'); @@ -36,22 +37,26 @@ async function updateProxy(firstRun) { return a.name > b.name ? 1 : a.name < b.name ? -1 : 0; } - const nodes = []; + const workers = []; + const managers = []; + for (const node of info.nodes) { const address = node.ManagerStatus ? node.ManagerStatus.Addr.split(':')[0] : node.Status.Addr; const role = node.Spec && node.Spec.Role + const nodes = role == 'worker' ? workers : managers; nodes.push({ name: node.Description.Hostname, - address, - isWorker: role == 'worker' + address }); } - nodes.sort(sortFn); - const services = []; + const http = []; + const tcp = []; + const udp = []; + for (const service of info.services) { const ports = service.Endpoint.Ports; if (!Array.isArray(ports) || !ports.length) continue; @@ -60,21 +65,35 @@ async function updateProxy(firstRun) { const match = name.match(/^(.+)_main$/); if (match) name = match[1]; - const port = ports[0]; - const protocol = port.Protocol; + for (let i = 0; i < ports.length; i++) { + const port = ports[i]; + const protocol = port.Protocol; - services.push({ - name, - port: port.PublishedPort, - protocol, - isTcp: protocol == 'tcp' - }); + let backends; + if (protocol == 'tcp') { + if (i == 0 && httpServices.has(name)) + backends = http; + else + backends = tcp; + } else + backends = udp; + + backends.push({ + name, + port: port.PublishedPort + }); + } } - services.sort(sortFn); + + for (const list of [workers, managers, http, tcp, udp]) + list.sort(sortFn); const configString = template({ - services, - nodes, + http, + tcp, + udp, + workers, + managers, info }); @@ -169,6 +188,8 @@ async function updateProxy(firstRun) { } function addService(service, serviceConf, mainDomain) { + httpServices.add(service); + let rules; if (typeof serviceConf == 'string') { rules = serviceConf; @@ -183,7 +204,7 @@ async function updateProxy(firstRun) { if (serviceConf.https) https.push(service); if (serviceConf.zone) { - zoneMap.push([service, serviceConf.zone]); + zoneMap.push([service, serviceConf.zone]); zones.add(serviceConf.zone); } diff --git a/rproxy.handlebars b/rproxy.handlebars index e89f425..34af26b 100644 --- a/rproxy.handlebars +++ b/rproxy.handlebars @@ -36,15 +36,21 @@ frontend http backend not-found http-request deny deny_status 400 -# Auto-generated backends +#+++++++++++++++++++++++++++++++ HTTP backends -{{#each services}} -{{#if isTcp}} +{{#each http}} backend {{name}} - {{#each ../nodes}} - {{#if isWorker}} - server {{name}}:{{../port}} {{address}}:{{../port}} check - {{/if}} + {{#each ../workers}} + server {{name}} {{address}}:{{../port}} check + {{/each}} +{{/each}} + +#+++++++++++++++++++++++++++++++ TCP backends + +{{#each tcp}} +listen {{name}}:{{port}} + mode tcp + {{#each ../workers}} + server {{name}} {{address}}:{{../port}} check {{/each}} -{{/if}} {{/each}} diff --git a/test.json b/test.json index ebac96a..c3a1b8d 100644 --- a/test.json +++ b/test.json @@ -17,6 +17,22 @@ { "PublishedPort": 10003, "Protocol": "tcp" + }, { + "PublishedPort": 10004, + "Protocol": "tcp" + } + ] + } + }, { + "Spec": {"Name": "bar_baz"}, + "Endpoint": { + "Ports": [ + { + "PublishedPort": 10005, + "Protocol": "tcp" + }, { + "PublishedPort": 10006, + "Protocol": "tcp" } ] } @@ -25,7 +41,7 @@ "Endpoint": { "Ports": [ { - "PublishedPort": 10003, + "PublishedPort": 10001, "Protocol": "udp" } ]