Skip to content
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

Adding hosting env check for #149

Merged
merged 5 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Microsoft.Azure.WebJobs.Extensions.Dapr/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ public class EnvironmentKeys
public const string AppPort = "DAPR_APP_PORT";
public const string DisableSidecarMetadataCheck = "DAPR_DISABLE_SIDECAR_METADATA_CHECK";
public const string SidecarHttpPort = "DAPR_HTTP_PORT";
public const string AzureWebsiteInstanceId = "WEBSITE_INSTANCE_ID";
public const string AzureWebsiteSku = "WEBSITE_SKU";
public const string ContainerName = "CONTAINER_NAME";
public const string WebsitePodName = "WEBSITE_POD_NAME";
public const string LegionServiceHost = "LEGION_SERVICE_HOST";
public const string ManagedEnvironment = "MANAGED_ENVIRONMENT";
}

public class HostingPlanSkuConstants
{
public const string ElasticPremiumSku = "ElasticPremium";
public const string DynamicSku = "Dynamic";
public const string FlexConsumptionSku = "FlexConsumption";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Microsoft.Azure.WebJobs.Extensions.Dapr
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Dapr.Services;
using Microsoft.Azure.WebJobs.Extensions.Dapr.Utils;
using Microsoft.Extensions.DependencyInjection;

/// <summary>
Expand All @@ -27,12 +28,21 @@ public static IWebJobsBuilder AddDapr(this IWebJobsBuilder builder)
throw new ArgumentNullException(nameof(builder));
}

var serviceProvider = builder.Services.BuildServiceProvider();
var nameResolver = serviceProvider.GetRequiredService<INameResolver>();

if (!EnvironmentUtils.ShouldRegisterDaprExtension(nameResolver))
{
return builder;
}

builder.AddExtension<DaprExtensionConfigProvider>()
.Services
.AddSingleton<IDaprServiceClient, DaprServiceClient>()
.AddSingleton<IDaprServiceListener, DaprServiceListener>()
.AddSingleton<IDaprClient, DaprHttpClient>()
.AddHttpClient();

return builder;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ------------------------------------------------------------

namespace Microsoft.Azure.WebJobs.Extensions.Dapr.Utils
{
using System;
using Microsoft.Azure.WebJobs;

internal static class EnvironmentUtils
{
public static bool IsAppService(INameResolver nameResolver)
ASHIQUEMD marked this conversation as resolved.
Show resolved Hide resolved
{
return !string.IsNullOrEmpty(nameResolver.Resolve(Constants.EnvironmentKeys.AzureWebsiteInstanceId));
}

public static bool IsWindowsElasticPremium(INameResolver nameResolver)
{
string value = nameResolver.Resolve(Constants.EnvironmentKeys.AzureWebsiteSku);
return string.Equals(value, Constants.HostingPlanSkuConstants.ElasticPremiumSku, StringComparison.OrdinalIgnoreCase);
}

public static bool IsLinuxConsumptionOnLegion(INameResolver nameResolver)
{
return !IsAppService(nameResolver) &&
(!string.IsNullOrEmpty(nameResolver.Resolve(Constants.EnvironmentKeys.ContainerName)) ||
!string.IsNullOrEmpty(nameResolver.Resolve(Constants.EnvironmentKeys.WebsitePodName))) &&
!string.IsNullOrEmpty(nameResolver.Resolve(Constants.EnvironmentKeys.LegionServiceHost));
}

public static bool IsFlexConsumptionSku(INameResolver nameResolver)
{
string value = nameResolver.Resolve(Constants.EnvironmentKeys.AzureWebsiteSku);
if (string.Equals(value, Constants.HostingPlanSkuConstants.FlexConsumptionSku, StringComparison.OrdinalIgnoreCase))
{
return true;
}

// when in placeholder mode, site settings like SKU are not available
// to enable this check to run in both modes, we check additional settings
return IsLinuxConsumptionOnLegion(nameResolver);
}

public static bool IsLinuxConsumptionOnAtlas(INameResolver nameResolver)
{
return !IsAppService(nameResolver) &&
!string.IsNullOrEmpty(nameResolver.Resolve(Constants.EnvironmentKeys.ContainerName)) &&
string.IsNullOrEmpty(nameResolver.Resolve(Constants.EnvironmentKeys.LegionServiceHost));
}

public static bool IsManagedAppEnvironment(INameResolver nameResolver)
{
return !string.IsNullOrEmpty(nameResolver.Resolve(Constants.EnvironmentKeys.ManagedEnvironment));
}

public static bool IsAnyLinuxConsumption(INameResolver nameResolver)
{
return (IsLinuxConsumptionOnAtlas(nameResolver) || IsFlexConsumptionSku(nameResolver)) && !IsManagedAppEnvironment(nameResolver);
}

public static bool IsWindowsConsumption(INameResolver nameResolver)
{
string value = nameResolver.Resolve(Constants.EnvironmentKeys.AzureWebsiteSku);
return string.Equals(value, Constants.HostingPlanSkuConstants.DynamicSku, StringComparison.OrdinalIgnoreCase);
}

public static bool IsConsumptionSku(INameResolver nameResolver)
{
return IsWindowsConsumption(nameResolver) || IsAnyLinuxConsumption(nameResolver) || IsFlexConsumptionSku(nameResolver);
}

public static bool ShouldRegisterDaprExtension(INameResolver nameResolver)
{
return !IsWindowsElasticPremium(nameResolver) && !IsAppService(nameResolver) && !IsConsumptionSku(nameResolver);
}
}
}
2 changes: 1 addition & 1 deletion test/DaprExtensionTests/DaprTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ public DaprTestBase(ITestOutputHelper output,

this.functionsHost = new HostBuilder()
.ConfigureLogging(loggingBuilder => loggingBuilder.AddProvider(this.logProvider))
.ConfigureWebJobs(webJobsBuilder => webJobsBuilder.AddDapr())
.ConfigureServices(
collection =>
{
collection.AddSingleton<INameResolver>(this.nameResolver);
collection.AddSingleton<ITypeLocator>(this.typeLocator);
})
.ConfigureWebJobs(webJobsBuilder => webJobsBuilder.AddDapr())
.Build();
this.daprRuntime = new DaprRuntimeEmulator(DaprPort);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace DaprExtensionTests.Tests
{
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Extensions.Dapr;
using Xunit;
using Xunit.Abstractions;

public class ExtensionUnsupportedHostingPlanTests : DaprTestBase
{
private static readonly IDictionary<string, string> EnvironmentVariables = new Dictionary<string, string>()
{
{ Constants.EnvironmentKeys.AzureWebsiteInstanceId, "someValue" }
};

public ExtensionUnsupportedHostingPlanTests(ITestOutputHelper output)
: base(output, EnvironmentVariables)
{
this.AddFunctions(typeof(Functions));
}

public static IEnumerable<object[]> GetTheoryDataInputs() => new List<object[]>
{
new object[] { nameof(Functions.ReturnInt), 42 }
};

[Theory]
[MemberData(nameof(GetTheoryDataInputs))]
public async Task ValidateDaprExtensionNotSupportedOnAppServiceHostingPlan(string methodName, object input)
{
await Assert.ThrowsAsync<HttpRequestException>(async () => await this.SendRequestAsync(
HttpMethod.Post,
$"http://localhost:3001/{methodName}",
jsonContent: input));
}
}

static class Functions
{
public static int ReturnInt([DaprBindingTrigger] int input) => input;
}
}
Loading
Loading