Skip to content

Commit 3a67dff

Browse files
committed
Add support for pod changes due to node selectors and resource limits/requests
1 parent c4b4c20 commit 3a67dff

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static oracle.kubernetes.operator.VersionConstants.DEFAULT_DOMAIN_VERSION;
99

1010
import io.kubernetes.client.custom.IntOrString;
11+
import io.kubernetes.client.custom.Quantity;
1112
import io.kubernetes.client.models.*;
1213
import java.io.File;
1314
import java.util.*;
@@ -23,6 +24,7 @@
2324
import oracle.kubernetes.operator.work.Step;
2425
import oracle.kubernetes.weblogic.domain.v2.Domain;
2526
import oracle.kubernetes.weblogic.domain.v2.ServerSpec;
27+
import org.apache.commons.collections.MapUtils;
2628
import org.apache.commons.lang3.builder.EqualsBuilder;
2729

2830
@SuppressWarnings("deprecation")
@@ -279,19 +281,23 @@ private static boolean isCurrentPodValid(V1Pod build, V1Pod current) {
279281
private static boolean isCurrentPodMetadataValid(V1ObjectMeta build, V1ObjectMeta current) {
280282
return VersionHelper.matchesResourceVersion(current, DEFAULT_DOMAIN_VERSION)
281283
&& isRestartVersionValid(build, current)
282-
&& Objects.equals(getCustomerLabels(current), getCustomerLabels(build))
283-
&& Objects.equals(current.getAnnotations(), build.getAnnotations());
284+
&& mapEquals(getCustomerLabels(current), getCustomerLabels(build))
285+
&& mapEquals(current.getAnnotations(), build.getAnnotations());
284286
}
285287

286288
private static boolean isCurrentPodSpecValid(
287289
V1PodSpec build, V1PodSpec current, List<String> ignoring) {
288290
return Objects.equals(current.getSecurityContext(), build.getSecurityContext())
289-
// && Objects.equals(current.getNodeSelector(), build.getNodeSelector())
291+
&& mapEquals(current.getNodeSelector(), build.getNodeSelector())
290292
&& equalSets(volumesWithout(current.getVolumes(), ignoring), build.getVolumes())
291293
&& equalSets(current.getImagePullSecrets(), build.getImagePullSecrets())
292294
&& areCompatible(build.getContainers(), current.getContainers(), ignoring);
293295
}
294296

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+
295301
private static boolean areCompatible(
296302
List<V1Container> build, List<V1Container> current, List<String> ignoring) {
297303
if (build != null) {
@@ -323,6 +329,7 @@ private static boolean isCompatible(
323329
&& Objects.equals(current.getSecurityContext(), build.getSecurityContext())
324330
&& equalSettings(current.getLivenessProbe(), build.getLivenessProbe())
325331
&& equalSettings(current.getReadinessProbe(), build.getReadinessProbe())
332+
&& resourcesEqual(current.getResources(), build.getResources())
326333
&& equalSets(mountsWithout(current.getVolumeMounts(), ignoring), build.getVolumeMounts())
327334
&& equalSets(current.getPorts(), build.getPorts())
328335
&& equalSets(current.getEnv(), build.getEnv())
@@ -335,6 +342,18 @@ private static boolean equalSettings(V1Probe probe1, V1Probe probe2) {
335342
&& Objects.equals(probe1.getPeriodSeconds(), probe2.getPeriodSeconds());
336343
}
337344

345+
private static boolean resourcesEqual(V1ResourceRequirements a, V1ResourceRequirements b) {
346+
return mapEquals(getLimits(a), getLimits(b)) && mapEquals(getRequests(a), getRequests(b));
347+
}
348+
349+
private static Map<String, Quantity> getLimits(V1ResourceRequirements requirements) {
350+
return requirements == null ? Collections.emptyMap() : requirements.getLimits();
351+
}
352+
353+
private static Map<String, Quantity> getRequests(V1ResourceRequirements requirements) {
354+
return requirements == null ? Collections.emptyMap() : requirements.getRequests();
355+
}
356+
338357
private static List<V1Volume> volumesWithout(
339358
List<V1Volume> volumeMounts, List<String> volumesToIgnore) {
340359
List<V1Volume> result = new ArrayList<>(volumeMounts);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ protected void verifyReplacePodWhen(PodMutator mutator) {
7676
assertThat(logRecords, containsInfo(getPodReplacedMessageKey()));
7777
}
7878

79-
private void verifyAdminPodNotReplacedWhen(PodMutator mutator) {
79+
@Override
80+
protected void verifyPodNotReplacedWhen(PodMutator mutator) {
8081
testSupport.addComponent(
8182
ProcessingConstants.PODWATCHER_COMPONENT_NAME,
8283
PodAwaiterStepFactory.class,
@@ -162,7 +163,7 @@ public void whenExistingAdminPodSpecHasUnknownAddedVolumes_replaceIt() {
162163

163164
@Test
164165
public void whenExistingAdminPodSpecHasK8sVolume_ignoreIt() {
165-
verifyAdminPodNotReplacedWhen(
166+
verifyPodNotReplacedWhen(
166167
(pod) -> {
167168
pod.getSpec().addVolumesItem(new V1Volume().name("k8s"));
168169
getSpecContainer(pod)
@@ -202,7 +203,7 @@ public void whenExistingAdminPodSpecHasUnneededVolumeMount_replaceIt() {
202203

203204
@Test
204205
public void whenExistingAdminPodSpecHasK8sVolumeMount_ignoreIt() {
205-
verifyAdminPodNotReplacedWhen(
206+
verifyPodNotReplacedWhen(
206207
(pod) ->
207208
getSpecContainer(pod)
208209
.addVolumeMountsItem(

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public void whenExistingManagedPodSpecHasSuperfluousVolume_replaceIt() {
154154

155155
@Test
156156
public void whenExistingManagedPodSpecHasK8sVolume_ignoreIt() {
157-
verifyManagedPodNotRolledWhen(
157+
verifyPodNotReplacedWhen(
158158
(pod) -> {
159159
pod.getSpec().addVolumesItem(new V1Volume().name("k8s"));
160160
getSpecContainer(pod)
@@ -189,7 +189,7 @@ public void whenExistingManagedPodSpecHasExtraVolumeMount_replaceIt() {
189189

190190
@Test
191191
public void whenExistingManagedPodSpecHasK8sVolumeMount_ignoreIt() {
192-
verifyManagedPodNotRolledWhen(
192+
verifyPodNotReplacedWhen(
193193
(pod) ->
194194
getSpecContainer(pod)
195195
.addVolumeMountsItem(
@@ -532,7 +532,8 @@ private Map<String, StepAndPacket> computePodsToRoll(PodMutator mutator) {
532532
return rolling;
533533
}
534534

535-
private void verifyManagedPodNotRolledWhen(PodMutator mutator) {
535+
@Override
536+
protected void verifyPodNotReplacedWhen(PodMutator mutator) {
536537
Map<String, StepAndPacket> rolling = computePodsToRoll(mutator);
537538

538539
assertThat(rolling, is(anEmptyMap()));

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ private ServerConfigurator configureDomainV2Server() {
305305

306306
protected abstract void verifyReplacePodWhen(PodMutator mutator);
307307

308+
protected abstract void verifyPodNotReplacedWhen(PodMutator mutator);
309+
308310
protected abstract ServerConfigurator getServerConfigurator(
309311
DomainConfigurator configurator, String serverName);
310312

@@ -465,12 +467,16 @@ public void whenPodSecurityContextIsDifferent_replaceIt() {
465467
}
466468

467469
@Test
468-
@Ignore
469-
public void whenPodHasDifferentNodeSelector_dontReplaceIt() {
470+
public void whenPodHasDifferentNodeSelector_replaceIt() {
470471
configurator.withNodeSelector("key", "value");
471472
verifyReplacePodWhen(pod -> {});
472473
}
473474

475+
@Test
476+
public void whenNullVsEmptyNodeSelector_dontReplaceIt() {
477+
verifyPodNotReplacedWhen(pod -> pod.getSpec().setNodeSelector(null));
478+
}
479+
474480
@Test
475481
public void whenPodContainerSecurityContextIsDifferent_replaceIt() {
476482
configurator.withContainerSecurityContext(new V1SecurityContext().runAsGroup(9876L));
@@ -489,6 +495,23 @@ public void whenPodReadinessProbeSettingsAreDifferent_replaceIt() {
489495
verifyReplacePodWhen(pod -> {});
490496
}
491497

498+
@Test
499+
public void whenPodRequestRequirementIsDifferent_replaceIt() {
500+
configurator.withRequestRequirement("resource", "5");
501+
verifyReplacePodWhen(pod -> {});
502+
}
503+
504+
@Test
505+
public void whenPodRequestRequirementsEmptyVsNull_dontReplaceIt() {
506+
verifyPodNotReplacedWhen(pod -> pod.getSpec().getContainers().get(0).resources(null));
507+
}
508+
509+
@Test
510+
public void whenPodLimitRequirementIsDifferent_replaceIt() {
511+
configurator.withLimitRequirement("limit", "7");
512+
verifyReplacePodWhen(pod -> {});
513+
}
514+
492515
protected void onAdminExpectListPersistentVolume() {
493516
// default is no-op
494517
}

0 commit comments

Comments
 (0)