-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add some tests for web operators (#638)
- Loading branch information
Showing
36 changed files
with
1,947 additions
and
1,547 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
97
src/KubeOps.Operator.Web/Builder/OperatorBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.