java-operator-sdk: CPU resource unit comparison error
Bug Report
In Kubernetes, some resource units are automatically converted to a more convenient format internally. For example:
resources:
limits:
cpu: 2000m
Even if the CPU is set to 2000m, when you retrieve this configuration using kubectl, you might see:
resources:
limits:
cpu: 2
This indicates that the unit has been converted.
The problem arises when an Operator sets the CPU unit as 2000m, but the Kubernetes API server keeps converting it back to 2. This leads to a situation where, during the DependentResource.match process, the Operator and the API server identify the resources as different, causing match to return false.
The reason for this issue is that when comparing the actual and desired states, the values are checked through simple string comparison.
As a result, the operator keeps sending patch requests, causing the resourceVersion to increase infinitely.
Environment
$ java -version
17
$ kubectl version
v1.24
Possible Solution
Quantity("2000m") == Quantity("2")
In fact, when using Quantity in fabric8 for comparison, it can be determined that these two values are the same. It might be better to use Quantity for resource comparisons. However, given the current structure, this doesn’t seem straightforward. Do you think it might be possible?
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 19 (10 by maintainers)
Commits related to this issue
- hotfix serializing quantity Kubernetes will sometimes convert quantity to a more convenient format eg. 2048Mi -> 2Gi. This results in an endless loop since the operator is trying to update it. There ... — committed to FINTLabs/flaiserator by murillio4 2 years ago
- hotfix serializing quantity Kubernetes will sometimes convert quantity to a more convenient format eg. 2048Mi -> 2Gi. This results in an endless loop since the operator is trying to update it. There ... — committed to FINTLabs/flaiserator by murillio4 2 years ago
- add normalisation on resource matching Improved fix on https://github.com/operator-framework/java-operator-sdk/issues/2509 until it gets fixed in library — committed to FINTLabs/flaiserator by murillio4 2 years ago
Fixed in #2565 and #2568.
I didn’t realize my mention of this issue would be tracked here 😅
Anyway, I’ve implemented a temporary fix in my operator. My approach modifies the
SSABasedGenericKubernetesResourceMatcherto normalize Quantity values in both theactualMapanddesiredMap, replacing them withnumericalAmount.stripTrailingZeros(). This resulted in quite a bit of code, and may not be ideal for a long-term solution.Since all methods in
SSABasedGenericKubernetesResourceMatcherexceptmatchare private, I had to clone the entire class to make changes. Would it be possible to convert these methods (or some of them) to protected? This would allow easier extension and customization without needing to replicate all the logic.I had a first look a this, and I’m not sure anymore if this is the right way to go.
The canonical format conversion is complex with a lot of rules and open TODOs in the K8s GoLang code. The Quantity implementation in GoLang is also quite different with two subtypes (based on
int64AmountandinfDecAmount) instead of twoStringfields in Java. The GoLang implementation is also aware of it’s format type (DecimalExponent,BinarySIorDecimalSI), an information that is heavily used for the canonical format conversion.In GoLang the canonical conversion is invoked in
MarshalJSON()andString(), but I couldn’t find anystring -> stringtest cases, just something like{"1.2e3", decQuantity(12, 2, DecimalExponent)},. In the end I came up with this parameterized unit test, based on this manual deployment in a real cluster (to be sure how the converted values actually looks like).Examples for complex conversions:
Eis partially converted to lowercasee. For example:1E-8->1e-3(converted to minimum value)1E-2->10e-3(converted to other exponent)1E-0->1E-0(no conversion)1E0->1E0(no conversion)0.0000001Ki->1m(converted to minimum value)0.128Ki->131072m(converted to SI unit)1.5Ki->1536(converted to decimal)256Ki->256Ki(no conversion)1024Ki->1Mi(converted to binary unit)Examples for TODOs:
// TODO: If BinarySI formatting is requested but would cause rounding, upgrade to one of the other formats.In total we have multiple conversion output formats (decimal, exponential, SI units, binary units), we have a minimum value, there are rules for precision and rounding and we have special handling for zero values.
Of course it would be possible to implement the same logic as we currently have in Kubernetes. But I think this will cause problems down the line. My biggest concern is if the implementation will ever be changed in K8s, e.g. if the TODOs are worked on. If an existing conversion would ever change, we would need to know the K8s version to anticipate the correct canonical format in the fabric8 client. This would be a nightmare to keep in sync.
In the end we could have the same problem again. We try to anticipate the canonical format, but the deployed K8s version has a different implementation. And then we have an infinite update loop again.
I would rather like to explore if we can make the
SSABasedGenericKubernetesResourceMatchersmarter and e.g. enhance this existing method to align the actual instance with desired, in case theQuantityinstances define the same amount.This approach should be much simpler, doesn’t need any invocation of canonical format conversion in the
desired()method, should be much more performant (comparison usesQuantity.getNumericalAmount()under the hood vs. much more arithmetic calculations for the canonical format conversion) and resilient to future changes on canonical format implementation in K8s.We are facing the same issue and solved this by actually creating our desired resource in the format that K8s would transform into. With this approach the actual and desired are the same and the built-in matchers work (SSA and also the generic one).
Good to know, I think I have to build that into our utility class then.
Our use case is a
CRUDKubernetesDependentResource<StatefulSet, OurCustomResource>that is created from aStatefulSetSpecthat we have in our custom resource. In the desiredStatefulSetwe go over the containers and modify the resources like this:This is the utility class:
I know this is far from being perfect, but it solved the infinite update loop issue for us. But of course I’d love to see this fixed in the operator SDK or even the fabric8 client, so there are more eyes on this code (e.g. to cover other missing cases like the storage conversion).
yes right so i will try to do some experiments to find if there is a good way.
It would be good to handle such cases in the framework, unfortunatelly the current SSA Matcher is not prepared for that. But would be nice to do such experiments, and add such fixes even on-demand bases.
One option could be to have specific matchers for specific types to avoid this issue but that would add quite a maintenance burden as we’d need to ensure that these matchers are properly updated whenever the associated type changes in Kubernetes