Skip to content

Commit ba4a955

Browse files
author
Soroosh Sarabadani
committed
resolve conflicts
1 parent 485d167 commit ba4a955

File tree

5 files changed

+340
-307
lines changed

5 files changed

+340
-307
lines changed
Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
package io.javaoperatorsdk.operator;
22

3-
import org.apache.commons.lang3.ClassUtils;
4-
import org.slf4j.Logger;
5-
import org.slf4j.LoggerFactory;
6-
73
import java.io.BufferedReader;
84
import java.io.IOException;
95
import java.io.InputStreamReader;
106
import java.net.URL;
117
import java.util.*;
128
import java.util.stream.Collectors;
13-
9+
import org.apache.commons.lang3.ClassUtils;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
1412

1513
class ClassMappingProvider {
16-
private static final Logger log = LoggerFactory.getLogger(ClassMappingProvider.class);
17-
18-
static <T, V> Map<T, V> provide(final String resourcePath, T key, V value) {
19-
Map<T, V> result = new HashMap();
20-
try {
21-
final var classLoader = Thread.currentThread().getContextClassLoader();
22-
final Enumeration<URL> customResourcesMetadataList = classLoader.getResources(resourcePath);
23-
for (Iterator<URL> it = customResourcesMetadataList.asIterator(); it.hasNext(); ) {
24-
URL url = it.next();
25-
26-
List<String> classNamePairs = retrieveClassNamePairs(url);
27-
classNamePairs.forEach(clazzPair -> {
28-
try {
29-
final String[] classNames = clazzPair.split(",");
30-
if (classNames.length != 2) {
31-
throw new IllegalStateException(String.format("%s is not valid Mapping metadata, defined in %s", clazzPair, url.toString()));
32-
}
33-
34-
result.put(
35-
(T) ClassUtils.getClass(classNames[0]),
36-
(V) ClassUtils.getClass(classNames[1])
37-
);
38-
} catch (ClassNotFoundException e) {
39-
throw new RuntimeException(e);
40-
}
41-
});
42-
}
43-
log.debug("Loaded Controller to CustomResource mappings {}", result);
44-
return result;
45-
} catch (IOException e) {
46-
throw new RuntimeException(e);
47-
}
14+
private static final Logger log = LoggerFactory.getLogger(ClassMappingProvider.class);
15+
16+
static <T, V> Map<T, V> provide(final String resourcePath, T key, V value) {
17+
Map<T, V> result = new HashMap();
18+
try {
19+
final var classLoader = Thread.currentThread().getContextClassLoader();
20+
final Enumeration<URL> customResourcesMetadataList = classLoader.getResources(resourcePath);
21+
for (Iterator<URL> it = customResourcesMetadataList.asIterator(); it.hasNext(); ) {
22+
URL url = it.next();
23+
24+
List<String> classNamePairs = retrieveClassNamePairs(url);
25+
classNamePairs.forEach(
26+
clazzPair -> {
27+
try {
28+
final String[] classNames = clazzPair.split(",");
29+
if (classNames.length != 2) {
30+
throw new IllegalStateException(
31+
String.format(
32+
"%s is not valid Mapping metadata, defined in %s",
33+
clazzPair, url.toString()));
34+
}
35+
36+
result.put(
37+
(T) ClassUtils.getClass(classNames[0]), (V) ClassUtils.getClass(classNames[1]));
38+
} catch (ClassNotFoundException e) {
39+
throw new RuntimeException(e);
40+
}
41+
});
42+
}
43+
log.debug("Loaded Controller to CustomResource mappings {}", result);
44+
return result;
45+
} catch (IOException e) {
46+
throw new RuntimeException(e);
4847
}
48+
}
4949

50-
private static List<String> retrieveClassNamePairs(URL url) throws IOException {
51-
return new BufferedReader(
52-
new InputStreamReader(url.openStream())
53-
).lines().collect(Collectors.toList());
54-
}
50+
private static List<String> retrieveClassNamePairs(URL url) throws IOException {
51+
return new BufferedReader(new InputStreamReader(url.openStream()))
52+
.lines()
53+
.collect(Collectors.toList());
54+
}
5555
}

operator-framework/src/main/java/io/javaoperatorsdk/operator/ControllerUtils.java

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,76 +4,76 @@
44
import io.fabric8.kubernetes.client.CustomResourceDoneable;
55
import io.javaoperatorsdk.operator.api.Controller;
66
import io.javaoperatorsdk.operator.api.ResourceController;
7-
87
import java.util.Map;
98

10-
119
public class ControllerUtils {
1210

13-
private static final String FINALIZER_NAME_SUFFIX = "/finalizer";
14-
public static final String CONTROLLERS_RESOURCE_PATH = "javaoperatorsdk/controllers";
15-
public static final String DONEABLES_RESOURCE_PATH = "javaoperatorsdk/doneables";
16-
private static Map<Class<? extends ResourceController>, Class<? extends CustomResource>> controllerToCustomResourceMappings;
17-
private static Map<Class<? extends CustomResource>, Class<? extends CustomResourceDoneable>> resourceToDoneableMappings;
11+
private static final String FINALIZER_NAME_SUFFIX = "/finalizer";
12+
public static final String CONTROLLERS_RESOURCE_PATH = "javaoperatorsdk/controllers";
13+
public static final String DONEABLES_RESOURCE_PATH = "javaoperatorsdk/doneables";
14+
private static Map<Class<? extends ResourceController>, Class<? extends CustomResource>>
15+
controllerToCustomResourceMappings;
16+
private static Map<Class<? extends CustomResource>, Class<? extends CustomResourceDoneable>>
17+
resourceToDoneableMappings;
1818

19+
static {
20+
controllerToCustomResourceMappings =
21+
ClassMappingProvider.provide(
22+
CONTROLLERS_RESOURCE_PATH, ResourceController.class, CustomResource.class);
23+
resourceToDoneableMappings =
24+
ClassMappingProvider.provide(
25+
DONEABLES_RESOURCE_PATH, CustomResource.class, CustomResourceDoneable.class);
26+
}
1927

20-
static {
21-
controllerToCustomResourceMappings = ClassMappingProvider
22-
.provide(CONTROLLERS_RESOURCE_PATH,
23-
ResourceController.class,
24-
CustomResource.class);
25-
resourceToDoneableMappings = ClassMappingProvider
26-
.provide(DONEABLES_RESOURCE_PATH,
27-
CustomResource.class,
28-
CustomResourceDoneable.class
29-
);
30-
}
31-
32-
static String getFinalizer(ResourceController controller) {
33-
final String annotationFinalizerName = getAnnotation(controller).finalizerName();
34-
if (!Controller.NULL.equals(annotationFinalizerName)) {
35-
return annotationFinalizerName;
36-
}
37-
return getAnnotation(controller).crdName() + FINALIZER_NAME_SUFFIX;
28+
static String getFinalizer(ResourceController controller) {
29+
final String annotationFinalizerName = getAnnotation(controller).finalizerName();
30+
if (!Controller.NULL.equals(annotationFinalizerName)) {
31+
return annotationFinalizerName;
3832
}
33+
return getAnnotation(controller).crdName() + FINALIZER_NAME_SUFFIX;
34+
}
3935

40-
static boolean getGenerationEventProcessing(ResourceController<?> controller) {
41-
return getAnnotation(controller).generationAwareEventProcessing();
42-
}
36+
static boolean getGenerationEventProcessing(ResourceController<?> controller) {
37+
return getAnnotation(controller).generationAwareEventProcessing();
38+
}
4339

44-
static <R extends CustomResource> Class<R> getCustomResourceClass(ResourceController<R> controller) {
45-
final Class<? extends CustomResource> customResourceClass = controllerToCustomResourceMappings
46-
.get(controller.getClass());
47-
if (customResourceClass == null) {
48-
throw new IllegalArgumentException(
49-
String.format(
50-
"No custom resource has been found for controller %s",
51-
controller.getClass().getCanonicalName()
52-
)
53-
);
54-
}
55-
return (Class<R>) customResourceClass;
40+
static <R extends CustomResource> Class<R> getCustomResourceClass(
41+
ResourceController<R> controller) {
42+
final Class<? extends CustomResource> customResourceClass =
43+
controllerToCustomResourceMappings.get(controller.getClass());
44+
if (customResourceClass == null) {
45+
throw new IllegalArgumentException(
46+
String.format(
47+
"No custom resource has been found for controller %s",
48+
controller.getClass().getCanonicalName()));
5649
}
50+
return (Class<R>) customResourceClass;
51+
}
5752

58-
static String getCrdName(ResourceController controller) {
59-
return getAnnotation(controller).crdName();
60-
}
53+
static String getCrdName(ResourceController controller) {
54+
return getAnnotation(controller).crdName();
55+
}
6156

62-
public static <T extends CustomResource> Class<? extends CustomResourceDoneable<T>>
63-
getCustomResourceDoneableClass(ResourceController<T> controller) {
64-
final Class<T> customResourceClass = getCustomResourceClass(controller);
65-
final Class<? extends CustomResourceDoneable<T>> doneableClass = (Class<? extends CustomResourceDoneable<T>>) resourceToDoneableMappings.get(customResourceClass);
66-
if (doneableClass == null) {
67-
throw new RuntimeException(String.format("No matching Doneable class found for %s", customResourceClass));
68-
}
69-
return doneableClass;
57+
public static <T extends CustomResource>
58+
Class<? extends CustomResourceDoneable<T>> getCustomResourceDoneableClass(
59+
ResourceController<T> controller) {
60+
final Class<T> customResourceClass = getCustomResourceClass(controller);
61+
final Class<? extends CustomResourceDoneable<T>> doneableClass =
62+
(Class<? extends CustomResourceDoneable<T>>)
63+
resourceToDoneableMappings.get(customResourceClass);
64+
if (doneableClass == null) {
65+
throw new RuntimeException(
66+
String.format("No matching Doneable class found for %s", customResourceClass));
7067
}
68+
return doneableClass;
69+
}
7170

72-
private static Controller getAnnotation(ResourceController<?> controller) {
73-
return controller.getClass().getAnnotation(Controller.class);
74-
}
71+
private static Controller getAnnotation(ResourceController<?> controller) {
72+
return controller.getClass().getAnnotation(Controller.class);
73+
}
7574

76-
public static boolean hasGivenFinalizer(CustomResource resource, String finalizer) {
77-
return resource.getMetadata().getFinalizers() != null && resource.getMetadata().getFinalizers().contains(finalizer);
78-
}
75+
public static boolean hasGivenFinalizer(CustomResource resource, String finalizer) {
76+
return resource.getMetadata().getFinalizers() != null
77+
&& resource.getMetadata().getFinalizers().contains(finalizer);
78+
}
7979
}
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,70 @@
11
package io.javaoperatorsdk.operator.processing.annotation;
22

3-
import javax.annotation.processing.ProcessingEnvironment;
4-
import javax.tools.StandardLocation;
53
import java.io.BufferedReader;
64
import java.io.IOException;
75
import java.io.InputStreamReader;
86
import java.io.PrintWriter;
97
import java.util.Map;
108
import java.util.concurrent.ConcurrentHashMap;
119
import java.util.stream.Collectors;
10+
import javax.annotation.processing.ProcessingEnvironment;
11+
import javax.tools.StandardLocation;
1212

1313
class AccumulativeMappingWriter {
14-
private Map<String, String> mappings = new ConcurrentHashMap<>();
15-
private final String resourcePath;
16-
private final ProcessingEnvironment processingEnvironment;
14+
private Map<String, String> mappings = new ConcurrentHashMap<>();
15+
private final String resourcePath;
16+
private final ProcessingEnvironment processingEnvironment;
1717

18-
public AccumulativeMappingWriter(String resourcePath, ProcessingEnvironment processingEnvironment) {
19-
this.resourcePath = resourcePath;
20-
this.processingEnvironment = processingEnvironment;
21-
}
22-
23-
public AccumulativeMappingWriter loadExistingMappings() {
24-
try {
25-
final var readonlyResource = processingEnvironment
26-
.getFiler()
27-
.getResource(StandardLocation.CLASS_OUTPUT, "", resourcePath);
18+
public AccumulativeMappingWriter(
19+
String resourcePath, ProcessingEnvironment processingEnvironment) {
20+
this.resourcePath = resourcePath;
21+
this.processingEnvironment = processingEnvironment;
22+
}
2823

29-
final var bufferedReader = new BufferedReader(new InputStreamReader(readonlyResource.openInputStream()));
30-
final var existingLines = bufferedReader
31-
.lines()
32-
.map(l -> l.split(","))
33-
.collect(Collectors.toMap(parts -> parts[0], parts -> parts[1]));
34-
mappings.putAll(existingLines);
35-
} catch (IOException e) {
36-
}
37-
return this;
38-
}
24+
public AccumulativeMappingWriter loadExistingMappings() {
25+
try {
26+
final var readonlyResource =
27+
processingEnvironment
28+
.getFiler()
29+
.getResource(StandardLocation.CLASS_OUTPUT, "", resourcePath);
3930

40-
public AccumulativeMappingWriter add(String key, String value) {
41-
this.mappings.put(key, value);
42-
return this;
31+
final var bufferedReader =
32+
new BufferedReader(new InputStreamReader(readonlyResource.openInputStream()));
33+
final var existingLines =
34+
bufferedReader
35+
.lines()
36+
.map(l -> l.split(","))
37+
.collect(Collectors.toMap(parts -> parts[0], parts -> parts[1]));
38+
mappings.putAll(existingLines);
39+
} catch (IOException e) {
4340
}
41+
return this;
42+
}
4443

45-
public void flush() {
46-
PrintWriter printWriter = null;
47-
try {
48-
final var resource = processingEnvironment
49-
.getFiler()
50-
.createResource(StandardLocation.CLASS_OUTPUT, "", resourcePath);
51-
printWriter = new PrintWriter(resource.openOutputStream());
44+
public AccumulativeMappingWriter add(String key, String value) {
45+
this.mappings.put(key, value);
46+
return this;
47+
}
5248

49+
public void flush() {
50+
PrintWriter printWriter = null;
51+
try {
52+
final var resource =
53+
processingEnvironment
54+
.getFiler()
55+
.createResource(StandardLocation.CLASS_OUTPUT, "", resourcePath);
56+
printWriter = new PrintWriter(resource.openOutputStream());
5357

54-
for (Map.Entry<String, String> entry : mappings.entrySet()) {
55-
printWriter.println(entry.getKey() + "," + entry.getValue());
56-
}
57-
} catch (IOException e) {
58-
e.printStackTrace();
59-
throw new RuntimeException(e);
60-
} finally {
61-
if (printWriter != null) {
62-
printWriter.close();
63-
}
64-
}
58+
for (Map.Entry<String, String> entry : mappings.entrySet()) {
59+
printWriter.println(entry.getKey() + "," + entry.getValue());
60+
}
61+
} catch (IOException e) {
62+
e.printStackTrace();
63+
throw new RuntimeException(e);
64+
} finally {
65+
if (printWriter != null) {
66+
printWriter.close();
67+
}
6568
}
69+
}
6670
}

0 commit comments

Comments
 (0)