Skip to content

Commit 92fc1c4

Browse files
committed
Updates
1 parent fe4f955 commit 92fc1c4

12 files changed

+116
-117
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/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/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

test/ingress/dockerfileSamples/sample5.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/sample6.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)