Skip to content

Commit 87c3112

Browse files
committed
extract label/annotation utilities for reuse
1 parent 3a67dff commit 87c3112

File tree

3 files changed

+75
-22
lines changed

3 files changed

+75
-22
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2018, 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 io.kubernetes.client.models.V1ObjectMeta;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
import java.util.Objects;
11+
import org.apache.commons.collections.MapUtils;
12+
13+
class KubernetesUtils {
14+
15+
/**
16+
* Returns true if the two maps of values match. A null map is considered to match an empty map.
17+
*
18+
* @param first the first map to compare
19+
* @param second the second map to compare
20+
* @return true if the maps match.
21+
*/
22+
static <K, V> boolean mapEquals(Map<K, V> first, Map<K, V> second) {
23+
return Objects.equals(first, second) || (MapUtils.isEmpty(first) && MapUtils.isEmpty(second));
24+
}
25+
26+
/**
27+
* Returns true if the labels on the current artifact metadata match those on the build version.
28+
* This excludes any weblogic-specific labels, as identified by the existence of the weblogic.
29+
* prefix.
30+
*
31+
* @param build the desired version of the metadata
32+
* @param current the current version of the metadata
33+
* @return true if the labels match
34+
*/
35+
static boolean areLabelsValid(V1ObjectMeta build, V1ObjectMeta current) {
36+
return mapEquals(getCustomerLabels(current), getCustomerLabels(build));
37+
}
38+
39+
private static Map<String, String> getCustomerLabels(V1ObjectMeta metadata) {
40+
Map<String, String> result = new HashMap<>();
41+
for (Map.Entry<String, String> entry : metadata.getLabels().entrySet())
42+
if (!isOperatorLabel(entry)) result.put(entry.getKey(), entry.getValue());
43+
return result;
44+
}
45+
46+
private static boolean isOperatorLabel(Map.Entry<String, String> label) {
47+
return label.getKey().startsWith("weblogic.");
48+
}
49+
50+
/**
51+
* Returns true if the annotations on the current artifact metadata match those on the build
52+
* version.
53+
*
54+
* @param build the desired version of the metadata
55+
* @param current the current version of the metadata
56+
* @return true if the annotations match
57+
*/
58+
static boolean areAnnotationsValid(V1ObjectMeta build, V1ObjectMeta current) {
59+
return mapEquals(current.getAnnotations(), build.getAnnotations());
60+
}
61+
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44

55
package oracle.kubernetes.operator.helpers;
66

7-
import static oracle.kubernetes.operator.KubernetesConstants.*;
8-
import static oracle.kubernetes.operator.helpers.StepContextConstants.*;
7+
import static oracle.kubernetes.operator.KubernetesConstants.DOMAIN_CONFIG_MAP_NAME;
8+
import static oracle.kubernetes.operator.KubernetesConstants.DOMAIN_DEBUG_CONFIG_MAP_SUFFIX;
9+
import static oracle.kubernetes.operator.KubernetesConstants.INTROSPECTOR_CONFIG_MAP_NAME_SUFFIX;
10+
import static oracle.kubernetes.operator.helpers.StepContextConstants.ALL_READ_AND_EXECUTE;
11+
import static oracle.kubernetes.operator.helpers.StepContextConstants.DEBUG_CM_MOUNTS_PATH;
12+
import static oracle.kubernetes.operator.helpers.StepContextConstants.DEBUG_CM_VOLUME;
13+
import static oracle.kubernetes.operator.helpers.StepContextConstants.SCRIPTS_MOUNTS_PATH;
14+
import static oracle.kubernetes.operator.helpers.StepContextConstants.SCRIPTS_VOLUME;
15+
import static oracle.kubernetes.operator.helpers.StepContextConstants.SIT_CONFIG_MAP_VOLUME_SUFFIX;
916

1017
import io.kubernetes.client.models.V1ConfigMapVolumeSource;
1118
import io.kubernetes.client.models.V1Volume;

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

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import oracle.kubernetes.operator.work.Step;
2525
import oracle.kubernetes.weblogic.domain.v2.Domain;
2626
import oracle.kubernetes.weblogic.domain.v2.ServerSpec;
27-
import org.apache.commons.collections.MapUtils;
2827
import org.apache.commons.lang3.builder.EqualsBuilder;
2928

3029
@SuppressWarnings("deprecation")
@@ -281,23 +280,19 @@ private static boolean isCurrentPodValid(V1Pod build, V1Pod current) {
281280
private static boolean isCurrentPodMetadataValid(V1ObjectMeta build, V1ObjectMeta current) {
282281
return VersionHelper.matchesResourceVersion(current, DEFAULT_DOMAIN_VERSION)
283282
&& isRestartVersionValid(build, current)
284-
&& mapEquals(getCustomerLabels(current), getCustomerLabels(build))
285-
&& mapEquals(current.getAnnotations(), build.getAnnotations());
283+
&& KubernetesUtils.areLabelsValid(build, current)
284+
&& KubernetesUtils.areAnnotationsValid(build, current);
286285
}
287286

288287
private static boolean isCurrentPodSpecValid(
289288
V1PodSpec build, V1PodSpec current, List<String> ignoring) {
290289
return Objects.equals(current.getSecurityContext(), build.getSecurityContext())
291-
&& mapEquals(current.getNodeSelector(), build.getNodeSelector())
290+
&& KubernetesUtils.mapEquals(current.getNodeSelector(), build.getNodeSelector())
292291
&& equalSets(volumesWithout(current.getVolumes(), ignoring), build.getVolumes())
293292
&& equalSets(current.getImagePullSecrets(), build.getImagePullSecrets())
294293
&& areCompatible(build.getContainers(), current.getContainers(), ignoring);
295294
}
296295

297-
private static <K, V> boolean mapEquals(Map<K, V> first, Map<K, V> second) {
298-
return Objects.equals(first, second) || (MapUtils.isEmpty(first) && MapUtils.isEmpty(second));
299-
}
300-
301296
private static boolean areCompatible(
302297
List<V1Container> build, List<V1Container> current, List<String> ignoring) {
303298
if (build != null) {
@@ -343,7 +338,8 @@ private static boolean equalSettings(V1Probe probe1, V1Probe probe2) {
343338
}
344339

345340
private static boolean resourcesEqual(V1ResourceRequirements a, V1ResourceRequirements b) {
346-
return mapEquals(getLimits(a), getLimits(b)) && mapEquals(getRequests(a), getRequests(b));
341+
return KubernetesUtils.mapEquals(getLimits(a), getLimits(b))
342+
&& KubernetesUtils.mapEquals(getRequests(a), getRequests(b));
347343
}
348344

349345
private static Map<String, Quantity> getLimits(V1ResourceRequirements requirements) {
@@ -390,17 +386,6 @@ private static List<V1VolumeMount> getVolumeMounts(V1Container container) {
390386
return Optional.ofNullable(container.getVolumeMounts()).orElse(Collections.emptyList());
391387
}
392388

393-
private static Map<String, String> getCustomerLabels(V1ObjectMeta metadata) {
394-
Map<String, String> result = new HashMap<>();
395-
for (Map.Entry<String, String> entry : metadata.getLabels().entrySet())
396-
if (!isOperatorLabel(entry)) result.put(entry.getKey(), entry.getValue());
397-
return result;
398-
}
399-
400-
private static boolean isOperatorLabel(Map.Entry<String, String> label) {
401-
return label.getKey().startsWith("weblogic.");
402-
}
403-
404389
private static boolean isRestartVersionValid(V1ObjectMeta build, V1ObjectMeta current) {
405390
return isLabelSame(build, current, LabelConstants.DOMAINRESTARTVERSION_LABEL)
406391
&& isLabelSame(build, current, LabelConstants.CLUSTERRESTARTVERSION_LABEL)

0 commit comments

Comments
 (0)