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, { verbatim: true }, opts)
return lookup(name, Object.assign({ verbatim: true }, opts), cb) return lookup(name, Object.assign({ verbatim: true }, opts), cb)
require('log-timestamp')
Supervisor = require './supervisor' Supervisor = require './supervisor'
supervisor = new Supervisor() supervisor = new Supervisor()

View File

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

View File

@ -122,6 +122,17 @@ export class Service {
} }
// Prefix the network entries with the app id // Prefix the network entries with the app id
networks = _.mapKeys(networks, (_v, k) => `${service.appId}_${k}`); 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; delete config.networks;
// Check for unsupported networkMode entries // Check for unsupported networkMode entries
@ -791,8 +802,8 @@ export class Service {
appId: number, appId: number,
serviceName: string, serviceName: string,
): { [envVarName: string]: string } { ): { [envVarName: string]: string } {
let defaultEnv: { [envVarName: string]: string } = {}; const defaultEnv: { [envVarName: string]: string } = {};
for (let namespace of ['BALENA', 'RESIN']) { for (const namespace of ['BALENA', 'RESIN']) {
_.assign( _.assign(
defaultEnv, defaultEnv,
_.mapKeys( _.mapKeys(
@ -837,24 +848,11 @@ export class Service {
const currentAliases = _.filter(current.aliases, (alias: string) => { const currentAliases = _.filter(current.aliases, (alias: string) => {
return !_.startsWith(this.containerId!, alias); return !_.startsWith(this.containerId!, alias);
}); });
const targetAliases = _.filter(current.aliases, (alias: string) => { const targetAliases = target.aliases || [];
return !_.startsWith(this.containerId!, alias);
});
// Docker adds container ids to the alias list, directly after sameNetwork = _.isEmpty(
// the service name, to detect this, check for both target having _.xorWith(currentAliases, targetAliases, _.isEqual),
// 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),
);
}
} }
} }
if (target.ipv4Address != null) { if (target.ipv4Address != null) {

View File

@ -348,7 +348,7 @@ export function addFeaturesFromLabels(
}`; }`;
} }
// We keep balena.sock for backwards compatibility // 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( service.config.volumes.push(
`${constants.dockerSocket}:/var/run/balena.sock`, `${constants.dockerSocket}:/var/run/balena.sock`,
); );

View File

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