Skip to content

Commit

Permalink
Merge branch 'refs/heads/endgame-202404' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
wangmingliang-ms committed Apr 30, 2024
2 parents 9cf6b21 + 56ddf5f commit 816918b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ public com.azure.resourcemanager.appservice.models.FunctionApp createResourceInA
}
return app;
}, Status.CREATING));
final Action<AzResource> deploy = Optional.ofNullable(AzureActionManager.getInstance().getAction(AzResource.DEPLOY))
// deploy action did not fit docker runtime function app
final Action<AzResource> deploy = getRuntime().isDocker() ? null : Optional.ofNullable(AzureActionManager.getInstance().getAction(AzResource.DEPLOY))
.map(action -> action.bind(this)).orElse(null);
messager.success(AzureString.format("Function App({0}) is successfully created", name), deploy);
Optional.ofNullable(getEnvironment()).ifPresent(ContainerAppsEnvironment::refresh); // refresh container apps environment after create container hosted function app
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

package com.microsoft.azure.toolkit.lib.common.exception;

import com.microsoft.azure.toolkit.lib.common.utils.StreamingLogSupport;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nonnull;

@Getter
public class StreamingDiagnosticsException extends AzureToolkitRuntimeException {
private final StreamingLogSupport streamingLog;

public StreamingDiagnosticsException(@Nonnull final String cause, @Nonnull final StreamingLogSupport streamingLog) {
super(cause);
this.streamingLog = streamingLog;
}

public StreamingDiagnosticsException(@Nonnull final String cause, @Nonnull final StreamingLogSupport streamingLog, final Object... actions) {
super(cause, actions);
this.streamingLog = streamingLog;
}

public StreamingDiagnosticsException(final String error, @NotNull final Throwable cause, @Nonnull final StreamingLogSupport streamingLog, final Object... actions) {
super(error, cause, actions);
this.streamingLog = streamingLog;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.microsoft.azure.toolkit.lib.common.action.Action;
import com.microsoft.azure.toolkit.lib.common.action.AzureActionManager;
import com.microsoft.azure.toolkit.lib.common.bundle.AzureString;
import com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException;
import com.microsoft.azure.toolkit.lib.common.messager.AzureMessager;
import com.microsoft.azure.toolkit.lib.common.messager.IAzureMessager;
import com.microsoft.azure.toolkit.lib.common.model.AzResource;
Expand Down Expand Up @@ -213,6 +214,9 @@ public void buildImageIfNeeded(ImageConfig imageConfig) {
final ContainerRegistry registry = getOrCreateRegistry(imageConfig);
tarSourceIfNeeded(buildConfig);
final RegistryTaskRun run = registry.buildImage(imageConfig.getAcrImageNameWithTag(), buildConfig.getSource());
if (Objects.isNull(run)) {
throw new AzureToolkitRuntimeException("ACR is not ready, Failed to build image through ACR.");
}
fullImageName = registry.waitForImageBuilding(run);
} else {
if (Files.isDirectory(buildConfig.source)) {
Expand Down Expand Up @@ -268,7 +272,10 @@ private ContainerRegistry getOrCreateRegistry(final ImageConfig config) {
}
}
}
if (!registry.isAdminUserEnabled()) {// enable admin user
if (registry.isDraftForCreating()) {
((ContainerRegistryDraft) registry).setAdminUserEnabled(true);
((ContainerRegistryDraft) registry).commit();
} else if (!registry.isAdminUserEnabled()) {// enable admin user
AzureMessager.getMessager().info(AzureString.format("Enabling admin user for container registry %s.", registry.getName()));
registry.enableAdminUser();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.microsoft.azure.toolkit.lib.common.action.AzureActionManager;
import com.microsoft.azure.toolkit.lib.common.bundle.AzureString;
import com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException;
import com.microsoft.azure.toolkit.lib.common.exception.StreamingDiagnosticsException;
import com.microsoft.azure.toolkit.lib.common.messager.AzureMessager;
import com.microsoft.azure.toolkit.lib.common.model.AbstractAzResource;
import com.microsoft.azure.toolkit.lib.common.model.AbstractAzResourceModule;
Expand Down Expand Up @@ -128,7 +129,7 @@ public String getLogStreamEndpoint() {
if (Objects.isNull(remoteEnv)) {
throw new AzureToolkitRuntimeException(AzureString.format("resource ({0}) not found", getName()).toString());
}
final String baseUrl = String.format("https://%s.azurecontainerapps.dev", remoteEnv.location());
final String baseUrl = String.format("https://%s.azurecontainerapps.dev", Region.fromName(remoteEnv.location()).getName());
return String.format("%s/subscriptions/%s/resourceGroups/%s/managedEnvironments/%s/eventstream",
baseUrl, getSubscriptionId(), getResourceGroupName(), getName());
}
Expand Down Expand Up @@ -197,7 +198,7 @@ public String waitForImageBuilding(final BuildResource build) {
final UrlStreamingLog urlStreamingLog = UrlStreamingLog.builder()
.authorization("Bearer " + getImageBuildAuthToken(build)).endpoint(build.logStreamEndpoint()).name(build.name()).build();
final Action<StreamingLogSupport> viewLogInToolkit = AzureActionManager.getInstance().getAction(StreamingLogSupport.OPEN_STREAMING_LOG)
.bind(urlStreamingLog).withLabel("Open streaming logs in toolkit");
.bind(urlStreamingLog).withLabel("Open streaming logs");
AzureMessager.getMessager().info(AzureString.format("Waiting for the build %s to be provisioned...", build.name()), viewLogInToolkit);
BuildProvisioningState provisioningState = build.provisioningState();
while (waitingProvisioningStates.contains(provisioningState)) {
Expand All @@ -220,7 +221,8 @@ public String waitForImageBuilding(final BuildResource build) {
buildStatus = build.buildStatus();
}
if (errorBuildingStates.contains(buildStatus)) {
throw new AzureToolkitRuntimeException(String.format("The build %s is provisioned properly but its build status is %s", build.name(), buildStatus));
final String message = String.format("The build %s is provisioned properly but its build status is %s", build.name(), buildStatus);
throw new StreamingDiagnosticsException(message, urlStreamingLog);
}
final String image = build.destinationContainerRegistry().image();
AzureMessager.getMessager().info(AzureString.format("Build %s is completed successfully, image %s is built.", build.name(), image));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.microsoft.azure.toolkit.lib.common.action.AzureActionManager;
import com.microsoft.azure.toolkit.lib.common.bundle.AzureString;
import com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException;
import com.microsoft.azure.toolkit.lib.common.exception.StreamingDiagnosticsException;
import com.microsoft.azure.toolkit.lib.common.messager.AzureMessager;
import com.microsoft.azure.toolkit.lib.common.model.AbstractAzResource;
import com.microsoft.azure.toolkit.lib.common.model.AbstractAzResourceModule;
Expand All @@ -33,8 +34,6 @@
import com.microsoft.azure.toolkit.lib.containerregistry.model.Sku;
import lombok.Getter;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -47,7 +46,6 @@

public class ContainerRegistry extends AbstractAzResource<ContainerRegistry, AzureContainerRegistryServiceSubscription, Registry> {
public static final String ACR_IMAGE_SUFFIX = ".azurecr.io";
private static final Logger log = LoggerFactory.getLogger(ContainerRegistry.class);
@Getter
private final RepositoryModule repositoryModule;

Expand Down Expand Up @@ -137,6 +135,10 @@ public String getType() {
return remoteOptional().map(Registry::type).orElse(null);
}

/**
* @return image build task run, null if registry is not ready
*/
@Nullable
public RegistryTaskRun buildImage(final String imageNameWithTag, final Path sourceTar) {
return this.remoteOptional().map(r -> {
// upload tar.gz file
Expand All @@ -158,7 +160,7 @@ public RegistryTaskRun buildImage(final String imageNameWithTag, final Path sour
}

@Nullable
public String waitForImageBuilding(final RegistryTaskRun run) {
public String waitForImageBuilding(@Nonnull final RegistryTaskRun run) {
final ImmutableSet<RunStatus> errorStatus = ImmutableSet.of(RunStatus.FAILED, RunStatus.CANCELED, RunStatus.ERROR, RunStatus.TIMEOUT);
final ImmutableSet<RunStatus> waitingStatus = ImmutableSet.of(RunStatus.QUEUED, RunStatus.STARTED, RunStatus.RUNNING);

Expand All @@ -171,7 +173,7 @@ public String waitForImageBuilding(final RegistryTaskRun run) {
final Action<String> viewLogInBrowser = openUrl.bind(logSasUrl).withLabel("Open streaming logs in browser");
final RegistryTaskRunStreamingLog urlStreamingLog = RegistryTaskRunStreamingLog.builder().logSasUrl(logSasUrl).task(run).build();
final Action<StreamingLogSupport> viewLogInToolkit = AzureActionManager.getInstance().getAction(StreamingLogSupport.OPEN_STREAMING_LOG)
.bind(urlStreamingLog).withLabel("Open streaming logs in toolkit");
.bind(urlStreamingLog).withLabel("Open streaming logs");
AzureMessager.getMessager().info(AzureString.format("Waiting for image building task run (%s) to be completed...", run.runId()), viewLogInToolkit, viewLogInBrowser);
RunStatus status = run.status();
while (waitingStatus.contains(status)) {
Expand All @@ -181,11 +183,15 @@ public String waitForImageBuilding(final RegistryTaskRun run) {
}
final List<ImageDescriptor> images = run.innerModel().outputImages();
if (errorStatus.contains(status) || CollectionUtils.isEmpty(images)) {
throw new AzureToolkitRuntimeException(String.format("Failed to build image (status: %s). View logs at %s for more details.", status, logSasUrl));
final String message = String.format("Failed to build image (status: %s). View logs at %s for more details.", status, logSasUrl);
throw new StreamingDiagnosticsException(message, urlStreamingLog);
}
final ImageDescriptor image = images.get(0);
final String fullImageName = String.format("%s/%s:%s", image.registry(), image.repository(), image.tag());
AzureMessager.getMessager().info(AzureString.format("Image building task run %s is completed successfully, image %s is built.", run.runId(), fullImageName), viewLogInBrowser);
AzureMessager.getMessager().info(AzureString.format("Image building task run %s is completed successfully, image %s is built.", run.runId(), fullImageName), viewLogInToolkit, viewLogInBrowser);
// refresh to load newly build images.
this.refresh();
ResourceManagerUtils.sleep(Duration.ofSeconds(3));
return fullImageName;
}
}

0 comments on commit 816918b

Please sign in to comment.