Skip to content

Commit de11fc0

Browse files
committed
Updates
1 parent fe4f955 commit de11fc0

14 files changed

+151
-143
lines changed

extension.bundle.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ export * from '@microsoft/vscode-azext-utils';
1919
export * from './src/commands/deployWorkspaceProject/DeployWorkspaceProjectContext';
2020
export * from './src/commands/deployWorkspaceProject/getDefaultValues/DefaultResourcesNameStep';
2121
export * from './src/commands/ingress/IngressContext';
22+
export * from './src/commands/ingress/IngressPromptStep';
2223
export * from './src/commands/ingress/editTargetPort/getDefaultPort';
23-
export * from './src/commands/ingress/tryConfigureIngressUsingDockerfile';
24+
export * from './src/commands/ingress/tryGetDockerfileExposePorts';
2425
export { activate, deactivate } from './src/extension';
2526
export * from './src/extensionVariables';
2627
export * from './src/utils/validateUtils';

src/commands/ingress/IngressContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import type { ExecuteActivityContext } from "@microsoft/vscode-azext-utils";
77
import type { IContainerAppContext } from "../IContainerAppContext";
8-
import type { PortRange } from "./tryConfigureIngressUsingDockerfile";
8+
import { PortRange } from "./tryGetDockerfileExposePorts";
99

