Skip to content

Commit

Permalink
Support for Default Azure Identity
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Karpala <[email protected]>
  • Loading branch information
karpikpl committed Jun 11, 2024
1 parent a00c272 commit 24eae81
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.11.4" />
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.40.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
Expand Down
7 changes: 6 additions & 1 deletion src/Scaler.Demo/OrderGenerator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Azure.Identity;
using Bogus;
using Bogus.DataSets;
using Keda.CosmosDb.Scaler.Demo.Shared;
Expand Down Expand Up @@ -85,7 +86,11 @@ private static bool ReadIsSingleArticle()

private static async Task CreateOrdersAsync(int count, bool isSingleArticle)
{
Container container = new CosmosClient(_cosmosDbConfig.Connection)
using var cosmosClient = _cosmosDbConfig.Connection.Contains("AccountKey")
? new CosmosClient(_cosmosDbConfig.Connection, new CosmosClientOptions { ConnectionMode = ConnectionMode.Gateway })
: new CosmosClient(_cosmosDbConfig.Connection, new DefaultAzureCredential(), new CosmosClientOptions { ConnectionMode = ConnectionMode.Direct });

Container container = cosmosClient
.GetContainer(_cosmosDbConfig.DatabaseId, _cosmosDbConfig.ContainerId);

int remainingCount = count;
Expand Down
1 change: 1 addition & 0 deletions src/Scaler/Keda.CosmosDb.Scaler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.11.4" />
<PackageReference Include="Grpc.AspNetCore" Version="2.63.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.40.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
Expand Down
16 changes: 8 additions & 8 deletions src/Scaler/Services/CosmosDbFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using Azure.Identity;
using Microsoft.Azure.Cosmos;

namespace Keda.CosmosDb.Scaler
Expand All @@ -9,14 +10,13 @@ internal sealed class CosmosDbFactory
// maintain a single instance of CosmosClient per lifetime of the application.
private readonly ConcurrentDictionary<string, CosmosClient> _cosmosClientCache = new();

public CosmosClient GetCosmosClient(string connection)
{
return _cosmosClientCache.GetOrAdd(connection, CreateCosmosClient);
}
public CosmosClient GetCosmosClient(string connection) =>
_cosmosClientCache.GetOrAdd(connection, CreateCosmosClient);

private CosmosClient CreateCosmosClient(string connection) =>
connection.Contains("Accountkey") ?
new CosmosClient(connection, new CosmosClientOptions { ConnectionMode = ConnectionMode.Gateway }) :
new CosmosClient(connection, new DefaultAzureCredential(), new CosmosClientOptions { ConnectionMode = ConnectionMode.Direct });

private CosmosClient CreateCosmosClient(string connection)
{
return new CosmosClient(connection, new CosmosClientOptions { ConnectionMode = ConnectionMode.Gateway });
}
}
}
1 change: 1 addition & 0 deletions src/Scaler/Services/CosmosDbMetricProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Azure.Identity;
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.Logging;

Expand Down

0 comments on commit 24eae81

Please sign in to comment.