Skip to content

Commit 3a16e12

Browse files
committed
revert to KEP compatible way
Signed-off-by: Attila Mészáros <[email protected]>
1 parent 118226b commit 3a16e12

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/PrimaryUpdateAndCacheUtils.java

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -453,23 +453,53 @@ public static <P extends HasMetadata> P addFinalizerWithSSA(
453453

454454
public static int compareResourceVersions(String v1, String v2) {
455455
var v1Length = v1.length();
456-
var v2Length = v2.length();
457-
if (v1Length > v2Length) {
458-
return 1;
456+
if (v1Length == 0) {
457+
throw new IllegalArgumentException("Resource version (1) is empty");
459458
}
460-
if (v1Length < v2Length) {
461-
return -1;
459+
var v2Length = v2.length();
460+
if (v2Length == 0) {
461+
throw new IllegalArgumentException("Resource version (2) is empty");
462462
}
463-
for (int i = 0; i < v1Length; i++) {
464-
var char1 = v1.charAt(i);
465-
var char2 = v2.charAt(i);
466-
if (char1 > char2) {
467-
return 1;
463+
var maxLength = Math.max(v1Length, v2Length);
464+
boolean v1LeadingZero = true;
465+
boolean v2LeadingZero = true;
466+
int comparison = 0;
467+
for (int i = 0; i < maxLength; i++) {
468+
char char1 = 0;
469+
if (i < v1Length) {
470+
char1 = v1.charAt(i);
471+
if (v1LeadingZero) {
472+
if (char1 == '0') {
473+
throw new IllegalArgumentException("Resource version (1) cannot begin with 0");
474+
}
475+
v1LeadingZero = false;
476+
}
477+
if (!Character.isDigit(char1)) {
478+
throw new IllegalArgumentException(
479+
"Non numeric characters in resource version (1): " + char1);
480+
}
468481
}
469-
if (char1 < char2) {
470-
return -1;
482+
if (i < v2Length) {
483+
var char2 = v2.charAt(i);
484+
if (v2LeadingZero) {
485+
if (char2 == '0') {
486+
throw new IllegalArgumentException("Resource version (2) cannot begin with 0");
487+
}
488+
v2LeadingZero = false;
489+
}
490+
if (!Character.isDigit(char2)) {
491+
throw new IllegalArgumentException(
492+
"Non numeric characters in resource version (2): " + char2);
493+
}
494+
if (char1 == 0) {
495+
comparison = -1;
496+
} else if (comparison == 0) {
497+
comparison = Character.compare(char1, char2);
498+
}
499+
} else {
500+
comparison = 1;
471501
}
472502
}
473-
return 0;
503+
return comparison;
474504
}
475505
}

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/reconciler/PrimaryUpdateAndCacheUtilsTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,19 @@ public void compareResourceVersionsTest() {
190190

191191
assertThat(compareResourceVersions("123", "2")).isPositive();
192192
assertThat(compareResourceVersions("3", "211")).isNegative();
193+
194+
assertThrows(IllegalArgumentException.class, () -> compareResourceVersions("aa", "22"));
195+
assertThrows(IllegalArgumentException.class, () -> compareResourceVersions("11", "ba"));
196+
assertThrows(IllegalArgumentException.class, () -> compareResourceVersions("", "22"));
197+
assertThrows(IllegalArgumentException.class, () -> compareResourceVersions("11", ""));
198+
assertThrows(IllegalArgumentException.class, () -> compareResourceVersions("01", "123"));
199+
assertThrows(IllegalArgumentException.class, () -> compareResourceVersions("123", "01"));
193200
}
194201

195202
// naive performance that compares the works case scenario for non parsing variant
196203
@Test
197204
public void compareResourcePerformanceTest() {
198-
var execNum = 20000000;
205+
var execNum = 30000000;
199206
var startTime = System.currentTimeMillis();
200207
for (int i = 0; i < execNum; i++) {
201208
var res = compareResourceVersions("123456788", "123456789");

0 commit comments

Comments
 (0)