From 6425e3eb5c962fc56062c52ceed1c82c70a309c9 Mon Sep 17 00:00:00 2001 From: wineway Date: Wed, 7 Sep 2022 11:40:30 +0800 Subject: [PATCH 1/4] add annotation @PreserveUnknownFields for field Signed-off-by: wineway --- .../crd/generator/AbstractJsonSchema.java | 27 +++++++++++++++++-- .../annotation/PreserveUnknownFields.java | 26 ++++++++++++++++++ .../fabric8/crd/generator/v1/JsonSchema.java | 4 +++ .../crd/generator/v1beta1/JsonSchema.java | 4 +++ .../example/extraction/ExtractionSpec.java | 2 ++ .../crd/generator/v1/JsonSchemaTest.java | 1 + 6 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 crd-generator/api/src/main/java/io/fabric8/crd/generator/annotation/PreserveUnknownFields.java diff --git a/crd-generator/api/src/main/java/io/fabric8/crd/generator/AbstractJsonSchema.java b/crd-generator/api/src/main/java/io/fabric8/crd/generator/AbstractJsonSchema.java index 747ad6ad7c4..0c8b88906b3 100644 --- a/crd-generator/api/src/main/java/io/fabric8/crd/generator/AbstractJsonSchema.java +++ b/crd-generator/api/src/main/java/io/fabric8/crd/generator/AbstractJsonSchema.java @@ -86,6 +86,7 @@ public abstract class AbstractJsonSchema { public static final String ANNOTATION_REQUIRED = "io.fabric8.generator.annotation.Required"; public static final String ANNOTATION_NOT_NULL = "javax.validation.constraints.NotNull"; public static final String ANNOTATION_SCHEMA_FROM = "io.fabric8.crd.generator.annotation.SchemaFrom"; + public static final String ANNOTATION_PERSERVE_UNKNOWN_FIELDS = "io.fabric8.crd.generator.annotation.PreserveUnknownFields"; public static final String ANNOTATION_SCHEMA_SWAP = "io.fabric8.crd.generator.annotation.SchemaSwap"; public static final String JSON_NODE_TYPE = "com.fasterxml.jackson.databind.JsonNode"; @@ -123,21 +124,25 @@ protected static class SchemaPropsOptions { final boolean nullable; final boolean required; + final boolean preserveUnknownFields; + SchemaPropsOptions() { min = Optional.empty(); max = Optional.empty(); pattern = Optional.empty(); nullable = false; required = false; + preserveUnknownFields = false; } public SchemaPropsOptions(Optional min, Optional max, Optional pattern, - boolean nullable, boolean required) { + boolean nullable, boolean required, boolean preserveUnknownFields) { this.min = min; this.max = max; this.pattern = pattern; this.nullable = nullable; this.required = required; + this.preserveUnknownFields = preserveUnknownFields; } public Optional getMin() { @@ -159,6 +164,10 @@ public boolean isNullable() { public boolean getRequired() { return nullable; } + + public boolean isPreserveUnknownFields() { + return preserveUnknownFields; + } } /** @@ -321,7 +330,8 @@ private T internalFromImpl(TypeDef definition, Set visited, List barProps = bar.getProperties(); assertNotNull(barProps); + assertTrue(bar.getXKubernetesPreserveUnknownFields()); // you can change everything assertEquals("integer", barProps.get("BAZ").getType()); From 33240aadd455a486beba1b3f82485c1637e44143 Mon Sep 17 00:00:00 2001 From: wineway Date: Wed, 7 Sep 2022 18:02:17 +0800 Subject: [PATCH 2/4] add changelog & fixed copyright Signed-off-by: wineway --- CHANGELOG.md | 1 + .../annotation/PreserveUnknownFields.java | 2 +- doc/CRD-generator.md | 22 +++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e76312e895..233cabf152d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * Fix #4383: bump snakeyaml from 1.30 to 1.31 #### New Features +* Feat: add annotation @PreserveUnknownFields for generation field #### _**Note**_: Breaking changes in the API diff --git a/crd-generator/api/src/main/java/io/fabric8/crd/generator/annotation/PreserveUnknownFields.java b/crd-generator/api/src/main/java/io/fabric8/crd/generator/annotation/PreserveUnknownFields.java index 59ee2ddfb69..dd784b836b9 100644 --- a/crd-generator/api/src/main/java/io/fabric8/crd/generator/annotation/PreserveUnknownFields.java +++ b/crd-generator/api/src/main/java/io/fabric8/crd/generator/annotation/PreserveUnknownFields.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2022 Red Hat, Inc. + * Copyright (C) 2015 Red Hat, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/doc/CRD-generator.md b/doc/CRD-generator.md index 4275c1ae3df..89874b36862 100644 --- a/doc/CRD-generator.md +++ b/doc/CRD-generator.md @@ -357,6 +357,28 @@ Corresponding `x-kubernetes-preserve-unknown-fields: true` will be generated in x-kubernetes-preserve-unknown-fields: true ``` +You can also annotation a field with @PreserveUnknownFields: + +```java +interface ExampleInterface {} + +public class ExampleSpec { + @PreserveUnknownFields + ExampleInterface someValue; +} +``` + +will be generated as: + +```yaml + spec: + properties: + someValue: + type: object + x-kubernetes-preserve-unknown-fields: true + type: object +``` + ## Features cheatsheet | Annotation | Description | From 328b5bc1a82a0e07aa904e8012df35d62e365653 Mon Sep 17 00:00:00 2001 From: wineway Date: Wed, 14 Sep 2022 16:48:49 +0800 Subject: [PATCH 3/4] add more explain for changelog & doc Signed-off-by: wineway --- CHANGELOG.md | 2 +- .../annotation/PreserveUnknownFields.java | 2 +- doc/CRD-generator.md | 29 ++++++++++--------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 233cabf152d..4548ed0fdaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ * Fix #4383: bump snakeyaml from 1.30 to 1.31 #### New Features -* Feat: add annotation @PreserveUnknownFields for generation field +* Feat: add annotation @PreserveUnknownFields for marking generated field have `x-kubernetes-preserve-unknown-fields: true` defined #### _**Note**_: Breaking changes in the API diff --git a/crd-generator/api/src/main/java/io/fabric8/crd/generator/annotation/PreserveUnknownFields.java b/crd-generator/api/src/main/java/io/fabric8/crd/generator/annotation/PreserveUnknownFields.java index dd784b836b9..94b84618f07 100644 --- a/crd-generator/api/src/main/java/io/fabric8/crd/generator/annotation/PreserveUnknownFields.java +++ b/crd-generator/api/src/main/java/io/fabric8/crd/generator/annotation/PreserveUnknownFields.java @@ -18,7 +18,7 @@ import java.lang.annotation.*; /* - * Used to tweak the behavior of the crd-generator + * Used to emit 'x-kubernetes-preserve-unknown-fields' */ @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) diff --git a/doc/CRD-generator.md b/doc/CRD-generator.md index 89874b36862..611a7a20fc6 100644 --- a/doc/CRD-generator.md +++ b/doc/CRD-generator.md @@ -381,19 +381,20 @@ will be generated as: ## Features cheatsheet -| Annotation | Description | -|-----------------------------------------------------------|---------------------------------------------------------------------------------------| -| `com.fasterxml.jackson.annotation.JsonProperty` | The field is named after the provided value instead of looking up the java field name | -| `com.fasterxml.jackson.annotation.JsonPropertyDescription`| The provided text is be embedded in the `description` of the field | -| `com.fasterxml.jackson.annotation.JsonIgnore` | The field is ignored | -| `com.fasterxml.jackson.annotation.JsonAnyGetter` | The corresponding object have `x-kubernetes-preserve-unknown-fields: true` defined | -| `com.fasterxml.jackson.annotation.JsonAnySetter` | The corresponding object have `x-kubernetes-preserve-unknown-fields: true` defined | -| `io.fabric8.generator.annotation.Min` | The field defines a validation `min` | -| `io.fabric8.generator.annotation.Max` | The field defines a validation `max` | -| `io.fabric8.generator.annotation.Pattern` | The field defines a validation `pattern` | -| `io.fabric8.generator.annotation.Nullable` | The field is marked as `nullable` | -| `io.fabric8.generator.annotation.Required` | The field is marked as `required` | -| `io.fabric8.crd.generator.annotation.SchemaFrom` | The field type for the generation is the one coming from the annotation | -| `io.fabric8.crd.generator.annotation.SchemaSwap` | Same as SchemaFrom, but can be applied at any point in the class hierarchy | +| Annotation | Description | +|--------------------------------------------------------------|---------------------------------------------------------------------------------------| +| `com.fasterxml.jackson.annotation.JsonProperty` | The field is named after the provided value instead of looking up the java field name | +| `com.fasterxml.jackson.annotation.JsonPropertyDescription` | The provided text is be embedded in the `description` of the field | +| `com.fasterxml.jackson.annotation.JsonIgnore` | The field is ignored | +| `io.fabric8.crd.generator.annotation.PreserveUnknownFields` | The field have `x-kubernetes-preserve-unknown-fields: true` defined | +| `com.fasterxml.jackson.annotation.JsonAnyGetter` | The corresponding object have `x-kubernetes-preserve-unknown-fields: true` defined | +| `com.fasterxml.jackson.annotation.JsonAnySetter` | The corresponding object have `x-kubernetes-preserve-unknown-fields: true` defined | +| `io.fabric8.generator.annotation.Min` | The field defines a validation `min` | +| `io.fabric8.generator.annotation.Max` | The field defines a validation `max` | +| `io.fabric8.generator.annotation.Pattern` | The field defines a validation `pattern` | +| `io.fabric8.generator.annotation.Nullable` | The field is marked as `nullable` | +| `io.fabric8.generator.annotation.Required` | The field is marked as `required` | +| `io.fabric8.crd.generator.annotation.SchemaFrom` | The field type for the generation is the one coming from the annotation | +| `io.fabric8.crd.generator.annotation.SchemaSwap` | Same as SchemaFrom, but can be applied at any point in the class hierarchy | A field of type `com.fasterxml.jackson.databind.JsonNode` is encoded as an empty object with `x-kubernetes-preserve-unknown-fields: true` defined. From 5a3708e0e6ce492616d6bace1b65dc192d04cdab Mon Sep 17 00:00:00 2001 From: wineway Date: Wed, 14 Sep 2022 17:12:41 +0800 Subject: [PATCH 4/4] fixed typo --- doc/CRD-generator.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/CRD-generator.md b/doc/CRD-generator.md index 611a7a20fc6..fb822d3f428 100644 --- a/doc/CRD-generator.md +++ b/doc/CRD-generator.md @@ -357,7 +357,7 @@ Corresponding `x-kubernetes-preserve-unknown-fields: true` will be generated in x-kubernetes-preserve-unknown-fields: true ``` -You can also annotation a field with @PreserveUnknownFields: +You can also annotate a field with `io.fabric8.crd.generator.annotation.PreserveUnknownFields`: ```java interface ExampleInterface {}