Skip to content

Commit

Permalink
test: add some tests for web operators (#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
buehler authored Oct 16, 2023
1 parent 1641a10 commit ba957c3
Show file tree
Hide file tree
Showing 36 changed files with 1,947 additions and 1,547 deletions.
40 changes: 20 additions & 20 deletions src/KubeOps.Cli/Transpilation/BaseWebhook.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
using System.Reflection;

using KubeOps.Abstractions.Entities;

namespace KubeOps.Cli.Transpilation;

internal abstract record BaseWebhook(TypeInfo Webhook, EntityMetadata Metadata)
{
private bool HasCreate => Webhook.DeclaredMembers.Any(m => m.Name.StartsWith("Create"));

private bool HasUpdate => Webhook.DeclaredMembers.Any(m => m.Name.StartsWith("Update"));

private bool HasDelete => Webhook.DeclaredMembers.Any(m => m.Name.StartsWith("Delete"));

public abstract string WebhookPath { get; }

public string[] GetOperations() =>
new[] { HasCreate ? "CREATE" : null, HasUpdate ? "UPDATE" : null, HasDelete ? "DELETE" : null, }
.Where(o => o is not null).ToArray()!;
}
using System.Reflection;

using KubeOps.Abstractions.Entities;

namespace KubeOps.Cli.Transpilation;

internal abstract record BaseWebhook(TypeInfo Webhook, EntityMetadata Metadata)
{
private bool HasCreate => Webhook.DeclaredMembers.Any(m => m.Name.StartsWith("Create"));

private bool HasUpdate => Webhook.DeclaredMembers.Any(m => m.Name.StartsWith("Update"));

private bool HasDelete => Webhook.DeclaredMembers.Any(m => m.Name.StartsWith("Delete"));

public abstract string WebhookPath { get; }

public string[] GetOperations() =>
new[] { HasCreate ? "CREATE" : null, HasUpdate ? "UPDATE" : null, HasDelete ? "DELETE" : null, }
.Where(o => o is not null).ToArray()!;
}
22 changes: 11 additions & 11 deletions src/KubeOps.Cli/Transpilation/MutationWebhook.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Reflection;

using KubeOps.Abstractions.Entities;

namespace KubeOps.Cli.Transpilation;

internal record MutationWebhook(TypeInfo Validator, EntityMetadata Metadata) : BaseWebhook(Validator, Metadata)
{
public override string WebhookPath =>
$"/mutate/{Validator.BaseType!.GenericTypeArguments[0].Name.ToLowerInvariant()}";
}
using System.Reflection;

using KubeOps.Abstractions.Entities;

namespace KubeOps.Cli.Transpilation;

internal record MutationWebhook(TypeInfo Validator, EntityMetadata Metadata) : BaseWebhook(Validator, Metadata)
{
public override string WebhookPath =>
$"/mutate/{Validator.BaseType!.GenericTypeArguments[0].Name.ToLowerInvariant()}";
}
22 changes: 11 additions & 11 deletions src/KubeOps.Cli/Transpilation/ValidationWebhook.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Reflection;

using KubeOps.Abstractions.Entities;

namespace KubeOps.Cli.Transpilation;

internal record ValidationWebhook(TypeInfo Validator, EntityMetadata Metadata) : BaseWebhook(Validator, Metadata)
{
public override string WebhookPath =>
$"/validate/{Validator.BaseType!.GenericTypeArguments[0].Name.ToLowerInvariant()}";
}
using System.Reflection;

using KubeOps.Abstractions.Entities;

namespace KubeOps.Cli.Transpilation;

internal record ValidationWebhook(TypeInfo Validator, EntityMetadata Metadata) : BaseWebhook(Validator, Metadata)
{
public override string WebhookPath =>
$"/validate/{Validator.BaseType!.GenericTypeArguments[0].Name.ToLowerInvariant()}";
}
97 changes: 50 additions & 47 deletions src/KubeOps.Operator.Web/Builder/OperatorBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
using KubeOps.Abstractions.Builder;
using KubeOps.Operator.Web.LocalTunnel;

using Microsoft.Extensions.DependencyInjection;

namespace KubeOps.Operator.Web.Builder;

/// <summary>
/// Method extensions for the operator builder to register web specific services.
/// </summary>
public static class OperatorBuilderExtensions
{
/// <summary>
/// Adds a hosted service to the system that creates a "Local Tunnel"
/// (http://localtunnel.github.io/www/) to the running application.
/// The tunnel points to the configured host/port configuration and then
/// registers itself as webhook target within Kubernetes. This
/// enables developers to easily create webhooks without the requirement
/// of registering ngrok / localtunnel urls themselves.
/// </summary>
/// <param name="builder">The operator builder.</param>
/// <param name="port">The desired port that the asp.net application will run on.</param>
/// <param name="hostname">The desired hostname.</param>
/// <returns>The builder for chaining.</returns>
/// <example>
/// Attach the development tunnel to the operator if in debug mode.
/// <code>
/// var builder = WebApplication.CreateBuilder(args);
/// builder.Services
/// .AddKubernetesOperator()
/// .RegisterComponents()
/// #if DEBUG
/// .AddDevelopmentTunnel(5000)
/// #endif
/// ;
/// </code>
/// </example>
public static IOperatorBuilder AddDevelopmentTunnel(
this IOperatorBuilder builder,
ushort port,
string hostname = "localhost")
{
builder.Services.AddHostedService<DevelopmentTunnelService>();
builder.Services.AddSingleton(new TunnelConfig(hostname, port));
return builder;
}
}
using System.Reflection;

using KubeOps.Abstractions.Builder;
using KubeOps.Operator.Web.LocalTunnel;

using Microsoft.Extensions.DependencyInjection;

namespace KubeOps.Operator.Web.Builder;

/// <summary>
/// Method extensions for the operator builder to register web specific services.
/// </summary>
public static class OperatorBuilderExtensions
{
/// <summary>
/// Adds a hosted service to the system that creates a "Local Tunnel"
/// (http://localtunnel.github.io/www/) to the running application.
/// The tunnel points to the configured host/port configuration and then
/// registers itself as webhook target within Kubernetes. This
/// enables developers to easily create webhooks without the requirement
/// of registering ngrok / localtunnel urls themselves.
/// </summary>
/// <param name="builder">The operator builder.</param>
/// <param name="port">The desired port that the asp.net application will run on.</param>
/// <param name="hostname">The desired hostname.</param>
/// <returns>The builder for chaining.</returns>
/// <example>
/// Attach the development tunnel to the operator if in debug mode.
/// <code>
/// var builder = WebApplication.CreateBuilder(args);
/// builder.Services
/// .AddKubernetesOperator()
/// .RegisterComponents()
/// #if DEBUG
/// .AddDevelopmentTunnel(5000)
/// #endif
/// ;
/// </code>
/// </example>
public static IOperatorBuilder AddDevelopmentTunnel(
this IOperatorBuilder builder,
ushort port,
string hostname = "localhost")
{
builder.Services.AddHostedService<DevelopmentTunnelService>();
builder.Services.AddSingleton(new TunnelConfig(hostname, port));
builder.Services.AddSingleton(new WebhookLoader(Assembly.GetEntryAssembly()!));
return builder;
}
}
Loading

0 comments on commit ba957c3

Please sign in to comment.