|
| 1 | +package it.aboutbits.postgresql.core; |
| 2 | + |
| 3 | +import io.fabric8.crdv2.generator.v1.SchemaCustomizer; |
| 4 | +import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps; |
| 5 | +import io.fabric8.kubernetes.client.utils.KubernetesSerialization; |
| 6 | +import org.jspecify.annotations.NullMarked; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Set; |
| 10 | +import java.util.stream.Collectors; |
| 11 | + |
| 12 | +/// A [SchemaCustomizer.Customizer] that sets the `format` of string properties |
| 13 | +/// to `"hostname"` (RFC 1123) in the generated CRD JSON Schema. |
| 14 | +/// |
| 15 | +/// This customizer is intended to be used with the |
| 16 | +/// [@SchemaCustomizer][SchemaCustomizer] annotation on a class whose properties |
| 17 | +/// should be validated as RFC 1123 hostnames by the Kubernetes API server. |
| 18 | +/// |
| 19 | +/// ### Behavior |
| 20 | +/// |
| 21 | +/// - If `input` is **blank** (the default), the `"hostname"` format |
| 22 | +/// is applied to **all** string properties of the annotated class. |
| 23 | +/// - If `input` contains a **comma-separated list** of field names, |
| 24 | +/// the format is applied **only** to the specified properties. |
| 25 | +/// |
| 26 | +/// ### Usage examples |
| 27 | +/// |
| 28 | +/// **Apply to all string properties:** |
| 29 | +/// |
| 30 | +/// ```java |
| 31 | +/// @SchemaCustomizer(HostnameRFC1123Customizer.class) |
| 32 | +/// public class SecretRef { |
| 33 | +/// private String name = ""; // gets format: "hostname" |
| 34 | +/// private String namespace; // gets format: "hostname" |
| 35 | +/// } |
| 36 | +/// ``` |
| 37 | +/// |
| 38 | +/// **Apply to specific properties only:** |
| 39 | +/// |
| 40 | +/// ```java |
| 41 | +/// @SchemaCustomizer(value = HostnameRFC1123Customizer.class, input = "host") |
| 42 | +/// public class ClusterConnectionSpec { |
| 43 | +/// private String host = ""; // gets format: "hostname" |
| 44 | +/// private String anotherHost = ""; // gets format: "hostname" |
| 45 | +/// private String database = ""; // unchanged |
| 46 | +/// } |
| 47 | +/// ``` |
| 48 | +/// |
| 49 | +/// @see SchemaCustomizer |
| 50 | +/// @see SchemaCustomizer.Customizer |
| 51 | +@NullMarked |
| 52 | +public class HostnameRFC1123Customizer implements SchemaCustomizer.Customizer { |
| 53 | + @Override |
| 54 | + public JSONSchemaProps apply( |
| 55 | + JSONSchemaProps jsonSchemaProps, |
| 56 | + String input, |
| 57 | + KubernetesSerialization kubernetesSerialization |
| 58 | + ) { |
| 59 | + var properties = jsonSchemaProps.getProperties(); |
| 60 | + if (properties == null) { |
| 61 | + return jsonSchemaProps; |
| 62 | + } |
| 63 | + |
| 64 | + var targetFields = input.isBlank() |
| 65 | + ? Set.<String>of() |
| 66 | + : Arrays.stream(input.split(",")) |
| 67 | + .map(String::trim) |
| 68 | + .collect(Collectors.toSet()); |
| 69 | + |
| 70 | + for (var entry : properties.entrySet()) { |
| 71 | + var prop = entry.getValue(); |
| 72 | + if ("string".equals(prop.getType())) { |
| 73 | + if (targetFields.isEmpty() || targetFields.contains(entry.getKey())) { |
| 74 | + prop.setFormat("hostname"); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return jsonSchemaProps; |
| 80 | + } |
| 81 | +} |
0 commit comments