Skip to content

Commit

Permalink
feat(client): Use true generics again (#640)
Browse files Browse the repository at this point in the history
This readds all methods in the kubernetes client
that were annotated with <TEntity> in their methods.
This allows true generic calls.
  • Loading branch information
buehler authored Oct 18, 2023
1 parent 881111b commit a95278d
Show file tree
Hide file tree
Showing 42 changed files with 999 additions and 1,043 deletions.
4 changes: 0 additions & 4 deletions examples/Operator/Controller/V1TestEntityController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using KubeOps.Abstractions.Events;
using KubeOps.Abstractions.Queue;
using KubeOps.Abstractions.Rbac;
using KubeOps.KubernetesClient;

using Microsoft.Extensions.Logging;

Expand All @@ -14,18 +13,15 @@ namespace Operator.Controller;
public class V1TestEntityController : IEntityController<V1TestEntity>
{
private readonly ILogger<V1TestEntityController> _logger;
private readonly IKubernetesClient<V1TestEntity> _client;
private readonly EntityRequeue<V1TestEntity> _requeue;
private readonly EventPublisher _eventPublisher;

public V1TestEntityController(
ILogger<V1TestEntityController> logger,
IKubernetesClient<V1TestEntity> client,
EntityRequeue<V1TestEntity> requeue,
EventPublisher eventPublisher)
{
_logger = logger;
_client = client;
_requeue = requeue;
_eventPublisher = eventPublisher;
}
Expand Down
24 changes: 12 additions & 12 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
</PropertyGroup>

<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\"/>
<None Include="$(MSBuildThisFileDirectory)\..\LICENSE" Pack="true" PackagePath="\"/>
<None Include="README.md" Pack="true" PackagePath="\" />
<None Include="$(MSBuildThisFileDirectory)\..\LICENSE" Pack="true" PackagePath="\" />
<None Include="$(MSBuildThisFileDirectory)\..\res\icon.png">
<Pack>true</Pack>
<PackagePath>\</PackagePath>
Expand All @@ -35,16 +35,16 @@

<ItemGroup>
<PackageReference
Include="StyleCop.Analyzers"
Version="1.2.0-beta.507"
PrivateAssets="all"
Condition="$(MSBuildProjectExtension) == '.csproj'"/>
Include="StyleCop.Analyzers"
Version="1.2.0-beta.507"
PrivateAssets="all"
Condition="$(MSBuildProjectExtension) == '.csproj'" />
<PackageReference
Include="SonarAnalyzer.CSharp"
Version="9.12.0.78982"
PrivateAssets="all"
Condition="$(MSBuildProjectExtension) == '.csproj'"/>
<PackageReference Include="Roslynator.Analyzers" Version="4.5.0" PrivateAssets="All"/>
Include="SonarAnalyzer.CSharp"
Version="9.12.0.78982"
PrivateAssets="all"
Condition="$(MSBuildProjectExtension) == '.csproj'" />
<PackageReference Include="Roslynator.Analyzers" Version="4.5.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand All @@ -56,4 +56,4 @@
<_Parameter1>DynamicProxyGenAssembly2</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>
</Project>
29 changes: 0 additions & 29 deletions src/KubeOps.Abstractions/Builder/IOperatorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using k8s.Models;

using KubeOps.Abstractions.Controller;
using KubeOps.Abstractions.Entities;
using KubeOps.Abstractions.Finalizer;

using Microsoft.Extensions.DependencyInjection;
Expand All @@ -19,34 +18,6 @@ public interface IOperatorBuilder
/// </summary>
IServiceCollection Services { get; }

/// <summary>
/// <para>
/// Register an entity Kubernetes client within the operator.
/// This is used to register IKubernetesClient{TEntity} for the entity.
/// An alternative way to create any Kubernetes client is to use the
/// KubernetesClientFactory or instantiate the client by yourself.
/// </para>
/// </summary>
/// <param name="metadata">The metadata of the entity.</param>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <returns>The builder for chaining.</returns>
IOperatorBuilder AddEntityClient<TEntity>(EntityMetadata metadata)
where TEntity : IKubernetesObject<V1ObjectMeta>;

/// <summary>
/// <para>
/// Register an entity Kubernetes client within the operator.
/// This is used to register IKubernetesClient{TEntity} for the entity.
/// An alternative way to create any Kubernetes client is to use the
/// KubernetesClientFactory or instantiate the client by yourself.
/// This method uses reflection to get the metadata from the entity.
/// </para>
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <returns>The builder for chaining.</returns>
IOperatorBuilder AddEntityClient<TEntity>()
where TEntity : IKubernetesObject<V1ObjectMeta>;

/// <summary>
/// Add a controller implementation for a specific entity to the operator.
/// The metadata for the entity must be added as well.
Expand Down
2 changes: 1 addition & 1 deletion src/KubeOps.Abstractions/Builder/OperatorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public sealed class OperatorSettings
/// Defaults to `false`.
/// </para>
/// </summary>
public bool EnableLeaderElection { get; set; }
public bool EnableLeaderElection { get; set; } = false;

/// <summary>
/// Defines how long one lease is valid for any leader.
Expand Down
4 changes: 3 additions & 1 deletion src/KubeOps.Cli/Commands/Utilities/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public static Command Command
{
get
{
var cmd = new Command("api-version", "Prints the actual server version of the connected kubernetes cluster.");
var cmd = new Command(
"api-version",
"Prints the actual server version of the connected kubernetes cluster.");
cmd.AddAlias("av");
cmd.SetHandler(() =>
Handler(AnsiConsole.Console, new Kubernetes(KubernetesClientConfiguration.BuildDefaultConfig())));
Expand Down
3 changes: 2 additions & 1 deletion src/KubeOps.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
.UseParseErrorReporting(ExitCodes.UsageError)
.UseExceptionHandler((ex, ctx) =>
{
AnsiConsole.MarkupLineInterpolated($"[red]An error occurred whiled executing {ctx.ParseResult.CommandResult.Command}[/]");
AnsiConsole.MarkupLineInterpolated(
$"[red]An error occurred whiled executing {ctx.ParseResult.CommandResult.Command}[/]");
AnsiConsole.MarkupLineInterpolated($"[red]{ex.Message}[/]");
ctx.ExitCode = ExitCodes.Error;
})
Expand Down
36 changes: 1 addition & 35 deletions src/KubeOps.Generator/Generators/EntityDefinitionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,41 +78,7 @@ public void Execute(GeneratorExecutionContext context)
TokenList(
Token(SyntaxKind.PublicKeyword),
Token(SyntaxKind.StaticKeyword),
Token(SyntaxKind.ReadOnlyKeyword))))))
.AddMembers(MethodDeclaration(IdentifierName("IOperatorBuilder"), "RegisterEntities")
.WithModifiers(
TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
.WithParameterList(ParameterList(
SingletonSeparatedList(
Parameter(
Identifier("builder"))
.WithModifiers(
TokenList(
Token(SyntaxKind.ThisKeyword)))
.WithType(
IdentifierName("IOperatorBuilder")))))
.WithBody(Block(
receiver.Entities
.Select(e => ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("builder"),
GenericName(Identifier("AddEntityClient"))
.WithTypeArgumentList(
TypeArgumentList(
SingletonSeparatedList<TypeSyntax>(
IdentifierName(context.Compilation
.GetSemanticModel(e.Class.SyntaxTree)
.GetDeclaredSymbol(e.Class)!
.ToDisplayString(SymbolDisplayFormat
.FullyQualifiedFormat)))))))
.WithArgumentList(
ArgumentList(
SingletonSeparatedList(
Argument(
IdentifierName(e.Class.Identifier)))))))
.Append<StatementSyntax>(ReturnStatement(IdentifierName("builder"))))))))
Token(SyntaxKind.ReadOnlyKeyword))))))))
.NormalizeWhitespace();

context.AddSource(
Expand Down
22 changes: 8 additions & 14 deletions src/KubeOps.Generator/Generators/OperatorBuilderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,16 @@ public void Execute(GeneratorExecutionContext context)
.WithBody(Block(
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("builder"),
IdentifierName("RegisterEntities")))),
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("builder"),
IdentifierName("RegisterControllers")))),
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("builder"),
IdentifierName("RegisterControllers")))),
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("builder"),
IdentifierName("RegisterFinalizers")))),
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("builder"),
IdentifierName("RegisterFinalizers")))),
ReturnStatement(IdentifierName("builder")))))))
.NormalizeWhitespace();

Expand Down
Loading

0 comments on commit a95278d

Please sign in to comment.