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 Support for FromEnv properties for Connections #76

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ 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.
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.
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.
connection: <connection> # Mandatory. Connection string of Cosmos DB account with monitored container.
Sreemoyee26 marked this conversation as resolved.
Show resolved Hide resolved
connectionFromEnv: <connectionFromEnvVariable> # Optional. Used when connection is not used. (More secure)
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.
leaseConnectionFromEnv: <leaseConnectionFromEnvVariable> # Optional. Used when connection is not used. (More secure)
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.
```

### Parameter List
Expand All @@ -83,12 +85,16 @@ The specification below describes the `trigger` metadata in `ScaledObject` resou

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

- **`connectionFromEnv`** - ConnectionString is taken as an Environment Variable.
Sreemoyee26 marked this conversation as resolved.
Show resolved Hide resolved

- **`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.

- **`leaseConnectionFromEnv`** - LeaseConnectionString is taken as an Environment Variable.

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

- **`leaseContainerId`** - ID of the lease container containing the change feeds.
Expand Down
2 changes: 2 additions & 0 deletions deploy/deploy-scaledobject.yaml
JatinSanghvi marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ spec:
metadata:
scalerAddress: external-scaler-azure-cosmos-db.keda:4050
connection: <connection-string-of-monitored-container-account>
connectionFromEnv: <connection-string-environment-variable-of-monitored-container-account>
databaseId: <database-id>
containerId: <container-id>
leaseConnection: <connection-string-of-lease-container-account>
leaseConnectionFromEnv: <connection-string-environemnt-variable-of-lease-container-account>
Sreemoyee26 marked this conversation as resolved.
Show resolved Hide resolved
leaseDatabaseId: <lease-database-id>
leaseContainerId: <lease-container-id>
processorName: <processor-name>
96 changes: 90 additions & 6 deletions src/Scaler.Tests/CosmosDbScalerServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ public CosmosDbScalerServiceTests()
}

[Theory]
[InlineData("connection")]
[InlineData("databaseId")]
[InlineData("containerId")]
[InlineData("leaseConnection")]
[InlineData("leaseDatabaseId")]
[InlineData("leaseContainerId")]
[InlineData("processorName")]
Expand Down Expand Up @@ -50,10 +48,8 @@ public async Task IsActive_ReturnsFalseOnNonZeroPartitions(long partitionCount)
}

[Theory]
[InlineData("connection")]
[InlineData("databaseId")]
[InlineData("containerId")]
[InlineData("leaseConnection")]
[InlineData("leaseDatabaseId")]
[InlineData("leaseContainerId")]
[InlineData("processorName")]
Expand Down Expand Up @@ -99,10 +95,8 @@ public async Task GetMetrics_ReturnsSameMetricNameIfPassed(string requestMetricN
}

[Theory]
[InlineData("connection")]
[InlineData("databaseId")]
[InlineData("containerId")]
[InlineData("leaseConnection")]
[InlineData("leaseDatabaseId")]
[InlineData("leaseContainerId")]
[InlineData("processorName")]
Expand Down Expand Up @@ -156,6 +150,94 @@ public async Task GetMetricSpec_ReturnsNormalizedMetricName()
Assert.Equal(
"cosmosdb-partitioncount-example-com-dummy-lease-database-id-dummy-lease-container-id-dummy-processor-name",
response.MetricSpecs[0].MetricName);
}
Sreemoyee26 marked this conversation as resolved.
Show resolved Hide resolved

[Fact]
public async Task IsActive_ThrowsOnMissingConnectionAndConnectionFromEnv()
Sreemoyee26 marked this conversation as resolved.
Show resolved Hide resolved
{
var scaledObjectRef = GetScaledObjectRef();
scaledObjectRef.ScalerMetadata.Remove("connection");
scaledObjectRef.ScalerMetadata.Remove("connectionFromEnv");

await Assert.ThrowsAsync<JsonSerializationException>(
() => _cosmosDbScalerService.IsActive(scaledObjectRef, null));
}

