This commit is contained in:
parent
a9f157bff1
commit
762fbffe3c
47
index.js
47
index.js
|
@ -15,6 +15,7 @@ const isProduction = process.env.NODE_ENV === 'production';
|
||||||
const tmpDir = isProduction ? `/tmp/${appName}` : `${__dirname}/tmp`;
|
const tmpDir = isProduction ? `/tmp/${appName}` : `${__dirname}/tmp`;
|
||||||
const hashFile = `${tmpDir}/config.hash`;
|
const hashFile = `${tmpDir}/config.hash`;
|
||||||
const confDir = conf.rproxy.confDir;
|
const confDir = conf.rproxy.confDir;
|
||||||
|
const httpServices = new Set();
|
||||||
|
|
||||||
async function updateProxy(firstRun) {
|
async function updateProxy(firstRun) {
|
||||||
console.log('Updating reverse proxy configuration.');
|
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;
|
return a.name > b.name ? 1 : a.name < b.name ? -1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const nodes = [];
|
const workers = [];
|
||||||
|
const managers = [];
|
||||||
|
|
||||||
for (const node of info.nodes) {
|
for (const node of info.nodes) {
|
||||||
const address = node.ManagerStatus
|
const address = node.ManagerStatus
|
||||||
? node.ManagerStatus.Addr.split(':')[0]
|
? node.ManagerStatus.Addr.split(':')[0]
|
||||||
: node.Status.Addr;
|
: node.Status.Addr;
|
||||||
const role = node.Spec && node.Spec.Role
|
const role = node.Spec && node.Spec.Role
|
||||||
|
|
||||||
|
const nodes = role == 'worker' ? workers : managers;
|
||||||
nodes.push({
|
nodes.push({
|
||||||
name: node.Description.Hostname,
|
name: node.Description.Hostname,
|
||||||
address,
|
address
|
||||||
isWorker: role == 'worker'
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
nodes.sort(sortFn);
|
|
||||||
|
|
||||||
const services = [];
|
const http = [];
|
||||||
|
const tcp = [];
|
||||||
|
const udp = [];
|
||||||
|
|
||||||
for (const service of info.services) {
|
for (const service of info.services) {
|
||||||
const ports = service.Endpoint.Ports;
|
const ports = service.Endpoint.Ports;
|
||||||
if (!Array.isArray(ports) || !ports.length) continue;
|
if (!Array.isArray(ports) || !ports.length) continue;
|
||||||
|
@ -60,21 +65,35 @@ async function updateProxy(firstRun) {
|
||||||
const match = name.match(/^(.+)_main$/);
|
const match = name.match(/^(.+)_main$/);
|
||||||
if (match) name = match[1];
|
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;
|
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,
|
name,
|
||||||
port: port.PublishedPort,
|
port: port.PublishedPort
|
||||||
protocol,
|
|
||||||
isTcp: protocol == 'tcp'
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
services.sort(sortFn);
|
}
|
||||||
|
|
||||||
|
for (const list of [workers, managers, http, tcp, udp])
|
||||||
|
list.sort(sortFn);
|
||||||
|
|
||||||
const configString = template({
|
const configString = template({
|
||||||
services,
|
http,
|
||||||
nodes,
|
tcp,
|
||||||
|
udp,
|
||||||
|
workers,
|
||||||
|
managers,
|
||||||
info
|
info
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -169,6 +188,8 @@ async function updateProxy(firstRun) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function addService(service, serviceConf, mainDomain) {
|
function addService(service, serviceConf, mainDomain) {
|
||||||
|
httpServices.add(service);
|
||||||
|
|
||||||
let rules;
|
let rules;
|
||||||
if (typeof serviceConf == 'string') {
|
if (typeof serviceConf == 'string') {
|
||||||
rules = serviceConf;
|
rules = serviceConf;
|
||||||
|
|
|
@ -36,15 +36,21 @@ frontend http
|
||||||
backend not-found
|
backend not-found
|
||||||
http-request deny deny_status 400
|
http-request deny deny_status 400
|
||||||
|
|
||||||
# Auto-generated backends
|
#+++++++++++++++++++++++++++++++ HTTP backends
|
||||||
|
|
||||||
{{#each services}}
|
{{#each http}}
|
||||||
{{#if isTcp}}
|
|
||||||
backend {{name}}
|
backend {{name}}
|
||||||
{{#each ../nodes}}
|
{{#each ../workers}}
|
||||||
{{#if isWorker}}
|
server {{name}} {{address}}:{{../port}} check
|
||||||
server {{name}}:{{../port}} {{address}}:{{../port}} check
|
{{/each}}
|
||||||
{{/if}}
|
{{/each}}
|
||||||
|
|
||||||
|
#+++++++++++++++++++++++++++++++ TCP backends
|
||||||
|
|
||||||
|
{{#each tcp}}
|
||||||
|
listen {{name}}:{{port}}
|
||||||
|
mode tcp
|
||||||
|
{{#each ../workers}}
|
||||||
|
server {{name}} {{address}}:{{../port}} check
|
||||||
{{/each}}
|
{{/each}}
|
||||||
{{/if}}
|
|
||||||
{{/each}}
|
{{/each}}
|
||||||
|
|
18
test.json
18
test.json
|
@ -17,6 +17,22 @@
|
||||||
{
|
{
|
||||||
"PublishedPort": 10003,
|
"PublishedPort": 10003,
|
||||||
"Protocol": "tcp"
|
"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": {
|
"Endpoint": {
|
||||||
"Ports": [
|
"Ports": [
|
||||||
{
|
{
|
||||||
"PublishedPort": 10003,
|
"PublishedPort": 10001,
|
||||||
"Protocol": "udp"
|
"Protocol": "udp"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
Reference in New Issue