Skip to content

Commit b64b3b6

Browse files
authored
Misc. improvements to ingress logic (#783)
1 parent 1064e44 commit b64b3b6

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

src/commands/image/imageSource/ContainerAppUpdateStep.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,41 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6+
import { type Ingress } from "@azure/arm-appcontainers";
67
import { AzureWizardExecuteStep, GenericParentTreeItem, GenericTreeItem, activityFailContext, activityFailIcon, activitySuccessContext, activitySuccessIcon, createUniversallyUniqueContextValue, nonNullProp, type ExecuteActivityOutput } from "@microsoft/vscode-azext-utils";
78
import { type Progress } from "vscode";
89
import { ext } from "../../../extensionVariables";
910
import { getContainerEnvelopeWithSecrets, type ContainerAppModel } from "../../../tree/ContainerAppItem";
1011
import { localize } from "../../../utils/localize";
12+
import { type IngressContext } from "../../ingress/IngressContext";
13+
import { enabledIngressDefaults } from "../../ingress/enableIngress/EnableIngressStep";
1114
import { updateContainerApp } from "../../updateContainerApp";
1215
import { type ImageSourceContext } from "./ImageSourceContext";
1316
import { getContainerNameForImage } from "./containerRegistry/getContainerNameForImage";
1417

15-
export class ContainerAppUpdateStep<T extends ImageSourceContext> extends AzureWizardExecuteStep<T> {
18+
export class ContainerAppUpdateStep<T extends ImageSourceContext & IngressContext> extends AzureWizardExecuteStep<T> {
1619
public priority: number = 680;
1720

1821
public async execute(context: T, progress: Progress<{ message?: string | undefined; increment?: number | undefined }>): Promise<void> {
1922
const containerApp: ContainerAppModel = nonNullProp(context, 'containerApp');
2023
const containerAppEnvelope = await getContainerEnvelopeWithSecrets(context, context.subscription, containerApp);
2124

25+
let ingress: Ingress | undefined;
26+
if (context.enableIngress) {
27+
ingress = {
28+
...enabledIngressDefaults,
29+
...containerAppEnvelope.configuration.ingress ?? {}, // Overwrite any default settings if we already have previous configurations set
30+
external: context.enableExternal ?? containerAppEnvelope.configuration.ingress?.external,
31+
targetPort: context.targetPort ?? containerAppEnvelope.configuration.ingress?.targetPort,
32+
};
33+
} else if (context.enableIngress === false) {
34+
ingress = undefined;
35+
} else {
36+
// If enableIngress is not set, just default to the previous settings if they exist
37+
ingress = containerAppEnvelope.configuration.ingress;
38+
}
39+
40+
containerAppEnvelope.configuration.ingress = ingress;
2241
containerAppEnvelope.configuration.secrets = context.secrets;
2342
containerAppEnvelope.configuration.registries = context.registryCredentials;
2443

src/commands/ingress/IngressPromptStep.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,28 +59,31 @@ export async function tryConfigureIngressUsingDockerfile(context: IngressContext
5959
return;
6060
}
6161

62-
if (!context.dockerfileExposePorts) {
63-
context.enableIngress = false;
64-
context.enableExternal = false;
65-
} else if (context.dockerfileExposePorts) {
62+
if (context.dockerfileExposePorts) {
6663
context.enableIngress = true;
6764
context.enableExternal = true;
6865
context.targetPort = getDefaultPort(context);
66+
} else {
67+
context.enableIngress = false;
68+
context.enableExternal = false;
6969
}
7070

71-
// If a container app already exists, activity children will be added automatically in later execute steps
72-
if (!context.containerApp) {
73-
context.activityChildren?.push(
74-
new GenericTreeItem(undefined, {
75-
contextValue: createUniversallyUniqueContextValue(['ingressPromptStepSuccessItem', activitySuccessContext]),
76-
label: context.enableIngress ?
77-
localize('ingressEnableLabel', 'Enable ingress on port {0} (from Dockerfile configuration)', context.targetPort) :
78-
localize('ingressDisableLabel', 'Disable ingress (from Dockerfile configuration)'),
79-
iconPath: activitySuccessIcon
80-
})
81-
);
71+
const currentExternalEnabled: boolean | undefined = context.containerApp?.configuration?.ingress?.external;
72+
const currentTargetPort: number | undefined = context.containerApp?.configuration?.ingress?.targetPort;
73+
if (currentExternalEnabled === context.enableExternal && currentTargetPort === context.targetPort) {
74+
return;
8275
}
8376

77+
context.activityChildren?.push(
78+
new GenericTreeItem(undefined, {
79+
contextValue: createUniversallyUniqueContextValue(['ingressPromptStepSuccessItem', activitySuccessContext]),
80+
label: context.enableIngress ?
81+
localize('ingressEnableLabel', 'Enable ingress on port {0} (from Dockerfile configuration)', context.targetPort) :
82+
localize('ingressDisableLabel', 'Disable ingress (from Dockerfile configuration)'),
83+
iconPath: activitySuccessIcon
84+
})
85+
);
86+
8487
ext.outputChannel.appendLog(context.enableIngress ?
8588
localize('ingressEnabledLabel', 'Detected ingress on port {0} using Dockerfile configuration.', context.targetPort) :
8689
localize('ingressDisabledLabel', 'Detected no ingress using Dockerfile configuration.')

src/commands/ingress/enableIngress/EnableIngressStep.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ import { localize } from "../../../utils/localize";
1010
import { updateContainerApp } from "../../updateContainerApp";
1111
import { type IngressBaseContext } from "../IngressContext";
1212

13+
export const enabledIngressDefaults = {
14+
transport: 'auto',
15+
allowInsecure: false,
16+
traffic: [
17+
{
18+
weight: 100,
19+
latestRevision: true
20+
}
21+
],
22+
};
23+
1324
export class EnableIngressStep extends AzureWizardExecuteStep<IngressBaseContext> {
1425
public priority: number = 750;
1526

@@ -18,19 +29,13 @@ export class EnableIngressStep extends AzureWizardExecuteStep<IngressBaseContext
1829

1930
const containerApp = nonNullProp(context, 'containerApp');
2031
const ingress: Ingress = {
32+
...enabledIngressDefaults,
33+
...containerApp.configuration?.ingress ?? {},
2134
targetPort: context.targetPort,
2235
external: context.enableExternal,
23-
transport: 'auto',
24-
allowInsecure: false,
25-
traffic: [
26-
{
27-
weight: 100,
28-
latestRevision: true
29-
}
30-
],
3136
}
3237

33-
await updateContainerApp(context, context.subscription, containerApp, { configuration: { ingress: ingress as Ingress | undefined } });
38+
context.containerApp = await updateContainerApp(context, context.subscription, containerApp, { configuration: { ingress: ingress as Ingress | undefined } });
3439
}
3540

3641
public shouldExecute(context: IngressBaseContext): boolean {

0 commit comments

Comments
 (0)