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 Managed Identity support for connecting to Cosmos DB, Update dependencies with latest. #71

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ The external scaler calls Cosmos DB APIs to estimate the amount of changes pendi

Create `ScaledObject` resource that contains the information about your application (the scale target), the external scaler service, Cosmos DB containers, and other scaling configuration values. Check [`ScaledObject` specification](https://keda.sh/docs/concepts/scaling-deployments/) and [`External` trigger specification](https://keda.sh/docs/scalers/external/) for information on different properties supported for `ScaledObject` and their allowed values.

You can use file `deploy/deploy-scaledobject.yaml` as a template for creating the `ScaledObject`. The trigger metadata properties required to use the external scaler for Cosmos DB are described in [Trigger Specification](#trigger-specification) section below.
You can use the `deploy/deploy-scaledobject.yaml` file as a template for creating the `ScaledObject` when connecting to Cosmos DB with a managed identity. If you are using a connection string for the connection, use the `deploy/deploy-scaledobject-cs.yaml` file as your template instead. The trigger metadata properties required to use the external scaler for Cosmos DB are described in [Trigger Specification](#trigger-specification) section below.

> **Note:** If you are having trouble setting up the external scaler or the listener application, the step-by-step instructions for [deploying the sample application](./src/Scaler.Demo/README.md) might help.

Expand All @@ -68,10 +68,10 @@ The specification below describes the `trigger` metadata in `ScaledObject` resou
- type: external
metadata:
scalerAddress: external-scaler-azure-cosmos-db.keda:4050 # Mandatory. Address of the external scaler service.
connection: <connection> # Mandatory. Connection string of Cosmos DB account with monitored container.
endpoint: <endpoint> # Mandatory. Endpoint URL of Cosmos DB account with monitored container.
databaseId: <database-id> # Mandatory. ID of Cosmos DB database containing monitored container.
containerId: <container-id> # Mandatory. ID of monitored container.
leaseConnection: <lease-connection> # Mandatory. Connection string of Cosmos DB account with lease container.
leaseEndpoint: <lease-endpoint> # Mandatory. Endpoint URL of Cosmos DB account with lease container.
leaseDatabaseId: <lease-database-id> # Mandatory. ID of Cosmos DB database containing lease container.
leaseContainerId: <lease-container-id> # Mandatory. ID of lease container.
processorName: <processor-name> # Mandatory. Name of change-feed processor used by listener application.
Expand All @@ -81,13 +81,13 @@ The specification below describes the `trigger` metadata in `ScaledObject` resou

- **`scalerAddress`** - Address of the external scaler service. This would be in format `<scaler-name>.<scaler-namespace>:<port>`. If you installed Azure Cosmos DB external scaler Helm chart in `keda` namespace and did not specify custom values, the metadata value would be `external-scaler-azure-cosmos-db.keda:4050`.

- **`connection`** - Connection string of the Cosmos DB account that contains the monitored container.
- **`endpoint`** - Endpoint URL of the Cosmos DB account that contains the monitored container.

- **`databaseId`** - ID of Cosmos DB database that contains the monitored container.

- **`containerId`** - ID of the monitored container.

- **`leaseConnection`** - Connection string of the Cosmos DB account that contains the lease container. This can be same or different from the value of `connection` metadata.
- **`leaseEndpoint`** - Endpoint URL of the Cosmos DB account that contains the lease container. This can be same or different from the value of `endpoint` metadata.

- **`leaseDatabaseId`** - ID of Cosmos DB database that contains the lease container. This can be same or different from the value of `databaseId` metadata.

Expand Down
22 changes: 22 additions & 0 deletions deploy/deploy-scaledobject-cs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Template scaled-object for using KEDA external scaler for Azure Cosmos DB.

apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: <scaledobject-name>
namespace: default
spec:
pollingInterval: 20
scaleTargetRef:
name: <application-deployment-name>
triggers:
- type: external
metadata:
scalerAddress: external-scaler-azure-cosmos-db.keda:4050
connection: <connection-string-of-monitored-container-account>
databaseId: <database-id>
containerId: <container-id>
leaseConnection: <connection-string-of-lease-container-account>
leaseDatabaseId: <lease-database-id>
leaseContainerId: <lease-container-id>
processorName: <processor-name>
4 changes: 2 additions & 2 deletions deploy/deploy-scaledobject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ spec:
- type: external
metadata:
scalerAddress: external-scaler-azure-cosmos-db.keda:4050
connection: <connection-string-of-monitored-container-account>
endpoint: <endpoint-url-of-monitored-container-account>
databaseId: <database-id>
containerId: <container-id>
leaseConnection: <connection-string-of-lease-container-account>
leaseEndpoint: <endpoint-url-of-lease-container-account>
leaseDatabaseId: <lease-database-id>
leaseContainerId: <lease-container-id>
processorName: <processor-name>
Binary file modified images/architecture.pptx
Binary file not shown.
29 changes: 18 additions & 11 deletions src/Scaler.Demo/OrderGenerator/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
# https://hub.docker.com/_/microsoft-dotnet
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this Dockerfile generated using Visual Studio?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the original was not working. So, had to overwrite with VS generated Docker.


FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
WORKDIR /app

# Restore, build and publish project.
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /
COPY src/Scaler.Demo/OrderGenerator/ src/Scaler.Demo/OrderGenerator/
COPY src/Scaler.Demo/Shared/ src/Scaler.Demo/Shared/
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
Copy link
Collaborator

@JatinSanghvi JatinSanghvi Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should WORKDIR be /src/Scaler.Demo/OrderGenerator? Other files inside /src will not be required to be copied for either build or restore.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, the earlier Dockerfile was small and used to work. Can you minimize the changes to each Dockerfile, and keep it close to the original?

COPY ["Scaler.Demo/OrderGenerator/Keda.CosmosDb.Scaler.Demo.OrderGenerator.csproj", "Scaler.Demo/OrderGenerator/"]
COPY ["Scaler.Demo/Shared/Keda.CosmosDb.Scaler.Demo.Shared.csproj", "Scaler.Demo/Shared/"]
RUN dotnet restore "./Scaler.Demo/OrderGenerator/Keda.CosmosDb.Scaler.Demo.OrderGenerator.csproj"
COPY . .
WORKDIR "/src/Scaler.Demo/OrderGenerator"
RUN dotnet build "./Keda.CosmosDb.Scaler.Demo.OrderGenerator.csproj" -c $BUILD_CONFIGURATION -o /app/build

WORKDIR /src/Scaler.Demo/OrderGenerator
RUN dotnet publish --configuration Release --output /app
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./Keda.CosmosDb.Scaler.Demo.OrderGenerator.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# Stage application.
FROM mcr.microsoft.com/dotnet/runtime:6.0
FROM base AS final
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "Keda.CosmosDb.Scaler.Demo.OrderGenerator.dll"]
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Keda.CosmosDb.Scaler.Demo.OrderGenerator.dll"]
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<Project Sdk="Microsoft.NET.Sdk;Microsoft.NET.Sdk.Publish">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="Bogus" Version="35.5.1" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.40.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Keda.CosmosDb.Scaler.Demo.Shared.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="appsettings.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
</Project>
3 changes: 2 additions & 1 deletion src/Scaler.Demo/OrderGenerator/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"CosmosDbConfig": {
"Connection": "<connection-string-of-monitored-container-account>",
"Endpoint": "https://{Cosmos Account Name}.documents.azure.com:443/",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either keep just the 'Endpoint' config or make it clear that either one of the two properties are required.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of Order Generator, we are not deploying it on AKS. It just used locally to generate CosmosDB records and trigger the Change Feed, hence Order Generator is using connection string method to authenticate.

"Connection": "<Delete this line if using managed identity to connect to Cosmos DB, else update with connection-string-of-monitored-container-account>",
"DatabaseId": "StoreDatabase",
"ContainerId": "OrderContainer",
"ContainerThroughput": 11000
Expand Down
29 changes: 18 additions & 11 deletions src/Scaler.Demo/OrderProcessor/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
# https://hub.docker.com/_/microsoft-dotnet
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
WORKDIR /app

# Restore, build and publish project.
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /
COPY src/Scaler.Demo/OrderProcessor/ src/Scaler.Demo/OrderProcessor/
COPY src/Scaler.Demo/Shared/ src/Scaler.Demo/Shared/
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Scaler.Demo/OrderProcessor/Keda.CosmosDb.Scaler.Demo.OrderProcessor.csproj", "Scaler.Demo/OrderProcessor/"]
COPY ["Scaler.Demo/Shared/Keda.CosmosDb.Scaler.Demo.Shared.csproj", "Scaler.Demo/Shared/"]
RUN dotnet restore "./Scaler.Demo/OrderProcessor/Keda.CosmosDb.Scaler.Demo.OrderProcessor.csproj"
COPY . .
WORKDIR "/src/Scaler.Demo/OrderProcessor"
RUN dotnet build "./Keda.CosmosDb.Scaler.Demo.OrderProcessor.csproj" -c $BUILD_CONFIGURATION -o /app/build

WORKDIR /src/Scaler.Demo/OrderProcessor
RUN dotnet publish --configuration Release --output /app
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./Keda.CosmosDb.Scaler.Demo.OrderProcessor.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# Stage application.
FROM mcr.microsoft.com/dotnet/runtime:6.0
FROM base AS final
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "Keda.CosmosDb.Scaler.Demo.OrderProcessor.dll"]
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Keda.CosmosDb.Scaler.Demo.OrderProcessor.dll"]
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.11.4" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.40.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
</ItemGroup>

<ItemGroup>
Expand Down
50 changes: 47 additions & 3 deletions src/Scaler.Demo/OrderProcessor/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Azure.Identity;
using static Azure.Core.HttpHeader;

namespace Keda.CosmosDb.Scaler.Demo.OrderProcessor
{
Expand All @@ -25,8 +27,49 @@ public Worker(CosmosDbConfig cosmosDbConfig, ILogger<Worker> logger)

public override async Task StartAsync(CancellationToken cancellationToken)
{
Database leaseDatabase = await new CosmosClient(_cosmosDbConfig.LeaseConnection)
.CreateDatabaseIfNotExistsAsync(_cosmosDbConfig.LeaseDatabaseId, cancellationToken: cancellationToken);
Database leaseDatabase;
CosmosClient cosmosClient;

if (string.IsNullOrEmpty(_cosmosDbConfig.Connection))
{
var credential = new DefaultAzureCredential();

cosmosClient = new Microsoft.Azure.Cosmos.CosmosClient(_cosmosDbConfig.Endpoint, credential);
}
else
{
cosmosClient = new Microsoft.Azure.Cosmos.CosmosClient(_cosmosDbConfig.Connection);
}

//use connection string or credentials
if (string.IsNullOrEmpty(_cosmosDbConfig.LeaseConnection))
{

// maintain a single instance of CosmosClient per lifetime of the application.
if (_cosmosDbConfig.LeaseEndpoint == _cosmosDbConfig.Endpoint)
{
leaseDatabase = await cosmosClient.CreateDatabaseIfNotExistsAsync(_cosmosDbConfig.LeaseDatabaseId);
}
else
{
var credential = new DefaultAzureCredential();
leaseDatabase = await new Microsoft.Azure.Cosmos.CosmosClient(_cosmosDbConfig.LeaseEndpoint, credential)
.CreateDatabaseIfNotExistsAsync(_cosmosDbConfig.LeaseDatabaseId);
}
}
else
{
// maintain a single instance of CosmosClient per lifetime of the application.
if (_cosmosDbConfig.LeaseConnection == _cosmosDbConfig.Connection)
{
leaseDatabase = await cosmosClient.CreateDatabaseIfNotExistsAsync(_cosmosDbConfig.LeaseDatabaseId);
}
else
{
leaseDatabase = await new Microsoft.Azure.Cosmos.CosmosClient(_cosmosDbConfig.LeaseConnection)
.CreateDatabaseIfNotExistsAsync(_cosmosDbConfig.LeaseDatabaseId);
}
}

Container leaseContainer = await leaseDatabase
.CreateContainerIfNotExistsAsync(
Expand All @@ -37,7 +80,8 @@ public override async Task StartAsync(CancellationToken cancellationToken)
// Change feed processor instance name should be unique for each container application.
string instanceName = $"Instance-{Dns.GetHostName()}";

_processor = new CosmosClient(_cosmosDbConfig.Connection)

_processor = cosmosClient
.GetContainer(_cosmosDbConfig.DatabaseId, _cosmosDbConfig.ContainerId)
.GetChangeFeedProcessorBuilder<Order>(_cosmosDbConfig.ProcessorName, ProcessOrdersAsync)
.WithInstanceName(instanceName)
Expand Down
2 changes: 0 additions & 2 deletions src/Scaler.Demo/OrderProcessor/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
}
},
"CosmosDbConfig": {
"Connection": "<connection-string-of-monitored-container-account>",
"DatabaseId": "StoreDatabase",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need Endpoint and LeaseEndpoint settings here? It's not a secret so should be specifiable inside app-settings too?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You only need it for local development. In AKS all config is passed via YAML. We use Endpoint in case of AAD and Connection in case of CS.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We ensured that local development is possible, that we will stop supporting with the change. Please ensure that all demo steps for connection string-based demo and connection string-based scaler continue to work.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want these settings in the appsettings.json of the docker images, if add these here the user will have to explicitly delete it before publishing. I can add it in appsettings.development.json. Does that work?

"ContainerId": "OrderContainer",
"LeaseConnection": "<connection-string-of-lease-container-account>",
"LeaseDatabaseId": "StoreDatabase",
"LeaseContainerId": "OrderProcessorLeases",
"ProcessorName": "OrderProcessor"
Expand Down
26 changes: 26 additions & 0 deletions src/Scaler.Demo/OrderProcessor/deploy-cs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Deploy order processor application.

apiVersion: apps/v1
kind: Deployment
metadata:
name: cosmosdb-order-processor
namespace: default
spec:
replicas: 1 # A replica is required to be up momentarily to initialize the change-feed.
selector:
matchLabels:
app: cosmosdb-order-processor
template:
metadata:
labels:
app: cosmosdb-order-processor
spec:
containers:
- name: cosmosdb-order-processor
image: <docker-id>/cosmosdb-order-processor:latest
imagePullPolicy: Always
env:
- name: CosmosDbConfig__Connection
value: <connection-string-of-monitored-container>
- name: CosmosDbConfig__LeaseConnection
value: <connection-string-of-lease-container>
22 changes: 22 additions & 0 deletions src/Scaler.Demo/OrderProcessor/deploy-scaledobject-cs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Create KEDA scaled object to scale order processor application.

apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: cosmosdb-order-processor-scaledobject
namespace: default
spec:
pollingInterval: 20
scaleTargetRef:
name: cosmosdb-order-processor
triggers:
- type: external
metadata:
scalerAddress: cosmosdb-scaler.default:4050
connection: <connection-string-of-monitored-container-account>
databaseId: StoreDatabase
containerId: OrderContainer
leaseConnection: <connection-string-of-lease-container-account>
leaseDatabaseId: StoreDatabase
leaseContainerId: OrderProcessorLeases
processorName: OrderProcessor
4 changes: 2 additions & 2 deletions src/Scaler.Demo/OrderProcessor/deploy-scaledobject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ spec:
- type: external
metadata:
scalerAddress: cosmosdb-scaler.default:4050
connection: <connection-string-of-monitored-container-account>
endpoint: <endpoint-url-for-cosmosdb-account-monitored-container>
databaseId: StoreDatabase
containerId: OrderContainer
leaseConnection: <connection-string-of-lease-container-account>
leaseEndpoint: <endpoint-url-for-cosmosdb-account-lease-container>
leaseDatabaseId: StoreDatabase
leaseContainerId: OrderProcessorLeases
processorName: OrderProcessor
12 changes: 8 additions & 4 deletions src/Scaler.Demo/OrderProcessor/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ kind: Deployment
metadata:
name: cosmosdb-order-processor
namespace: default
labels:
aadpodidbinding: "my-pod-identity" # refer to https://learn.microsoft.com/en-us/azure/aks/use-azure-ad-pod-identity#create-a-pod-identity
app: cosmosdb-order-processor
spec:
replicas: 1 # A replica is required to be up momentarily to initialize the change-feed.
selector:
Expand All @@ -14,13 +17,14 @@ spec:
metadata:
labels:
app: cosmosdb-order-processor
aadpodidbinding: "my-pod-identity" # refer to https://learn.microsoft.com/en-us/azure/aks/use-azure-ad-pod-identity#create-a-pod-identity
spec:
containers:
- name: cosmosdb-order-processor
image: <docker-id>/cosmosdb-order-processor:latest
imagePullPolicy: Always
env:
- name: CosmosDbConfig__Connection
value: <connection-string-of-monitored-container>
- name: CosmosDbConfig__LeaseConnection
value: <connection-string-of-lease-container>
- name: CosmosDbConfig__Endpoint
value: <endpoint-URL-of-cosmos-DB-account-monitored-container>
- name: CosmosDbConfig__LeaseEndpoint
value: <endpoint-URL-of-cosmos-DB-account-lease-container>
Loading