-
Notifications
You must be signed in to change notification settings - Fork 17
Add command updateImage for updating a container image via revision draft
#477
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
14 commits
Select commit
Hold shift + click to select a range
6060f1a
Impl
MicroFish91 d80b108
Misc
MicroFish91 b3bb7ad
More
MicroFish91 f1c4700
Impl
MicroFish91 1880dd7
Updates
MicroFish91 2b8aa8b
Update execute priority
MicroFish91 c26a876
Import type
MicroFish91 b8a1d5b
Merge branch 'mwf/image-organize' of https://github.com/microsoft/vsc…
MicroFish91 53de3bf
Import type
MicroFish91 ce16017
Update
MicroFish91 822d394
Merge with main
MicroFish91 8c71fbb
Feedback
MicroFish91 d309d97
Update
MicroFish91 ccea660
Update
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import type { Revision } from "@azure/arm-appcontainers"; | ||
| import { nonNullProp, randomUtils } from "@microsoft/vscode-azext-utils"; | ||
| import type { Progress } from "vscode"; | ||
| import { ext } from "../../../extensionVariables"; | ||
| import type { ContainerAppItem, ContainerAppModel } from "../../../tree/ContainerAppItem"; | ||
| import type { RevisionsItemModel } from "../../../tree/revisionManagement/RevisionItem"; | ||
| import { localize } from "../../../utils/localize"; | ||
| import { getParentResourceFromItem } from "../../../utils/revisionDraftUtils"; | ||
| import { RevisionDraftUpdateBaseStep } from "../../revisionDraft/RevisionDraftUpdateBaseStep"; | ||
| import { getContainerNameForImage } from "../imageSource/containerRegistry/getContainerNameForImage"; | ||
| import type { UpdateImageContext } from "./updateImage"; | ||
|
|
||
| export class UpdateImageDraftStep<T extends UpdateImageContext> extends RevisionDraftUpdateBaseStep<T> { | ||
| public priority: number = 490; | ||
|
|
||
| constructor(baseItem: ContainerAppItem | RevisionsItemModel) { | ||
| super(baseItem); | ||
| } | ||
|
|
||
| public async execute(context: UpdateImageContext, progress: Progress<{ message?: string | undefined; increment?: number | undefined }>): Promise<void> { | ||
| progress.report({ message: localize('updatingImage', 'Updating image (draft)...') }); | ||
|
|
||
| this.revisionDraftTemplate.containers = []; | ||
| this.revisionDraftTemplate.containers.push({ | ||
| env: context.environmentVariables, | ||
| image: context.image, | ||
| // We need the revision draft to always show up as having unsaved changes, we can ensure this by adding a unique ID at end of the container name | ||
| name: getContainerNameForImage(nonNullProp(context, 'image')) + `-${randomUtils.getRandomHexString(5)}`, | ||
| }); | ||
|
|
||
| this.updateRevisionDraftWithTemplate(); | ||
|
|
||
| const parentResource: ContainerAppModel | Revision = getParentResourceFromItem(this.baseItem); | ||
| ext.outputChannel.appendLog(localize('updatedImage', 'Updated container app "{0}" with image "{1}" (draft).', parentResource.name, context.image)); | ||
| } | ||
|
|
||
| public shouldExecute(context: UpdateImageContext): boolean { | ||
| return !!context.containerApp && !!context.image; | ||
| } | ||
| } |
66 changes: 66 additions & 0 deletions
66
src/commands/image/updateImage/UpdateRegistryAndSecretsStep.ts
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,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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| 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); | ||
| } | ||
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,77 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { KnownActiveRevisionsMode, Revision } from "@azure/arm-appcontainers"; | ||
| import { VerifyProvidersStep } from "@microsoft/vscode-azext-azureutils"; | ||
| import { AzureWizard, AzureWizardExecuteStep, AzureWizardPromptStep, ExecuteActivityContext, IActionContext, createSubscriptionContext } from "@microsoft/vscode-azext-utils"; | ||
| import { webProvider } from "../../../constants"; | ||
| import { ext } from "../../../extensionVariables"; | ||
| import type { ContainerAppItem, ContainerAppModel } from "../../../tree/ContainerAppItem"; | ||
| import type { RevisionDraftItem } from "../../../tree/revisionManagement/RevisionDraftItem"; | ||
| import type { RevisionItem } from "../../../tree/revisionManagement/RevisionItem"; | ||
| import { createActivityContext } from "../../../utils/activity/activityUtils"; | ||
| import { localize } from "../../../utils/localize"; | ||
| import { pickContainerApp } from "../../../utils/pickItem/pickContainerApp"; | ||
| import { pickRevision, pickRevisionDraft } from "../../../utils/pickItem/pickRevision"; | ||
| import { getParentResourceFromItem } from "../../../utils/revisionDraftUtils"; | ||
| import type { ImageSourceBaseContext } from "../imageSource/ImageSourceBaseContext"; | ||
| import { ImageSourceListStep } from "../imageSource/ImageSourceListStep"; | ||
| import { UpdateImageDraftStep } from "./UpdateImageDraftStep"; | ||
| import { UpdateRegistryAndSecretsStep } from "./UpdateRegistryAndSecretsStep"; | ||
|
|
||
| export type UpdateImageContext = ImageSourceBaseContext & ExecuteActivityContext; | ||
|
|
||
| /** | ||
| * An ACA exclusive command that updates the container app or revision's container image via revision draft. | ||
| * The draft must be deployed for the changes to take effect and can be used to bundle together template changes. | ||
| */ | ||
| export async function updateImage(context: IActionContext, node?: ContainerAppItem | RevisionItem): Promise<void> { | ||
| let item: ContainerAppItem | RevisionItem | RevisionDraftItem | undefined = node; | ||
| if (!item) { | ||
| const containerAppItem: ContainerAppItem = await pickContainerApp(context); | ||
|
|
||
| if (containerAppItem.containerApp.revisionsMode === KnownActiveRevisionsMode.Single) { | ||
| item = containerAppItem; | ||
| } else { | ||
| if (ext.revisionDraftFileSystem.doesContainerAppsItemHaveRevisionDraft(containerAppItem)) { | ||
| item = await pickRevisionDraft(context, containerAppItem); | ||
| } else { | ||
| item = await pickRevision(context, containerAppItem); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const { subscription, containerApp } = item; | ||
|
|
||
| const wizardContext: UpdateImageContext = { | ||
| ...context, | ||
| ...createSubscriptionContext(subscription), | ||
| ...await createActivityContext(), | ||
| subscription, | ||
| containerApp | ||
| }; | ||
|
|
||
| const promptSteps: AzureWizardPromptStep<UpdateImageContext>[] = [ | ||
| new ImageSourceListStep(), | ||
| ]; | ||
|
|
||
| const executeSteps: AzureWizardExecuteStep<UpdateImageContext>[] = [ | ||
| new VerifyProvidersStep([webProvider]), | ||
| new UpdateRegistryAndSecretsStep(), | ||
| new UpdateImageDraftStep(item) | ||
| ]; | ||
|
|
||
| const parentResource: ContainerAppModel | Revision = getParentResourceFromItem(item); | ||
|
|
||
| const wizard: AzureWizard<UpdateImageContext> = new AzureWizard(wizardContext, { | ||
| title: localize('updateImage', 'Update container image for "{0}" (draft)', parentResource.name), | ||
| promptSteps, | ||
| executeSteps, | ||
| showLoadingPrompt: true | ||
| }); | ||
|
|
||
| await wizard.prompt(); | ||
| await wizard.execute(); | ||
| } |
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
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.
I kind of find this hard to read:
I feel like a use of a divider or something could really help readability. I won't block for that, just wanted to point it out.
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.
Made an engineering issue