1- package it .aboutbits .postgresql .core ;
1+ package it .aboutbits .postgresql .core . schema_customizer ;
22
33import io .fabric8 .crdv2 .generator .v1 .SchemaCustomizer ;
44import io .fabric8 .kubernetes .api .model .apiextensions .v1 .JSONSchemaProps ;
55import io .fabric8 .kubernetes .client .utils .KubernetesSerialization ;
66import org .jspecify .annotations .NullMarked ;
77
88import java .util .Arrays ;
9+ import java .util .List ;
910import java .util .Set ;
1011import java .util .stream .Collectors ;
1112
1213/// A [SchemaCustomizer.Customizer] that sets the `format` of string properties
13- /// to `"hostname"` (RFC 1123) in the generated CRD JSON Schema.
14+ /// to `{"anyOf":[{"format":"hostname"},{"format":"ipv4"},{"format":"ipv6"}]}`
15+ /// in the generated CRD JSON Schema.
1416///
1517/// This customizer is intended to be used with the
1618/// [@SchemaCustomizer][SchemaCustomizer] annotation on a class whose properties
17- /// should be validated as RFC 1123 hostnames by the Kubernetes API server .
19+ /// should be validated to valid hosts defined .
1820///
1921/// ### Behavior
2022///
2830/// **Apply to all string properties:**
2931///
3032/// ```java
31- /// @SchemaCustomizer(HostnameRFC1123Customizer .class)
32- /// public class SecretRef {
33- /// private String name = ""; // gets format: "hostname"
34- /// private String namespace ; // gets format: "hostname"
33+ /// @SchemaCustomizer(value = HostCustomizer .class)
34+ /// public class ClusterConnectionSpec {
35+ /// private String host = ""; // gets custom format
36+ /// private String anotherHost = "" ; // gets custom format
3537/// }
3638/// ```
3739///
3840/// **Apply to specific properties only:**
3941///
4042/// ```java
41- /// @SchemaCustomizer(value = HostnameRFC1123Customizer .class, input = "host")
43+ /// @SchemaCustomizer(value = HostCustomizer .class, input = "host,anotherHost ")
4244/// public class ClusterConnectionSpec {
43- /// private String host = ""; // gets format: "hostname"
44- /// private String anotherHost = ""; // gets format: "hostname"
45- /// private String database = ""; // unchanged
45+ /// private String host = ""; // gets custom format
46+ /// private String anotherHost = ""; // gets custom format
47+ /// private String unchangedHost = ""; // unchanged
4648/// }
4749/// ```
4850///
4951/// @see SchemaCustomizer
5052/// @see SchemaCustomizer.Customizer
5153@ NullMarked
52- public class HostnameRFC1123Customizer implements SchemaCustomizer .Customizer {
54+ public class HostCustomizer implements SchemaCustomizer .Customizer {
5355 @ Override
5456 public JSONSchemaProps apply (
5557 JSONSchemaProps jsonSchemaProps ,
@@ -69,10 +71,25 @@ public JSONSchemaProps apply(
6971
7072 for (var entry : properties .entrySet ()) {
7173 var prop = entry .getValue ();
72- if ("string" .equals (prop .getType ())) {
73- if (targetFields .isEmpty () || targetFields .contains (entry .getKey ())) {
74- prop .setFormat ("hostname" );
75- }
74+ if ("string" .equals (prop .getType ())
75+ && (targetFields .isEmpty () || targetFields .contains (entry .getKey ()))
76+ ) {
77+ prop .setFormat (null );
78+
79+ var hostnameProp = new JSONSchemaProps ();
80+ hostnameProp .setFormat ("hostname" );
81+
82+ var ipv4Prop = new JSONSchemaProps ();
83+ ipv4Prop .setFormat ("ipv4" );
84+
85+ var ipv6Prop = new JSONSchemaProps ();
86+ ipv6Prop .setFormat ("ipv6" );
87+
88+ prop .setAnyOf (List .of (
89+ hostnameProp ,
90+ ipv4Prop ,
91+ ipv6Prop
92+ ));
7693 }
7794 }
7895
0 commit comments