Skip to content

Setup code formatter #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
uses: actions/setup-java@v1
with:
java-version: ${{ matrix.java }}
- name: Check code format
run: mvn fmt:check --file pom.xml
- name: Run unit tests
run: mvn -B test -P no-integration-tests --file pom.xml
- name: Set up Minikube
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
package io.javaoperatorsdk.operator;

import org.apache.commons.lang3.ClassUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

import org.apache.commons.lang3.ClassUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class ClassMappingProvider {
private static final Logger log = LoggerFactory.getLogger(ClassMappingProvider.class);

static <T, V> Map<T, V> provide(final String resourcePath, T key, V value) {
Map<T, V> result = new HashMap();
try {
final var classLoader = Thread.currentThread().getContextClassLoader();
final Enumeration<URL> customResourcesMetadataList = classLoader.getResources(resourcePath);
for (Iterator<URL> it = customResourcesMetadataList.asIterator(); it.hasNext(); ) {
URL url = it.next();

List<String> classNamePairs = retrieveClassNamePairs(url);
classNamePairs.forEach(clazzPair -> {
try {
final String[] classNames = clazzPair.split(",");
if (classNames.length != 2) {
throw new IllegalStateException(String.format("%s is not valid Mapping metadata, defined in %s", clazzPair, url.toString()));
}

result.put(
(T) ClassUtils.getClass(classNames[0]),
(V) ClassUtils.getClass(classNames[1])
);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
});
}
log.debug("Loaded Controller to CustomResource mappings {}", result);
return result;
} catch (IOException e) {
throw new RuntimeException(e);
}
private static final Logger log = LoggerFactory.getLogger(ClassMappingProvider.class);

static <T, V> Map<T, V> provide(final String resourcePath, T key, V value) {
Map<T, V> result = new HashMap();
try {
final var classLoader = Thread.currentThread().getContextClassLoader();
final Enumeration<URL> customResourcesMetadataList = classLoader.getResources(resourcePath);
for (Iterator<URL> it = customResourcesMetadataList.asIterator(); it.hasNext(); ) {
URL url = it.next();

List<String> classNamePairs = retrieveClassNamePairs(url);
classNamePairs.forEach(
clazzPair -> {
try {
final String[] classNames = clazzPair.split(",");
if (classNames.length != 2) {
throw new IllegalStateException(
String.format(
"%s is not valid Mapping metadata, defined in %s",
clazzPair, url.toString()));
}

result.put(
(T) ClassUtils.getClass(classNames[0]), (V) ClassUtils.getClass(classNames[1]));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
});
}
log.debug("Loaded Controller to CustomResource mappings {}", result);
return result;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static List<String> retrieveClassNamePairs(URL url) throws IOException {
return new BufferedReader(
new InputStreamReader(url.openStream())
).lines().collect(Collectors.toList());
}
private static List<String> retrieveClassNamePairs(URL url) throws IOException {
return new BufferedReader(new InputStreamReader(url.openStream()))
.lines()
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,76 @@
import io.fabric8.kubernetes.client.CustomResourceDoneable;
import io.javaoperatorsdk.operator.api.Controller;
import io.javaoperatorsdk.operator.api.ResourceController;

import java.util.Map;


public class ControllerUtils {

private static final String FINALIZER_NAME_SUFFIX = "/finalizer";
public static final String CONTROLLERS_RESOURCE_PATH = "javaoperatorsdk/controllers";
public static final String DONEABLES_RESOURCE_PATH = "javaoperatorsdk/doneables";
private static Map<Class<? extends ResourceController>, Class<? extends CustomResource>> controllerToCustomResourceMappings;
private static Map<Class<? extends CustomResource>, Class<? extends CustomResourceDoneable>> resourceToDoneableMappings;
private static final String FINALIZER_NAME_SUFFIX = "/finalizer";
public static final String CONTROLLERS_RESOURCE_PATH = "javaoperatorsdk/controllers";
public static final String DONEABLES_RESOURCE_PATH = "javaoperatorsdk/doneables";
private static Map<Class<? extends ResourceController>, Class<? extends CustomResource>>
controllerToCustomResourceMappings;
private static Map<Class<? extends CustomResource>, Class<? extends CustomResourceDoneable>>
resourceToDoneableMappings;

static {
controllerToCustomResourceMappings =
ClassMappingProvider.provide(
CONTROLLERS_RESOURCE_PATH, ResourceController.class, CustomResource.class);
resourceToDoneableMappings =
ClassMappingProvider.provide(
DONEABLES_RESOURCE_PATH, CustomResource.class, CustomResourceDoneable.class);
}

static {
controllerToCustomResourceMappings = ClassMappingProvider
.provide(CONTROLLERS_RESOURCE_PATH,
ResourceController.class,
CustomResource.class);
resourceToDoneableMappings = ClassMappingProvider
.provide(DONEABLES_RESOURCE_PATH,
CustomResource.class,
CustomResourceDoneable.class
);
}

static String getFinalizer(ResourceController controller) {
final String annotationFinalizerName = getAnnotation(controller).finalizerName();
if (!Controller.NULL.equals(annotationFinalizerName)) {
return annotationFinalizerName;
}
return getAnnotation(controller).crdName() + FINALIZER_NAME_SUFFIX;
static String getFinalizer(ResourceController controller) {
final String annotationFinalizerName = getAnnotation(controller).finalizerName();
if (!Controller.NULL.equals(annotationFinalizerName)) {
return annotationFinalizerName;
}
return getAnnotation(controller).crdName() + FINALIZER_NAME_SUFFIX;
}

static boolean getGenerationEventProcessing(ResourceController<?> controller) {
return getAnnotation(controller).generationAwareEventProcessing();
}
static boolean getGenerationEventProcessing(ResourceController<?> controller) {
return getAnnotation(controller).generationAwareEventProcessing();
}

static <R extends CustomResource> Class<R> getCustomResourceClass(ResourceController<R> controller) {
final Class<? extends CustomResource> customResourceClass = controllerToCustomResourceMappings
.get(controller.getClass());
if (customResourceClass == null) {
throw new IllegalArgumentException(
String.format(
"No custom resource has been found for controller %s",
controller.getClass().getCanonicalName()
)
);
}
return (Class<R>) customResourceClass;
static <R extends CustomResource> Class<R> getCustomResourceClass(
ResourceController<R> controller) {
final Class<? extends CustomResource> customResourceClass =
controllerToCustomResourceMappings.get(controller.getClass());
if (customResourceClass == null) {
throw new IllegalArgumentException(
String.format(
"No custom resource has been found for controller %s",
controller.getClass().getCanonicalName()));
}
return (Class<R>) customResourceClass;
}

static String getCrdName(ResourceController controller) {
return getAnnotation(controller).crdName();
}
static String getCrdName(ResourceController controller) {
return getAnnotation(controller).crdName();
}

public static <T extends CustomResource> Class<? extends CustomResourceDoneable<T>>
getCustomResourceDoneableClass(ResourceController<T> controller) {
final Class<T> customResourceClass = getCustomResourceClass(controller);
final Class<? extends CustomResourceDoneable<T>> doneableClass = (Class<? extends CustomResourceDoneable<T>>) resourceToDoneableMappings.get(customResourceClass);
if (doneableClass == null) {
throw new RuntimeException(String.format("No matching Doneable class found for %s", customResourceClass));
}
return doneableClass;
public static <T extends CustomResource>
Class<? extends CustomResourceDoneable<T>> getCustomResourceDoneableClass(
ResourceController<T> controller) {
final Class<T> customResourceClass = getCustomResourceClass(controller);
final Class<? extends CustomResourceDoneable<T>> doneableClass =
(Class<? extends CustomResourceDoneable<T>>)
resourceToDoneableMappings.get(customResourceClass);
if (doneableClass == null) {
throw new RuntimeException(
String.format("No matching Doneable class found for %s", customResourceClass));
}
return doneableClass;
}

private static Controller getAnnotation(ResourceController<?> controller) {
return controller.getClass().getAnnotation(Controller.class);
}
private static Controller getAnnotation(ResourceController<?> controller) {
return controller.getClass().getAnnotation(Controller.class);
}

public static boolean hasGivenFinalizer(CustomResource resource, String finalizer) {
return resource.getMetadata().getFinalizers() != null && resource.getMetadata().getFinalizers().contains(finalizer);
}
public static boolean hasGivenFinalizer(CustomResource resource, String finalizer) {
return resource.getMetadata().getFinalizers() != null
&& resource.getMetadata().getFinalizers().contains(finalizer);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package io.javaoperatorsdk.operator;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.Watcher;
import io.javaoperatorsdk.operator.processing.event.Event;
import io.javaoperatorsdk.operator.processing.event.internal.CustomResourceEvent;

import java.util.List;

public class EventListUtils {

public static boolean containsCustomResourceDeletedEvent(List<Event> events) {
return events.stream().anyMatch(e -> {
if (e instanceof CustomResourceEvent) {
public static boolean containsCustomResourceDeletedEvent(List<Event> events) {
return events.stream()
.anyMatch(
e -> {
if (e instanceof CustomResourceEvent) {
return ((CustomResourceEvent) e).getAction() == Watcher.Action.DELETED;
} else {
} else {
return false;
}
});
}
}
});
}
}
Loading