Skip to content

Commit e0743cc

Browse files
authored
fix(crd): added guid support (#1113)
1 parent ae86735 commit e0743cc

3 files changed

Lines changed: 30 additions & 21 deletions

File tree

docs/docs/operator/building-blocks/entities.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public class V1DemoEntityStatus
8383
}
8484
```
8585

86+
The CRD transpiler maps common CLR scalar types to OpenAPI schema types. For example, `Guid` is emitted as
87+
`type: string` with `format: uuid`, and `DateTime` / `DateTimeOffset` are emitted as `type: string` with
88+
`format: date-time`.
89+
8690
## Entity Attributes
8791

8892
KubeOps provides various attributes to customize and validate your entities:

src/KubeOps.Transpiler/Crds.cs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public static class Crds
3434
private const string Double = "double";
3535
private const string Decimal = "decimal";
3636
private const string DateTime = "date-time";
37+
private const string Uuid = "uuid";
3738

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

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

362363
var additionalProperties = context.Map(dictionaryImpl.GenericTypeArguments[1]);
363-
return new() { Type = Object, AdditionalProperties = additionalProperties, };
364+
return new() { Type = Object, AdditionalProperties = additionalProperties };
364365
}
365366

366367
if (interfaceNames.Contains(typeof(IDictionary).FullName))
@@ -405,7 +406,7 @@ static Type GetRootBaseType(Type type)
405406
{
406407
"System.Object" => context.MapObjectType(type),
407408
"System.ValueType" => context.MapValueType(type),
408-
"System.Enum" => new() { Type = String, EnumProperty = GetEnumNames(context, type), },
409+
"System.Enum" => new() { Type = String, EnumProperty = context.GetEnumNames(type) },
409410
_ => throw InvalidType(type),
410411
};
411412
}
@@ -424,21 +425,11 @@ private static IList<object> GetEnumNames(this MetadataLoadContext context, Type
424425
}
425426
}
426427

427-
var enumNames = new List<object>();
428-
429-
foreach (var value in Enum.GetNames(type))
430-
{
431-
if (attributeNameByFieldName.TryGetValue(value, out var name))
432-
{
433-
enumNames.Add(name);
434-
}
435-
else
436-
{
437-
enumNames.Add(value);
438-
}
439-
}
440-
441-
return enumNames;
428+
return Enum
429+
.GetNames(type)
430+
.Select(value => attributeNameByFieldName.GetValueOrDefault(value, value))
431+
.Cast<object>()
432+
.ToList();
442433
#else
443434
return Enum.GetNames(type);
444435
#endif
@@ -516,7 +507,7 @@ private static V1JSONSchemaProps MapEnumerationType(
516507
if (listType.IsGenericType && listType.GetGenericTypeDefinition().FullName == typeof(KeyValuePair<,>).FullName)
517508
{
518509
var additionalProperties = context.Map(listType.GenericTypeArguments[1]);
519-
return new() { Type = Object, AdditionalProperties = additionalProperties, };
510+
return new() { Type = Object, AdditionalProperties = additionalProperties };
520511
}
521512

522513
var items = context.Map(listType);
@@ -532,8 +523,8 @@ private static V1JSONSchemaProps MapValueType(this MetadataLoadContext _, Type t
532523
"System.Double" => new() { Type = Number, Format = Double },
533524
"System.Decimal" => new() { Type = Number, Format = Decimal },
534525
"System.Boolean" => new() { Type = Boolean },
535-
"System.DateTime" => new() { Type = String, Format = DateTime },
536-
"System.DateTimeOffset" => new() { Type = String, Format = DateTime },
526+
"System.DateTime" or "System.DateTimeOffset" => new() { Type = String, Format = DateTime },
527+
"System.Guid" => new() { Type = String, Format = Uuid },
537528
_ => throw InvalidType(type),
538529
};
539530

test/KubeOps.Transpiler.Test/Crds.Mlc.Test.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public partial class CrdsMlcTest(MlcProvider provider) : TranspilerTestBase(prov
3636
[InlineData(typeof(NullableDateTimeTestEntity), "string", "date-time", true)]
3737
[InlineData(typeof(DateTimeOffsetTestEntity), "string", "date-time", null)]
3838
[InlineData(typeof(NullableDateTimeOffsetTestEntity), "string", "date-time", true)]
39+
[InlineData(typeof(GuidTestEntity), "string", "uuid", null)]
40+
[InlineData(typeof(NullableGuidTestEntity), "string", "uuid", true)]
3941
[InlineData(typeof(V1ObjectMetaTestEntity), "object", null, null)]
4042
[InlineData(typeof(ResourceQuantityTestEntity), "string", null, null)]
4143
[InlineData(typeof(StringArrayEntity), "array", null, null)]
@@ -717,6 +719,18 @@ private sealed class NullableDateTimeOffsetTestEntity : CustomKubernetesEntity
717719
public DateTimeOffset? Property { get; set; }
718720
}
719721

722+
[KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")]
723+
private sealed class GuidTestEntity : CustomKubernetesEntity
724+
{
725+
public Guid Property { get; set; }
726+
}
727+
728+
[KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")]
729+
private sealed class NullableGuidTestEntity : CustomKubernetesEntity
730+
{
731+
public Guid? Property { get; set; }
732+
}
733+
720734
[KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")]
721735
private sealed class V1ObjectMetaTestEntity : CustomKubernetesEntity
722736
{

0 commit comments

Comments
 (0)