-
Notifications
You must be signed in to change notification settings - Fork 17
Add Dockerfile port detection to ingress configuration logic
#449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
a27a2bc
Partial
MicroFish91 42d6c19
Merge branch 'main' of https://github.com/microsoft/vscode-azureconta…
MicroFish91 c13c61e
Update validation logic
MicroFish91 0e25f45
Wip
MicroFish91 1134743
Wip tests
MicroFish91 426b5e4
Add tests
MicroFish91 64dccfc
Revert main.js
MicroFish91 fbffd33
Update test file names
MicroFish91 0fa2f4c
Import type
MicroFish91 91eff67
Update test case
MicroFish91 06a7ba9
Make pr smaller
MicroFish91 d21cdb2
Fix
MicroFish91 9e8f37f
Impl
MicroFish91 d8d2345
Merge with part I
MicroFish91 a9ab3ac
Add back name validation refactors
MicroFish91 dae4d98
Update var names
MicroFish91 830ecb5
Revert old change
MicroFish91 3e8ea38
nit
MicroFish91 57e86fe
Impl
MicroFish91 3405415
Combine if statements
MicroFish91 0ff2e01
Import type
MicroFish91 d689816
Update test display
MicroFish91 a721f97
Misc
MicroFish91 0f86bdc
Add additional parse notes
MicroFish91 4cab767
Update var name
MicroFish91 fe4f955
Merge branch 'mwf/deployWorkspaceProject-II' of https://github.com/mi…
MicroFish91 de11fc0
Updates
MicroFish91 3a3942c
Merge with main
MicroFish91 7a236f0
Update microsoft header
MicroFish91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { AzExtFsExtra } from "@microsoft/vscode-azext-utils"; | ||
|
|
||
| export async function tryGetDockerfileExposePorts(dockerfilePath: string): Promise<PortRange[] | undefined> { | ||
| if (!await AzExtFsExtra.pathExists(dockerfilePath)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const content: string = await AzExtFsExtra.readFile(dockerfilePath); | ||
| const lines: string[] = content.split('\n'); | ||
|
|
||
| const portRanges: PortRange[] = []; | ||
| for (const line of lines) { | ||
| if (!/^EXPOSE/i.test(line.trim())) { | ||
| continue; | ||
| } | ||
|
|
||
| // Identify all single port numbers that aren't for udp | ||
| // Example formats: `3000` or `3000/tcp` but not `3000/udp` | ||
| // Note: (?<=\s) prevents the last number in a range 3000-3010 from being selected | ||
| const singlePorts: string[] = line.match(/(?<=\s)\d{2,5}(?!(\-)|(\/udp))\b/g) ?? []; | ||
| for (const sp of singlePorts) { | ||
| portRanges.push(new PortRange(parseInt(sp))); | ||
| } | ||
|
|
||
| // Identify all port ranges | ||
| // Example format: `3000-3010` | ||
| const portRange: string[] = line.match(/\d{2,5}\-\d{2,5}/g) ?? []; | ||
| for (const pr of portRange) { | ||
| const [start, end] = pr.split('-'); | ||
| portRanges.push(new PortRange(parseInt(start), parseInt(end))); | ||
| } | ||
| } | ||
|
|
||
| return portRanges.length ? portRanges : undefined; | ||
| } | ||
|
|
||
| export class PortRange { | ||
| private readonly _start: number; | ||
| private readonly _end: number; | ||
|
|
||
| constructor(start: number, end?: number) { | ||
| this._start = start; | ||
| this._end = end ? end : start; | ||
| } | ||
|
|
||
| get start(): number { | ||
| return this._start; | ||
| } | ||
|
|
||
| get end(): number { | ||
| return this._end; | ||
| } | ||
|
|
||
| includes(port: number): boolean { | ||
| return port >= this.start && port <= this.end; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { AzExtFsExtra } from "@microsoft/vscode-azext-utils"; | ||
| import * as assert from "assert"; | ||
| import * as path from "path"; | ||
| import { IngressContext, tryConfigureIngressUsingDockerfile } from "../../extension.bundle"; | ||
| import type { MockIngressContext } from "./MockIngressContext"; | ||
| import { expectedSamplePorts } from "./tryGetDockerfileExposePorts.test"; | ||
|
|
||
| suite('IngressPromptStep', async () => { | ||
| test('tryConfigureIngressUsingDockerfile', async () => { | ||
| const dockerfileSamplesPath: string = path.join(__dirname, 'dockerfileSamples'); | ||
| const dockerfileSamples = await AzExtFsExtra.readDirectory(dockerfileSamplesPath); | ||
|
|
||
| const expectedResult: MockIngressContext[] = [ | ||
| { enableIngress: true, enableExternal: true, dockerfileExposePorts: expectedSamplePorts[0], targetPort: 443 }, | ||
| { enableIngress: undefined, enableExternal: undefined, dockerfileExposePorts: undefined, targetPort: undefined }, // no dockerfilePath | ||
| { enableIngress: true, enableExternal: true, dockerfileExposePorts: expectedSamplePorts[2], targetPort: 80 }, | ||
| { enableIngress: undefined, enableExternal: undefined, dockerfileExposePorts: expectedSamplePorts[3], targetPort: undefined }, // alwaysPromptIngress=true | ||
| { enableIngress: true, enableExternal: true, dockerfileExposePorts: expectedSamplePorts[4], targetPort: 443 }, | ||
| { enableIngress: true, enableExternal: true, dockerfileExposePorts: expectedSamplePorts[5], targetPort: 80 }, | ||
| { enableIngress: false, enableExternal: false, dockerfileExposePorts: undefined, targetPort: undefined }, // no expose | ||
| ]; | ||
|
|
||
| for (const [i, ds] of dockerfileSamples.entries()) { | ||
| const context: MockIngressContext = { | ||
| dockerfilePath: i === 1 ? undefined : ds.fsPath, | ||
| alwaysPromptIngress: i === 3 | ||
| }; | ||
|
|
||
| await tryConfigureIngressUsingDockerfile(context as IngressContext); | ||
|
|
||
| assert.deepStrictEqual({ | ||
| enableIngress: context.enableIngress, | ||
| enableExternal: context.enableExternal, | ||
| dockerfileExposePortsLength: context.dockerfileExposePorts?.length, | ||
| targetPort: context.targetPort | ||
| }, { | ||
| enableIngress: expectedResult[i].enableIngress, | ||
| enableExternal: expectedResult[i].enableExternal, | ||
| dockerfileExposePortsLength: expectedResult[i].dockerfileExposePorts?.length, | ||
| targetPort: expectedResult[i].targetPort | ||
| }); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import type { PortRange } from "../../extension.bundle"; | ||
|
|
||
| export interface MockIngressContext { | ||
| containerApp?: { configuration: { ingress: { targetPort: number } } }; | ||
|
|
||
| enableIngress?: boolean; | ||
| enableExternal?: boolean; | ||
|
|
||
| targetPort?: number; | ||
|
|
||
| dockerfilePath?: string; | ||
| dockerfileExposePorts?: PortRange[]; | ||
| alwaysPromptIngress?: boolean; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| ## Modified example from Azure-Samples/acr-build-helloworld-node | ||
| FROM node:lts-alpine | ||
|
|
||
| COPY . /src | ||
| RUN cd /src && npm install | ||
| EXPOSE 443 80 | ||
| CMD ["node", "/src/server.js"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| ## Modified example from Azure-Samples/acr-build-helloworld-node | ||
| FROM node:lts-alpine | ||
|
|
||
| COPY . /src | ||
| RUN cd /src && npm install | ||
| EXPOSE 80 | ||
| EXPOSE 443 | ||
| CMD ["node", "/src/server.js"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| ## Modified example from Azure-Samples/acr-build-helloworld-node | ||
| FROM node:lts-alpine | ||
|
|
||
| COPY . /src | ||
| RUN cd /src && npm install | ||
| EXPOSE 80 8080-8090 | ||
| CMD ["node", "/src/server.js"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| ## Modified example from Azure-Samples/acr-build-helloworld-node | ||
| FROM node:lts-alpine | ||
|
|
||
| COPY . /src | ||
| RUN cd /src && npm install | ||
| EXPOSE 8080-8090 80 | ||
| CMD ["node", "/src/server.js"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| ## Modified example from Azure-Samples/acr-build-helloworld-node | ||
| FROM node:lts-alpine | ||
|
|
||
| COPY . /src | ||
| RUN cd /src && npm install | ||
| EXPOSE 443/tcp | ||
| EXPOSE 5000/udp | ||
| CMD ["node", "/src/server.js"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| ## Modified example from Azure-Samples/acr-build-helloworld-node | ||
| FROM node:lts-alpine | ||
|
|
||
| COPY . /src | ||
| RUN cd /src && npm install | ||
|
|
||
| ## Extra unrealistic expose scenario used to double-check formatting logic | ||
| EXPOSE 80 443/tcp 8080-8090 5000/udp | ||
| CMD ["node", "/src/server.js"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| ## Modified example from Azure-Samples/acr-build-helloworld-node | ||
| FROM node:lts-alpine | ||
|
|
||
| COPY . /src | ||
| RUN cd /src && npm install | ||
|
|
||
| CMD ["node", "/src/server.js"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import * as assert from "assert"; | ||
| import { IngressContext, PortRange, getDefaultPort } from "../../extension.bundle"; | ||
| import type { MockIngressContext } from "./MockIngressContext"; | ||
|
|
||
| suite('getDefaultPort', async () => { | ||
| test('Correctly suggests a new port when Dockerfile expose ports are detected with no existing container app port', async () => { | ||
| const context: MockIngressContext = { | ||
| dockerfileExposePorts: [new PortRange(443), new PortRange(8080, 8090)] | ||
| }; | ||
| assert.equal(getDefaultPort(context as IngressContext), 443); | ||
| }); | ||
|
|
||
| test('Correctly suggests deployed port when Dockerfile expose ports are detected that overlap with existing container app port', async () => { | ||
| const context: MockIngressContext = { | ||
| containerApp: { configuration: { ingress: { targetPort: 8081 } } }, | ||
| dockerfileExposePorts: [new PortRange(80), new PortRange(443), new PortRange(8080, 8090)] | ||
| }; | ||
| assert.equal(getDefaultPort(context as IngressContext), 8081); | ||
| }); | ||
|
|
||
| test('Correctly suggests existing deploy port when no expose ports are detected', async () => { | ||
| const context: MockIngressContext = { | ||
| containerApp: { configuration: { ingress: { targetPort: 3000 } } }, | ||
| }; | ||
| assert.equal(getDefaultPort(context as IngressContext), 3000); | ||
| }); | ||
|
|
||
| test('Correctly suggests fallback port when no other ports are available', async () => { | ||
| assert.equal(getDefaultPort({} as IngressContext), 80); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just want to confirm, but the reason you're not just using the
IngressContextbecause of thecontainerApphas too many required properties that aren't worth mocking?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's a big part of the reason