Skip to content

Commit abbbebb

Browse files
authored
Fix version check to use Coherence version comparison utilities (#625)
1 parent 3c4f9e2 commit abbbebb

File tree

10 files changed

+66
-48
lines changed

10 files changed

+66
-48
lines changed

api/v1/coherencejobresource_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ func (in *CoherenceJob) GetAPIVersion() string {
5858
return in.APIVersion
5959
}
6060

61+
func (in *CoherenceJob) IsForceExit() bool {
62+
return in.Spec.ForceExit != nil && *in.Spec.ForceExit
63+
}
64+
6165
// GetSpec returns this resource's CoherenceResourceSpec
6266
func (in *CoherenceJob) GetSpec() *CoherenceResourceSpec {
6367
return &in.Spec.CoherenceResourceSpec
@@ -376,6 +380,10 @@ type CoherenceJobResourceSpec struct {
376380
// will also be executed on every Pod that becomes ready after that time.
377381
// +optional
378382
ReadyAction *CoherenceJobProbe `json:"readyAction,omitempty"`
383+
384+
// ForceExit is a flag to indicate whether the Operator should call System.exit to forcefully exit the process
385+
// when the configured main class completes execution.
386+
ForceExit *bool `json:"forceExit,omitempty"`
379387
}
380388

381389
// GetRestartPolicy returns the name of the application image to use

api/v1/coherenceresource.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,6 @@ type CoherenceResource interface {
8585
DeepCopyResource() CoherenceResource
8686
// GetAPIVersion returns the TypeMeta API version
8787
GetAPIVersion() string
88+
// IsForceExit is a flag to determine whether the Operator calls System.exit when the main class finishes.
89+
IsForceExit() bool
8890
}

api/v1/coherenceresource_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ func (in *Coherence) SetReplicas(replicas int32) {
175175
}
176176
}
177177

178+
func (in *Coherence) IsForceExit() bool {
179+
return false
180+
}
181+
178182
// FindFullyQualifiedPortServiceNames returns a map of the exposed ports of this resource mapped to their Service's
179183
// fully qualified domain name.
180184
func (in *Coherence) FindFullyQualifiedPortServiceNames() map[string]string {

api/v1/coherenceresourcespec_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,14 @@ func (in *CoherenceResourceSpec) CreateCoherenceContainer(deployment CoherenceRe
731731

732732
c.Env = append(c.Env, in.CreateDefaultEnv(deployment)...)
733733

734+
forceExit := deployment.IsForceExit()
735+
if forceExit {
736+
c.Env = append(c.Env, corev1.EnvVar{
737+
Name: EnvVarCohForceExit,
738+
Value: "true",
739+
})
740+
}
741+
734742
in.Application.UpdateCoherenceContainer(&c)
735743

736744
if in.Resources != nil {

api/v1/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ const (
200200
EnvVarCohMetricsPrefix = "COH_METRICS"
201201
EnvVarCohEnabledSuffix = "_ENABLED"
202202
EnvVarCohPortSuffix = "_PORT"
203+
EnvVarCohForceExit = "COH_FORCE_EXIT"
203204
EnvVarCoherenceLocalPort = "COHERENCE_LOCALPORT"
204205
EnvVarCoherenceLocalPortAdjust = "COHERENCE_LOCALPORT_ADJUST"
205206
EnvVarEnableIPMonitor = "COH_ENABLE_IPMONITOR"

api/v1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/coherence-operator/src/main/java/com/oracle/coherence/k8s/CoherenceVersion.java

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
package com.oracle.coherence.k8s;
88

9-
import java.util.regex.Matcher;
10-
import java.util.regex.Pattern;
11-
9+
import com.tangosol.coherence.component.net.memberSet.actualMemberSet.ServiceMemberSet;
1210
import com.tangosol.net.CacheFactory;
1311

1412
/**
@@ -17,9 +15,6 @@
1715
* version is greater than or equal to that version.
1816
*/
1917
public class CoherenceVersion {
20-
21-
private static final Pattern PATTERN = Pattern.compile("(\\d*)\\D*(\\d*)\\D*(\\d*)\\D*(\\d*)\\D*(\\d*)\\D*(\\d*)\\D*");
22-
2318
/**
2419
* Private constructor for utility class.
2520
*/
@@ -54,57 +49,35 @@ public static void main(String[] args) {
5449
* @return {@code true} if the actual Coherence version is at least the check version
5550
*/
5651
public static boolean versionCheck(String coherenceVersion, String... args) {
52+
System.out.print("CoherenceOperator: version check actual=\"" + coherenceVersion + "\" required=\"" + args[0] + '"');
5753
if (coherenceVersion.contains(" ")) {
5854
coherenceVersion = coherenceVersion.substring(0, coherenceVersion.indexOf(" "));
5955
}
6056
if (coherenceVersion.contains(":")) {
6157
coherenceVersion = coherenceVersion.substring(coherenceVersion.indexOf(":") + 1);
6258
}
6359

64-
int[] coherenceParts = splitVersion(coherenceVersion);
65-
66-
if (coherenceParts.length == 0) {
67-
return false;
60+
int[] nCoherenceParts = ServiceMemberSet.toVersionArray(coherenceVersion);
61+
int nActual;
62+
if (nCoherenceParts[0] > 20) {
63+
nActual = ServiceMemberSet.encodeVersion(nCoherenceParts[0], nCoherenceParts[1], nCoherenceParts[2]);
6864
}
69-
70-
int[] versionParts = splitVersion(args[0]);
71-
int partCount = Math.min(coherenceParts.length, versionParts.length);
72-
73-
if (partCount > 0) {
74-
for (int i = 0; i < partCount; i++) {
75-
if (coherenceParts[i] == versionParts[i]) {
76-
continue;
77-
}
78-
// else versions differ
79-
return coherenceParts[i] > versionParts[i];
80-
}
65+
else {
66+
nActual = ServiceMemberSet.parseVersion(coherenceVersion);
8167
}
8268

83-
// versions are equal
84-
return true;
85-
}
86-
87-
private static int[] splitVersion(String version) {
88-
Matcher matcher = PATTERN.matcher(version);
89-
int[] count;
90-
91-
if (matcher.matches()) {
92-
int groupCount = matcher.groupCount();
93-
count = new int[groupCount];
94-
95-
for (int i = 1; i <= groupCount; i++) {
96-
try {
97-
count[i - 1] = Integer.parseInt(matcher.group(i));
98-
}
99-
catch (NumberFormatException e) {
100-
count[i - 1] = 0;
101-
}
102-
}
69+
int[] nParts = ServiceMemberSet.toVersionArray(args[0]);
70+
int nRequired;
71+
if (nParts[0] > 20) {
72+
nRequired = ServiceMemberSet.encodeVersion(nParts[0], nParts[1], nParts[2]);
10373
}
10474
else {
105-
count = new int[0];
75+
nRequired = ServiceMemberSet.parseVersion(args[0]);
10676
}
77+
boolean fResult = nActual >= nRequired;
10778

108-
return count;
79+
// versions are equal
80+
System.out.println(" result=" + fResult);
81+
return fResult;
10982
}
11083
}

java/coherence-operator/src/main/java/com/oracle/coherence/k8s/Main.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public class Main {
1919

2020
private static final String DEFAULT_MAIN = "$DEFAULT$";
2121

22+
private static final String PROP_FORCE_EXIT = "coherence.k8s.operator.force.exit";
23+
2224
private static boolean initialised = false;
2325

2426
/**
@@ -50,6 +52,11 @@ else if (DEFAULT_MAIN.equals(args[0])) {
5052
Class<?> clsMain = Class.forName(sMainClass);
5153
Method method = clsMain.getMethod("main", asArgsReal.getClass());
5254
method.invoke(null, (Object) asArgsReal);
55+
56+
boolean fExit = Boolean.getBoolean(PROP_FORCE_EXIT);
57+
if (fExit) {
58+
System.exit(0);
59+
}
5360
}
5461

5562
/**

java/coherence-operator/src/test/java/com/oracle/coherence/k8s/CoherenceVersionTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,16 @@ public void shouldBeLess() {
5454
@Test
5555
public void shouldWorkWithInterimBuild() throws Exception {
5656
assertThat(CoherenceVersion.versionCheck("14.1.1.0.15 (101966-Int)", "14.1.1.0.0"), is(true));
57-
assertThat(CoherenceVersion.versionCheck("14.1.1.0.15 (101966-Int)", "22.06.0"), is(false));
57+
assertThat(CoherenceVersion.versionCheck("14.1.1.0.15 (101966-Int)", "14.1.1.0.16"), is(false));
58+
}
59+
60+
@Test
61+
public void shouldWorkWithCE() throws Exception {
62+
assertThat(CoherenceVersion.versionCheck("14.1.1.0.15", "22.06.1"), is(false));
63+
assertThat(CoherenceVersion.versionCheck("14.1.1.2206.5", "22.06.6"), is(false));
64+
assertThat(CoherenceVersion.versionCheck("14.1.1.2206.7", "22.06.7"), is(true));
65+
assertThat(CoherenceVersion.versionCheck("22.06.5", "22.06.6"), is(false));
66+
assertThat(CoherenceVersion.versionCheck("22.06.7", "22.06.7"), is(true));
5867
}
5968

6069
}

pkg/runner/runner.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ func createCommand(details *RunDetails) (string, *exec.Cmd, error) {
294294
details.addArgFromEnvVar(v1.EnvVarCohClusterName, "-Dcoherence.cluster")
295295
details.addArgFromEnvVar(v1.EnvVarCohCacheConfig, "-Dcoherence.cacheconfig")
296296
details.addArgFromEnvVar(v1.EnvVarCohIdentity, "-Dcoherence.k8s.operator.identity")
297+
details.addArgFromEnvVar(v1.EnvVarCohForceExit, "-Dcoherence.k8s.operator.force.exit")
297298
details.setSystemPropertyFromEnvVarOrDefault(v1.EnvVarCohHealthPort, "-Dcoherence.k8s.operator.health.port", fmt.Sprintf("%d", v1.DefaultHealthPort))
298299
details.setSystemPropertyFromEnvVarOrDefault(v1.EnvVarCohMgmtPrefix+v1.EnvVarCohPortSuffix, "-Dcoherence.management.http.port", fmt.Sprintf("%d", v1.DefaultManagementPort))
299300
details.setSystemPropertyFromEnvVarOrDefault(v1.EnvVarCohMetricsPrefix+v1.EnvVarCohPortSuffix, "-Dcoherence.metrics.http.port", fmt.Sprintf("%d", v1.DefaultMetricsPort))
@@ -358,15 +359,15 @@ func createCommand(details *RunDetails) (string, *exec.Cmd, error) {
358359
cohPre12214(details)
359360
}
360361

361-
post2206 := checkCoherenceVersion("22.06.0", details)
362+
post2206 := checkCoherenceVersion("14.1.1.2206.0", details)
362363
if post2206 {
363364
// at least CE 22.06
364365
cohPost2206(details)
365366
} else {
366-
post2006 := checkCoherenceVersion("20.06.0", details)
367+
post2006 := checkCoherenceVersion("14.1.1.2006.0", details)
367368
if !post2006 {
368369
// pre CE 20.06 - could be 14.1.1.2206
369-
if post14112206 := checkCoherenceVersion("14.1.1.2206", details); post14112206 {
370+
if post14112206 := checkCoherenceVersion("14.1.1.2206.0", details); post14112206 {
370371
// at least 14.1.1.2206
371372
cohPost2206(details)
372373
}

0 commit comments

Comments
 (0)