From 003950a9cd6dc2c5c5a10f2d71a6dc1191fd4c47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BChler?= Date: Fri, 6 Oct 2023 15:30:35 +0200 Subject: [PATCH] chore: remove old code --- .../.template.config/template.json | 38 --- .../GeneratedOperatorProject.fsproj | 12 - .../Templates/EmptyOperator.FSharp/Program.fs | 19 -- .../Templates/EmptyOperator.FSharp/Startup.fs | 15 -- .../appsettings.Development.json | 9 - .../EmptyOperator.FSharp/appsettings.json | 10 - .../.template.config/template.json | 37 --- .../Controller/DemoController.fs | 36 --- .../Operator.FSharp/Entities/V1DemoEntity.fs | 14 - .../Finalizer/DemoFinalizer.fs | 13 - .../GeneratedOperatorProject.fsproj | 17 -- .../Templates/Operator.FSharp/Program.fs | 19 -- .../Templates/Operator.FSharp/Startup.fs | 15 -- .../Operator.FSharp/Webhooks/DemoMutator.fs | 13 - .../Operator.FSharp/Webhooks/DemoValidator.fs | 14 - .../appsettings.Development.json | 9 - .../Operator.FSharp/appsettings.json | 10 - .../KubeOps.Testing/KubeOps.Testing.csproj | 29 --- .../KubernetesOperatorFactory.cs | 123 --------- _old/src/KubeOps.Testing/MockEventQueue.cs | 35 --- .../KubeOps.Testing/MockKubernetesClient.cs | 244 ------------------ .../KubeOps.Testing/MockedLeaderElection.cs | 15 -- _old/src/KubeOps.Testing/README.md | 100 ------- examples/Operator/todos.txt | 1 - 24 files changed, 847 deletions(-) delete mode 100644 _old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/.template.config/template.json delete mode 100644 _old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/GeneratedOperatorProject.fsproj delete mode 100644 _old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/Program.fs delete mode 100644 _old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/Startup.fs delete mode 100644 _old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/appsettings.Development.json delete mode 100644 _old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/appsettings.json delete mode 100644 _old/src/KubeOps.Templates/Templates/Operator.FSharp/.template.config/template.json delete mode 100644 _old/src/KubeOps.Templates/Templates/Operator.FSharp/Controller/DemoController.fs delete mode 100644 _old/src/KubeOps.Templates/Templates/Operator.FSharp/Entities/V1DemoEntity.fs delete mode 100644 _old/src/KubeOps.Templates/Templates/Operator.FSharp/Finalizer/DemoFinalizer.fs delete mode 100644 _old/src/KubeOps.Templates/Templates/Operator.FSharp/GeneratedOperatorProject.fsproj delete mode 100644 _old/src/KubeOps.Templates/Templates/Operator.FSharp/Program.fs delete mode 100644 _old/src/KubeOps.Templates/Templates/Operator.FSharp/Startup.fs delete mode 100644 _old/src/KubeOps.Templates/Templates/Operator.FSharp/Webhooks/DemoMutator.fs delete mode 100644 _old/src/KubeOps.Templates/Templates/Operator.FSharp/Webhooks/DemoValidator.fs delete mode 100644 _old/src/KubeOps.Templates/Templates/Operator.FSharp/appsettings.Development.json delete mode 100644 _old/src/KubeOps.Templates/Templates/Operator.FSharp/appsettings.json delete mode 100644 _old/src/KubeOps.Testing/KubeOps.Testing.csproj delete mode 100644 _old/src/KubeOps.Testing/KubernetesOperatorFactory.cs delete mode 100644 _old/src/KubeOps.Testing/MockEventQueue.cs delete mode 100644 _old/src/KubeOps.Testing/MockKubernetesClient.cs delete mode 100644 _old/src/KubeOps.Testing/MockedLeaderElection.cs delete mode 100644 _old/src/KubeOps.Testing/README.md diff --git a/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/.template.config/template.json b/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/.template.config/template.json deleted file mode 100644 index 5249e20b..00000000 --- a/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/.template.config/template.json +++ /dev/null @@ -1,38 +0,0 @@ -{ - "$schema": "http://json.schemastore.org/template", - "author": "Christoph Bühler", - "classifications": [ - "Kubernetes", - "Operator", - "Empty" - ], - "identity": "KubeOps.Templates.EmptyOperator.FSharp", - "groupIdentity": "KubeOps.Templates.EmptyOperator", - "name": "Kubernetes Operator Empty", - "description": "Create an empty kubernetes operator with the KubeOps SDK", - "shortName": "operator-empty", - "tags": { - "language": "F#", - "type": "project" - }, - "sourceName": "GeneratedOperatorProject", - "defaultName": "Operator", - "preferNameDirectory": true, - "postActions": [ - { - "actionId": "B17581D1-C5C9-4489-8F0A-004BE667B814", - "description": "Add KubeOps SDK reference", - "continueOnError": false, - "manualInstructions": [ - { - "text": "Add the KubeOps package to your project via nuget" - } - ], - "args": { - "referenceType": "package", - "reference": "KubeOps", - "projectFileExtensions": ".fsproj" - } - } - ] -} diff --git a/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/GeneratedOperatorProject.fsproj b/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/GeneratedOperatorProject.fsproj deleted file mode 100644 index dfb64969..00000000 --- a/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/GeneratedOperatorProject.fsproj +++ /dev/null @@ -1,12 +0,0 @@ - - - - net6.0 - - - - - - - - diff --git a/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/Program.fs b/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/Program.fs deleted file mode 100644 index 9cae6827..00000000 --- a/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/Program.fs +++ /dev/null @@ -1,19 +0,0 @@ -namespace GeneratedOperatorProject - -open Microsoft.AspNetCore.Hosting -open Microsoft.Extensions.Hosting -open KubeOps.Operator - -module Program = - let createHostBuilder args = - Host - .CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(fun webBuilder -> webBuilder.UseStartup() |> ignore) - - [] - let main args = - createHostBuilder(args) - .Build() - .RunOperatorAsync args - |> Async.AwaitTask - |> Async.RunSynchronously diff --git a/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/Startup.fs b/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/Startup.fs deleted file mode 100644 index 95fb19c3..00000000 --- a/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/Startup.fs +++ /dev/null @@ -1,15 +0,0 @@ -namespace GeneratedOperatorProject - -open Microsoft.AspNetCore.Builder -open Microsoft.Extensions.DependencyInjection -open KubeOps.Operator - -type Startup() = - - member _.ConfigureServices(services: IServiceCollection) = - services.AddKubernetesOperator() |> ignore - () - - member _.Configure(app: IApplicationBuilder) = - app.UseKubernetesOperator() - () diff --git a/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/appsettings.Development.json b/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/appsettings.Development.json deleted file mode 100644 index 8983e0fc..00000000 --- a/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/appsettings.Development.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } - } -} diff --git a/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/appsettings.json b/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/appsettings.json deleted file mode 100644 index d9d9a9bf..00000000 --- a/_old/src/KubeOps.Templates/Templates/EmptyOperator.FSharp/appsettings.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } - }, - "AllowedHosts": "*" -} diff --git a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/.template.config/template.json b/_old/src/KubeOps.Templates/Templates/Operator.FSharp/.template.config/template.json deleted file mode 100644 index e02ac12b..00000000 --- a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/.template.config/template.json +++ /dev/null @@ -1,37 +0,0 @@ -{ - "$schema": "http://json.schemastore.org/template", - "author": "Christoph Bühler", - "classifications": [ - "Kubernetes", - "Operator" - ], - "identity": "KubeOps.Templates.Operator.FSharp", - "groupIdentity": "KubeOps.Templates.Operator", - "name": "Kubernetes Operator", - "description": "Create a kubernetes operator with demo implementation", - "shortName": "operator", - "tags": { - "language": "F#", - "type": "project" - }, - "sourceName": "GeneratedOperatorProject", - "defaultName": "Operator", - "preferNameDirectory": true, - "postActions": [ - { - "actionId": "B17581D1-C5C9-4489-8F0A-004BE667B814", - "description": "Add KubeOps SDK reference", - "continueOnError": false, - "manualInstructions": [ - { - "text": "Add the KubeOps package to your project via nuget" - } - ], - "args": { - "referenceType": "package", - "reference": "KubeOps", - "projectFileExtensions": ".fsproj" - } - } - ] -} diff --git a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Controller/DemoController.fs b/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Controller/DemoController.fs deleted file mode 100644 index fce747ca..00000000 --- a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Controller/DemoController.fs +++ /dev/null @@ -1,36 +0,0 @@ -namespace GeneratedOperatorProject.Controller - -open System -open System.Threading.Tasks -open GeneratedOperatorProject.Entities -open GeneratedOperatorProject.Finalizer -open k8s.Models -open KubeOps.Operator.Controller -open KubeOps.Operator.Controller.Results -open KubeOps.Operator.Finalizer -open KubeOps.Operator.Rbac -open Microsoft.Extensions.Logging - -[, Verbs = RbacVerb.All)>] -type DemoController(logger: ILogger, finalizerManager: IFinalizerManager) = - interface IResourceController with - member this.ReconcileAsync entity = - async { - logger.LogInformation $"entity {entity.Name()} called reconcile" - - do! finalizerManager.RegisterFinalizerAsync entity - |> Async.AwaitTask - - return - ResourceControllerResult.RequeueEvent - <| TimeSpan.FromSeconds 15.0 - } - |> Async.StartAsTask - - member this.StatusModifiedAsync entity = - logger.LogInformation $"entity {entity.Name()} called not modified" - Task.CompletedTask - - member this.DeletedAsync entity = - logger.LogInformation $"entity {entity.Name()} called deleted" - Task.CompletedTask diff --git a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Entities/V1DemoEntity.fs b/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Entities/V1DemoEntity.fs deleted file mode 100644 index 5786840b..00000000 --- a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Entities/V1DemoEntity.fs +++ /dev/null @@ -1,14 +0,0 @@ -namespace GeneratedOperatorProject.Entities - -open k8s.Models -open KubeOps.Operator.Entities - -type V1DemoEntitySpec() = - member val Username = "" with get, set - -type V1DemoEntityStatus() = - member val DemoStatus = "" with get, set - -[] -type V1DemoEntity() = - inherit CustomKubernetesEntity() diff --git a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Finalizer/DemoFinalizer.fs b/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Finalizer/DemoFinalizer.fs deleted file mode 100644 index c54ee654..00000000 --- a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Finalizer/DemoFinalizer.fs +++ /dev/null @@ -1,13 +0,0 @@ -namespace GeneratedOperatorProject.Finalizer - -open System.Threading.Tasks -open GeneratedOperatorProject.Entities -open k8s.Models -open KubeOps.Operator.Finalizer -open Microsoft.Extensions.Logging - -type DemoFinalizer(logger: ILogger) = - interface IResourceFinalizer with - member this.FinalizeAsync entity = - logger.LogInformation $"entity {entity.Name()} called finalize" - Task.CompletedTask diff --git a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/GeneratedOperatorProject.fsproj b/_old/src/KubeOps.Templates/Templates/Operator.FSharp/GeneratedOperatorProject.fsproj deleted file mode 100644 index c4e195e6..00000000 --- a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/GeneratedOperatorProject.fsproj +++ /dev/null @@ -1,17 +0,0 @@ - - - - net6.0 - - - - - - - - - - - - - diff --git a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Program.fs b/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Program.fs deleted file mode 100644 index 9cae6827..00000000 --- a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Program.fs +++ /dev/null @@ -1,19 +0,0 @@ -namespace GeneratedOperatorProject - -open Microsoft.AspNetCore.Hosting -open Microsoft.Extensions.Hosting -open KubeOps.Operator - -module Program = - let createHostBuilder args = - Host - .CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(fun webBuilder -> webBuilder.UseStartup() |> ignore) - - [] - let main args = - createHostBuilder(args) - .Build() - .RunOperatorAsync args - |> Async.AwaitTask - |> Async.RunSynchronously diff --git a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Startup.fs b/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Startup.fs deleted file mode 100644 index 95fb19c3..00000000 --- a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Startup.fs +++ /dev/null @@ -1,15 +0,0 @@ -namespace GeneratedOperatorProject - -open Microsoft.AspNetCore.Builder -open Microsoft.Extensions.DependencyInjection -open KubeOps.Operator - -type Startup() = - - member _.ConfigureServices(services: IServiceCollection) = - services.AddKubernetesOperator() |> ignore - () - - member _.Configure(app: IApplicationBuilder) = - app.UseKubernetesOperator() - () diff --git a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Webhooks/DemoMutator.fs b/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Webhooks/DemoMutator.fs deleted file mode 100644 index 7a5ab4ed..00000000 --- a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Webhooks/DemoMutator.fs +++ /dev/null @@ -1,13 +0,0 @@ -namespace GeneratedOperatorProject.Webhooks - -open GeneratedOperatorProject.Entities -open KubeOps.Operator.Webhooks -open Microsoft.AspNetCore.Http - -type DemoMutator() = - interface IMutationWebhook with - member this.Operations = AdmissionOperations.Create - - member this.Create(newEntity, _) = - newEntity.Spec.Username <- "not foobar" - MutationResult.Modified(newEntity) diff --git a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Webhooks/DemoValidator.fs b/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Webhooks/DemoValidator.fs deleted file mode 100644 index c8070cc2..00000000 --- a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/Webhooks/DemoValidator.fs +++ /dev/null @@ -1,14 +0,0 @@ -namespace GeneratedOperatorProject.Webhooks - -open GeneratedOperatorProject.Entities -open KubeOps.Operator.Webhooks -open Microsoft.AspNetCore.Http - -type DemoValidator() = - interface IValidationWebhook with - member this.Operations = AdmissionOperations.Create - - member this.Create(newEntity, _) = - if newEntity.Spec.Username = "forbiddenUsername" - then ValidationResult.Fail(StatusCodes.Status400BadRequest, "Username is forbidden") - else ValidationResult.Success() diff --git a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/appsettings.Development.json b/_old/src/KubeOps.Templates/Templates/Operator.FSharp/appsettings.Development.json deleted file mode 100644 index 8983e0fc..00000000 --- a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/appsettings.Development.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } - } -} diff --git a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/appsettings.json b/_old/src/KubeOps.Templates/Templates/Operator.FSharp/appsettings.json deleted file mode 100644 index d9d9a9bf..00000000 --- a/_old/src/KubeOps.Templates/Templates/Operator.FSharp/appsettings.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } - }, - "AllowedHosts": "*" -} diff --git a/_old/src/KubeOps.Testing/KubeOps.Testing.csproj b/_old/src/KubeOps.Testing/KubeOps.Testing.csproj deleted file mode 100644 index 9e2298bc..00000000 --- a/_old/src/KubeOps.Testing/KubeOps.Testing.csproj +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - true - KubeOps.Testing - Kubernetes Operator SDK CustomResourceDefinition Testing Test - - Additional Resources that helps / supports integration - testing on a kubernetes operator written with the KubeOps - operator sdk. - - - - - - - - - - - - - - - - diff --git a/_old/src/KubeOps.Testing/KubernetesOperatorFactory.cs b/_old/src/KubeOps.Testing/KubernetesOperatorFactory.cs deleted file mode 100644 index 670c4a89..00000000 --- a/_old/src/KubeOps.Testing/KubernetesOperatorFactory.cs +++ /dev/null @@ -1,123 +0,0 @@ -using k8s; -using k8s.Models; - -using KubeOps.KubernetesClient; -using KubeOps.Operator.Controller; -using KubeOps.Operator.Kubernetes; -using KubeOps.Operator.Leadership; - -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc.Testing; -using Microsoft.AspNetCore.TestHost; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; - -namespace KubeOps.Testing; - -/// -/// Operator factory for testing an operator created with the asp web server. -/// -/// Type of the Startup type (see asp.net). -public class KubernetesOperatorFactory : WebApplicationFactory - where TTestStartup : class -{ - private string? _solutionRelativeContentRoot; - - /// - /// Return a mocked kubernetes client. This client defines - /// no-op for all related methods. - /// - public MockKubernetesClient MockedKubernetesClient => - Services.GetRequiredService() as MockKubernetesClient ?? - throw new ArgumentException("Wrong kubernetes client registered."); - - /// - /// Set a specific content root path to the given factory. - /// - /// The solution relative content root path to configure. - /// The for chaining. - public KubernetesOperatorFactory WithSolutionRelativeContentRoot(string root) - { - _solutionRelativeContentRoot = root; - return this; - } - - public IManagedResourceController GetController() - where TEntity : IKubernetesObject => - Services - .GetRequiredService() - .BuildControllers() - .First(); - - /// - /// Start the server. - /// - public void Run() - { - // This triggers "EnsureServer()" in the base class. - var server = Server; - } - - public Task EnqueueEvent( - ResourceEventType type, - TEntity resource, - int attempt = 0, - TimeSpan? delay = null) - where TEntity : class, IKubernetesObject - { - var queue = Services.GetService>(); - - queue?.EnqueueLocal(new ResourceEvent(type, resource, attempt, delay)); - - return Task.CompletedTask; - } - - public Task EnqueueFinalization(TEntity resource) - where TEntity : class, IKubernetesObject - { - var queue = Services.GetService>(); - - queue?.EnqueueLocal(new ResourceEvent(ResourceEventType.Finalizing, resource)); - - return Task.CompletedTask; - } - - /// - /// Create the host builder. Needed for the factory. - /// - /// The created . - protected override IHostBuilder CreateHostBuilder() => - Host.CreateDefaultBuilder() - .ConfigureWebHostDefaults( - webBuilder => webBuilder - .UseStartup()); - - /// - /// Configure the web-host. - /// This registers the mocked client as well as mocked - /// event queues. - /// - /// The web host builder. - protected override void ConfigureWebHost(IWebHostBuilder builder) - { - base.ConfigureWebHost(builder); - builder.ConfigureTestServices( - services => - { - var elector = services.First( - d => d.ServiceType == typeof(IHostedService) && d.ImplementationType == typeof(LeaderElector)); - services.Remove(elector); - - services.RemoveAll(typeof(IEventQueue<>)); - services.AddSingleton(typeof(IEventQueue<>), typeof(MockEventQueue<>)); - }); - if (_solutionRelativeContentRoot != null) - { - builder.UseSolutionRelativeContentRoot(_solutionRelativeContentRoot); - } - - builder.ConfigureLogging(logging => logging.ClearProviders()); - } -} diff --git a/_old/src/KubeOps.Testing/MockEventQueue.cs b/_old/src/KubeOps.Testing/MockEventQueue.cs deleted file mode 100644 index 5081ef09..00000000 --- a/_old/src/KubeOps.Testing/MockEventQueue.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Reactive.Subjects; - -using k8s; -using k8s.Models; - -using KubeOps.Operator.Controller; - -namespace KubeOps.Testing; - -internal class MockEventQueue : IEventQueue - where TEntity : class, IKubernetesObject -{ - private readonly Subject> _localEvents; - - public MockEventQueue() - { - _localEvents = new Subject>(); - - Events = _localEvents; - } - - public IObservable> Events { get; } - - public Task StartAsync(Action> onWatcherEvent) - { - return Task.CompletedTask; - } - - public Task StopAsync() - { - return Task.CompletedTask; - } - - public void EnqueueLocal(ResourceEvent resourceEvent) => _localEvents.OnNext(resourceEvent); -} diff --git a/_old/src/KubeOps.Testing/MockKubernetesClient.cs b/_old/src/KubeOps.Testing/MockKubernetesClient.cs deleted file mode 100644 index 301eb766..00000000 --- a/_old/src/KubeOps.Testing/MockKubernetesClient.cs +++ /dev/null @@ -1,244 +0,0 @@ -using System.Diagnostics.CodeAnalysis; - -using k8s; -using k8s.Models; - -using KubeOps.KubernetesClient; -using KubeOps.KubernetesClient.LabelSelectors; - -namespace KubeOps.Testing; - -/// -/// Mocked implementation for the kubernetes client. -/// Returns the "result" objects if given. -/// -[SuppressMessage( - "StyleCop.CSharp.MaintainabilityRules", - "SA1625:Element documentation should not be copied and pasted", - Justification = "This is the mock implementation which basically does nothing.")] -public class MockKubernetesClient : IKubernetesClient -{ - /// - /// Instance of the default client. - /// - public IKubernetes ApiClient { get; } = new Kubernetes(KubernetesClientConfiguration.BuildDefaultConfig()); - - /// - /// Mocked result for the call. - /// If null, then null is returned. - /// This field must be manually reset. - /// - public object? GetResult { get; set; } - - /// - /// Mocked result for the call. - /// If null, an empty list is returned. - /// This field must be manually reset. - /// - public IList? ListResult { get; set; } - - /// - /// Mocked result for the call. - /// If null, then null is returned. - /// This field must be manually reset. - /// - public object? SaveResult { get; set; } - - /// - /// Mocked result for the call. - /// If null, then null is returned. - /// This field must be manually reset. - /// - public object? CreateResult { get; set; } - - /// - /// Mocked result for the call. - /// If null, then null is returned. - /// This field must be manually reset. - /// - public object? UpdateResult { get; set; } - - public Uri BaseUri => new("https://localhost:8080"); - - /// - /// Returns "default". - /// - /// Not used env name. - /// "default". - public Task GetCurrentNamespace(string downwardApiEnvName = "POD_NAMESPACE") => - Task.FromResult("default"); - - /// - /// Create mocked server version. - /// - /// Empty "new" instance. - public Task GetServerVersion() => Task.FromResult(new VersionInfo()); - - /// - /// Mocked Get method. - /// - /// Not used. - /// Not used. - /// Type of the resource. - /// The value of . - public Task Get(string name, string? @namespace = null) - where TEntity : class, IKubernetesObject - => Task.FromResult(GetResult as TEntity); - - /// - /// Mocked list method. - /// - /// Not used. - /// Not used. - /// Type of the resource. - /// The value of or empty list if the result is null. - public Task> List(string? @namespace = null, string? labelSelector = null) - where TEntity : IKubernetesObject - => Task.FromResult(ListResult as IList ?? new List()); - - /// - /// Mocked list method. - /// - /// Not used. - /// Not used. - /// Type of the resource. - /// The value of or empty list if the result is null. - public Task> List(string? @namespace = null, params ILabelSelector[] labelSelectors) - where TEntity : IKubernetesObject - => Task.FromResult(ListResult as IList ?? new List()); - - /// - /// Mocked Save method. - /// - /// Not used. - /// Type of the resource. - /// The value of . - public Task Save(TEntity resource) - where TEntity : class, IKubernetesObject - => Task.FromResult(SaveResult as TEntity)!; - - /// - /// Mocked Create method. - /// - /// Not used. - /// Type of the resource. - /// The value of . - public Task Create(TEntity resource) - where TEntity : IKubernetesObject - => Task.FromResult((TEntity)CreateResult!)!; - - /// - /// Mocked Update method. - /// - /// Not used. - /// Type of the resource. - /// The value of . - public Task Update(TEntity resource) - where TEntity : IKubernetesObject - => Task.FromResult((TEntity)UpdateResult!)!; - - /// - /// Mocked UpdateStatus method. - /// - /// Not used. - /// Type of the resource. - /// Empty completed task. - public Task UpdateStatus(TEntity resource) - where TEntity : IKubernetesObject - => Task.CompletedTask; - - /// - /// Mocked Delete method. - /// - /// Not used. - /// Type of the resource. - /// Empty completed task. - public Task Delete(TEntity resource) - where TEntity : IKubernetesObject - => Task.CompletedTask; - - /// - /// Mocked Delete method. - /// - /// Not used. - /// Type of the resource. - /// Empty completed task. - public Task Delete(IEnumerable resources) - where TEntity : IKubernetesObject - => Task.CompletedTask; - - /// - /// Mocked Delete method. - /// - /// Not used. - /// Type of the resource. - /// Empty completed task. - public Task Delete(params TEntity[] resources) - where TEntity : IKubernetesObject - => Task.CompletedTask; - - /// - /// Mocked Delete method. - /// - /// Not used. - /// Not used. - /// Type of the resource. - /// Empty completed task. - public Task Delete(string name, string? @namespace = null) - where TEntity : IKubernetesObject - => Task.CompletedTask; - - /// - /// Mocked Watch method. - /// - /// Not used. - /// Not used. - /// Not used. - /// Not used. - /// Not used. - /// Not used. - /// Not used. - /// Type of the resource. - /// Empty new watcher from a memory stream. - public Task> Watch( - TimeSpan timeout, - Action onEvent, - Action? onError = null, - Action? onClose = null, - string? @namespace = null, - CancellationToken cancellationToken = default, - params ILabelSelector[] selectors) - where TEntity : IKubernetesObject - => Task.FromResult( - new Watcher( - () => Task.FromResult(new StreamReader(new MemoryStream())), - (_, __) => { }, - _ => { })); - - /// - /// Mocked Watch method. - /// - /// Not used. - /// Not used. - /// Not used. - /// Not used. - /// Not used. - /// Not used. - /// Not used. - /// Type of the resource. - /// Empty new watcher from a memory stream. - public Task> Watch( - TimeSpan timeout, - Action onEvent, - Action? onError = null, - Action? onClose = null, - string? @namespace = null, - CancellationToken cancellationToken = default, - string? labelSelector = default) - where TEntity : IKubernetesObject - => Task.FromResult( - new Watcher( - () => Task.FromResult(new StreamReader(new MemoryStream())), - (_, __) => { }, - _ => { })); -} diff --git a/_old/src/KubeOps.Testing/MockedLeaderElection.cs b/_old/src/KubeOps.Testing/MockedLeaderElection.cs deleted file mode 100644 index 83319b9c..00000000 --- a/_old/src/KubeOps.Testing/MockedLeaderElection.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Reactive.Subjects; - -using KubeOps.Operator.Leadership; - -namespace KubeOps.Testing; - -internal class MockedLeaderElection : ILeaderElection -{ - public IObservable LeadershipChange { get; } = - new BehaviorSubject(LeaderState.Leader); - - void ILeaderElection.LeadershipChanged(LeaderState state) - { - } -} diff --git a/_old/src/KubeOps.Testing/README.md b/_old/src/KubeOps.Testing/README.md deleted file mode 100644 index 3e8591d9..00000000 --- a/_old/src/KubeOps.Testing/README.md +++ /dev/null @@ -1,100 +0,0 @@ -# KubeOps Testing Utilities - -The `KubeOps.Testing` package provides tools to test the custom operator. - -To have an example for integration testing a custom operator -have a look at the test code in the repository: -[Integration Tests](https://github.com/buehler/dotnet-operator-sdk/tree/master/tests/KubeOps.TestOperator) - -> The tools provided aid with Integration testing. -> For normal unit testing, you can just mock all the things. - -The main entry point for testing your custom operator is the -`KubernetesOperatorFactory` class. It is meant to be -injected into your test. - -> The following documentation assumes you are using xUnit as a -> testing framework. The techniques used should be present -> with other testing frameworks as well. - -The following steps are needed for integration testing the controller: - -- Create a `TestStartup.cs` file (or any other name you want) -- Inject the `KubernetesOperatorFactory` -- Use the mocked client and helper functions to test your operator - -## Test Startup - -This file is very similar to a "normal" `Startup.cs` file of an -asp.net application. Either you subclass it and replace your test mocked -services, or you create a new one. - -## Mocked elements - -The main part of this operator factory does mock the used Kubernetes client -and helper functions to enqueue events and finalizer events. - -Both mocked elements can be retrieved via the operator factory with: - -- "MockedKubernetesClient" -- "KubernetesOperatorFactory.EnqueueEvent" -- "KubernetesOperatorFactory.EnqueueFinalization" - -### Mocked events - -With the mentioned factory functions one can fire events for entities. -This can be used to test your controllers. - -The controllers are normally instantiated via the scope of the DI system. - -### Mocked IKubernetesClient - -The client is essentially an empty implementation of the interface. -All methods do return `null` or the object that was passed -into the mocked class. There are five different object -references that can be set to return certain results upon calling the client. - -> The mocked client is injected as singleton, this means -> the used "result" references can vary. Be aware of that -> and don't run your tests in parallel if you rely -> on those results. - -## Writing a test - -Now with all parts in place, we can write a test. -You probably need to set the solution relative content root for -asp.net testing. But then you can run the factory -and create a test. - -```csharp -// Constructor -public TestControllerTest(KubernetesOperatorFactory factory) -{ - _factory = factory.WithSolutionRelativeContentRoot("tests/KubeOps.TestOperator"); - - _controller = _factory.Services - .GetRequiredService() - .BuildControllers() - .First(); - - _managerMock = _factory.Services.GetRequiredService>(); - _managerMock.Reset(); -} - -// snip - -[Fact] -public async Task Test_If_Manager_Created_Is_Called() -{ - _factory.Run(); - - _managerMock.Setup(o => o.Reconciled(It.IsAny())); - _managerMock.Verify(o => o.Reconciled(It.IsAny()), Times.Never); - - await _controller.StartAsync(); - await _factory.EnqueueEvent(ResourceEventType.Reconcile, new V1TestEntity()); - await _controller.StopAsync(); - - _managerMock.Verify(o => o.Reconciled(It.IsAny()), Times.Once); -} -``` diff --git a/examples/Operator/todos.txt b/examples/Operator/todos.txt index bc87ef49..be72489b 100644 --- a/examples/Operator/todos.txt +++ b/examples/Operator/todos.txt @@ -2,4 +2,3 @@ - error handling - web: webhooks - docs -- try .net 8 AOT?