refs #5101 Publish TCP backends
gitea/docker-discover/pipeline/head This commit looks good Details

This commit is contained in:
Juan Ferrer 2023-02-22 18:33:35 +01:00
parent a9f157bff1
commit 762fbffe3c
3 changed files with 69 additions and 26 deletions

View File

@ -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];
for (let i = 0; i < ports.length; i++) {
const port = ports[i];
const protocol = port.Protocol;
services.push({
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,
protocol,
isTcp: protocol == 'tcp'
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;

View File

@ -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}}

View File

@ -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"
}
]