Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/docs/operator/building-blocks/entities.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public class V1DemoEntityStatus
}
```

The CRD transpiler maps common CLR scalar types to OpenAPI schema types. For example, `Guid` is emitted as
`type: string` with `format: uuid`, and `DateTime` / `DateTimeOffset` are emitted as `type: string` with
`format: date-time`.

## Entity Attributes

KubeOps provides various attributes to customize and validate your entities:
Expand Down
33 changes: 12 additions & 21 deletions src/KubeOps.Transpiler/Crds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static class Crds
private const string Double = "double";
private const string Decimal = "decimal";
private const string DateTime = "date-time";
private const string Uuid = "uuid";

private static readonly string[] IgnoredToplevelProperties = ["metadata", "apiversion", "kind"];

Expand Down Expand Up @@ -311,7 +312,7 @@ private static V1JSONSchemaProps Map(this MetadataLoadContext context, PropertyI
if (prop.GetCustomAttributesData<ValidationRuleAttribute>().ToArray() is { Length: > 0 } validations)
{
props.XKubernetesValidations = validations
.Select(validation => new V1ValidationRule()
.Select(validation => new V1ValidationRule
{
Rule = validation.GetCustomAttributeCtorArg<string>(context, 0),
FieldPath = validation.GetCustomAttributeCtorArg<string?>(context, 1),
Expand Down Expand Up @@ -360,7 +361,7 @@ private static V1JSONSchemaProps Map(this MetadataLoadContext context, Type type
&& i.GetGenericTypeDefinition().FullName == typeof(IDictionary<,>).FullName);

var additionalProperties = context.Map(dictionaryImpl.GenericTypeArguments[1]);
return new() { Type = Object, AdditionalProperties = additionalProperties, };
return new() { Type = Object, AdditionalProperties = additionalProperties };
}

if (interfaceNames.Contains(typeof(IDictionary).FullName))
Expand Down Expand Up @@ -405,7 +406,7 @@ static Type GetRootBaseType(Type type)
{
"System.Object" => context.MapObjectType(type),
"System.ValueType" => context.MapValueType(type),
"System.Enum" => new() { Type = String, EnumProperty = GetEnumNames(context, type), },
"System.Enum" => new() { Type = String, EnumProperty = context.GetEnumNames(type) },
_ => throw InvalidType(type),
};
}
Expand All @@ -424,21 +425,11 @@ private static IList<object> GetEnumNames(this MetadataLoadContext context, Type
}
}

var enumNames = new List<object>();

foreach (var value in Enum.GetNames(type))
{
if (attributeNameByFieldName.TryGetValue(value, out var name))
{
enumNames.Add(name);
}
else
{
enumNames.Add(value);
}
}

return enumNames;
return Enum
.GetNames(type)
.Select(value => attributeNameByFieldName.GetValueOrDefault(value, value))
.Cast<object>()
.ToList();
#else
return Enum.GetNames(type);
#endif
Expand Down Expand Up @@ -516,7 +507,7 @@ private static V1JSONSchemaProps MapEnumerationType(
if (listType.IsGenericType && listType.GetGenericTypeDefinition().FullName == typeof(KeyValuePair<,>).FullName)
{
var additionalProperties = context.Map(listType.GenericTypeArguments[1]);
return new() { Type = Object, AdditionalProperties = additionalProperties, };
return new() { Type = Object, AdditionalProperties = additionalProperties };
}

var items = context.Map(listType);
Expand All @@ -532,8 +523,8 @@ private static V1JSONSchemaProps MapValueType(this MetadataLoadContext _, Type t
"System.Double" => new() { Type = Number, Format = Double },
"System.Decimal" => new() { Type = Number, Format = Decimal },
"System.Boolean" => new() { Type = Boolean },
"System.DateTime" => new() { Type = String, Format = DateTime },
"System.DateTimeOffset" => new() { Type = String, Format = DateTime },
"System.DateTime" or "System.DateTimeOffset" => new() { Type = String, Format = DateTime },
"System.Guid" => new() { Type = String, Format = Uuid },
_ => throw InvalidType(type),
};

Expand Down
14 changes: 14 additions & 0 deletions test/KubeOps.Transpiler.Test/Crds.Mlc.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public partial class CrdsMlcTest(MlcProvider provider) : TranspilerTestBase(prov
[InlineData(typeof(NullableDateTimeTestEntity), "string", "date-time", true)]
[InlineData(typeof(DateTimeOffsetTestEntity), "string", "date-time", null)]
[InlineData(typeof(NullableDateTimeOffsetTestEntity), "string", "date-time", true)]
[InlineData(typeof(GuidTestEntity), "string", "uuid", null)]
[InlineData(typeof(NullableGuidTestEntity), "string", "uuid", true)]
[InlineData(typeof(V1ObjectMetaTestEntity), "object", null, null)]
[InlineData(typeof(ResourceQuantityTestEntity), "string", null, null)]
[InlineData(typeof(StringArrayEntity), "array", null, null)]
Expand Down Expand Up @@ -717,6 +719,18 @@ private sealed class NullableDateTimeOffsetTestEntity : CustomKubernetesEntity
public DateTimeOffset? Property { get; set; }
}

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

[KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")]
private sealed class NullableGuidTestEntity : CustomKubernetesEntity
{
public Guid? Property { get; set; }
}

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