Skip to content

Commit

Permalink
feat(generator): add controller registrations (#623)
Browse files Browse the repository at this point in the history
This generates `.AddController<T,T>` register calls
for the operator builder to enable convenient registering.
  • Loading branch information
buehler authored Oct 2, 2023
1 parent d614a1f commit af3836a
Show file tree
Hide file tree
Showing 55 changed files with 3,836 additions and 3,430 deletions.
4 changes: 2 additions & 2 deletions examples/Operator/Controller/V1TestEntityController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public V1TestEntityController(ILogger<V1TestEntityController> logger)

public Task ReconcileAsync(V1TestEntity entity)
{
_logger.LogInformation("Reconciling entity {EntityName}.", entity.Metadata.Name);
_logger.LogInformation("Reconciling entity {Entity}.", entity);
return Task.CompletedTask;
}

public Task DeletedAsync(V1TestEntity entity)
{
_logger.LogInformation("Deleting entity {EntityName}.", entity.Metadata.Name);
_logger.LogInformation("Deleting entity {Entity}.", entity);
return Task.CompletedTask;
}
}
2 changes: 2 additions & 0 deletions examples/Operator/Entities/V1TestEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public class V1TestEntity : IKubernetesObject<V1ObjectMeta>, ISpec<V1TestEntityS
public V1TestEntitySpec Spec { get; set; } = new();

public V1TestEntityStatus Status { get; set; } = new();

public override string ToString() => $"Test Entity ({Metadata.Name}): {Spec.Username} ({Spec.Email})";
}
6 changes: 1 addition & 5 deletions examples/Operator/Entities/V1TestEntitySpec.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using k8s.Models;

namespace Operator.Entities;

public class V1TestEntitySpec
{
public string Spec { get; set; } = string.Empty;

public string Username { get; set; } = string.Empty;

public IntstrIntOrString StringOrInteger { get; set; } = 42;
public string Email { get; set; } = string.Empty;
}
6 changes: 1 addition & 5 deletions examples/Operator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

using Operator.Controller;
using Operator.Entities;

var builder = Host.CreateApplicationBuilder(args);

builder.Logging.SetMinimumLevel(LogLevel.Trace);

builder.Services
.AddKubernetesOperator()
.RegisterEntitiyMetadata()
.AddController<V1TestEntityController, V1TestEntity>();
.RegisterResources();

using var host = builder.Build();
await host.RunAsync();
3 changes: 3 additions & 0 deletions examples/Operator/test_entity.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ apiVersion: testing.dev/v1
kind: TestEntity
metadata:
name: my-test-entity
spec:
username: my-username
email: [email protected]
10 changes: 10 additions & 0 deletions examples/Operator/todos.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
todo:
- finalizer
- events
- leadership election
- requeue
- build targets
- other CLI commands
- web: webhooks
- cache?
- try .net 8 AOT?
32 changes: 16 additions & 16 deletions src/KubeOps.Abstractions/Entities/CustomKubernetesEntity.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using k8s;
using k8s.Models;

namespace KubeOps.Abstractions.Entities;

/// <summary>
/// Base class for custom Kubernetes entities. The interface <see cref="IKubernetesObject{TMetadata}"/>
/// can be used on its own, but this class provides convenience initializers.
/// </summary>
public abstract class CustomKubernetesEntity : KubernetesObject, IKubernetesObject<V1ObjectMeta>
{
/// <summary>
/// The metadata of the kubernetes object.
/// </summary>
public V1ObjectMeta Metadata { get; set; } = new();
}
using k8s;
using k8s.Models;

namespace KubeOps.Abstractions.Entities;

