Skip to content

Commit

Permalink
feat: make CompareNETObjects ComparisonConfig configurable (#395)
Browse files Browse the repository at this point in the history
* Expose ComparisonConfig as an option in OperatorSettings

Fixes #364

Signed-off-by: Erin Allison <erin@eallison.us>

* Fix tests

Signed-off-by: Erin Allison <erin@eallison.us>
  • Loading branch information
erin-allison authored and buehler committed Apr 28, 2022
1 parent 1e7b53d commit afdd3b9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/KubeOps/Operator/Builder/OperatorBuilder.cs
Expand Up @@ -3,6 +3,7 @@
using DotnetKubernetesClient;
using k8s;
using k8s.Models;
using KellermanSoftware.CompareNetObjects;
using KubeOps.Operator.Caching;
using KubeOps.Operator.Controller;
using KubeOps.Operator.DevOps;
Expand Down
9 changes: 4 additions & 5 deletions src/KubeOps/Operator/Caching/ResourceCache{TEntity}.cs
Expand Up @@ -13,21 +13,20 @@ namespace KubeOps.Operator.Caching;
internal class ResourceCache<TEntity>
where TEntity : IKubernetesObject<V1ObjectMeta>
{
private const string ResourceVersion = "ResourceVersion";
private const string ManagedFields = "ManagedFields";
private const string Finalizers = "Metadata.Finalizers";
private const string Status = "Status";

private readonly CompareLogic _compare = new(
new() { Caching = true, AutoClearCache = false, MembersToIgnore = new() { ResourceVersion, ManagedFields }, });
private readonly CompareLogic _compare;

private readonly ConcurrentDictionary<string, TEntity> _cache = new();

private readonly ResourceCacheMetrics<TEntity> _metrics;

public ResourceCache(ResourceCacheMetrics<TEntity> metrics)
public ResourceCache(ResourceCacheMetrics<TEntity> metrics, OperatorSettings settings)
{
_metrics = metrics;

_compare = new CompareLogic(settings.CacheComparisonConfig);
}

public TEntity Get(string id) => _cache[id];
Expand Down
12 changes: 12 additions & 0 deletions src/KubeOps/Operator/OperatorSettings.cs
Expand Up @@ -2,6 +2,7 @@
using System.Reflection;
using System.Text.RegularExpressions;
using DotnetKubernetesClient;
using KellermanSoftware.CompareNetObjects;
using KubeOps.Operator.Errors;
using KubeOps.Operator.Serialization;
using Microsoft.Rest.Serialization;
Expand Down Expand Up @@ -158,6 +159,17 @@ public sealed class OperatorSettings
/// </summary>
public short HttpsPort { get; set; } = 5001;

/// <summary>
/// The configuration used when comparing resources against each-other for caching
/// or other similar processing.
/// </summary>
public ComparisonConfig CacheComparisonConfig { get; set; } = new()
{
Caching = true,
AutoClearCache = false,
MembersToIgnore = new List<string> { "ResourceVersion", "ManagedFields" },
};

internal JsonSerializerSettings SerializerSettings { get; } = new()
{
DateFormatHandling = DateFormatHandling.IsoDateFormat,
Expand Down
2 changes: 1 addition & 1 deletion tests/KubeOps.Test/Operator/Caching/ResourceCache.Test.cs
Expand Up @@ -10,7 +10,7 @@ namespace KubeOps.Test.Operator.Caching;

public class ResourceCacheTest
{
private readonly ResourceCache<TestStatusEntity> _cache = new(new(new()));
private readonly ResourceCache<TestStatusEntity> _cache = new(new(new()), new());

[Fact]
public void Throws_When_Not_Found()
Expand Down

0 comments on commit afdd3b9

Please sign in to comment.