From f76c6442c56f9940248a08d7ed75f92ef514ffc7 Mon Sep 17 00:00:00 2001 From: Chris Laprun Date: Thu, 11 Mar 2021 20:24:54 +0100 Subject: [PATCH] redactor: Move CRD annotations to kubernetes-model-common (#2890) * refactor!: move CRD annotations to kubernetes-model-common * feat: change retention policy so that generator could work at runtime * docs: add changelog entry * fix: imports * refactor!: move Scope to kubernetes-model-common and clean it up * refactor!: use Scope instead of String constants * fix: Export-package for common model classes Co-authored-by: manusa --- CHANGELOG.md | 3 ++- .../crd/generator/apt/CustomResourceInfo.java | 3 ++- .../generator/v1/CustomResourceHandler.java | 2 +- .../v1beta1/CustomResourceHandler.java | 2 +- .../AdditionalPrinterColumnDetector.java | 2 +- .../visitor/LabelSelectorPathDetector.java | 2 +- .../visitor/SpecReplicasPathDetector.java | 2 +- .../visitor/StatusReplicasPathDetector.java | 2 +- .../kubernetes/client/CustomResource.java | 7 +++--- .../zookeeper/ZookeeperCustomResourceIT.java | 15 +++++------- .../crd/zookeeper/v1/ZookeeperSpec.java | 3 +-- .../crd/zookeeper/v1/ZookeeperStatus.java | 7 +++--- .../crd/zookeeper/v1alpha1/ZookeeperSpec.java | 2 +- .../zookeeper/v1alpha1/ZookeeperStatus.java | 4 ++-- .../kubernetes-model-common/pom.xml | 3 +-- .../io/fabric8/kubernetes/model}/Scope.java | 23 +++++++++++++++++-- .../model}/annotation/LabelSelector.java | 4 ++-- .../model}/annotation/PrinterColumn.java | 4 ++-- .../model}/annotation/SpecReplicas.java | 4 ++-- .../model}/annotation/StatusReplicas.java | 4 ++-- 20 files changed, 56 insertions(+), 42 deletions(-) rename {crd-generator/src/main/java/io/fabric8/crd/generator/apt => kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model}/Scope.java (58%) rename {crd-generator/src/main/java/io/fabric8/crd => kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model}/annotation/LabelSelector.java (90%) rename {crd-generator/src/main/java/io/fabric8/crd => kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model}/annotation/PrinterColumn.java (93%) rename {crd-generator/src/main/java/io/fabric8/crd => kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model}/annotation/SpecReplicas.java (90%) rename {crd-generator/src/main/java/io/fabric8/crd => kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model}/annotation/StatusReplicas.java (90%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b737875327..eae2fe68089 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,8 @@ * Fix #2853: Cannot change the type of the Service from ClusterIP to ExternalName with PATCH * Fix #2783: OpenIDConnectionUtils#persistKubeConfigWithUpdatedToken persists access token instead of refresh token * Fix #2871: Change longFileMode to LONGFILE\_POSIX for creating tar in PodUpload, improve exception handling in PodUpload. -* Fix #2746: SharedInformerFactory should use key formed from OperationContext +* Fix #2746: SharedInformerFactory should use key formed from OperationContext +* Fix #2736: Move CRD annotations to kubernetes-model-common module for greater coherence #### Improvements * Fix #2781: RawCustomResourceOperationsImpl#delete now returns a boolean value for deletion status diff --git a/crd-generator/src/main/java/io/fabric8/crd/generator/apt/CustomResourceInfo.java b/crd-generator/src/main/java/io/fabric8/crd/generator/apt/CustomResourceInfo.java index 3ea88501e55..7833ce4aaaf 100644 --- a/crd-generator/src/main/java/io/fabric8/crd/generator/apt/CustomResourceInfo.java +++ b/crd-generator/src/main/java/io/fabric8/crd/generator/apt/CustomResourceInfo.java @@ -17,6 +17,7 @@ import io.fabric8.kubernetes.api.model.Namespaced; import io.fabric8.kubernetes.client.utils.Pluralize; +import io.fabric8.kubernetes.model.Scope; import io.fabric8.kubernetes.model.annotation.Group; import io.fabric8.kubernetes.model.annotation.Kind; import io.fabric8.kubernetes.model.annotation.Plural; @@ -66,7 +67,7 @@ public String key() { public Scope scope() { return customResource.getInterfaces().stream() .filter(t -> t.toString().equals(Namespaced.class.getTypeName())) - .map(t -> Scope.Namespaced).findFirst().orElse(Scope.Cluster); + .map(t -> Scope.NAMESPACED).findFirst().orElse(Scope.CLUSTER); } public String crdName() { diff --git a/crd-generator/src/main/java/io/fabric8/crd/generator/v1/CustomResourceHandler.java b/crd-generator/src/main/java/io/fabric8/crd/generator/v1/CustomResourceHandler.java index 2b731a22daa..42191a15639 100644 --- a/crd-generator/src/main/java/io/fabric8/crd/generator/v1/CustomResourceHandler.java +++ b/crd-generator/src/main/java/io/fabric8/crd/generator/v1/CustomResourceHandler.java @@ -56,7 +56,7 @@ protected void addDecorators(CustomResourceInfo config, TypeDef def, final String version = config.version(); resources.decorate( new AddCustomResourceDefinitionResourceDecorator(name, config.group(), config.kind(), - config.scope().name(), config.shortNames(), config.plural(), config.singular())); + config.scope().value(), config.shortNames(), config.plural(), config.singular())); resources.decorate(new AddCustomResourceDefinitionVersionDecorator(name, version)); diff --git a/crd-generator/src/main/java/io/fabric8/crd/generator/v1beta1/CustomResourceHandler.java b/crd-generator/src/main/java/io/fabric8/crd/generator/v1beta1/CustomResourceHandler.java index 5d133d6024f..f6c80a91d40 100644 --- a/crd-generator/src/main/java/io/fabric8/crd/generator/v1beta1/CustomResourceHandler.java +++ b/crd-generator/src/main/java/io/fabric8/crd/generator/v1beta1/CustomResourceHandler.java @@ -57,7 +57,7 @@ protected void addDecorators(CustomResourceInfo config, TypeDef def, final String version = config.version(); resources.decorate( new AddCustomResourceDefinitionResourceDecorator(name, config.group(), config.kind(), - config.scope().name(), config.shortNames(), config.plural(), config.singular())); + config.scope().value(), config.shortNames(), config.plural(), config.singular())); resources.decorate(new AddCustomResourceDefinitionVersionDecorator(name, version)); diff --git a/crd-generator/src/main/java/io/fabric8/crd/generator/visitor/AdditionalPrinterColumnDetector.java b/crd-generator/src/main/java/io/fabric8/crd/generator/visitor/AdditionalPrinterColumnDetector.java index 2a44918d3d3..6f87a176bc6 100644 --- a/crd-generator/src/main/java/io/fabric8/crd/generator/visitor/AdditionalPrinterColumnDetector.java +++ b/crd-generator/src/main/java/io/fabric8/crd/generator/visitor/AdditionalPrinterColumnDetector.java @@ -15,7 +15,7 @@ */ package io.fabric8.crd.generator.visitor; -import io.fabric8.crd.annotation.PrinterColumn; +import io.fabric8.kubernetes.model.annotation.PrinterColumn; import java.util.ArrayList; public class AdditionalPrinterColumnDetector extends AnnotatedMultiPropertyPathDetector { diff --git a/crd-generator/src/main/java/io/fabric8/crd/generator/visitor/LabelSelectorPathDetector.java b/crd-generator/src/main/java/io/fabric8/crd/generator/visitor/LabelSelectorPathDetector.java index 8abc5c4b44f..216854795c2 100644 --- a/crd-generator/src/main/java/io/fabric8/crd/generator/visitor/LabelSelectorPathDetector.java +++ b/crd-generator/src/main/java/io/fabric8/crd/generator/visitor/LabelSelectorPathDetector.java @@ -15,7 +15,7 @@ */ package io.fabric8.crd.generator.visitor; -import io.fabric8.crd.annotation.LabelSelector; +import io.fabric8.kubernetes.model.annotation.LabelSelector; import java.util.ArrayList; public class LabelSelectorPathDetector extends AnnotatedPropertyPathDetector { diff --git a/crd-generator/src/main/java/io/fabric8/crd/generator/visitor/SpecReplicasPathDetector.java b/crd-generator/src/main/java/io/fabric8/crd/generator/visitor/SpecReplicasPathDetector.java index f995cca0780..162d944fe6e 100644 --- a/crd-generator/src/main/java/io/fabric8/crd/generator/visitor/SpecReplicasPathDetector.java +++ b/crd-generator/src/main/java/io/fabric8/crd/generator/visitor/SpecReplicasPathDetector.java @@ -15,7 +15,7 @@ */ package io.fabric8.crd.generator.visitor; -import io.fabric8.crd.annotation.SpecReplicas; +import io.fabric8.kubernetes.model.annotation.SpecReplicas; import java.util.ArrayList; public class SpecReplicasPathDetector extends AnnotatedPropertyPathDetector { diff --git a/crd-generator/src/main/java/io/fabric8/crd/generator/visitor/StatusReplicasPathDetector.java b/crd-generator/src/main/java/io/fabric8/crd/generator/visitor/StatusReplicasPathDetector.java index ea8a304bb71..c23ee4d0708 100644 --- a/crd-generator/src/main/java/io/fabric8/crd/generator/visitor/StatusReplicasPathDetector.java +++ b/crd-generator/src/main/java/io/fabric8/crd/generator/visitor/StatusReplicasPathDetector.java @@ -15,7 +15,7 @@ */ package io.fabric8.crd.generator.visitor; -import io.fabric8.crd.annotation.StatusReplicas; +import io.fabric8.kubernetes.model.annotation.StatusReplicas; import java.util.ArrayList; public class StatusReplicasPathDetector extends AnnotatedPropertyPathDetector { diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/CustomResource.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/CustomResource.java index 33d3e730883..5170cce4f36 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/CustomResource.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/CustomResource.java @@ -26,6 +26,7 @@ import io.fabric8.kubernetes.api.model.Namespaced; import io.fabric8.kubernetes.api.model.ObjectMeta; import io.fabric8.kubernetes.client.utils.Pluralize; +import io.fabric8.kubernetes.model.Scope; import io.fabric8.kubernetes.model.annotation.Group; import io.fabric8.kubernetes.model.annotation.Plural; import io.fabric8.kubernetes.model.annotation.Singular; @@ -68,8 +69,6 @@ public abstract class CustomResource implements HasMetadata { private static final Logger LOG = LoggerFactory.getLogger(CustomResource.class); - public static final String NAMESPACE_SCOPE = "Namespaced"; - public static final String CLUSTER_SCOPE = "Cluster"; private ObjectMeta metadata = new ObjectMeta(); @JsonProperty("spec") @@ -96,7 +95,7 @@ public CustomResource() { } this.apiVersion = version; this.kind = HasMetadata.super.getKind(); - scope = this instanceof Namespaced ? NAMESPACE_SCOPE : CLUSTER_SCOPE; + scope = this instanceof Namespaced ? Scope.NAMESPACED.value() : Scope.CLUSTER.value(); this.singular = getSingular(clazz); this.plural = getPlural(clazz); this.crdName = getCRDName(clazz); @@ -228,7 +227,7 @@ public String getCRDName() { /** * Retrieves the scope that this CustomResource targets * - * @return the scope that this CustomResource targets. Possible values are {@link #CLUSTER_SCOPE} or {@link #NAMESPACE_SCOPE}. + * @return the scope that this CustomResource targets. Possible values are {@link Scope#CLUSTER} or {@link Scope#NAMESPACED}. */ @JsonIgnore public String getScope() { diff --git a/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/ZookeeperCustomResourceIT.java b/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/ZookeeperCustomResourceIT.java index 62a63e39c47..ada54097bc0 100644 --- a/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/ZookeeperCustomResourceIT.java +++ b/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/ZookeeperCustomResourceIT.java @@ -15,21 +15,18 @@ */ package io.fabric8.crd.zookeeper; -import java.util.List; -import java.util.Optional; - -import org.junit.Test; -import io.fabric8.kubernetes.client.utils.Serialization; -import io.fabric8.kubernetes.api.model.HasMetadata; -import io.fabric8.kubernetes.api.model.KubernetesList; import io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition; import io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionVersion; import io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaProps; +import io.fabric8.kubernetes.client.utils.Serialization; +import org.junit.Test; + +import java.util.List; +import java.util.Optional; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; public class ZookeeperCustomResourceIT { diff --git a/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/v1/ZookeeperSpec.java b/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/v1/ZookeeperSpec.java index 5d03101c7f4..83541bea1e1 100644 --- a/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/v1/ZookeeperSpec.java +++ b/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/v1/ZookeeperSpec.java @@ -15,10 +15,9 @@ */ package io.fabric8.crd.zookeeper.v1; +import io.fabric8.kubernetes.model.annotation.SpecReplicas; import javax.validation.constraints.NotNull; -import io.fabric8.crd.annotation.SpecReplicas; - public class ZookeeperSpec { @SpecReplicas diff --git a/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/v1/ZookeeperStatus.java b/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/v1/ZookeeperStatus.java index deddfe443d9..077b828f2ee 100644 --- a/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/v1/ZookeeperStatus.java +++ b/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/v1/ZookeeperStatus.java @@ -15,12 +15,11 @@ */ package io.fabric8.crd.zookeeper.v1; +import io.fabric8.kubernetes.model.annotation.LabelSelector; +import io.fabric8.kubernetes.model.annotation.PrinterColumn; +import io.fabric8.kubernetes.model.annotation.StatusReplicas; import java.util.Map; -import io.fabric8.crd.annotation.LabelSelector; -import io.fabric8.crd.annotation.StatusReplicas; -import io.fabric8.crd.annotation.PrinterColumn; - public class ZookeeperStatus { @PrinterColumn diff --git a/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/v1alpha1/ZookeeperSpec.java b/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/v1alpha1/ZookeeperSpec.java index 3e3669cc456..fcb2eebd814 100644 --- a/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/v1alpha1/ZookeeperSpec.java +++ b/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/v1alpha1/ZookeeperSpec.java @@ -15,7 +15,7 @@ */ package io.fabric8.crd.zookeeper.v1alpha1; -import io.fabric8.crd.annotation.SpecReplicas; +import io.fabric8.kubernetes.model.annotation.SpecReplicas; public class ZookeeperSpec { diff --git a/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/v1alpha1/ZookeeperStatus.java b/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/v1alpha1/ZookeeperStatus.java index 291449e83ad..a261222c6e3 100644 --- a/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/v1alpha1/ZookeeperStatus.java +++ b/kubernetes-itests/src/test/java/io/fabric8/crd/zookeeper/v1alpha1/ZookeeperStatus.java @@ -17,8 +17,8 @@ import java.util.Map; -import io.fabric8.crd.annotation.LabelSelector; -import io.fabric8.crd.annotation.StatusReplicas; +import io.fabric8.kubernetes.model.annotation.LabelSelector; +import io.fabric8.kubernetes.model.annotation.StatusReplicas; public class ZookeeperStatus { diff --git a/kubernetes-model-generator/kubernetes-model-common/pom.xml b/kubernetes-model-generator/kubernetes-model-common/pom.xml index bdf46dddd85..20733632efc 100644 --- a/kubernetes-model-generator/kubernetes-model-common/pom.xml +++ b/kubernetes-model-generator/kubernetes-model-common/pom.xml @@ -51,8 +51,7 @@ ${project.groupId}.${project.artifactId} * - io.fabric8.kubernetes.model.annotation, - io.fabric8.kubernetes.model.util + io.fabric8.kubernetes.model** diff --git a/crd-generator/src/main/java/io/fabric8/crd/generator/apt/Scope.java b/kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/Scope.java similarity index 58% rename from crd-generator/src/main/java/io/fabric8/crd/generator/apt/Scope.java rename to kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/Scope.java index 71ea1e7112e..ed9db6d6df4 100644 --- a/crd-generator/src/main/java/io/fabric8/crd/generator/apt/Scope.java +++ b/kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/Scope.java @@ -13,8 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.fabric8.crd.generator.apt; +package io.fabric8.kubernetes.model; +/** + * Represents the scope of a resource in the cluster. Resources can either be cluster-scoped or only + * are visible within the confines of a given namespace. + */ public enum Scope { - Namespaced, Cluster, + NAMESPACED("Namespaced"), CLUSTER("Cluster"); + + private final String value; + + Scope(String value) { + this.value = value; + } + + public String value() { + return value; + } + + @Override + public String toString() { + return value; + } } diff --git a/crd-generator/src/main/java/io/fabric8/crd/annotation/LabelSelector.java b/kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/annotation/LabelSelector.java similarity index 90% rename from crd-generator/src/main/java/io/fabric8/crd/annotation/LabelSelector.java rename to kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/annotation/LabelSelector.java index 18ab00b2429..f29272ffbb2 100644 --- a/crd-generator/src/main/java/io/fabric8/crd/annotation/LabelSelector.java +++ b/kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/annotation/LabelSelector.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.fabric8.crd.annotation; +package io.fabric8.kubernetes.model.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -21,7 +21,7 @@ import java.lang.annotation.Target; @Target({ ElementType.FIELD }) -@Retention(RetentionPolicy.SOURCE) +@Retention(RetentionPolicy.RUNTIME) public @interface LabelSelector { } diff --git a/crd-generator/src/main/java/io/fabric8/crd/annotation/PrinterColumn.java b/kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/annotation/PrinterColumn.java similarity index 93% rename from crd-generator/src/main/java/io/fabric8/crd/annotation/PrinterColumn.java rename to kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/annotation/PrinterColumn.java index 564febabf61..9638f0b43d2 100644 --- a/crd-generator/src/main/java/io/fabric8/crd/annotation/PrinterColumn.java +++ b/kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/annotation/PrinterColumn.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.fabric8.crd.annotation; +package io.fabric8.kubernetes.model.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -21,7 +21,7 @@ import java.lang.annotation.Target; @Target({ ElementType.FIELD }) -@Retention(RetentionPolicy.SOURCE) +@Retention(RetentionPolicy.RUNTIME) public @interface PrinterColumn { /** diff --git a/crd-generator/src/main/java/io/fabric8/crd/annotation/SpecReplicas.java b/kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/annotation/SpecReplicas.java similarity index 90% rename from crd-generator/src/main/java/io/fabric8/crd/annotation/SpecReplicas.java rename to kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/annotation/SpecReplicas.java index 66365885117..3ccf1d388f6 100644 --- a/crd-generator/src/main/java/io/fabric8/crd/annotation/SpecReplicas.java +++ b/kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/annotation/SpecReplicas.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.fabric8.crd.annotation; +package io.fabric8.kubernetes.model.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -21,7 +21,7 @@ import java.lang.annotation.Target; @Target({ ElementType.FIELD }) -@Retention(RetentionPolicy.SOURCE) +@Retention(RetentionPolicy.RUNTIME) public @interface SpecReplicas { } diff --git a/crd-generator/src/main/java/io/fabric8/crd/annotation/StatusReplicas.java b/kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/annotation/StatusReplicas.java similarity index 90% rename from crd-generator/src/main/java/io/fabric8/crd/annotation/StatusReplicas.java rename to kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/annotation/StatusReplicas.java index 3a13f66bddc..ae87e81a39c 100644 --- a/crd-generator/src/main/java/io/fabric8/crd/annotation/StatusReplicas.java +++ b/kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/annotation/StatusReplicas.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.fabric8.crd.annotation; +package io.fabric8.kubernetes.model.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -21,7 +21,7 @@ import java.lang.annotation.Target; @Target({ ElementType.FIELD }) -@Retention(RetentionPolicy.SOURCE) +@Retention(RetentionPolicy.RUNTIME) public @interface StatusReplicas { }