Repository navigation
fix: match HorizontalPodAutoscaler target quantities in SSA matcher #3682
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
Open
cjbooms
wants to merge
2
commits into
operator-framework:main
Choose a base branch
from
cjbooms:fix/hpa-quantity-sanitizer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
100 changes: 100 additions & 0 deletions
100
...peratorsdk/operator/processing/dependent/kubernetes/HorizontalPodAutoscalerSanitizer.java
This file contains hidden or 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 @@ | ||
| /* | ||
| * Copyright Java Operator SDK Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.javaoperatorsdk.operator.processing.dependent.kubernetes; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.function.Function; | ||
|
|
||
| import io.fabric8.kubernetes.api.model.GenericKubernetesResource; | ||
| import io.fabric8.kubernetes.api.model.autoscaling.v2.HorizontalPodAutoscalerSpec; | ||
| import io.fabric8.kubernetes.api.model.autoscaling.v2.MetricSpec; | ||
| import io.fabric8.kubernetes.api.model.autoscaling.v2.MetricTarget; | ||
|
|
||
| /** | ||
| * Sanitizes the target quantities ({@code value} and {@code averageValue}) of the metrics in a pair | ||
| * of {@link HorizontalPodAutoscalerSpec} instances. | ||
| * | ||
| * <p>When the number of metrics differs, the sanitizer returns early without fixing the actual map, | ||
| * because the specs differ anyway. A metric source that is only present on one side, for example | ||
| * because the metric type differs, is skipped for the same reason. | ||
| */ | ||
| class HorizontalPodAutoscalerSanitizer { | ||
|
|
||
| private static final Map<String, Function<MetricSpec, MetricTarget>> TARGET_BY_METRIC_SOURCE = | ||
| Map.of( | ||
| "external", m -> m.getExternal() == null ? null : m.getExternal().getTarget(), | ||
| "object", m -> m.getObject() == null ? null : m.getObject().getTarget(), | ||
| "pods", m -> m.getPods() == null ? null : m.getPods().getTarget(), | ||
| "resource", m -> m.getResource() == null ? null : m.getResource().getTarget(), | ||
| "containerResource", | ||
| m -> m.getContainerResource() == null ? null : m.getContainerResource().getTarget()); | ||
|
|
||
| static void sanitizeHorizontalPodAutoscalerSpec( | ||
| final Map<String, Object> actualMap, | ||
| final HorizontalPodAutoscalerSpec actualSpec, | ||
| final HorizontalPodAutoscalerSpec desiredSpec) { | ||
| if (actualSpec == null || desiredSpec == null) { | ||
| return; | ||
| } | ||
| final List<MetricSpec> actualMetrics = actualSpec.getMetrics(); | ||
| final List<MetricSpec> desiredMetrics = desiredSpec.getMetrics(); | ||
| if (actualMetrics == null | ||
| || desiredMetrics == null | ||
| || actualMetrics.size() != desiredMetrics.size()) { | ||
| return; | ||
| } | ||
| for (int metricIndex = 0; metricIndex < desiredMetrics.size(); metricIndex++) { | ||
| final var actualMetric = actualMetrics.get(metricIndex); | ||
| final var desiredMetric = desiredMetrics.get(metricIndex); | ||
| for (var source : TARGET_BY_METRIC_SOURCE.entrySet()) { | ||
| sanitizeTarget( | ||
| actualMap, | ||
| metricIndex, | ||
| source.getKey(), | ||
| source.getValue().apply(actualMetric), | ||
| source.getValue().apply(desiredMetric)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static void sanitizeTarget( | ||
| final Map<String, Object> actualMap, | ||
| final int metricIndex, | ||
| final String metricSource, | ||
| final MetricTarget actualTarget, | ||
| final MetricTarget desiredTarget) { | ||
| if (actualTarget == null || desiredTarget == null) { | ||
| return; | ||
| } | ||
| Optional.ofNullable( | ||
| GenericKubernetesResource.get( | ||
| actualMap, "spec", "metrics", metricIndex, metricSource, "target")) | ||
| .map(Map.class::cast) | ||
| .ifPresent( | ||
| m -> { | ||
| QuantitySanitizer.sanitizeQuantity( | ||
| m, "value", actualTarget.getValue(), desiredTarget.getValue()); | ||
| QuantitySanitizer.sanitizeQuantity( | ||
| m, | ||
| "averageValue", | ||
| actualTarget.getAverageValue(), | ||
| desiredTarget.getAverageValue()); | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or 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
61 changes: 61 additions & 0 deletions
61
...n/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/QuantitySanitizer.java
This file contains hidden or 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,61 @@ | ||
| /* | ||
| * Copyright Java Operator SDK Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.javaoperatorsdk.operator.processing.dependent.kubernetes; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| import io.fabric8.kubernetes.api.model.Quantity; | ||
|
|
||
| /** | ||
| * Replaces an actual {@link Quantity} with the desired one when both are numerically equal. The API | ||
| * server stores quantities in canonical form (for example {@code 1000} as {@code 1k}), which would | ||
| * otherwise cause a mismatch. | ||
| */ | ||
| class QuantitySanitizer { | ||
|
|
||
| static void sanitizeQuantity( | ||
| final Map<String, Object> parent, | ||
| final String key, | ||
| final Quantity actualQuantity, | ||
| final Quantity desiredQuantity) { | ||
| if (actualQuantity == null || desiredQuantity == null) { | ||
| return; | ||
| } | ||
| // check if the string representation of the Quantity instances is equal | ||
| if (Objects.equals(actualQuantity.getAmount(), desiredQuantity.getAmount()) | ||
| && Objects.equals(actualQuantity.getFormat(), 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 | ||
| parent.replace(key, desiredQuantity.toString()); | ||
| } | ||
| } | ||
|
|
||
| static void sanitizeQuantities( | ||
| final Map<String, Object> parent, | ||
| final Map<String, Quantity> actualQuantities, | ||
| final Map<String, Quantity> desiredQuantities) { | ||
| if (actualQuantities == null || desiredQuantities == null) { | ||
| return; | ||
| } | ||
| actualQuantities.forEach( | ||
| (key, actualQuantity) -> | ||
| sanitizeQuantity(parent, key, actualQuantity, desiredQuantities.get(key))); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
67 changes: 67 additions & 0 deletions
67
...vaoperatorsdk/operator/processing/dependent/kubernetes/sample-hpa-quantities-desired.yaml
This file contains hidden or 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,67 @@ | ||
| # | ||
| # Copyright Java Operator SDK Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| # desired HorizontalPodAutoscaler with numerically equal, non-canonical target quantities | ||
| apiVersion: autoscaling/v2 | ||
| kind: HorizontalPodAutoscaler | ||
| metadata: | ||
| name: "test" | ||
| spec: | ||
| scaleTargetRef: | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| name: test | ||
| minReplicas: 1 | ||
| maxReplicas: 4 | ||
| metrics: | ||
| - type: External | ||
| external: | ||
| metric: | ||
| name: queue-depth | ||
| target: | ||
| type: AverageValue | ||
| averageValue: "1000" | ||
| - type: Object | ||
| object: | ||
| describedObject: | ||
| apiVersion: v1 | ||
| kind: Service | ||
| name: test | ||
| metric: | ||
| name: requests-per-second | ||
| target: | ||
| type: Value | ||
| value: "2000m" | ||
| - type: Pods | ||
| pods: | ||
| metric: | ||
| name: packets-per-second | ||
| target: | ||
| type: AverageValue | ||
| averageValue: "0.5" | ||
| - type: Resource | ||
| resource: | ||
| name: memory | ||
| target: | ||
| type: AverageValue | ||
| averageValue: "1024Mi" | ||
| - type: ContainerResource | ||
| containerResource: | ||
| name: cpu | ||
| container: app | ||
| target: | ||
| type: AverageValue | ||
| averageValue: "1.5" |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.