Skip to content

Commit

Permalink
Adding hosting env check for (#149)
Browse files Browse the repository at this point in the history
* Adding hosting env check for dapr ext

Signed-off-by: MD Ashique <[email protected]>

* Adding hosting plan check

Signed-off-by: MD Ashique <[email protected]>

* Adding check before loading the extension

Signed-off-by: MD Ashique <[email protected]>

* Adding check before loading the extension

Signed-off-by: MD Ashique <[email protected]>

* Adding sku check for consumption plan

Signed-off-by: MD Ashique <[email protected]>

---------

Signed-off-by: MD Ashique <[email protected]>
  • Loading branch information
ASHIQUEMD authored Sep 19, 2023
1 parent 4f6817c commit d8ab3c7
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 1 deletion.
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)
{
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

0 comments on commit d8ab3c7

Please sign in to comment.