Skip to content

Commit 154ba67

Browse files
Christoph Bühlerbuehler
authored andcommitted
feat(operator): register kubernetes client as transient
1 parent 2a3a2c2 commit 154ba67

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

src/KubeOps.Abstractions/Builder/IOperatorBuilder.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ public interface IOperatorBuilder
1919
IServiceCollection Services { get; }
2020

2121
/// <summary>
22-
/// Add an entity with its metadata to the operator.
23-
/// Metadata must be added for each entity to be used in
24-
/// controllers and other elements.
22+
/// <para>
23+
/// Register an entity within the operator.
24+
/// Entities must be registered to be used in controllers and other
25+
/// elements like Kubernetes clients.
26+
/// </para>
27+
/// <para>
28+
/// This method will also register a transient IKubernetesClient{TEntity} for
29+
/// the entity.
30+
/// </para>
2531
/// </summary>
2632
/// <param name="metadata">The metadata of the entity.</param>
2733
/// <typeparam name="TEntity">The type of the entity.</typeparam>

src/KubeOps.KubernetesClient/KubernetesClient.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace KubeOps.KubernetesClient;
1111

1212
/// <inheritdoc cref="IKubernetesClient{TEntity}"/>
13-
public class KubernetesClient<TEntity> : IKubernetesClient<TEntity>
13+
public class KubernetesClient<TEntity> : IKubernetesClient<TEntity>, IDisposable
1414
where TEntity : IKubernetesObject<V1ObjectMeta>
1515
{
1616
private const string DownwardApiNamespaceFile = "/var/run/secrets/kubernetes.io/serviceaccount/namespace";
@@ -57,12 +57,14 @@ public KubernetesClient(EntityMetadata metadata, KubernetesClientConfiguration c
5757
null => new GenericClient(
5858
client,
5959
metadata.Version,
60-
metadata.PluralName),
60+
metadata.PluralName,
61+
false),
6162
_ => new GenericClient(
6263
client,
6364
metadata.Group,
6465
metadata.Version,
65-
metadata.PluralName),
66+
metadata.PluralName,
67+
false),
6668
};
6769
}
6870

@@ -259,4 +261,21 @@ public Watcher<TEntity> Watch(
259261
watch: true,
260262
cancellationToken: cancellationToken),
261263
}).Watch(onEvent, onError, onClose);
264+
265+
public void Dispose()
266+
{
267+
Dispose(true);
268+
GC.SuppressFinalize(this);
269+
}
270+
271+
protected virtual void Dispose(bool disposing)
272+
{
273+
if (!disposing)
274+
{
275+
return;
276+
}
277+
278+
_client.Dispose();
279+
_genericClient.Dispose();
280+
}
262281
}

src/KubeOps.Operator/Builder/OperatorBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public OperatorBuilder(IServiceCollection services)
2323
public IOperatorBuilder AddEntity<TEntity>(EntityMetadata metadata)
2424
where TEntity : IKubernetesObject<V1ObjectMeta>
2525
{
26-
Services.AddSingleton<IKubernetesClient<TEntity>>(new KubernetesClient<TEntity>(metadata));
26+
Services.AddTransient<IKubernetesClient<TEntity>>(_ => new KubernetesClient<TEntity>(metadata));
2727
return this;
2828
}
2929

0 commit comments

Comments
 (0)