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

Add reconciliation loop #614

Merged
merged 7 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 18 additions & 7 deletions src/crd-controller/HostedServices/V1Alpha2Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ public class V1Alpha2Controller : IHostedService
private readonly IKubernetes mKubernetes;
private readonly IKeyManagement mKeyManagement;
private readonly bool mSetOwnerReference;
private readonly double mReconciliationIntervalInSeconds;
private IDisposable mSubscription;
private readonly ILogger mAuditLogger = Log.ForContext<V1Alpha2Controller>().AsAudit();
private readonly ILogger mLogger = Log.ForContext<V1Alpha2Controller>();
private readonly IMetrics mMetrics;
private const string ApiVersion = "v1alpha2";

public V1Alpha2Controller(IKubernetes kubernetes, IKeyManagement keyManagement, bool setOwnerReference, IMetrics metrics)
public V1Alpha2Controller(IKubernetes kubernetes, IKeyManagement keyManagement, bool setOwnerReference, double reconciliationIntervalInSeconds, IMetrics metrics)
{
mKubernetes = kubernetes;
mKeyManagement = keyManagement;
mSetOwnerReference = setOwnerReference;
mReconciliationIntervalInSeconds = reconciliationIntervalInSeconds;
mMetrics = metrics;
}

Expand All @@ -44,13 +46,13 @@ public Task StopAsync(CancellationToken cancellationToken)
return Task.CompletedTask;
}

public Task StartAsync(CancellationToken token)
private IDisposable ObserveKamusSecret(CancellationToken token)
{
mSubscription = mKubernetes.ObserveClusterCustomObject<KamusSecret>(
return mKubernetes.ObserveClusterCustomObject<KamusSecret>(
"soluto.com",
ApiVersion,
"kamussecrets",
token)
ApiVersion,
"kamussecrets",
token)
.SelectMany(x =>
Observable.FromAsync(async () => await HandleEvent(x.Item1, x.Item2))
)
Expand All @@ -66,7 +68,16 @@ public Task StartAsync(CancellationToken token)
mLogger.Information("Watching KamusSecret events completed, terminating process");
Environment.Exit(0);
});

}
public Task StartAsync(CancellationToken token)
{
mSubscription = ObserveKamusSecret(token);
Observable.Interval(TimeSpan.FromSeconds(mReconciliationIntervalInSeconds)).Subscribe((s) =>
{
mSubscription.Dispose();
mSubscription = ObserveKamusSecret(token);
});

mLogger.Information("Starting watch for KamusSecret V1Alpha2 events");

return Task.CompletedTask;
Expand Down
3 changes: 2 additions & 1 deletion src/crd-controller/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ public void ConfigureServices (IServiceCollection services) {
services.AddHostedService(serviceProvider =>
{
var setOwnerReference = Configuration.GetValue<bool>("Controller:SetOwnerReference", true);
var reconciliationIntervalInSeconds = Configuration.GetValue<double>("Controller:ReconciliationIntervalInSeconds", 60);
var kubernetes = serviceProvider.GetService<IKubernetes>();
var kms = serviceProvider.GetService<IKeyManagement>();
var metrics = serviceProvider.GetService<IMetrics>();
return new V1Alpha2Controller(kubernetes, kms, setOwnerReference, metrics);
return new V1Alpha2Controller(kubernetes, kms, setOwnerReference, reconciliationIntervalInSeconds, metrics);
});

services.AddHealthChecks()
Expand Down
44 changes: 44 additions & 0 deletions tests/crd-controller/FlowTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,50 @@ public async Task CreateKamusSecret_LabelsAndAnnotationsCopied()
Assert.Equal("value", v1Secret.Metadata.Annotations.First(x => x.Key == "key").Value);
}

[Fact]
public async Task CreateKamusSecret_DeleteSecret_ReconciliationRecreateIt()
{
Cleanup();
await DeployController();
var kubernetes = new Kubernetes(KubernetesClientConfiguration.BuildDefaultConfig());

var result = await kubernetes.ListNamespacedSecretWithHttpMessagesAsync(
"default",
watch: true
);

var subject = new ReplaySubject<(WatchEventType, V1Secret)>();

result.Watch<V1Secret>(
onEvent: (@type, @event) => subject.OnNext((@type, @event)),
onError: e => subject.OnError(e),
onClosed: () => subject.OnCompleted());

RunKubectlCommand("apply -f tls-KamusSecretV1Alpha2-with-annotations.yaml");
mTestOutputHelper.WriteLine("Waiting for secret creation");
var (_, v1Secret) = await subject
.Where(t => t.Item1 == WatchEventType.Added && t.Item2.Metadata.Name == "my-tls-secret").Timeout(TimeSpan.FromSeconds(30)).FirstAsync();

Assert.Equal(1, v1Secret.Metadata.Labels.Count);
Assert.True(v1Secret.Metadata.Labels.Keys.Contains("key"));
Assert.Equal("value", v1Secret.Metadata.Labels.First(x => x.Key == "key").Value);
Assert.Equal(1, v1Secret.Metadata.Annotations.Count);
Assert.True(v1Secret.Metadata.Annotations.Keys.Contains("key"));
Assert.Equal("value", v1Secret.Metadata.Annotations.First(x => x.Key == "key").Value);

RunKubectlCommand($"delete secret {v1Secret.Metadata.Name}");

var (_, v1SecretRecreation) = await subject
.Where(t => t.Item1 == WatchEventType.Added && t.Item2.Metadata.Name == "my-tls-secret").Timeout(TimeSpan.FromSeconds(15)).FirstAsync();

Assert.Equal(1, v1SecretRecreation.Metadata.Labels.Count);
Assert.True(v1SecretRecreation.Metadata.Labels.Keys.Contains("key"));
Assert.Equal("value", v1SecretRecreation.Metadata.Labels.First(x => x.Key == "key").Value);
Assert.Equal(1, v1SecretRecreation.Metadata.Annotations.Count);
Assert.True(v1SecretRecreation.Metadata.Annotations.Keys.Contains("key"));
Assert.Equal("value", v1SecretRecreation.Metadata.Annotations.First(x => x.Key == "key").Value);
}

[Theory]
[InlineData("updated-tls-KamusSecretV1Alpha2.yaml")]
public async Task UpdateKamusSecret_SecretUpdated(string fileName)
Expand Down
1 change: 1 addition & 0 deletions tests/crd-controller/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ spec:
image: crd-controller
imagePullPolicy: IfNotPresent
env:
Controller__ReconciliationIntervalInSeconds: 10
livenessProbe:
httpGet:
path: /healthz
Expand Down