Restructure solution layout by module
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using StellaOps.Zastava.Core.Configuration;
|
||||
using StellaOps.Zastava.Observer.Configuration;
|
||||
using StellaOps.Zastava.Observer.ContainerRuntime.Cri;
|
||||
using StellaOps.Zastava.Observer.ContainerRuntime;
|
||||
using StellaOps.Zastava.Observer.Posture;
|
||||
using StellaOps.Zastava.Observer.Runtime;
|
||||
using StellaOps.Zastava.Observer.Worker;
|
||||
using StellaOps.Zastava.Observer.Backend;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
public static class ObserverServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddZastavaObserver(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(services);
|
||||
ArgumentNullException.ThrowIfNull(configuration);
|
||||
|
||||
services.AddZastavaRuntimeCore(configuration, componentName: "observer");
|
||||
|
||||
services.AddOptions<ZastavaObserverOptions>()
|
||||
.Bind(configuration.GetSection(ZastavaObserverOptions.SectionName))
|
||||
.ValidateDataAnnotations()
|
||||
.PostConfigure(options =>
|
||||
{
|
||||
if (options.Backoff.Initial <= TimeSpan.Zero)
|
||||
{
|
||||
options.Backoff.Initial = TimeSpan.FromSeconds(1);
|
||||
}
|
||||
|
||||
if (options.Backoff.Max < options.Backoff.Initial)
|
||||
{
|
||||
options.Backoff.Max = options.Backoff.Initial;
|
||||
}
|
||||
|
||||
if (!options.Backend.AllowInsecureHttp && !string.Equals(options.Backend.BaseAddress.Scheme, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new InvalidOperationException("Observer backend baseAddress must use HTTPS unless allowInsecureHttp is explicitly enabled.");
|
||||
}
|
||||
|
||||
if (!options.Backend.PolicyPath.StartsWith("/", StringComparison.Ordinal))
|
||||
{
|
||||
throw new InvalidOperationException("Observer backend policyPath must be absolute (start with '/').");
|
||||
}
|
||||
|
||||
if (!options.Backend.EventsPath.StartsWith("/", StringComparison.Ordinal))
|
||||
{
|
||||
throw new InvalidOperationException("Observer backend eventsPath must be absolute (start with '/').");
|
||||
}
|
||||
})
|
||||
.ValidateOnStart();
|
||||
|
||||
services.TryAddSingleton(TimeProvider.System);
|
||||
services.TryAddSingleton<ICriRuntimeClientFactory, CriRuntimeClientFactory>();
|
||||
services.TryAddSingleton<IRuntimeEventBuffer, RuntimeEventBuffer>();
|
||||
services.TryAddSingleton<IRuntimeProcessCollector, RuntimeProcessCollector>();
|
||||
services.TryAddSingleton<IRuntimePostureCache, RuntimePostureCache>();
|
||||
services.TryAddSingleton<IRuntimePostureEvaluator, RuntimePostureEvaluator>();
|
||||
services.TryAddSingleton<ContainerStateTrackerFactory>();
|
||||
services.TryAddSingleton<ContainerRuntimePoller>();
|
||||
|
||||
services.AddHttpClient<IRuntimePolicyClient, RuntimePolicyClient>()
|
||||
.ConfigureHttpClient((provider, client) =>
|
||||
{
|
||||
var optionsMonitor = provider.GetRequiredService<IOptionsMonitor<ZastavaObserverOptions>>();
|
||||
var backend = optionsMonitor.CurrentValue.Backend;
|
||||
client.BaseAddress = backend.BaseAddress;
|
||||
client.Timeout = TimeSpan.FromSeconds(Math.Clamp(backend.RequestTimeoutSeconds, 1, 120));
|
||||
});
|
||||
|
||||
services.AddHttpClient<IRuntimeEventsClient, RuntimeEventsClient>()
|
||||
.ConfigureHttpClient((provider, client) =>
|
||||
{
|
||||
var optionsMonitor = provider.GetRequiredService<IOptionsMonitor<ZastavaObserverOptions>>();
|
||||
var backend = optionsMonitor.CurrentValue.Backend;
|
||||
client.BaseAddress = backend.BaseAddress;
|
||||
client.Timeout = TimeSpan.FromSeconds(Math.Clamp(backend.RequestTimeoutSeconds, 1, 120));
|
||||
});
|
||||
|
||||
services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<ZastavaRuntimeOptions>, ObserverRuntimeOptionsPostConfigure>());
|
||||
|
||||
services.AddHostedService<ObserverBootstrapService>();
|
||||
services.AddHostedService<ContainerLifecycleHostedService>();
|
||||
services.AddHostedService<RuntimeEventDispatchService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class ObserverRuntimeOptionsPostConfigure : IPostConfigureOptions<ZastavaRuntimeOptions>
|
||||
{
|
||||
public void PostConfigure(string? name, ZastavaRuntimeOptions options)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(options.Component))
|
||||
{
|
||||
options.Component = "observer";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user