Skip to content

Commit

Permalink
fix(container): allow the same port number with different protocols (#…
Browse files Browse the repository at this point in the history
…3508) (#3988)

# Backport

This will backport the following commits from `k8s-28/main` to `k8s-26/main`:
 - [fix(container): allow the same port number with different protocols (#3508)](#3508)



### Questions ?
Please refer to the [Backport tool documentation](https://github.com/sqren/backport)
  • Loading branch information
cdk8s-automation authored Apr 7, 2024
1 parent e526be1 commit 18d4faf
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,14 +820,15 @@ export class Container {
public addPort(port: ContainerPort) {

const names = this._ports.map(p => p.name).filter(x => x);
const numbers = this._ports.map(p => p.number);
const numberProtocols = this._ports.map(p => `${p.number}/${p.protocol || Protocol.TCP}`);

if (port.name && names.includes(port.name)) {
throw new Error(`Port with name ${port.name} already exists`);
}

if (numbers.includes(port.number)) {
throw new Error(`Port with number ${port.number} already exists`);
const protocol = `${port.number}/${port.protocol || Protocol.TCP}`;
if (numberProtocols.includes(protocol)) {
throw new Error(`Port with number ${port.number} and protocol ${port.protocol || Protocol.TCP} already exists`);
}

this._ports.push(port);
Expand Down
71 changes: 67 additions & 4 deletions test/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,56 @@ describe('EnvValue', () => {

describe('Container', () => {

test('cannot configure identical ports at instantiation', () => {
test('cannot configure identical ports and protocols at instantiation', () => {

expect(() => new kplus.Container({
image: 'image',
ports: [
{
number: 8080,
protocol: kplus.Protocol.TCP,
},
{
number: 8080,
protocol: kplus.Protocol.TCP,
},
],
})).toThrowError('Port with number 8080 already exists');
})).toThrowError('Port with number 8080 and protocol TCP already exists');

});

test('cannot add an already existing port number', () => {
test('can configure identical ports with different protocols at instantiation', () => {
const container = new kplus.Container({
image: 'image',
ports: [
{
number: 8080,
protocol: kplus.Protocol.TCP,
},
{
number: 8080,
protocol: kplus.Protocol.UDP,
},
],
});

expect(container._toKube().ports).toEqual([{
containerPort: 8080,
protocol: 'TCP',
}, {
containerPort: 8080,
protocol: 'UDP',
}]);
expect(container.ports).toEqual([{
number: 8080,
protocol: kplus.Protocol.TCP,
}, {
number: 8080,
protocol: kplus.Protocol.UDP,
}]);
});

test('cannot add an already existing port number with identical protocol', () => {

const container = new kplus.Container({
image: 'image',
Expand All @@ -205,8 +238,38 @@ describe('Container', () => {
}],
});

expect(() => container.addPort({ number: 8080 })).toThrowError('Port with number 8080 already exists');
expect(() => container.addPort({ number: 8080 })).toThrowError('Port with number 8080 and protocol TCP already exists');

});

test('can add an already existing port number with a different protocol', () => {

const container = new kplus.Container({
image: 'image',
ports: [{
number: 8080,
protocol: kplus.Protocol.TCP,
}],
});
container.addPort({
number: 8080,
protocol: kplus.Protocol.UDP,
});

expect(container._toKube().ports).toEqual([{
containerPort: 8080,
protocol: 'TCP',
}, {
containerPort: 8080,
protocol: 'UDP',
}]);
expect(container.ports).toEqual([{
number: 8080,
protocol: kplus.Protocol.TCP,
}, {
number: 8080,
protocol: kplus.Protocol.UDP,
}]);
});

test('cannot add an already existing port name', () => {
Expand Down

0 comments on commit 18d4faf

Please sign in to comment.