-
Notifications
You must be signed in to change notification settings - Fork 218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix infinite resource updates due to canonical format conversion of resource requirements #2565
Merged
metacosm
merged 4 commits into
operator-framework:next
from
Donnerbart:bugfix/2509-ssa-matcher-quantity/next
Nov 6, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6630613
refactor: clean up SSABasedGenericKubernetesResourceMatcher
Donnerbart 3e6b158
test: add missing tests for StatefulSet with VolumeClaimTemplates for…
Donnerbart f4c0f68
fix: Fix infinite resource updates due to canonical format conversion…
Donnerbart 012f87b
test: Add test cases with init containers to ResourceRequirementsSani…
Donnerbart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...vaoperatorsdk/operator/processing/dependent/kubernetes/ResourceRequirementsSanitizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package io.javaoperatorsdk.operator.processing.dependent.kubernetes; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import io.fabric8.kubernetes.api.model.Container; | ||
import io.fabric8.kubernetes.api.model.GenericKubernetesResource; | ||
import io.fabric8.kubernetes.api.model.PodTemplateSpec; | ||
import io.fabric8.kubernetes.api.model.Quantity; | ||
import io.fabric8.kubernetes.api.model.ResourceRequirements; | ||
|
||
/** | ||
* Sanitizes the {@link ResourceRequirements} in the containers of a pair of {@link PodTemplateSpec} | ||
* instances. | ||
* <p> | ||
* When the sanitizer finds a mismatch in the structure of the given templates, before it gets to | ||
* the nested resource limits and requests, it returns early without fixing the actual map. This is | ||
* an optimization because the given templates will anyway differ at this point. This means we do | ||
* not have to attempt to sanitize the resources for these use cases, since there will anyway be an | ||
* update of the K8s resource. | ||
* <p> | ||
* The algorithm traverses the whole template structure because we need the actual and desired | ||
* {@link Quantity} instances to compare their numerical amount. Using the | ||
* {@link GenericKubernetesResource#get(Map, Object...)} shortcut would need to create new instances | ||
* just for the sanitization check. | ||
*/ | ||
class ResourceRequirementsSanitizer { | ||
|
||
static void sanitizeResourceRequirements(final Map<String, Object> actualMap, | ||
final PodTemplateSpec actualTemplate, final PodTemplateSpec desiredTemplate) { | ||
if (actualTemplate == null || desiredTemplate == null) { | ||
return; | ||
} | ||
if (actualTemplate.getSpec() == null || desiredTemplate.getSpec() == null) { | ||
return; | ||
} | ||
sanitizeResourceRequirements(actualMap, actualTemplate.getSpec().getInitContainers(), | ||
desiredTemplate.getSpec().getInitContainers(), "initContainers"); | ||
sanitizeResourceRequirements(actualMap, actualTemplate.getSpec().getContainers(), | ||
desiredTemplate.getSpec().getContainers(), "containers"); | ||
} | ||
|
||
private static void sanitizeResourceRequirements(final Map<String, Object> actualMap, | ||
final List<Container> actualContainers, final List<Container> desiredContainers, | ||
final String containerPath) { | ||
int containers = desiredContainers.size(); | ||
if (containers == actualContainers.size()) { | ||
for (int containerIndex = 0; containerIndex < containers; containerIndex++) { | ||
var desiredContainer = desiredContainers.get(containerIndex); | ||
var actualContainer = actualContainers.get(containerIndex); | ||
if (!desiredContainer.getName().equals(actualContainer.getName())) { | ||
return; | ||
} | ||
sanitizeResourceRequirements(actualMap, actualContainer.getResources(), | ||
desiredContainer.getResources(), | ||
containerPath, containerIndex); | ||
} | ||
} | ||
} | ||
|
||
private static void sanitizeResourceRequirements(final Map<String, Object> actualMap, | ||
final ResourceRequirements actualResource, final ResourceRequirements desiredResource, | ||
final String containerPath, final int containerIndex) { | ||
if (desiredResource == null || actualResource == null) { | ||
return; | ||
} | ||
sanitizeQuantities(actualMap, actualResource.getRequests(), desiredResource.getRequests(), | ||
containerPath, containerIndex, "requests"); | ||
sanitizeQuantities(actualMap, actualResource.getLimits(), desiredResource.getLimits(), | ||
containerPath, containerIndex, "limits"); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static void sanitizeQuantities(final Map<String, Object> actualMap, | ||
final Map<String, Quantity> actualResource, final Map<String, Quantity> desiredResource, | ||
final String containerPath, final int containerIndex, final String quantityPath) { | ||
Optional.ofNullable( | ||
GenericKubernetesResource.get(actualMap, "spec", "template", "spec", containerPath, | ||
containerIndex, "resources", quantityPath)) | ||
.map(Map.class::cast) | ||
.filter(m -> m.size() == desiredResource.size()) | ||
.ifPresent(m -> actualResource.forEach((key, actualQuantity) -> { | ||
var desiredQuantity = desiredResource.get(key); | ||
if (desiredQuantity == null) { | ||
return; | ||
} | ||
// check if the string representation of the Quantity instances is equal | ||
if (actualQuantity.getAmount().equals(desiredQuantity.getAmount()) | ||
&& actualQuantity.getFormat().equals(desiredQuantity.getFormat())) { | ||
return; | ||
} | ||
// check if the numerical amount of the Quantity instances is equal | ||
if (actualQuantity.equals(desiredQuantity)) { | ||
// replace the actual Quantity with the desired Quantity to prevent a resource update | ||
m.replace(key, desiredQuantity.toString()); | ||
} | ||
})); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure if all of these
null
checks are needed. I've added them for all fields that have no default value in their declaration.