Skip to content

Commit

Permalink
redactor: Move CRD annotations to kubernetes-model-common (#2890)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
metacosm and manusa authored Mar 11, 2021
1 parent 1b4a456 commit f76c644
Show file tree
Hide file tree
Showing 20 changed files with 56 additions and 42 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,8 +69,6 @@
public abstract class CustomResource<S, T> 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")
Expand All @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
3 changes: 1 addition & 2 deletions kubernetes-model-generator/kubernetes-model-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Import-Package>*</Import-Package>
<Export-Package>
io.fabric8.kubernetes.model.annotation,
io.fabric8.kubernetes.model.util
io.fabric8.kubernetes.model**
</Export-Package>
</instructions>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
* 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;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.RUNTIME)
public @interface LabelSelector {

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
* 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;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.RUNTIME)
public @interface PrinterColumn {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
* 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;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SpecReplicas {

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
* 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;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.RUNTIME)
public @interface StatusReplicas {

}

0 comments on commit f76c644

Please sign in to comment.