-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathUpdateRegistryAndSecretsStep.ts
More file actions
66 lines (53 loc) · 3.35 KB
/
UpdateRegistryAndSecretsStep.ts
File metadata and controls
66 lines (53 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { RegistryCredentials, Secret } from "@azure/arm-appcontainers";
import { AzureWizardExecuteStep, nonNullProp } from "@microsoft/vscode-azext-utils";
import * as deepEqual from "deep-eql";
import type { Progress } from "vscode";
import { ext } from "../../../extensionVariables";
import { ContainerAppModel, getContainerEnvelopeWithSecrets } from "../../../tree/ContainerAppItem";
import { localize } from "../../../utils/localize";
import { updateContainerApp } from "../../updateContainerApp";
import type { UpdateImageContext } from "./updateImage";
export class UpdateRegistryAndSecretsStep extends AzureWizardExecuteStep<UpdateImageContext> {
public priority: number = 480;
public async execute(context: UpdateImageContext, progress: Progress<{ message?: string | undefined; increment?: number | undefined }>): Promise<void> {
const containerApp: ContainerAppModel = nonNullProp(context, 'containerApp');
const containerAppEnvelope = await getContainerEnvelopeWithSecrets(context, context.subscription, containerApp);
// If the credentials have not changed, we can skip this update
if (
this.areSecretsDeepEqual(containerAppEnvelope.configuration.secrets, context.secrets) &&
this.areRegistriesDeepEqual(containerAppEnvelope.configuration.registries, context.registries)
) {
return;
}
progress.report({ message: localize('configuringSecrets', 'Configuring registry secrets...') });
containerAppEnvelope.configuration.secrets = context.secrets;
containerAppEnvelope.configuration.registries = context.registries;
await updateContainerApp(context, context.subscription, containerAppEnvelope);
ext.outputChannel.appendLog(localize('updatedSecrets', 'Updated container app "{0}" with new registry secrets.', containerApp.name));
}
public shouldExecute(context: UpdateImageContext): boolean {
return !!context.registries && !!context.secrets;
}
private areSecretsDeepEqual(originalSecrets: Secret[] | undefined, newSecrets: Secret[] | undefined): boolean {
originalSecrets?.sort((a, b) => sortAlphabeticallyByKey(a, b, 'name'));
newSecrets?.sort((a, b) => sortAlphabeticallyByKey(a, b, 'name'));
return deepEqual(originalSecrets, newSecrets);
}
private areRegistriesDeepEqual(originalRegistries: RegistryCredentials[] | undefined, newRegistries: RegistryCredentials[] | undefined): boolean {
originalRegistries?.sort((a, b) => sortAlphabeticallyByKey(a, b, 'passwordSecretRef'));
newRegistries?.sort((a, b) => sortAlphabeticallyByKey(a, b, 'passwordSecretRef'));
return deepEqual(originalRegistries, newRegistries);
}
}
function sortAlphabeticallyByKey<T extends Secret | RegistryCredentials>(a: T, b: T, key: keyof T): number {
if (typeof a[key] !== 'string' || typeof b[key] !== 'string') {
return 0;
}
const valOne = a[key] as string;
const valTwo = b[key] as string;
return valOne.localeCompare(valTwo);
}