[Fact]
public async Task IsActive_ThrowsOnMissingLeaseConnectionAndLeaseConnectionFromEnv()
{
var scaledObjectRef = GetScaledObjectRef();
scaledObjectRef.ScalerMetadata.Remove("leaseConnection");
scaledObjectRef.ScalerMetadata.Remove("leaseConnectionFromEnv");

await Assert.ThrowsAsync<JsonSerializationException>(
() => _cosmosDbScalerService.IsActive(scaledObjectRef, null));
}

[Fact]
public async Task GetMetrics_ThrowsOnMissingConnectionAndConnectionFromEnv()
{
var request = GetGetMetricsRequest();
request.ScaledObjectRef.ScalerMetadata.Remove("connection");
request.ScaledObjectRef.ScalerMetadata.Remove("connectionFromEnv");

await Assert.ThrowsAsync<JsonSerializationException>(
() => _cosmosDbScalerService.GetMetrics(request, null));
}

[Fact]
public async Task GetMetrics_ThrowsOnMissingLeaseConnectionAndLeaseConnectionFromEnv()
{
var request = GetGetMetricsRequest();
request.ScaledObjectRef.ScalerMetadata.Remove("leaseConnection");
request.ScaledObjectRef.ScalerMetadata.Remove("leaseConnectionFromEnv");

await Assert.ThrowsAsync<JsonSerializationException>(
() => _cosmosDbScalerService.GetMetrics(request, null));
}

[Fact]
public async Task GetMetricSpec_ThrowsOnMissingConnectionAndConnectionFromEnv()
{
var scaledObjectRef = GetScaledObjectRef();
scaledObjectRef.ScalerMetadata.Remove("connection");
scaledObjectRef.ScalerMetadata.Remove("connectionFromEnv");

await Assert.ThrowsAsync<JsonSerializationException>(
() => _cosmosDbScalerService.GetMetricSpec(scaledObjectRef, null));
}

[Fact]
public async Task GetMetricSpec_ThrowsOnMissingLeaseConnectionAndLeaseConnectionFromEnv()
{
var scaledObjectRef = GetScaledObjectRef();
scaledObjectRef.ScalerMetadata.Remove("leaseConnection");
scaledObjectRef.ScalerMetadata.Remove("leaseConnectionFromEnv");

await Assert.ThrowsAsync<JsonSerializationException>(
() => _cosmosDbScalerService.GetMetricSpec(scaledObjectRef, null));
}

[Fact]
public async Task IsActive_UsesConnectionFromEnvIfConnectionMissing()
{
var scaledObjectRef = GetScaledObjectRef();
scaledObjectRef.ScalerMetadata.Remove("connection");

_metricProviderMock.Setup(provider => provider.GetPartitionCountAsync(It.IsAny<ScalerMetadata>())).ReturnsAsync(1L);
IsActiveResponse response = await _cosmosDbScalerService.IsActive(scaledObjectRef, null);
Assert.True(response.Result);
}

[Fact]
public async Task IsActive_UsesLeaseConnectionFromEnvIfLeaseConnectionMissing()
{
var scaledObjectRef = GetScaledObjectRef();
scaledObjectRef.ScalerMetadata.Remove("leaseConnection");

_metricProviderMock.Setup(provider => provider.GetPartitionCountAsync(It.IsAny<ScalerMetadata>())).ReturnsAsync(1L);
IsActiveResponse response = await _cosmosDbScalerService.IsActive(scaledObjectRef, null);
Assert.True(response.Result);
}

private static GetMetricsRequest GetGetMetricsRequest()
Expand Down Expand Up @@ -195,9 +277,11 @@ private static ScaledObjectRef GetScaledObjectRef()
MapField<string, string> scalerMetadata = scaledObjectRef.ScalerMetadata;

