|
| 1 | +using System.Reflection; |
| 2 | + |
| 3 | +using k8s; |
| 4 | +using k8s.Models; |
| 5 | + |
| 6 | +using KubeOps.Operator.Client; |
| 7 | +using KubeOps.Operator.Web.Webhooks.Mutation; |
| 8 | +using KubeOps.Operator.Web.Webhooks.Validation; |
| 9 | +using KubeOps.Transpiler; |
| 10 | + |
| 11 | +using Localtunnel; |
| 12 | +using Localtunnel.Endpoints.Http; |
| 13 | +using Localtunnel.Handlers.Kestrel; |
| 14 | +using Localtunnel.Processors; |
| 15 | +using Localtunnel.Tunnels; |
| 16 | + |
| 17 | +using Microsoft.Extensions.Hosting; |
| 18 | +using Microsoft.Extensions.Logging; |
| 19 | + |
| 20 | +namespace KubeOps.Operator.Web.LocalTunnel; |
| 21 | + |
| 22 | +internal class DevelopmentTunnelService : IHostedService |
| 23 | +{ |
| 24 | + private readonly TunnelConfig _config; |
| 25 | + private readonly LocaltunnelClient _tunnelClient; |
| 26 | + private Tunnel? _tunnel; |
| 27 | + |
| 28 | + public DevelopmentTunnelService(ILoggerFactory loggerFactory, TunnelConfig config) |
| 29 | + { |
| 30 | + _config = config; |
| 31 | + _tunnelClient = new(loggerFactory); |
| 32 | + } |
| 33 | + |
| 34 | + public async Task StartAsync(CancellationToken cancellationToken) |
| 35 | + { |
| 36 | + _tunnel = await _tunnelClient.OpenAsync( |
| 37 | + new KestrelTunnelConnectionHandler( |
| 38 | + new HttpRequestProcessingPipelineBuilder() |
| 39 | + .Append(new HttpHostHeaderRewritingRequestProcessor(_config.Hostname)).Build(), |
| 40 | + new HttpTunnelEndpointFactory(_config.Hostname, _config.Port)), |
| 41 | + cancellationToken: cancellationToken); |
| 42 | + await _tunnel.StartAsync(cancellationToken: cancellationToken); |
| 43 | + await RegisterValidators(_tunnel.Information.Url); |
| 44 | + await RegisterMutators(_tunnel.Information.Url); |
| 45 | + } |
| 46 | + |
| 47 | + public Task StopAsync(CancellationToken cancellationToken) |
| 48 | + { |
| 49 | + _tunnel?.Dispose(); |
| 50 | + return Task.CompletedTask; |
| 51 | + } |
| 52 | + |
| 53 | + private static async Task RegisterValidators(Uri uri) |
| 54 | + { |
| 55 | + var validationWebhooks = Assembly |
| 56 | + .GetEntryAssembly()! |
| 57 | + .DefinedTypes |
| 58 | + .Where(t => t.BaseType?.IsGenericType == true && |
| 59 | + t.BaseType?.GetGenericTypeDefinition() == typeof(ValidationWebhook<>)) |
| 60 | + .Select(t => (HookTypeName: t.BaseType!.GenericTypeArguments[0].Name.ToLowerInvariant(), |
| 61 | + Entities.ToEntityMetadata(t.BaseType!.GenericTypeArguments[0]).Metadata)) |
| 62 | + .Select(hook => new V1ValidatingWebhook |
| 63 | + { |
| 64 | + Name = $"validate.{hook.Metadata.SingularName}.{hook.Metadata.Group}.{hook.Metadata.Version}", |
| 65 | + MatchPolicy = "Exact", |
| 66 | + AdmissionReviewVersions = new[] { "v1" }, |
| 67 | + SideEffects = "None", |
| 68 | + Rules = new[] |
| 69 | + { |
| 70 | + new V1RuleWithOperations |
| 71 | + { |
| 72 | + Operations = new[] { "*" }, |
| 73 | + Resources = new[] { hook.Metadata.PluralName }, |
| 74 | + ApiGroups = new[] { hook.Metadata.Group }, |
| 75 | + ApiVersions = new[] { hook.Metadata.Version }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + ClientConfig = new Admissionregistrationv1WebhookClientConfig |
| 79 | + { |
| 80 | + Url = $"{uri}validate/{hook.HookTypeName}", |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + var validatorConfig = new V1ValidatingWebhookConfiguration( |
| 85 | + metadata: new V1ObjectMeta(name: "dev-validators"), |
| 86 | + webhooks: validationWebhooks.ToList()).Initialize(); |
| 87 | + |
| 88 | + using var validatorClient = KubernetesClientFactory.Create<V1ValidatingWebhookConfiguration>(); |
| 89 | + await validatorClient.SaveAsync(validatorConfig); |
| 90 | + } |
| 91 | + |
| 92 | + private static async Task RegisterMutators(Uri uri) |
| 93 | + { |
| 94 | + var mutationWebhooks = Assembly |
| 95 | + .GetEntryAssembly()! |
| 96 | + .DefinedTypes |
| 97 | + .Where(t => t.BaseType?.IsGenericType == true && |
| 98 | + t.BaseType?.GetGenericTypeDefinition() == typeof(MutationWebhook<>)) |
| 99 | + .Select(t => (HookTypeName: t.BaseType!.GenericTypeArguments[0].Name.ToLowerInvariant(), |
| 100 | + Entities.ToEntityMetadata(t.BaseType!.GenericTypeArguments[0]).Metadata)) |
| 101 | + .Select(hook => new V1MutatingWebhook |
| 102 | + { |
| 103 | + Name = $"validate.{hook.Metadata.SingularName}.{hook.Metadata.Group}.{hook.Metadata.Version}", |
| 104 | + MatchPolicy = "Exact", |
| 105 | + AdmissionReviewVersions = new[] { "v1" }, |
| 106 | + SideEffects = "None", |
| 107 | + Rules = new[] |
| 108 | + { |
| 109 | + new V1RuleWithOperations |
| 110 | + { |
| 111 | + Operations = new[] { "*" }, |
| 112 | + Resources = new[] { hook.Metadata.PluralName }, |
| 113 | + ApiGroups = new[] { hook.Metadata.Group }, |
| 114 | + ApiVersions = new[] { hook.Metadata.Version }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + ClientConfig = new Admissionregistrationv1WebhookClientConfig |
| 118 | + { |
| 119 | + Url = $"{uri}validate/{hook.HookTypeName}", |
| 120 | + }, |
| 121 | + }); |
| 122 | + |
| 123 | + var mutatorConfig = new V1MutatingWebhookConfiguration( |
| 124 | + metadata: new V1ObjectMeta(name: "dev-mutators"), |
| 125 | + webhooks: mutationWebhooks.ToList()).Initialize(); |
| 126 | + |
| 127 | + using var mutatorClient = KubernetesClientFactory.Create<V1MutatingWebhookConfiguration>(); |
| 128 | + await mutatorClient.SaveAsync(mutatorConfig); |
| 129 | + } |
| 130 | +} |
0 commit comments