Skip to content

Commit

Permalink
Bulk formatting and removing unused imports (fabric8io#4517)
Browse files Browse the repository at this point in the history
* fix fabric8io#3984: bulk formatting the rest of the codebase

* fix fabric8io#3984: removing unused imports

* fix fabric8io#3984: correcting headers
  • Loading branch information
shawkins committed Nov 29, 2022
1 parent 432c48a commit 009974f
Show file tree
Hide file tree
Showing 542 changed files with 8,033 additions and 7,825 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,33 @@ public void handle(CustomResourceInfo config) {
}

def = builder
.accept(labelSelectorPathDetector)
.accept(additionalPrinterColumnDetector)
.accept(traversedClassesVisitor)
.build();
.accept(labelSelectorPathDetector)
.accept(additionalPrinterColumnDetector)
.accept(traversedClassesVisitor)
.build();

addDecorators(config, def, specReplicasPathDetector.getPath(),
statusReplicasPathDetector.getPath(), labelSelectorPathDetector.getPath());
statusReplicasPathDetector.getPath(), labelSelectorPathDetector.getPath());

Map<String, Property> additionalPrinterColumns = new HashMap<>(additionalPrinterColumnDetector.getProperties());
additionalPrinterColumns.forEach((path, property) -> {
Map<String, Object> parameters = property.getAnnotations().stream()
.filter(a -> a.getClassRef().getName().equals("PrinterColumn")).map(AnnotationRef::getParameters)
.findFirst().orElse(Collections.emptyMap());
.filter(a -> a.getClassRef().getName().equals("PrinterColumn")).map(AnnotationRef::getParameters)
.findFirst().orElse(Collections.emptyMap());
String type = AbstractJsonSchema.getSchemaTypeFor(property.getTypeRef());
String column = (String) parameters.get("name");
if (Utils.isNullOrEmpty(column)) {
column = property.getName().toUpperCase();
}
String description = property.getComments().stream().filter(l -> !l.trim().startsWith("@"))
.collect(Collectors.joining(" ")).trim();
.collect(Collectors.joining(" ")).trim();
String format = (String) parameters.get("format");

resources.decorate(
getPrinterColumnDecorator(name, version, path, type, column, description, format));
getPrinterColumnDecorator(name, version, path, type, column, description, format));
});
}

/**
* Provides the decorator implementation associated with the CRD generation version.
*
Expand All @@ -104,19 +104,21 @@ public void handle(CustomResourceInfo config) {
* @return the concrete decorator implementing the addition of a printer column to the currently built CRD
*/
protected abstract Decorator getPrinterColumnDecorator(String name, String version, String path,
String type, String column, String description, String format);
String type, String column, String description, String format);

/**
* Adds all the necessary decorators to build the specific CRD version. For optional paths, see
* https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#customresourcesubresourcescale-v1-apiextensions-k8s-io
* These paths
*
* @param config the gathered {@link CustomResourceInfo} used as basis for the CRD generation
* @param def the {@link TypeDef} associated with the {@link io.fabric8.kubernetes.client.CustomResource} from which the CRD is generated
* @param def the {@link TypeDef} associated with the {@link io.fabric8.kubernetes.client.CustomResource} from which the CRD
* is generated
* @param specReplicasPath an optionally detected path of field defining spec replicas
* @param statusReplicasPath an optionally detected path of field defining status replicas
* @param labelSelectorPath an optionally detected path of field defining `status.selector`
* @param labelSelectorPath an optionally detected path of field defining `status.selector`
*/
protected abstract void addDecorators(CustomResourceInfo config, TypeDef def,
Optional<String> specReplicasPath, Optional<String> statusReplicasPath,
Optional<String> labelSelectorPath);
Optional<String> specReplicasPath, Optional<String> statusReplicasPath,
Optional<String> labelSelectorPath);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public Map<String, Map<String, CRDInfo>> getCRDDetailsPerNameAndVersion() {

void add(String crdName, String version, URI fileURI) {
crdNameToVersionToCRDInfoMap.computeIfAbsent(crdName, k -> new HashMap<>())
.put(version, new CRDInfo(crdName, version, new File(fileURI).getAbsolutePath(), ClassDependenciesVisitor.getDependentClassesFromCRDName(crdName)));
.put(version, new CRDInfo(crdName, version, new File(fileURI).getAbsolutePath(),
ClassDependenciesVisitor.getDependentClassesFromCRDName(crdName)));
}

public int numberOfGeneratedCRDs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ public class CRDGenerator {
private Map<String, CustomResourceInfo> infos;

private static final ObjectMapper YAML_MAPPER = new ObjectMapper(
new YAMLFactory()
.enable(Feature.MINIMIZE_QUOTES)
.enable(Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS)
.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
);
new YAMLFactory()
.enable(Feature.MINIMIZE_QUOTES)
.enable(Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS)
.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER));