scalerMetadata["connection"] = "AccountEndpoint=https://example1.com:443/;AccountKey=ZHVtbXkx";
scalerMetadata["connectionFromEnv"] = "dummy-connection-from-env"; //KEDA external scaler will receive the actual connection string.
Sreemoyee26 marked this conversation as resolved.
Show resolved Hide resolved
scalerMetadata["databaseId"] = "dummy-database-id";
scalerMetadata["containerId"] = "dummy-container-id";
scalerMetadata["leaseConnection"] = "AccountEndpoint=https://example2.com:443/;AccountKey=ZHVtbXky";
scalerMetadata["leaseConnectionFromEnv"] = "dummy-lease-connection-from-env"; //KEDA external scaler will receive the actual connection string.
scalerMetadata["leaseDatabaseId"] = "dummy-lease-database-id";
scalerMetadata["leaseContainerId"] = "dummy-lease-container-id";
scalerMetadata["processorName"] = "dummy-processor-name";
Expand Down
1 change: 0 additions & 1 deletion src/Scaler/Services/CosmosDbScalerService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Threading.Tasks;
using Grpc.Core;
using Microsoft.Extensions.Logging;

namespace Keda.CosmosDb.Scaler
{
Expand Down
40 changes: 34 additions & 6 deletions src/Scaler/Services/ScalerMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@

namespace Keda.CosmosDb.Scaler
{
[JsonObject(ItemRequired = Required.Always)]
[JsonObject]
internal sealed class ScalerMetadata
{
private string _metricName;

public string Connection { get; set; }
private string _metricName; // Private backing field for MetricName property
private string _connection; // Private backing field for Connection property
public string ConnectionFromEnv { get; set; }
Sreemoyee26 marked this conversation as resolved.
Show resolved Hide resolved
public string Connection
{
get => string.IsNullOrEmpty(_connection) ? ConnectionFromEnv : _connection;
Sreemoyee26 marked this conversation as resolved.
Show resolved Hide resolved
set => _connection = value;
}
[JsonProperty(Required = Required.Always)]
Sreemoyee26 marked this conversation as resolved.
Show resolved Hide resolved
public string DatabaseId { get; set; }
[JsonProperty(Required = Required.Always)]
public string ContainerId { get; set; }
public string LeaseConnection { get; set; }
public string LeaseConnectionFromEnv { get; set; }
private string _leaseConnection; // Private backing field for LeaseConnection property
Sreemoyee26 marked this conversation as resolved.
Show resolved Hide resolved
public string LeaseConnection
{
get => string.IsNullOrEmpty(_leaseConnection) ? LeaseConnectionFromEnv : _leaseConnection;
set => _leaseConnection = value;
}
[JsonProperty(Required = Required.Always)]
public string LeaseDatabaseId { get; set; }
[JsonProperty(Required = Required.Always)]
public string LeaseContainerId { get; set; }
[JsonProperty(Required = Required.Always)]
public string ProcessorName { get; set; }

[JsonProperty(Required = Required.DisallowNull)]
Expand Down Expand Up @@ -46,7 +62,19 @@ private string LeaseAccountHost

public static ScalerMetadata Create(ScaledObjectRef scaledObjectRef)
{
return JsonConvert.DeserializeObject<ScalerMetadata>(scaledObjectRef.ScalerMetadata.ToString());
var metadata = JsonConvert.DeserializeObject<ScalerMetadata>(scaledObjectRef.ScalerMetadata.ToString());

if (string.IsNullOrEmpty(metadata.Connection) && string.IsNullOrEmpty(metadata.ConnectionFromEnv))
Sreemoyee26 marked this conversation as resolved.
Show resolved Hide resolved
{
throw new JsonSerializationException("Required property 'Connection' not found in JSON.");
Sreemoyee26 marked this conversation as resolved.
Show resolved Hide resolved
}

if (string.IsNullOrEmpty(metadata.LeaseConnection) && string.IsNullOrEmpty(metadata.LeaseConnectionFromEnv))
{
throw new JsonSerializationException("Required property 'LeaseConnection' not found in JSON.");
}

return metadata;
}
}
}