Skip to content

Commit 5046966

Browse files
committed
add debugging and improve reporting
1 parent c263938 commit 5046966

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

operator/src/main/java/oracle/kubernetes/operator/helpers/AnnotationHelper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
/** Annotates pods, services with details about the Domain instance and checks these annotations. */
1414
public class AnnotationHelper {
15+
private static final boolean DEBUG = true;
1516
static final String SHA256_ANNOTATION = "weblogic.sha256";
1617
private static Function<V1Pod, String> HASH_FUNCTION =
1718
pod -> DigestUtils.sha256Hex(Yaml.dump(pod));
@@ -32,7 +33,9 @@ static void annotateForPrometheus(V1ObjectMeta meta, int httpPort) {
3233
}
3334

3435
static V1Pod withSha256Hash(V1Pod pod) {
36+
String dump = Yaml.dump(pod);
3537
pod.getMetadata().putAnnotationsItem(SHA256_ANNOTATION, HASH_FUNCTION.apply(pod));
38+
if (DEBUG) pod.getMetadata().putAnnotationsItem("hashedString", dump);
3639
return pod;
3740
}
3841

operator/src/main/java/oracle/kubernetes/operator/helpers/PodCompatibility.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import io.kubernetes.client.models.V1PodSpec;
1919
import io.kubernetes.client.models.V1Probe;
2020
import io.kubernetes.client.models.V1ResourceRequirements;
21+
import java.lang.reflect.InvocationTargetException;
22+
import java.lang.reflect.Method;
2123
import java.util.ArrayList;
2224
import java.util.Collection;
2325
import java.util.Collections;
@@ -311,7 +313,7 @@ public String getIncompatibility() {
311313
}
312314

313315
<T> void addSets(String description, List<T> expected, List<T> actual) {
314-
add(new CompatibleSets<>(description, expected, actual));
316+
add(CheckFactory.create(description, expected, actual));
315317
}
316318

317319
protected <T> void add(String description, T expected, T actual) {
@@ -349,6 +351,39 @@ public String getIncompatibility() {
349351
}
350352
}
351353

354+
class CheckFactory {
355+
static <T> CompatibilityCheck create(String description, List<T> expected, List<T> actual) {
356+
if (canBeMap(expected) && canBeMap(actual))
357+
return new CompatibleMaps<>(description, asMap(expected), asMap(actual));
358+
else return new CompatibleSets<>(description, expected, actual);
359+
}
360+
361+
private static <T> boolean canBeMap(List<T> list) {
362+
return asMap(list) != null;
363+
}
364+
365+
private static <T> Map<String, T> asMap(List<T> values) {
366+
if (values == null) return null;
367+
Map<String, T> result = new HashMap<>();
368+
for (T value : values) {
369+
String key = getKey(value);
370+
if (key == null) return null;
371+
result.put(key, value);
372+
}
373+
374+
return result;
375+
}
376+
377+
private static <T> String getKey(T value) {
378+
try {
379+
Method getKey = value.getClass().getDeclaredMethod("getName");
380+
return (String) getKey.invoke(value);
381+
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
382+
return null;
383+
}
384+
}
385+
}
386+
352387
class CompatibleSets<T> implements CompatibilityCheck {
353388
private String description;
354389
private final Collection<T> expected;
@@ -398,13 +433,13 @@ public String getIncompatibility() {
398433

399434
Set<K> missingKeys = getMissingElements(expected.keySet(), actual.keySet());
400435
if (!missingKeys.isEmpty())
401-
sb.append(String.format("%s has no entry for %s%n", description, missingKeys));
436+
sb.append(String.format("actual %s has no entry for '%s'%n", description, missingKeys));
402437

403438
for (K key : expected.keySet())
404439
if (actual.containsKey(key) && !Objects.equals(expected.get(key), actual.get(key)))
405440
sb.append(
406441
String.format(
407-
"%s has entry %s with value %s rather than %s%n",
442+
"actual %s has entry '%s' with value '%s' rather than '%s'%n",
408443
description, key, actual.get(key), expected.get(key)));
409444

410445
return sb.toString();

operator/src/test/java/oracle/kubernetes/operator/helpers/PodCompatibilityTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import io.kubernetes.client.models.V1ContainerPort;
1818
import io.kubernetes.client.models.V1Probe;
1919
import io.kubernetes.client.models.V1ResourceRequirements;
20+
import java.util.Arrays;
21+
import java.util.Objects;
22+
import org.apache.commons.lang3.builder.HashCodeBuilder;
2023
import org.junit.Test;
2124

2225
public class PodCompatibilityTest {
@@ -102,6 +105,55 @@ public void whenExpectedNotSubsetOfActual_reportMissingElements() {
102105
assertThat(check.getIncompatibility(), not(containsString("gamma")));
103106
}
104107

108+
@Test
109+
public void whenCanBeMapsAndExpectedAndActualDifferentValues_reportChangedElements() {
110+
CompatibilityCheck check =
111+
CheckFactory.create(
112+
"letters",
113+
Arrays.asList(object("alpha", 1), object("beta", 2), object("gamma", 3)),
114+
Arrays.asList(object("beta", 222), object("gamma", 3), object("alpha", 1)));
115+
116+
assertThat(check.getIncompatibility(), both(containsString("beta")).and(containsString("222")));
117+
assertThat(check.getIncompatibility(), not(containsString("alpha")));
118+
}
119+
120+
private Object object(String name, int value) {
121+
return new ObjectWithName(name, value);
122+
}
123+
124+
static class ObjectWithName {
125+
private String name;
126+
private int value;
127+
128+
ObjectWithName(String name, int value) {
129+
this.name = name;
130+
this.value = value;
131+
}
132+
133+
public String getName() {
134+
return name;
135+
}
136+
137+
@Override
138+
public boolean equals(Object o) {
139+
return (o instanceof ObjectWithName) && equals((ObjectWithName) o);
140+
}
141+
142+
private boolean equals(ObjectWithName that) {
143+
return value == that.value && Objects.equals(name, that.name);
144+
}
145+
146+
@Override
147+
public int hashCode() {
148+
return new HashCodeBuilder(17, 37).append(name).append(value).toHashCode();
149+
}
150+
151+
@Override
152+
public String toString() {
153+
return String.format("<%s = %d>", name, value);
154+
}
155+
}
156+
105157
@Test
106158
public void whenExpectedSubmapOfActual_reportCompatible() {
107159
CompatibilityCheck check =

0 commit comments

Comments
 (0)