1010
export interface IngressContext extends IContainerAppContext, ExecuteActivityContext {
1111
enableIngress?: boolean;

src/commands/ingress/IngressPromptStep.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { AzureWizardExecuteStep, AzureWizardPromptStep, IWizardOptions } from "@microsoft/vscode-azext-utils";
7+
import { ext } from "../../extensionVariables";
78
import { localize } from "../../utils/localize";
89
import type { IngressContext } from "./IngressContext";
910
import { DisableIngressStep } from "./disableIngress/DisableIngressStep";
1011
import { TargetPortInputStep } from "./editTargetPort/TargetPortInputStep";
12+
import { getDefaultPort } from "./editTargetPort/getDefaultPort";
1113
import { EnableIngressStep } from "./enableIngress/EnableIngressStep";
1214
import { IngressVisibilityStep } from "./enableIngress/IngressVisibilityStep";
13-
import { tryConfigureIngressUsingDockerfile } from "./tryConfigureIngressUsingDockerfile";
15+
import { tryGetDockerfileExposePorts } from "./tryGetDockerfileExposePorts";
1416

1517
export class IngressPromptStep extends AzureWizardPromptStep<IngressContext> {
1618
public async prompt(context: IngressContext): Promise<void> {
@@ -42,3 +44,42 @@ export class IngressPromptStep extends AzureWizardPromptStep<IngressContext> {
4244
return { promptSteps, executeSteps };
4345
}
4446
}
47+
48+
export async function tryConfigureIngressUsingDockerfile(context: IngressContext): Promise<void> {
49+
if (!context.dockerfilePath) {
50+
return;
51+
}
52+
53+
context.dockerfileExposePorts = await tryGetDockerfileExposePorts(context.dockerfilePath);
54+
55+
if (context.alwaysPromptIngress) {
56+
return;
57+
}
58+
59+
if (!context.dockerfileExposePorts) {
60+
context.enableIngress = false;
61+
context.enableExternal = false;
62+
} else if (context.dockerfileExposePorts) {
63+
context.enableIngress = true;
64+
context.enableExternal = true;
65+
context.targetPort = getDefaultPort(context);
66+
}
67+
68+
// If a container app already exists, activity children will be added automatically in later execute steps
69+
// if (!context.containerApp) {
70+
// context.activityChildren?.push(
71+
// new GenericTreeItem(undefined, {
72+
// contextValue: createActivityChildContext(['ingressPromptStep', activitySuccessContext]),
73+
// label: context.enableIngress ?
74+
// localize('ingressEnableLabel', 'Enable ingress on port {0} (found Dockerfile configuration)', context.targetPort) :
75+
// localize('ingressDisableLabel', 'Disable ingress (found Dockerfile configuration)'),
76+
// iconPath: activitySuccessIcon
77+
// })
78+
// );
79+
// }
80+
81+
ext.outputChannel.appendLog(context.enableIngress ?
82+
localize('ingressEnabledLabel', 'Detected ingress on port {0} using Dockerfile configuration.', context.targetPort) :
83+
localize('ingressDisabledLabel', 'Detected no ingress using Dockerfile configuration.')
84+
);
85+
}

src/commands/ingress/tryConfigureIngressUsingDockerfile.ts

Lines changed: 0 additions & 105 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { AzExtFsExtra } from "@microsoft/vscode-azext-utils";
7+
8+
export async function tryGetDockerfileExposePorts(dockerfilePath: string): Promise<PortRange[] | undefined> {
9+
if (!await AzExtFsExtra.pathExists(dockerfilePath)) {
10+
return undefined;
11+
}
12+
13+
const content: string = await AzExtFsExtra.readFile(dockerfilePath);
14+
const lines: string[] = content.split('\n');
15+
16+
const portRanges: PortRange[] = [];
17+
for (const line of lines) {
18+
if (!/^EXPOSE/i.test(line.trim())) {
19+
continue;
20+
}
21+
22+
// Identify all single port numbers that aren't for udp
23+
// Example formats: `3000` or `3000/tcp` but not `3000/udp`
24+
// Note: (?<=\s) prevents the last number in a range 3000-3010 from being selected
25+
const singlePorts: string[] = line.match(/(?<=\s)\d{2,5}(?!(\-)|(\/udp))\b/g) ?? [];
26+
for (const sp of singlePorts) {
27+
portRanges.push(new PortRange(parseInt(sp)));
28+
}
29+
30+
// Identify all port ranges
31+
// Example format: `3000-3010`
32+
const portRange: string[] = line.match(/\d{2,5}\-\d{2,5}/g) ?? [];
33+
for (const pr of portRange) {
34+
const [start, end] = pr.split('-');
35+
portRanges.push(new PortRange(parseInt(start), parseInt(end)));
36+
}
37+
}
38+
39+
return portRanges.length ? portRanges : undefined;
40+
}
41+
42+
export class PortRange {
43+
private readonly _start: number;
44+
private readonly _end: number;
45+
46+
constructor(start: number, end?: number) {
47+
this._start = start;
48+
this._end = end ? end : start;
49+
}
50+
51+
get start(): number {
52+
return this._start;
53+
}
54+
55+
get end(): number {
56+
return this._end;
57+
}
58+
59+
includes(port: number): boolean {
60+
return port >= this.start && port <= this.end;
61+
}
62+
}

test/ingress/tryConfigureIngressUsingDockerfile.test.ts renamed to test/ingress/IngressPromptStep.test.ts

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,20 @@
66
import { AzExtFsExtra } from "@microsoft/vscode-azext-utils";
77
import * as assert from "assert";
88
import * as path from "path";
9-
import { IngressContext, PortRange, getDockerfileExposePorts, tryConfigureIngressUsingDockerfile } from "../../extension.bundle";
9+
import { IngressContext, tryConfigureIngressUsingDockerfile } from "../../extension.bundle";
1010
import type { MockIngressContext } from "./MockIngressContext";
11+
import { expectedSamplePorts } from "./tryGetDockerfileExposePorts.test";
1112

12-
const expectedSamplePorts: PortRange[][] = [
13-
[new PortRange(443), new PortRange(80)],
14-
[new PortRange(80), new PortRange(443)],
15-
[new PortRange(80), new PortRange(8080, 8090)],
16-
[new PortRange(80), new PortRange(8080, 8090)],
17-
[new PortRange(443)],
18-
[new PortRange(80), new PortRange(443), new PortRange(8080, 8090)],
19-
[]
20-
];
21-
22-
suite('tryConfigureIngressUsingDockerfile', async () => {
23-
test('self', async () => {
13+
suite('IngressPromptStep', async () => {
14+
test('tryConfigureIngressUsingDockerfile', async () => {
2415
const dockerfileSamplesPath: string = path.join(__dirname, 'dockerfileSamples');
2516
const dockerfileSamples = await AzExtFsExtra.readDirectory(dockerfileSamplesPath);
2617

2718
const expectedResult: MockIngressContext[] = [
2819
{ enableIngress: true, enableExternal: true, dockerfileExposePorts: expectedSamplePorts[0], targetPort: 443 },
2920
{ enableIngress: undefined, enableExternal: undefined, dockerfileExposePorts: undefined, targetPort: undefined }, // no dockerfilePath
3021
{ enableIngress: true, enableExternal: true, dockerfileExposePorts: expectedSamplePorts[2], targetPort: 80 },
31-
{ enableIngress: undefined, enableExternal: undefined, dockerfileExposePorts: expectedSamplePorts[3], targetPort: undefined }, // alwaysPromptIngress
22+
{ enableIngress: undefined, enableExternal: undefined, dockerfileExposePorts: expectedSamplePorts[3], targetPort: undefined }, // alwaysPromptIngress=true
3223
{ enableIngress: true, enableExternal: true, dockerfileExposePorts: expectedSamplePorts[4], targetPort: 443 },
3324
{ enableIngress: true, enableExternal: true, dockerfileExposePorts: expectedSamplePorts[5], targetPort: 80 },
3425
{ enableIngress: false, enableExternal: false, dockerfileExposePorts: undefined, targetPort: undefined }, // no expose
@@ -55,18 +46,4 @@ suite('tryConfigureIngressUsingDockerfile', async () => {
5546
});
5647
}
5748
});
58-
59-
test('getDockerfileExposePorts', async () => {
60-
const dockerfileSamplesPath: string = path.join(__dirname, 'dockerfileSamples');
61-
const dockerfileSamples = await AzExtFsExtra.readDirectory(dockerfileSamplesPath);
62-
63-
for (const [i, ds] of dockerfileSamples.entries()) {
64-
const portRange: PortRange[] = await getDockerfileExposePorts(ds.fsPath) ?? [];
65-
66-
for (const [j, pr] of portRange.entries()) {
67-
assert.equal(pr.start, expectedSamplePorts[i][j].start);
68-
assert.equal(pr.end, expectedSamplePorts[i][j].end);
69-
}
70-
}
71-
});
7249
});

test/ingress/dockerfileSamples/sample1.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Modified example from Azure-Samples/acr-build-helloworld-node
2-
FROM node:15-alpine
2+
FROM node:lts-alpine
33

44
COPY . /src
55
RUN cd /src && npm install

test/ingress/dockerfileSamples/sample2.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Modified example from Azure-Samples/acr-build-helloworld-node
2-
FROM node:15-alpine
2+
FROM node:lts-alpine
33

44
COPY . /src
55
RUN cd /src && npm install

test/ingress/dockerfileSamples/sample3.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Modified example from Azure-Samples/acr-build-helloworld-node
2-
FROM node:15-alpine
2+
FROM node:lts-alpine
33

44
COPY . /src
55
RUN cd /src && npm install

test/ingress/dockerfileSamples/sample4.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Modified example from Azure-Samples/acr-build-helloworld-node
2-
FROM node:15-alpine
2+
FROM node:lts-alpine
33

44
COPY . /src
55
RUN cd /src && npm install

0 commit comments

Comments
 (0)