static {
YAML_MAPPER.configure(SerializationFeature.INDENT_OUTPUT, true);
Expand All @@ -71,7 +70,7 @@ public CRDGenerator withOutput(CRDOutput<? extends OutputStream> output) {

public CRDGenerator forCRDVersions(List<String> versions) {
return versions != null && !versions.isEmpty() ? forCRDVersions(versions.toArray(new String[0]))
: this;
: this;
}

public CRDGenerator forCRDVersions(String... versions) {
Expand All @@ -81,11 +80,11 @@ public CRDGenerator forCRDVersions(String... versions) {
switch (version) {
case CustomResourceHandler.VERSION:
handlers.computeIfAbsent(CustomResourceHandler.VERSION,
s -> new CustomResourceHandler(resources));
s -> new CustomResourceHandler(resources));
break;
case io.fabric8.crd.generator.v1beta1.CustomResourceHandler.VERSION:
handlers.computeIfAbsent(io.fabric8.crd.generator.v1beta1.CustomResourceHandler.VERSION,
s -> new io.fabric8.crd.generator.v1beta1.CustomResourceHandler(resources));
s -> new io.fabric8.crd.generator.v1beta1.CustomResourceHandler(resources));
break;
default:
LOGGER.warn("Ignoring unsupported CRD version: {}", version);
Expand All @@ -110,9 +109,9 @@ public CRDGenerator customResources(CustomResourceInfo... infos) {
this.infos = new HashMap<>(infos.length);
}
Arrays.stream(infos)
.filter(Objects::nonNull)
// make sure we record all versions of the CR
.forEach(info -> this.infos.put(getOutputName(info.crdName(), info.version()), info));
.filter(Objects::nonNull)
// make sure we record all versions of the CR
.forEach(info -> this.infos.put(getOutputName(info.crdName(), info.version()), info));
}
return this;
}
Expand All @@ -133,23 +132,23 @@ public CRDGenerationInfo detailedGenerate() {

if (output == null) {
LOGGER.warn(
"No output option was selected either using 'inOutputDir' or 'withOutput' methods. Skipping generation.");
"No output option was selected either using 'inOutputDir' or 'withOutput' methods. Skipping generation.");
return CRDGenerationInfo.EMPTY;
}

// if no CRD version is specified, generate for all supported versions
if (handlers.isEmpty()) {
forCRDVersions(CustomResourceHandler.VERSION,
io.fabric8.crd.generator.v1beta1.CustomResourceHandler.VERSION);
io.fabric8.crd.generator.v1beta1.CustomResourceHandler.VERSION);
}

for (CustomResourceInfo info : infos.values()) {
if (info != null) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Generating '{}' version '{}' with {} (spec: {} / status {})...",
info.crdName(), info.version(), info.crClassName(),
info.specClassName().orElse("undetermined"),
info.statusClassName().orElse("undetermined"));
info.crdName(), info.version(), info.crClassName(),
info.specClassName().orElse("undetermined"),
info.statusClassName().orElse("undetermined"));
}
handlers.values().forEach(h -> h.handle(info));
}
Expand All @@ -163,8 +162,8 @@ public CRDGenerationInfo detailedGenerate() {
final String outputName = getOutputName(crdName, version);
try (final OutputStream outputStream = output.outputFor(outputName)) {
outputStream.write(
"# Generated by Fabric8 CRDGenerator, manual edits might get overwritten!\n"
.getBytes());
"# Generated by Fabric8 CRDGenerator, manual edits might get overwritten!\n"
.getBytes());
YAML_MAPPER.writeValue(outputStream, crd);
final URI fileURI = output.crdURI(outputName);
crdGenerationInfo.add(crdName, version, fileURI);
Expand All @@ -175,7 +174,7 @@ public CRDGenerationInfo detailedGenerate() {
}
return crdGenerationInfo;
}