/// <summary>
/// Base class for custom Kubernetes entities. The interface <see cref="IKubernetesObject{TMetadata}"/>
/// can be used on its own, but this class provides convenience initializers.
/// </summary>
public abstract class CustomKubernetesEntity : KubernetesObject, IKubernetesObject<V1ObjectMeta>
{
/// <summary>
/// The metadata of the kubernetes object.
/// </summary>
public V1ObjectMeta Metadata { get; set; } = new();
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using k8s;

namespace KubeOps.Abstractions.Entities;

/// <summary>
/// Defines a custom Kubernetes entity.
/// This entity contains a spec (like <see cref="CustomKubernetesEntity{TSpec}"/>)
/// and a status (<see cref="Status"/>) which can be updated to reflect the state
/// of the entity.
/// </summary>
/// <typeparam name="TSpec">The type of the specified data.</typeparam>
/// <typeparam name="TStatus">The type of the status data.</typeparam>
public abstract class CustomKubernetesEntity<TSpec, TStatus> : CustomKubernetesEntity<TSpec>, IStatus<TStatus>
where TSpec : new()
where TStatus : new()
{
/// <summary>
/// Status object for the entity.
/// </summary>
public TStatus Status { get; set; } = new();
}
using k8s;

namespace KubeOps.Abstractions.Entities;

/// <summary>
/// Defines a custom Kubernetes entity.
/// This entity contains a spec (like <see cref="CustomKubernetesEntity{TSpec}"/>)
/// and a status (<see cref="Status"/>) which can be updated to reflect the state
/// of the entity.
/// </summary>
/// <typeparam name="TSpec">The type of the specified data.</typeparam>
/// <typeparam name="TStatus">The type of the status data.</typeparam>
public abstract class CustomKubernetesEntity<TSpec, TStatus> : CustomKubernetesEntity<TSpec>, IStatus<TStatus>
where TSpec : new()
where TStatus : new()
{
/// <summary>
/// Status object for the entity.
/// </summary>
public TStatus Status { get; set; } = new();
}
34 changes: 17 additions & 17 deletions src/KubeOps.Abstractions/Entities/CustomKubernetesEntity{TSpec}.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using k8s;

namespace KubeOps.Abstractions.Entities;

/// <summary>
/// Defines a custom kubernetes entity which can be used in finalizers and controllers.
/// This entity contains a <see cref="Spec"/>, which means in contains specified data.
/// </summary>
/// <typeparam name="TSpec">The type of the specified data.</typeparam>
public abstract class CustomKubernetesEntity<TSpec> : CustomKubernetesEntity, ISpec<TSpec>
where TSpec : new()
{
/// <summary>
/// Specification of the kubernetes object.
/// </summary>
public TSpec Spec { get; set; } = new();
}
using k8s;

namespace KubeOps.Abstractions.Entities;

/// <summary>
/// Defines a custom kubernetes entity which can be used in finalizers and controllers.
/// This entity contains a <see cref="Spec"/>, which means in contains specified data.
/// </summary>
/// <typeparam name="TSpec">The type of the specified data.</typeparam>
public abstract class CustomKubernetesEntity<TSpec> : CustomKubernetesEntity, ISpec<TSpec>
where TSpec : new()
{
/// <summary>
/// Specification of the kubernetes object.
/// </summary>
public TSpec Spec { get; set; } = new();
}
44 changes: 22 additions & 22 deletions src/KubeOps.Abstractions/Entities/EntityList.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
using k8s;
using k8s.Models;

namespace KubeOps.Abstractions.Entities;

/// <summary>
/// Type for a list of entities.
/// </summary>
/// <typeparam name="T">Type for the list entries.</typeparam>
public class EntityList<T> : KubernetesObject
where T : IKubernetesObject
{
/// <summary>
/// Official list metadata object of kubernetes.
/// </summary>
public V1ListMeta Metadata { get; set; } = new();

/// <summary>
/// The list of items.
/// </summary>
public IList<T> Items { get; set; } = new List<T>();
}
using k8s;
using k8s.Models;

namespace KubeOps.Abstractions.Entities;

/// <summary>
/// Type for a list of entities.
/// </summary>
/// <typeparam name="T">Type for the list entries.</typeparam>
public class EntityList<T> : KubernetesObject
where T : IKubernetesObject
{
/// <summary>
/// Official list metadata object of kubernetes.
/// </summary>
public V1ListMeta Metadata { get; set; } = new();

/// <summary>
/// The list of items.
/// </summary>
public IList<T> Items { get; set; } = new List<T>();
}
84 changes: 42 additions & 42 deletions src/KubeOps.Abstractions/Entities/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
using k8s;
using k8s.Models;

namespace KubeOps.Abstractions.Entities;

/// <summary>
/// Method extensions for <see cref="IKubernetesObject{TMetadata}"/>.
/// </summary>
public static class Extensions
{
/// <summary>
/// Sets the resource version of the specified Kubernetes object to the specified value.
/// </summary>
/// <typeparam name="TEntity">The type of the Kubernetes object.</typeparam>
/// <param name="entity">The Kubernetes object.</param>
/// <param name="resourceVersion">The resource version to set.</param>
/// <returns>The Kubernetes object with the updated resource version.</returns>
public static TEntity WithResourceVersion<TEntity>(
this TEntity entity,
string resourceVersion)
where TEntity : IKubernetesObject<V1ObjectMeta>
{
entity.EnsureMetadata().ResourceVersion = resourceVersion;
return entity;
}

/// <summary>
/// Sets the resource version of the specified Kubernetes object to the resource version of another object.
/// </summary>
/// <typeparam name="TEntity">The type of the Kubernetes object.</typeparam>
/// <param name="entity">The Kubernetes object.</param>
/// <param name="other">The other Kubernetes object.</param>
/// <returns>The Kubernetes object with the updated resource version.</returns>
public static TEntity WithResourceVersion<TEntity>(
this TEntity entity,
TEntity other)
where TEntity : IKubernetesObject<V1ObjectMeta>
{
entity.EnsureMetadata().ResourceVersion = other.ResourceVersion();
return entity;
}
}
using k8s;
using k8s.Models;

namespace KubeOps.Abstractions.Entities;

/// <summary>
/// Method extensions for <see cref="IKubernetesObject{TMetadata}"/>.
/// </summary>
public static class Extensions
{
/// <summary>
/// Sets the resource version of the specified Kubernetes object to the specified value.
/// </summary>
/// <typeparam name="TEntity">The type of the Kubernetes object.</typeparam>
/// <param name="entity">The Kubernetes object.</param>
/// <param name="resourceVersion">The resource version to set.</param>
/// <returns>The Kubernetes object with the updated resource version.</returns>
public static TEntity WithResourceVersion<TEntity>(
this TEntity entity,
string resourceVersion)
where TEntity : IKubernetesObject<V1ObjectMeta>
{
entity.EnsureMetadata().ResourceVersion = resourceVersion;
return entity;
}

/// <summary>
/// Sets the resource version of the specified Kubernetes object to the resource version of another object.
/// </summary>
/// <typeparam name="TEntity">The type of the Kubernetes object.</typeparam>
/// <param name="entity">The Kubernetes object.</param>
/// <param name="other">The other Kubernetes object.</param>
/// <returns>The Kubernetes object with the updated resource version.</returns>
public static TEntity WithResourceVersion<TEntity>(
this TEntity entity,
TEntity other)
where TEntity : IKubernetesObject<V1ObjectMeta>
{
entity.EnsureMetadata().ResourceVersion = other.ResourceVersion();
return entity;
}
}
Loading

0 comments on commit af3836a

Please sign in to comment.