Merge pull request #1694 from balena-os/1693-network-target-state

Show warning instead of exception for invalid network config
This commit is contained in:
bulldozer-balena[bot] 2021-05-06 21:18:53 +00:00 committed by GitHub
commit 07aa3a5001
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 38 deletions

View File

@ -5,14 +5,12 @@ import * as dockerode from 'dockerode';
import { docker } from '../lib/docker-utils';
import logTypes = require('../lib/log-types');
import * as logger from '../logger';
import log from '../lib/supervisor-console';
import * as ComposeUtils from './utils';
import { ComposeNetworkConfig, NetworkConfig } from './types/network';
import {
InvalidNetworkConfigurationError,
InvalidNetworkNameError,
} from './errors';
import { InvalidNetworkNameError } from './errors';
export class Network {
public appId: number;
@ -199,13 +197,17 @@ export class Network {
},
): void {
// Check if every ipam config entry has both a subnet and a gateway
_.each(_.get(config, 'ipam.config', []), ({ subnet, gateway }) => {
if (!subnet || !gateway) {
throw new InvalidNetworkConfigurationError(
'Network IPAM config entries must have both a subnet and gateway',
);
}
});
if (
_.some(
_.get(config, 'ipam.config', []),
({ subnet, gateway }) => !subnet || !gateway,
)
) {
log.warn(
'Network IPAM config entries must have both a subnet and gateway defined.' +
' Your network might not work properly otherwise.',
);
}
}
public static generateDockerName(appId: number, name: string) {

View File

@ -1,8 +1,11 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import { Network } from '../../../src/compose/network';
import { NetworkInspectInfo } from 'dockerode';
import { log } from '../../../src/lib/supervisor-console';
describe('compose/network', () => {
describe('creating a network from a compose object', () => {
it('creates a default network configuration if no config is given', () => {
@ -77,38 +80,46 @@ describe('compose/network', () => {
});
});
it('rejects IPAM configuration without both gateway and subnet', () => {
expect(() =>
Network.fromComposeObject('default', 12345, {
ipam: {
driver: 'default',
config: [
{
subnet: '172.20.0.0/16',
},
],
options: {},
},
}),
).to.throw(
it('warns about IPAM configuration without both gateway and subnet', () => {
const logSpy = sinon.spy(log, 'warn');
Network.fromComposeObject('default', 12345, {
ipam: {
driver: 'default',
config: [
{
subnet: '172.20.0.0/16',
},
],
options: {},
},
});
expect(logSpy).to.be.called.calledOnce;
expect(logSpy).to.be.called.calledWithMatch(
'Network IPAM config entries must have both a subnet and gateway',
);
expect(() =>
Network.fromComposeObject('default', 12345, {
ipam: {
driver: 'default',
config: [
{
gateway: '172.20.0.1',
},
],
options: {},
},
}),
).to.throw(
logSpy.resetHistory();
Network.fromComposeObject('default', 12345, {
ipam: {
driver: 'default',
config: [
{
gateway: '172.20.0.1',
},
],
options: {},
},
});
expect(logSpy).to.be.called.calledOnce;
expect(logSpy).to.be.called.calledWithMatch(
'Network IPAM config entries must have both a subnet and gateway',
);
logSpy.restore();
});
it('parses values from a compose object', () => {