|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using k8s; |
| 4 | +using KubeOps.Operator.Caching; |
| 5 | +using KubeOps.Operator.Client; |
| 6 | +using KubeOps.Operator.Controller; |
| 7 | +using KubeOps.Operator.DevOps; |
| 8 | +using KubeOps.Operator.Finalizer; |
| 9 | +using KubeOps.Operator.Queue; |
| 10 | +using KubeOps.Operator.Serialization; |
| 11 | +using KubeOps.Operator.Watcher; |
| 12 | +using Microsoft.Extensions.DependencyInjection; |
| 13 | +using Microsoft.Extensions.Diagnostics.HealthChecks; |
| 14 | +using Newtonsoft.Json; |
| 15 | +using Newtonsoft.Json.Converters; |
| 16 | +using Newtonsoft.Json.Serialization; |
| 17 | +using Prometheus; |
| 18 | +using YamlDotNet.Serialization; |
| 19 | + |
| 20 | +namespace KubeOps.Operator.Builder |
| 21 | +{ |
| 22 | + internal class OperatorBuilder : IOperatorBuilder |
| 23 | + { |
| 24 | + internal const string LivenessTag = "liveness"; |
| 25 | + internal const string ReadinessTag = "readiness"; |
| 26 | + |
| 27 | + public OperatorBuilder(IServiceCollection services) |
| 28 | + { |
| 29 | + Services = services; |
| 30 | + } |
| 31 | + |
| 32 | + public IServiceCollection Services { get; } |
| 33 | + |
| 34 | + public IOperatorBuilder AddHealthCheck<THealthCheck>(string? name = default) |
| 35 | + where THealthCheck : class, IHealthCheck |
| 36 | + { |
| 37 | + Services |
| 38 | + .AddHealthChecks() |
| 39 | + .AddCheck<THealthCheck>( |
| 40 | + name ?? typeof(THealthCheck).Name, |
| 41 | + tags: new[] { ReadinessTag, LivenessTag }); |
| 42 | + |
| 43 | + return this; |
| 44 | + } |
| 45 | + |
| 46 | + public IOperatorBuilder AddReadinessCheck<TReadinessCheck>(string? name = default) |
| 47 | + where TReadinessCheck : class, IHealthCheck |
| 48 | + { |
| 49 | + Services |
| 50 | + .AddHealthChecks() |
| 51 | + .AddCheck<TReadinessCheck>( |
| 52 | + name ?? typeof(TReadinessCheck).Name, |
| 53 | + tags: new[] { ReadinessTag }); |
| 54 | + |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + public IOperatorBuilder AddLivenessCheck<TLivenessCheck>(string? name = default) |
| 59 | + where TLivenessCheck : class, IHealthCheck |
| 60 | + { |
| 61 | + Services |
| 62 | + .AddHealthChecks() |
| 63 | + .AddCheck<TLivenessCheck>( |
| 64 | + name ?? typeof(TLivenessCheck).Name, |
| 65 | + tags: new[] { LivenessTag }); |
| 66 | + |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + public IOperatorBuilder AddController<TController>() |
| 71 | + where TController : class, IResourceController |
| 72 | + { |
| 73 | + Services.AddHostedService<TController>(); |
| 74 | + |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + public IOperatorBuilder AddFinalizer<TFinalizer>() |
| 79 | + where TFinalizer : class, IResourceFinalizer |
| 80 | + { |
| 81 | + Services.AddTransient(typeof(IResourceFinalizer), typeof(TFinalizer)); |
| 82 | + |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + internal IOperatorBuilder AddOperatorBase(OperatorSettings settings) |
| 87 | + { |
| 88 | + Services.AddSingleton(settings); |
| 89 | + |
| 90 | + // support lazy service resolution |
| 91 | + Services.AddTransient(typeof(Lazy<>), typeof(LazyService<>)); |
| 92 | + |
| 93 | + Services.AddTransient( |
| 94 | + _ => new JsonSerializerSettings |
| 95 | + { |
| 96 | + ContractResolver = new NamingConvention(), |
| 97 | + Converters = new List<JsonConverter> |
| 98 | + { |
| 99 | + new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy() }, |
| 100 | + }, |
| 101 | + }); |
| 102 | + JsonConvert.DefaultSettings = () => new JsonSerializerSettings |
| 103 | + { |
| 104 | + ContractResolver = new NamingConvention(), |
| 105 | + Converters = new List<JsonConverter> |
| 106 | + { |
| 107 | + new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy() }, |
| 108 | + }, |
| 109 | + }; |
| 110 | + |
| 111 | + Services.AddTransient( |
| 112 | + _ => new SerializerBuilder() |
| 113 | + .ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull) |
| 114 | + .WithNamingConvention(new NamingConvention()) |
| 115 | + .Build()); |
| 116 | + |
| 117 | + Services.AddTransient<EntitySerializer>(); |
| 118 | + |
| 119 | + Services.AddTransient<IKubernetesClient, KubernetesClient>(); |
| 120 | + Services.AddSingleton<IKubernetes>( |
| 121 | + _ => |
| 122 | + { |
| 123 | + var config = KubernetesClientConfiguration.BuildDefaultConfig(); |
| 124 | + |
| 125 | + return new Kubernetes(config, new ClientUrlFixer()) |
| 126 | + { |
| 127 | + SerializationSettings = |
| 128 | + { |
| 129 | + ContractResolver = new NamingConvention(), |
| 130 | + Converters = new List<JsonConverter> |
| 131 | + { new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy() } }, |
| 132 | + }, |
| 133 | + DeserializationSettings = |
| 134 | + { |
| 135 | + ContractResolver = new NamingConvention(), |
| 136 | + Converters = new List<JsonConverter> |
| 137 | + { new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy() } }, |
| 138 | + }, |
| 139 | + }; |
| 140 | + }); |
| 141 | + |
| 142 | + Services.AddTransient(typeof(IResourceCache<>), typeof(ResourceCache<>)); |
| 143 | + Services.AddTransient(typeof(IResourceWatcher<>), typeof(ResourceWatcher<>)); |
| 144 | + Services.AddTransient(typeof(IResourceEventQueue<>), typeof(ResourceEventQueue<>)); |
| 145 | + Services.AddTransient(typeof(IResourceServices<>), typeof(ResourceServices<>)); |
| 146 | + |
| 147 | + // Support for healthchecks and prometheus. |
| 148 | + Services |
| 149 | + .AddHealthChecks() |
| 150 | + .ForwardToPrometheus(); |
| 151 | + |
| 152 | + // Add the default controller liveness check. |
| 153 | + AddHealthCheck<ControllerLivenessCheck>(); |
| 154 | + |
| 155 | + return this; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments