Skip to content

Commit 3f1b75d

Browse files
committed
simplify implementation of 'isNewer'
1 parent ae5859a commit 3f1b75d

File tree

2 files changed

+55
-46
lines changed

2 files changed

+55
-46
lines changed

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

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import io.kubernetes.client.models.V1ObjectMeta;
88
import java.lang.reflect.Field;
9-
import java.util.HashMap;
109
import java.util.Map;
1110
import java.util.Objects;
1211
import javax.json.JsonPatchBuilder;
@@ -26,45 +25,6 @@ static <K, V> boolean mapEquals(Map<K, V> first, Map<K, V> second) {
2625
return Objects.equals(first, second) || (MapUtils.isEmpty(first) && MapUtils.isEmpty(second));
2726
}
2827

29-
/**
30-
* Returns true if the labels on the current artifact metadata match those on the build version.
31-
* This excludes any weblogic-specific labels, as identified by the existence of the weblogic.
32-
* prefix.
33-
*
34-
* @param build the desired version of the metadata
35-
* @param current the current version of the metadata
36-
* @return true if the labels match
37-
*/
38-
static boolean areLabelsValid(V1ObjectMeta build, V1ObjectMeta current) {
39-
return mapEquals(getCustomerLabels(current), getCustomerLabels(build));
40-
}
41-
42-
private static Map<String, String> getCustomerLabels(V1ObjectMeta metadata) {
43-
Map<String, String> result = new HashMap<>();
44-
for (Map.Entry<String, String> entry : metadata.getLabels().entrySet()) {
45-
if (!isOperatorLabel(entry)) {
46-
result.put(entry.getKey(), entry.getValue());
47-
}
48-
}
49-
return result;
50-
}
51-
52-
private static boolean isOperatorLabel(Map.Entry<String, String> label) {
53-
return label.getKey().startsWith("weblogic.");
54-
}
55-
56-
/**
57-
* Returns true if the annotations on the current artifact metadata match those on the build
58-
* version.
59-
*
60-
* @param build the desired version of the metadata
61-
* @param current the current version of the metadata
62-
* @return true if the annotations match
63-
*/
64-
static boolean areAnnotationsValid(V1ObjectMeta build, V1ObjectMeta current) {
65-
return mapEquals(current.getAnnotations(), build.getAnnotations());
66-
}
67-
6828
/**
6929
* Returns true if the current map is missing values from the required map. This method is
7030
* typically used to compare labels and annotations against specifications derived from the
@@ -100,7 +60,7 @@ private static boolean hasAllRequiredNames(Map<String, ?> current, Map<String, ?
10060
* @param current a map of the values found in a Kubernetes resource
10161
* @param required a map of the values specified for the resource by the domain
10262
*/
103-
public static void addPatches(
63+
static void addPatches(
10464
JsonPatchBuilder patchBuilder,
10565
String basePath,
10666
Map<String, String> current,
@@ -144,7 +104,9 @@ static V1ObjectMeta getResourceMetadata(Object resource) {
144104
}
145105

146106
/**
147-
* Returns true if the first metadata indicates a newer resource than does the second.
107+
* Returns true if the first metadata indicates a newer resource than does the second. 'Newer'
108+
* indicates that the creation time is later. If two items have the same creation time, a higher
109+
* resource version indicates the newer resource.
148110
*
149111
* @param first the first item to compare
150112
* @param second the second item to compare
@@ -157,11 +119,16 @@ public static boolean isFirstNewer(V1ObjectMeta first, V1ObjectMeta second) {
157119
DateTime time1 = first.getCreationTimestamp();
158120
DateTime time2 = second.getCreationTimestamp();
159121

160-
if (time2.isAfter(time1)) {
122+
if (time1.isAfter(time2)) {
123+
return true;
124+
} else if (time2.isAfter(time1)) {
161125
return false;
126+
} else {
127+
return getResourceVersion(first) > getResourceVersion(second);
162128
}
163-
return Integer.parseInt(second.getResourceVersion())
164-
< Integer.parseInt(first.getResourceVersion())
165-
|| time2.isBefore(time1);
129+
}
130+
131+
private static int getResourceVersion(V1ObjectMeta metadata) {
132+
return Integer.parseInt(metadata.getResourceVersion());
166133
}
167134
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2019, Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at
3+
// http://oss.oracle.com/licenses/upl.
4+
5+
package oracle.kubernetes.operator.helpers;
6+
7+
import static org.hamcrest.Matchers.is;
8+
import static org.junit.Assert.*;
9+
10+
import io.kubernetes.client.models.V1ObjectMeta;
11+
import org.joda.time.DateTime;
12+
import org.junit.Test;
13+
14+
public class KubernetesUtilsTest {
15+
16+
@Test
17+
public void whenCreationTimesDiffer_metadataWithLaterTimeIsNewer() {
18+
V1ObjectMeta meta1 = new V1ObjectMeta().creationTimestamp(new DateTime(2)).resourceVersion("2");
19+
V1ObjectMeta meta2 = new V1ObjectMeta().creationTimestamp(new DateTime(1)).resourceVersion("1");
20+
21+
assertThat(KubernetesUtils.isFirstNewer(meta1, meta2), is(true));
22+
assertThat(KubernetesUtils.isFirstNewer(meta2, meta1), is(false));
23+
}
24+
25+
@Test
26+
public void whenCreationTimesMatch_metadataWithHigherResourceVersionIsNewer() {
27+
V1ObjectMeta meta1 = new V1ObjectMeta().creationTimestamp(new DateTime(1)).resourceVersion("2");
28+
V1ObjectMeta meta2 = new V1ObjectMeta().creationTimestamp(new DateTime(1)).resourceVersion("1");
29+
30+
assertThat(KubernetesUtils.isFirstNewer(meta1, meta2), is(true));
31+
assertThat(KubernetesUtils.isFirstNewer(meta2, meta1), is(false));
32+
}
33+
34+
@Test
35+
public void whenCreationTimesAndResourceVersionsMatch_neitherIsNewer() {
36+
V1ObjectMeta meta1 = new V1ObjectMeta().creationTimestamp(new DateTime(1)).resourceVersion("2");
37+
V1ObjectMeta meta2 = new V1ObjectMeta().creationTimestamp(new DateTime(1)).resourceVersion("2");
38+
39+
assertThat(KubernetesUtils.isFirstNewer(meta2, meta1), is(false));
40+
assertThat(KubernetesUtils.isFirstNewer(meta1, meta2), is(false));
41+
}
42+
}

0 commit comments

Comments
 (0)