Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ private static void sanitizeQuantities(
"resources",
quantityPath))
.map(Map.class::cast)
.filter(m -> m.size() == desiredResource.size())
.ifPresent(
m ->
actualResource.forEach(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,6 @@ void testSanitizePodTemplateSpec_whenResourceIsNull_doNothing() {
verifyNoInteractions(actualMap);
}

@Test
void testSanitizeResourceRequirements_whenResourceSizeMismatch_doNothing() {
final var actualMap =
sanitizeRequestsAndLimits(
ContainerType.CONTAINER,
Map.of("cpu", new Quantity("2")),
Map.of(),
Map.of("cpu", new Quantity("4")),
Map.of("cpu", new Quantity("4"), "memory", new Quantity("4Gi")));
assertContainerResources(actualMap, "requests").hasSize(1).containsEntry("cpu", "2");
assertContainerResources(actualMap, "limits").hasSize(1).containsEntry("cpu", "4");
}

@Test
void testSanitizeResourceRequirements_whenResourceKeyMismatch_doNothing() {
final var actualMap =
Expand Down Expand Up @@ -187,6 +174,34 @@ void testSanitizePodTemplateSpec_whenResourcesHaveNumericalAmountMismatch_doNoth
assertInitContainerResources(actualMap, "limits").hasSize(1).containsEntry("cpu", "2");
}

@Test
void
testSanitizePodTemplateSpec_whenResourcesHaveNumericalAmountMismatch_withEphemeralStorageAddedByOtherOperator_doNothing() {
// mimics an environment like GKE Autopilot that enforces ephemeral-storage requests and limits
final var actualMap =
sanitizeRequestsAndLimits(
ContainerType.INIT_CONTAINER,
Map.of(
"cpu",
new Quantity("2"),
"memory",
new Quantity("4Gi"),
"ephemeral-storage",
new Quantity("1Gi")),
Map.of("cpu", new Quantity("4"), "memory", new Quantity("4Ti")),
Map.of("cpu", new Quantity("2"), "ephemeral-storage", new Quantity("1Gi")),
Map.of("cpu", new Quantity("4000m")));
assertInitContainerResources(actualMap, "requests")
.hasSize(3)
.containsEntry("cpu", "2")
.containsEntry("memory", "4Gi")
.containsEntry("ephemeral-storage", "1Gi");
assertInitContainerResources(actualMap, "limits")
.hasSize(2)
.containsEntry("cpu", "2")
.containsEntry("ephemeral-storage", "1Gi");
}

@Test
void
testSanitizeResourceRequirements_whenResourcesHaveAmountAndFormatMismatchWithSameNumericalAmount_thenSanitizeActualMap() {
Expand All @@ -204,6 +219,34 @@ void testSanitizePodTemplateSpec_whenResourcesHaveNumericalAmountMismatch_doNoth
assertContainerResources(actualMap, "limits").hasSize(1).containsEntry("cpu", "4000m");
}

@Test
void
testSanitizeResourceRequirements_whenResourcesHaveAmountAndFormatMismatchWithSameNumericalAmount_withEphemeralStorageAddedByOtherOperator_thenSanitizeActualMap() {
// mimics an environment like GKE Autopilot that enforces ephemeral-storage requests and limits
final var actualMap =
sanitizeRequestsAndLimits(
ContainerType.CONTAINER,
Map.of(
"cpu",
new Quantity("2"),
"memory",
new Quantity("4Gi"),
"ephemeral-storage",
new Quantity("1Gi")),
Map.of("cpu", new Quantity("2000m"), "memory", new Quantity("4096Mi")),
Map.of("cpu", new Quantity("4"), "ephemeral-storage", new Quantity("1Gi")),
Map.of("cpu", new Quantity("4000m")));
assertContainerResources(actualMap, "requests")
.hasSize(3)
.containsEntry("cpu", "2000m")
.containsEntry("memory", "4096Mi")
.containsEntry("ephemeral-storage", "1Gi");
assertContainerResources(actualMap, "limits")
.hasSize(2)
.containsEntry("cpu", "4000m")
.containsEntry("ephemeral-storage", "1Gi");
}

@Test
void testSanitizePodTemplateSpec_whenEnvVarsIsEmpty_doNothing() {
final var template =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

class SSABasedGenericKubernetesResourceMatcherTest {

private final Context<?> mockedContext = mock();
private final Context<HasMetadata> mockedContext = mock();

private final SSABasedGenericKubernetesResourceMatcher<HasMetadata> matcher =
SSABasedGenericKubernetesResourceMatcher.getInstance();
Expand Down Expand Up @@ -325,23 +325,17 @@ void testSanitizeState_daemonSet_withResourceTypeMismatch() {
void testCustomMatcher_returnsExpectedMatchBasedOnReadOnlyLabel(boolean readOnly) {
var dr = new ConfigMapDR();
dr.configureWith(
new KubernetesDependentResourceConfigBuilder()
.withSSAMatcher(new ReadOnlyAwareMatcher())
new KubernetesDependentResourceConfigBuilder<ConfigMap>()
.withSSAMatcher(new ReadOnlyAwareMatcher<>())
.build());
var desiredConfigMap =
loadResource("configmap.empty-owner-reference-desired.yaml", ConfigMap.class);
desiredConfigMap.getData().put("key1", "another value");
var actualConfigMap = loadResource("configmap.empty-owner-reference.yaml", ConfigMap.class);
actualConfigMap.getMetadata().getLabels().put("readonly", Boolean.toString(readOnly));

ConfigMap ignoredPrimary = null;
assertThat(
dr.match(
actualConfigMap,
desiredConfigMap,
ignoredPrimary,
(Context<ConfigMap>) mockedContext)
.matched())
HasMetadata primary = mock();
assertThat(dr.match(actualConfigMap, desiredConfigMap, primary, mockedContext).matched())
.isEqualTo(readOnly);
}

Expand Down Expand Up @@ -391,7 +385,7 @@ private static <R> R loadResource(String fileName, Class<R> clazz) {
clazz, SSABasedGenericKubernetesResourceMatcherTest.class, fileName);
}

private static class ConfigMapDR extends KubernetesDependentResource<ConfigMap, ConfigMap> {
private static class ConfigMapDR extends KubernetesDependentResource<ConfigMap, HasMetadata> {
Copy link
Collaborator

@csviri csviri Oct 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: What is the reason for this change?

Copy link
Contributor Author

@Donnerbart Donnerbart Oct 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make the existing field mockedContext re-useable without any casts or warnings.

I've changed the context from Context<?> to Context<HasMetadata>, which works for all existing tests. And then aligned ConfigMapDR to use HasMetadata as primary resource as well.

The whole test class is now warning free in my IDEA.

public ConfigMapDR() {
super(ConfigMap.class);
}
Expand Down