Merge pull request #933 from balena-io/fix-network-joining

Fix network alias creation and comparison
This commit is contained in:
CameronDiver 2019-03-28 12:08:24 +00:00 committed by GitHub
commit b26a527ed0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 25 deletions

View File

@ -9,8 +9,6 @@ do ->
return lookup(name, { verbatim: true }, opts)
return lookup(name, Object.assign({ verbatim: true }, opts), cb)
require('log-timestamp')
Supervisor = require './supervisor'
supervisor = new Supervisor()

View File

@ -20,6 +20,7 @@ import {
import * as LogTypes from '../lib/log-types';
import { checkInt, isValidDeviceName } from '../lib/validation';
import { Service } from './service';
import { serviceNetworksToDockerNetworks } from './utils';
interface ServiceConstructOpts {
docker: Docker;
@ -266,7 +267,9 @@ export class ServiceManager extends (EventEmitter as {
}
const conf = service.toDockerContainer({ deviceName });
const nets = service.extraNetworksToJoin();
const nets = serviceNetworksToDockerNetworks(
service.extraNetworksToJoin(),
);
this.logger.logSystemEvent(LogTypes.installService, { service });
this.reportNewStatus(mockContainerId, service, 'Installing');
@ -275,7 +278,7 @@ export class ServiceManager extends (EventEmitter as {
service.containerId = container.id;
await Promise.all(
_.map(nets, (endpointConfig, name) =>
_.map((nets || {}).EndpointsConfig, (endpointConfig, name) =>
this.docker.getNetwork(name).connect({
Container: container.id,
EndpointConfig: endpointConfig,

View File

@ -122,6 +122,17 @@ export class Service {
}
// Prefix the network entries with the app id
networks = _.mapKeys(networks, (_v, k) => `${service.appId}_${k}`);
// Ensure that we add an alias of the service name
networks = _.mapValues(networks, v => {
if (v.aliases == null) {
v.aliases = [];
}
const serviceName: string = service.serviceName || '';
if (!_.includes(v.aliases, serviceName)) {
v.aliases.push(serviceName);
}
return v;
});
delete config.networks;
// Check for unsupported networkMode entries
@ -791,8 +802,8 @@ export class Service {
appId: number,
serviceName: string,
): { [envVarName: string]: string } {
let defaultEnv: { [envVarName: string]: string } = {};
for (let namespace of ['BALENA', 'RESIN']) {
const defaultEnv: { [envVarName: string]: string } = {};
for (const namespace of ['BALENA', 'RESIN']) {
_.assign(
defaultEnv,
_.mapKeys(
@ -837,24 +848,11 @@ export class Service {
const currentAliases = _.filter(current.aliases, (alias: string) => {
return !_.startsWith(this.containerId!, alias);
});
const targetAliases = _.filter(current.aliases, (alias: string) => {
return !_.startsWith(this.containerId!, alias);
});
const targetAliases = target.aliases || [];
// Docker adds container ids to the alias list, directly after
// the service name, to detect this, check for both target having
// exactly half of the amount of entries as the current, and check
// that every second entry (starting from 0) is equal
if (currentAliases.length === targetAliases.length * 2) {
sameNetwork = _(currentAliases)
.filter((_v, k) => k % 2 === 0)
.isEqual(targetAliases);
} else {
// Otherwise compare them literally
sameNetwork = _.isEmpty(
_.xorWith(currentAliases, targetAliases, _.isEqual),
);
}
sameNetwork = _.isEmpty(
_.xorWith(currentAliases, targetAliases, _.isEqual),
);
}
}
if (target.ipv4Address != null) {

View File

@ -348,7 +348,7 @@ export function addFeaturesFromLabels(
}`;
}
// We keep balena.sock for backwards compatibility
if (constants.dockerSocket != '/var/run/balena.sock') {
if (constants.dockerSocket !== '/var/run/balena.sock') {
service.config.volumes.push(
`${constants.dockerSocket}:/var/run/balena.sock`,
);

View File

@ -311,7 +311,9 @@ describe 'compose/service', ->
IPAMConfig: {
IPv4Address: '1.2.3.4'
},
Aliases: []
Aliases: [
'test'
]
}
}
})