@@ -30,12 +30,13 @@ internal static class EntityToCrdExtensions
30
30
private static readonly string [ ] IgnoredToplevelProperties = { "metadata" , "apiversion" , "kind" } ;
31
31
32
32
internal static V1CustomResourceDefinition CreateCrd (
33
- this IKubernetesObject < V1ObjectMeta > kubernetesEntity ) => CreateCrd ( kubernetesEntity . GetType ( ) ) ;
33
+ this IKubernetesObject < V1ObjectMeta > kubernetesEntity , IList < ICrdBuilderTypeOverride > ? crdBuilderOverrides = null )
34
+ => CreateCrd ( kubernetesEntity . GetType ( ) , crdBuilderOverrides ) ;
34
35
35
- internal static V1CustomResourceDefinition CreateCrd < TEntity > ( )
36
- where TEntity : IKubernetesObject < V1ObjectMeta > => CreateCrd ( typeof ( TEntity ) ) ;
36
+ internal static V1CustomResourceDefinition CreateCrd < TEntity > ( IList < ICrdBuilderTypeOverride > ? crdBuilderOverrides = null )
37
+ where TEntity : IKubernetesObject < V1ObjectMeta > => CreateCrd ( typeof ( TEntity ) , crdBuilderOverrides ) ;
37
38
38
- internal static V1CustomResourceDefinition CreateCrd ( this Type entityType )
39
+ internal static V1CustomResourceDefinition CreateCrd ( this Type entityType , IList < ICrdBuilderTypeOverride > ? crdBuilderOverrides = null )
39
40
{
40
41
var entityDefinition = entityType . ToEntityDefinition ( ) ;
41
42
@@ -79,7 +80,7 @@ internal static V1CustomResourceDefinition CreateCrd(this Type entityType)
79
80
}
80
81
81
82
var columns = new List < V1CustomResourceColumnDefinition > ( ) ;
82
- version . Schema = new V1CustomResourceValidation ( MapType ( entityType , columns , string . Empty ) ) ;
83
+ version . Schema = new V1CustomResourceValidation ( MapType ( entityType , columns , string . Empty , crdBuilderOverrides ) ) ;
83
84
84
85
version . AdditionalPrinterColumns = entityType
85
86
. GetCustomAttributes < GenericAdditionalPrinterColumnAttribute > ( true )
@@ -98,12 +99,13 @@ internal static V1CustomResourceDefinition CreateCrd(this Type entityType)
98
99
private static V1JSONSchemaProps MapProperty (
99
100
PropertyInfo info ,
100
101
IList < V1CustomResourceColumnDefinition > additionalColumns ,
101
- string jsonPath )
102
+ string jsonPath ,
103
+ IList < ICrdBuilderTypeOverride > ? crdBuilderOverrides = null )
102
104
{
103
105
V1JSONSchemaProps props ;
104
106
try
105
107
{
106
- props = MapType ( info . PropertyType , additionalColumns , jsonPath ) ;
108
+ props = MapType ( info . PropertyType , additionalColumns , jsonPath , crdBuilderOverrides ) ;
107
109
}
108
110
catch ( Exception ex )
109
111
{
@@ -214,7 +216,8 @@ private static V1JSONSchemaProps MapProperty(
214
216
private static V1JSONSchemaProps MapType (
215
217
Type type ,
216
218
IList < V1CustomResourceColumnDefinition > additionalColumns ,
217
- string jsonPath )
219
+ string jsonPath ,
220
+ IList < ICrdBuilderTypeOverride > ? crdBuilderOverrides = null )
218
221
{
219
222
var props = new V1JSONSchemaProps ( ) ;
220
223
@@ -223,7 +226,12 @@ private static V1JSONSchemaProps MapType(
223
226
224
227
var isSimpleType = IsSimpleType ( type ) ;
225
228
226
- if ( type == typeof ( V1ObjectMeta ) )
229
+ var matchedOverride = crdBuilderOverrides ? . FirstOrDefault ( ovrd => ovrd . HandlesType ( type ) ) ;
230
+ if ( matchedOverride != null )
231
+ {
232
+ matchedOverride . ConfigureCustomSchemaForProp ( props ) ;
233
+ }
234
+ else if ( type == typeof ( V1ObjectMeta ) )
227
235
{
228
236
props . Type = Object ;
229
237
}
@@ -233,13 +241,14 @@ private static V1JSONSchemaProps MapType(
233
241
props . Items = MapType (
234
242
type . GetElementType ( ) ?? throw new NullReferenceException ( "No Array Element Type found" ) ,
235
243
additionalColumns ,
236
- jsonPath ) ;
244
+ jsonPath ,
245
+ crdBuilderOverrides ) ;
237
246
}
238
247
else if ( ! isSimpleType && type . IsGenericType && type . GetGenericTypeDefinition ( ) == typeof ( IDictionary < , > ) )
239
248
{
240
249
var genericTypes = type . GenericTypeArguments ;
241
250
props . Type = Object ;
242
- props . AdditionalProperties = MapType ( genericTypes [ 1 ] , additionalColumns , jsonPath ) ;
251
+ props . AdditionalProperties = MapType ( genericTypes [ 1 ] , additionalColumns , jsonPath , crdBuilderOverrides ) ;
243
252
}
244
253
else if ( ! isSimpleType &&
245
254
type . IsGenericType &&
@@ -250,7 +259,7 @@ private static V1JSONSchemaProps MapType(
250
259
{
251
260
var genericTypes = type . GenericTypeArguments . Single ( ) . GenericTypeArguments ;
252
261
props . Type = Object ;
253
- props . AdditionalProperties = MapType ( genericTypes [ 1 ] , additionalColumns , jsonPath ) ;
262
+ props . AdditionalProperties = MapType ( genericTypes [ 1 ] , additionalColumns , jsonPath , crdBuilderOverrides ) ;
254
263
}
255
264
else if ( ! isSimpleType &&
256
265
( typeof ( IDictionary ) . IsAssignableFrom ( type ) ||
@@ -265,7 +274,7 @@ private static V1JSONSchemaProps MapType(
265
274
else if ( ! isSimpleType && IsGenericEnumerableType ( type , out Type ? closingType ) )
266
275
{
267
276
props . Type = Array ;
268
- props . Items = MapType ( closingType , additionalColumns , jsonPath ) ;
277
+ props . Items = MapType ( closingType , additionalColumns , jsonPath , crdBuilderOverrides ) ;
269
278
}
270
279
else if ( type == typeof ( IntstrIntOrString ) )
271
280
{
@@ -280,7 +289,7 @@ private static V1JSONSchemaProps MapType(
280
289
}
281
290
else if ( ! isSimpleType )
282
291
{
283
- ProcessType ( type , props , additionalColumns , jsonPath ) ;
292
+ ProcessType ( type , props , additionalColumns , jsonPath , crdBuilderOverrides ) ;
284
293
}
285
294
else if ( type == typeof ( int ) || Nullable . GetUnderlyingType ( type ) == typeof ( int ) )
286
295
{
@@ -345,7 +354,8 @@ private static void ProcessType(
345
354
Type type ,
346
355
V1JSONSchemaProps props ,
347
356
IList < V1CustomResourceColumnDefinition > additionalColumns ,
348
- string jsonPath )
357
+ string jsonPath ,
358
+ IList < ICrdBuilderTypeOverride > ? crdBuilderOverrides = null )
349
359
{
350
360
props . Type = Object ;
351
361
@@ -358,7 +368,7 @@ private static void ProcessType(
358
368
. Select (
359
369
prop => KeyValuePair . Create (
360
370
GetPropertyName ( prop ) ,
361
- MapProperty ( prop , additionalColumns , $ "{ jsonPath } .{ GetPropertyName ( prop ) } ") ) ) ) ;
371
+ MapProperty ( prop , additionalColumns , $ "{ jsonPath } .{ GetPropertyName ( prop ) } ", crdBuilderOverrides ) ) ) ) ;
362
372
props . Required = type . GetProperties ( )
363
373
. Where (
364
374
prop => prop . GetCustomAttribute < RequiredAttribute > ( ) != null &&
0 commit comments