Skip to content

Commit

Permalink
feat: Add support for generating CRD's for entities that contain othe…
Browse files Browse the repository at this point in the history
…r custom resources (#823)

This adds support for a CustomKubernetesEntity that has another
CustomKubernetesEntity as a property. We use this for cluster resources
that create namespaced versions, so we want the spec to match without
having to duplicate the spec inside the parent CustomKubernetesEntity.
https://github.com/Contrast-Security-OSS/agent-operator/blob/master/src/Contrast.K8s.AgentOperator/Entities/V1Beta1ClusterAgentConnection.cs

In previous versions of the sdk the generator supported this scenario
but the current one errors out with `System.ArgumentException: The given
type Contrast.K8s.AgentOperator.Entities.V1Beta1AgentConfiguration is
not a valid Kubernetes entity.` inside the Map function.

Also cleaned up the xml doc on the description attribute since the new
generator doesn't support pulling the description from the xml summary.

Co-authored-by: Christoph Bühler <[email protected]>
  • Loading branch information
gamingrobot and buehler authored Dec 16, 2024
1 parent b42b2f6 commit f2129cf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace KubeOps.Abstractions.Entities.Attributes;

/// <summary>
/// Defines a description for a property. This precedes the description found in a
/// XML documentation file.
/// Defines a description for a property.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)]
public class DescriptionAttribute(string description) : Attribute
Expand Down
5 changes: 5 additions & 0 deletions src/KubeOps.Transpiler/Crds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ private static V1JSONSchemaProps Map(this MetadataLoadContext context, Type type
return context.MapEnumerationType(type, interfaces);
}

if (type.BaseType?.Name == nameof(CustomKubernetesEntity) || type.BaseType?.Name == typeof(CustomKubernetesEntity<>).Name)
{
return context.MapObjectType(type);
}

return type.BaseType?.FullName switch
{
"System.Object" => context.MapObjectType(type),
Expand Down
26 changes: 26 additions & 0 deletions test/KubeOps.Transpiler.Test/Crds.Mlc.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class CrdsMlcTest(MlcProvider provider) : TranspilerTestBase(provider)
[InlineData(typeof(EnumerableKeyPairsEntity), "object", null, false)]
[InlineData(typeof(IntstrOrStringEntity), null, null, false)]
[InlineData(typeof(EmbeddedResourceEntity), "object", null, false)]
[InlineData(typeof(EmbeddedCustomResourceEntity), "object", null, false)]
[InlineData(typeof(EmbeddedCustomResourceGenericEntity), "object", null, false)]
[InlineData(typeof(EmbeddedResourceListEntity), "array", null, false)]
public void Should_Transpile_Entity_Type_Correctly(Type type, string? expectedType, string? expectedFormat,
bool isNullable)
Expand Down Expand Up @@ -687,6 +689,30 @@ private class EmbeddedResourceEntity : CustomKubernetesEntity
public V1Pod Property { get; set; } = null!;
}

[KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")]
private class EmbeddedCustomResourceEntity : CustomKubernetesEntity
{
public EmbeddedCustomResource Property { get; set; } = null!;
}

[KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")]
private class EmbeddedCustomResource : CustomKubernetesEntity
{
public string Property { get; set; } = string.Empty;
}

[KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")]
private class EmbeddedCustomResourceGenericEntity : CustomKubernetesEntity
{
public EmbeddedCustomResourceGeneric Property { get; set; } = null!;
}

[KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")]
private class EmbeddedCustomResourceGeneric : CustomKubernetesEntity<EmbeddedCustomResourceGeneric.EntitySpec>
{
public class EntitySpec;
}

[KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")]
private class EmbeddedResourceListEntity : CustomKubernetesEntity
{
Expand Down

0 comments on commit f2129cf

Please sign in to comment.