-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): Add Kubernetes Client package
BREAKING CHANGE: The IKubernetesClient interface and implementation now require the TEntity typeparam instead of each method providing one. The implementation is instanced with EntityMetadata to allow the operator to inject the clients for each entity.
- Loading branch information
Christoph Bühler
committed
Sep 29, 2023
1 parent
bcd1f52
commit 42bc8ee
Showing
24 changed files
with
523 additions
and
396 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using k8s; | ||
using k8s.Models; | ||
|
||
namespace KubeOps.Abstractions.Entities; | ||
|
||
/// <summary> | ||
/// Type for a list of entities. | ||
/// </summary> | ||
/// <typeparam name="T">Type for the list entries.</typeparam> | ||
public class EntityList<T> : KubernetesObject | ||
where T : IKubernetesObject | ||
{ | ||
/// <summary> | ||
/// Official list metadata object of kubernetes. | ||
/// </summary> | ||
public V1ListMeta Metadata { get; set; } = new(); | ||
|
||
/// <summary> | ||
/// The list of items. | ||
/// </summary> | ||
public IList<T> Items { get; set; } = new List<T>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using k8s; | ||
using k8s.Models; | ||
|
||
namespace KubeOps.Abstractions.Entities; | ||
|
||
/// <summary> | ||
/// Method extensions for <see cref="IKubernetesObject{TMetadata}"/>. | ||
/// </summary> | ||
public static class Extensions | ||
{ | ||
/// <summary> | ||
/// Sets the resource version of the specified Kubernetes object to the specified value. | ||
/// </summary> | ||
/// <typeparam name="TEntity">The type of the Kubernetes object.</typeparam> | ||
/// <param name="entity">The Kubernetes object.</param> | ||
/// <param name="resourceVersion">The resource version to set.</param> | ||
/// <returns>The Kubernetes object with the updated resource version.</returns> | ||
public static TEntity WithResourceVersion<TEntity>( | ||
this TEntity entity, | ||
string resourceVersion) | ||
where TEntity : IKubernetesObject<V1ObjectMeta> | ||
{ | ||
entity.EnsureMetadata().ResourceVersion = resourceVersion; | ||
return entity; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the resource version of the specified Kubernetes object to the resource version of another object. | ||
/// </summary> | ||
/// <typeparam name="TEntity">The type of the Kubernetes object.</typeparam> | ||
/// <param name="entity">The Kubernetes object.</param> | ||
/// <param name="other">The other Kubernetes object.</param> | ||
/// <returns>The Kubernetes object with the updated resource version.</returns> | ||
public static TEntity WithResourceVersion<TEntity>( | ||
this TEntity entity, | ||
TEntity other) | ||
where TEntity : IKubernetesObject<V1ObjectMeta> | ||
{ | ||
entity.EnsureMetadata().ResourceVersion = other.ResourceVersion(); | ||
return entity; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 10 additions & 10 deletions
20
...tor/EntityDefinitions/AttributedEntity.cs → ...erator/SyntaxReceiver/AttributedEntity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace KubeOps.Generator.EntityDefinitions; | ||
|
||
public record struct AttributedEntity( | ||
ClassDeclarationSyntax Class, | ||
string Kind, | ||
string Version, | ||
string? Group, | ||
string? Plural); | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace KubeOps.Generator.SyntaxReceiver; | ||
|
||
public record struct AttributedEntity( | ||
ClassDeclarationSyntax Class, | ||
string Kind, | ||
string Version, | ||
string? Group, | ||
string? Plural); |
78 changes: 40 additions & 38 deletions
78
...nitions/KubernetesEntitySyntaxReceiver.cs → ...eceiver/KubernetesEntitySyntaxReceiver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,40 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace KubeOps.Generator.EntityDefinitions; | ||
|
||
public class KubernetesEntitySyntaxReceiver : ISyntaxContextReceiver | ||
{ | ||
private const string KindName = "Kind"; | ||
private const string GroupName = "Group"; | ||
private const string PluralName = "Plural"; | ||
private const string VersionName = "ApiVersion"; | ||
private const string DefaultVersion = "v1"; | ||
|
||
public List<AttributedEntity> Entities { get; } = new(); | ||
|
||
public void OnVisitSyntaxNode(GeneratorSyntaxContext context) | ||
{ | ||
if (context.Node is not ClassDeclarationSyntax { AttributeLists.Count: > 0 } cls || | ||
cls.AttributeLists.SelectMany(a => a.Attributes) | ||
.FirstOrDefault(a => a.Name.ToString() == "KubernetesEntity") is not { } attr) | ||
{ | ||
return; | ||
} | ||
|
||
Entities.Add(new( | ||
cls, | ||
GetArgumentValue(attr, KindName) ?? cls.Identifier.ToString(), | ||
GetArgumentValue(attr, VersionName) ?? DefaultVersion, | ||
GetArgumentValue(attr, GroupName), | ||
GetArgumentValue(attr, PluralName))); | ||
} | ||
|
||
private static string? GetArgumentValue(AttributeSyntax attr, string argName) => | ||
attr.ArgumentList?.Arguments.FirstOrDefault(a => a.NameEquals?.Name.ToString() == argName) is | ||
{ Expression: LiteralExpressionSyntax { Token.ValueText: { } value } } | ||
? value | ||
: null; | ||
} | ||
using KubeOps.Generator.EntityDefinitions; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace KubeOps.Generator.SyntaxReceiver; | ||
|
||
public class KubernetesEntitySyntaxReceiver : ISyntaxContextReceiver | ||
{ | ||
private const string KindName = "Kind"; | ||
private const string GroupName = "Group"; | ||
private const string PluralName = "Plural"; | ||
private const string VersionName = "ApiVersion"; | ||
private const string DefaultVersion = "v1"; | ||
|
||
public List<AttributedEntity> Entities { get; } = new(); | ||
|
||
public void OnVisitSyntaxNode(GeneratorSyntaxContext context) | ||
{ | ||
if (context.Node is not ClassDeclarationSyntax { AttributeLists.Count: > 0 } cls || | ||
cls.AttributeLists.SelectMany(a => a.Attributes) | ||
.FirstOrDefault(a => a.Name.ToString() == "KubernetesEntity") is not { } attr) | ||
{ | ||
return; | ||
} | ||
|
||
Entities.Add(new( | ||
cls, | ||
GetArgumentValue(attr, KindName) ?? cls.Identifier.ToString(), | ||
GetArgumentValue(attr, VersionName) ?? DefaultVersion, | ||
GetArgumentValue(attr, GroupName), | ||
GetArgumentValue(attr, PluralName))); | ||
} | ||
|
||
private static string? GetArgumentValue(AttributeSyntax attr, string argName) => | ||
attr.ArgumentList?.Arguments.FirstOrDefault(a => a.NameEquals?.Name.ToString() == argName) is | ||
{ Expression: LiteralExpressionSyntax { Token.ValueText: { } value } } | ||
? value | ||
: null; | ||
} |
Oops, something went wrong.