refs #5101 Select port when multiple ports are published for an HTTP service
gitea/docker-discover/pipeline/head This commit looks good Details

This commit is contained in:
Juan Ferrer 2023-02-27 22:36:53 +01:00
parent a437b28cb0
commit f221dead2b
3 changed files with 52 additions and 15 deletions

View File

@ -54,4 +54,7 @@ domains:
- /bar - /bar
https: false https: false
zone: dmz zone: dmz
foo_bar:
bar: bar:
port: 443
bar_baz:

View File

@ -15,7 +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(); const httpServices = new Map();
async function updateProxy(firstRun) { async function updateProxy(firstRun) {
console.log('Updating reverse proxy configuration.'); console.log('Updating reverse proxy configuration.');
@ -61,17 +61,30 @@ async function updateProxy(firstRun) {
const match = name.match(/^(.+)_main$/); const match = name.match(/^(.+)_main$/);
if (match) name = match[1]; if (match) name = match[1];
let httpPortIndex = null;
if (httpServices.has(name)) {
if (ports.length > 1) {
const httpPort = httpServices.get(name) || 80;
const index = ports.findIndex(
p => p.TargetPort == httpPort && p.Protocol === 'tcp');
if (index !== -1) httpPortIndex = index;
}
if (httpPortIndex === null) {
const index = ports.findIndex(p => p.Protocol === 'tcp');
if (index !== -1) httpPortIndex = index;
}
}
for (let i = 0; i < ports.length; i++) { for (let i = 0; i < ports.length; i++) {
const port = ports[i]; const port = ports[i];
const protocol = port.Protocol;
let backends; let backends;
if (protocol == 'tcp') {
if (i == 0 && httpServices.has(name)) if (i === httpPortIndex)
backends = http; backends = http;
else else if (port.Protocol === 'tcp')
backends = tcp; backends = tcp;
} else else
backends = udp; backends = udp;
backends.push({ backends.push({
@ -188,8 +201,6 @@ 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;
@ -201,6 +212,8 @@ async function updateProxy(firstRun) {
serviceConf serviceConf
); );
httpServices.set(service, serviceConf.port);
if (serviceConf.https) if (serviceConf.https)
https.push(service); https.push(service);
if (serviceConf.zone) { if (serviceConf.zone) {

View File

@ -6,6 +6,22 @@
"Ports": [ "Ports": [
{ {
"PublishedPort": 10001, "PublishedPort": 10001,
"TargetPort": 80,
"Protocol": "tcp"
}, {
"PublishedPort": 10002,
"TargetPort": 3306,
"Protocol": "tcp"
}
]
}
}, {
"Spec": {"Name": "foo_bar"},
"Endpoint": {
"Ports": [
{
"PublishedPort": 10003,
"TargetPort": 443,
"Protocol": "tcp" "Protocol": "tcp"
} }
] ]
@ -15,10 +31,12 @@
"Endpoint": { "Endpoint": {
"Ports": [ "Ports": [
{ {
"PublishedPort": 10003, "PublishedPort": 10004,
"TargetPort": 3584,
"Protocol": "tcp" "Protocol": "tcp"
}, { }, {
"PublishedPort": 10004, "PublishedPort": 10005,
"TargetPort": 443,
"Protocol": "tcp" "Protocol": "tcp"
} }
] ]
@ -28,10 +46,12 @@
"Endpoint": { "Endpoint": {
"Ports": [ "Ports": [
{ {
"PublishedPort": 10005, "PublishedPort": 10006,
"TargetPort": 101,
"Protocol": "tcp" "Protocol": "tcp"
}, { }, {
"PublishedPort": 10006, "PublishedPort": 10007,
"TargetPort": 80,
"Protocol": "tcp" "Protocol": "tcp"
} }
] ]
@ -42,6 +62,7 @@
"Ports": [ "Ports": [
{ {
"PublishedPort": 10001, "PublishedPort": 10001,
"TargetPort": 100,
"Protocol": "udp" "Protocol": "udp"
} }
] ]