mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-03-23 20:45:38 +00:00
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:
commit
07aa3a5001
@ -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) {
|
||||
|
@ -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', () => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user