public static String getOutputName(String crdName, String crdSpecVersion) {
return crdName + "-" + crdSpecVersion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
*/
package io.fabric8.crd.generator;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import io.fabric8.crd.generator.decorator.Decorator;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class Resources {
private final KubernetesListBuilder global = new KubernetesListBuilder();
private final Set<Decorator> globalDecorators = new TreeSet<>();
Expand All @@ -38,13 +38,13 @@ public KubernetesListBuilder global() {
return this.global;
}


/**
* Get the Decorator Set.
* The method is visible for testing purposes.
*
* @return the Set of registed Decorators.
*/
protected Set<Decorator> getDecorators() {
protected Set<Decorator> getDecorators() {
return globalDecorators;
}

Expand All @@ -57,7 +57,6 @@ public void decorate(Decorator decorator) {
globalDecorators.add(decorator);
}


/**
* Add a resource to all groups.
*
Expand All @@ -67,7 +66,6 @@ public void add(HasMetadata metadata) {
global.addToItems(metadata);
}


/**
* Generate all resources.
*
Expand All @@ -90,8 +88,8 @@ public List<Decorator> applyConstraints(Set<Decorator> decorators) {
// In other words we don't ANY sorting algorithm, we need bubble sort.
// We also might need it more than once. So, we'll do it as many times as we have to, till there are not more transformations.
// But hey, let's have an upper limit of 5 just to prevent infinite loops
for(int i=0; i<10 && bubbleSort(array); i++) {
System.out.println("Sorting again:" + (i+1));
for (int i = 0; i < 10 && bubbleSort(array); i++) {
System.out.println("Sorting again:" + (i + 1));
}

for (Decorator d : array) {
Expand All @@ -102,6 +100,7 @@ public List<Decorator> applyConstraints(Set<Decorator> decorators) {

/**
* Bubble sort for decorators.
*
* @param decorators the {@link Decorator} array to be sorted
*/
private boolean bubbleSort(Decorator[] decorators) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public int compareTo(Decorator o) {
return -1;
}
}
//Reproducible order every single time
//Reproducible order every single time
int result = getClass().getName().compareTo(o.getClass().getName());
if (result == 0) {
result = hashCode() - o.hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.fabric8.kubernetes.api.builder.VisitableBuilder;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.utils.Utils;

import java.util.Optional;

public abstract class NamedResourceDecorator<T> extends Decorator<VisitableBuilder> {
Expand Down Expand Up @@ -123,13 +124,13 @@ public ResourceVisitor withMetadata(ObjectMeta metadata) {

public Class<T> getType() {
return (Class) Generics
.getTypeArguments(NamedResourceDecorator.class, NamedResourceDecorator.this.getClass())
.get(0);
.getTypeArguments(NamedResourceDecorator.class, NamedResourceDecorator.this.getClass())
.get(0);
}
}

@Override
public Class<? extends Decorator>[] after() {
return new Class[]{ ResourceProvidingDecorator.class };
return new Class[] { ResourceProvidingDecorator.class };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.fabric8.crd.generator.decorator;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class Generics {
private Generics() {
throw new IllegalStateException("Utility class");
}

public static Class<?> getClass(Type type) {
if (type instanceof Class) {
return (Class) type;
Expand All @@ -56,7 +57,7 @@ public static Class<?> getClass(Type type) {
return null;
}
}

/**
* Get the actual type arguments a child class has used to extend a generic base class.
*
Expand All @@ -66,7 +67,7 @@ public static Class<?> getClass(Type type) {
* @return a list of the raw classes for the actual type arguments
*/
public static <T> List<Class> getTypeArguments(Class<T> baseClass,
Class<? extends T> childClass) {
Class<? extends T> childClass) {
Map<Type, Type> resolvedTypes = new LinkedHashMap<>();
Type type = childClass;
// start walking up the inheritance hierarchy until we hit baseClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.ObjectMetaFluent;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Optional;
Expand All @@ -29,6 +30,7 @@ public class Metadata {
private Metadata() {
throw new IllegalStateException("Utility class");
}

public static Optional<String> getKind(Builder builder) {
try {
Method method = builder.getClass().getMethod("getKind");
Expand Down Expand Up @@ -96,9 +98,9 @@ public static boolean removeFromLabels(Builder builder, String key) {
* @return The predicate.
*/
public static Predicate<VisitableBuilder<? extends HasMetadata, ?>> matching(
HasMetadata candidate) {
HasMetadata candidate) {
return matching(candidate.getApiVersion(), candidate.getKind(),
candidate.getMetadata().getName());
candidate.getMetadata().getName());
}

/**
Expand All @@ -110,13 +112,13 @@ public static boolean removeFromLabels(Builder builder, String key) {
* @return The predicate.
*/
public static Predicate<VisitableBuilder<? extends HasMetadata, ?>> matching(String apiVersion,
String kind, String name) {
String kind, String name) {
return builder -> {
HasMetadata item = builder.build();
ObjectMeta metadata = item.getMetadata();
return apiVersion.equals(item.getApiVersion()) &&
kind != null && kind.equals(item.getKind()) &&
name != null && name.equals(metadata.getName());
kind != null && kind.equals(item.getKind()) &&
name != null && name.equals(metadata.getName());
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
import io.fabric8.kubernetes.client.CustomResource;
import io.sundr.adapter.api.AdapterContext;
import io.sundr.adapter.api.Adapters;
import io.sundr.adapter.apt.AptAdapter;
import io.sundr.adapter.apt.AptContext;
import io.sundr.builder.TypedVisitor;
import io.sundr.model.*;
import io.sundr.model.functions.GetDefinition;
import io.sundr.model.repo.DefinitionRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.fabric8.crd.generator.CustomResourceInfo;
import io.fabric8.crd.generator.Resources;
import io.fabric8.crd.generator.decorator.Decorator;
import io.fabric8.crd.generator.utils.Types;
import io.fabric8.crd.generator.v1.decorator.AddAdditionPrinterColumnDecorator;
import io.fabric8.crd.generator.v1.decorator.AddCustomResourceDefinitionResourceDecorator;
import io.fabric8.crd.generator.v1.decorator.AddCustomResourceDefinitionVersionDecorator;
Expand Down

0 comments on commit 009974f

Please sign in to comment.