Skip to content
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 version check to use Coherence version comparison utilities #625

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions api/v1/coherencejobresource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (in *CoherenceJob) GetAPIVersion() string {
return in.APIVersion
}

func (in *CoherenceJob) IsForceExit() bool {
return in.Spec.ForceExit != nil && *in.Spec.ForceExit
}

// GetSpec returns this resource's CoherenceResourceSpec
func (in *CoherenceJob) GetSpec() *CoherenceResourceSpec {
return &in.Spec.CoherenceResourceSpec
Expand Down Expand Up @@ -376,6 +380,10 @@ type CoherenceJobResourceSpec struct {
// will also be executed on every Pod that becomes ready after that time.
// +optional
ReadyAction *CoherenceJobProbe `json:"readyAction,omitempty"`

// ForceExit is a flag to indicate whether the Operator should call System.exit to forcefully exit the process
// when the configured main class completes execution.
ForceExit *bool `json:"forceExit,omitempty"`
}

// GetRestartPolicy returns the name of the application image to use
Expand Down
2 changes: 2 additions & 0 deletions api/v1/coherenceresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,6 @@ type CoherenceResource interface {
DeepCopyResource() CoherenceResource
// GetAPIVersion returns the TypeMeta API version
GetAPIVersion() string
// IsForceExit is a flag to determine whether the Operator calls System.exit when the main class finishes.
IsForceExit() bool
}
4 changes: 4 additions & 0 deletions api/v1/coherenceresource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ func (in *Coherence) SetReplicas(replicas int32) {
}
}

func (in *Coherence) IsForceExit() bool {
return false
}

// FindFullyQualifiedPortServiceNames returns a map of the exposed ports of this resource mapped to their Service's
// fully qualified domain name.
func (in *Coherence) FindFullyQualifiedPortServiceNames() map[string]string {
Expand Down
8 changes: 8 additions & 0 deletions api/v1/coherenceresourcespec_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,14 @@ func (in *CoherenceResourceSpec) CreateCoherenceContainer(deployment CoherenceRe

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

forceExit := deployment.IsForceExit()
if forceExit {
c.Env = append(c.Env, corev1.EnvVar{
Name: EnvVarCohForceExit,
Value: "true",
})
}

in.Application.UpdateCoherenceContainer(&c)

if in.Resources != nil {
Expand Down
1 change: 1 addition & 0 deletions api/v1/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ const (
EnvVarCohMetricsPrefix = "COH_METRICS"
EnvVarCohEnabledSuffix = "_ENABLED"
EnvVarCohPortSuffix = "_PORT"
EnvVarCohForceExit = "COH_FORCE_EXIT"
EnvVarCoherenceLocalPort = "COHERENCE_LOCALPORT"
EnvVarCoherenceLocalPortAdjust = "COHERENCE_LOCALPORT_ADJUST"
EnvVarEnableIPMonitor = "COH_ENABLE_IPMONITOR"
Expand Down
5 changes: 5 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

package com.oracle.coherence.k8s;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.tangosol.coherence.component.net.memberSet.actualMemberSet.ServiceMemberSet;
import com.tangosol.net.CacheFactory;

/**
Expand All @@ -17,9 +15,6 @@
* version is greater than or equal to that version.
*/
public class CoherenceVersion {

private static final Pattern PATTERN = Pattern.compile("(\\d*)\\D*(\\d*)\\D*(\\d*)\\D*(\\d*)\\D*(\\d*)\\D*(\\d*)\\D*");

/**
* Private constructor for utility class.
*/
Expand Down Expand Up @@ -54,57 +49,35 @@ public static void main(String[] args) {
* @return {@code true} if the actual Coherence version is at least the check version
*/
public static boolean versionCheck(String coherenceVersion, String... args) {
System.out.print("CoherenceOperator: version check actual=\"" + coherenceVersion + "\" required=\"" + args[0] + '"');
if (coherenceVersion.contains(" ")) {
coherenceVersion = coherenceVersion.substring(0, coherenceVersion.indexOf(" "));
}
if (coherenceVersion.contains(":")) {
coherenceVersion = coherenceVersion.substring(coherenceVersion.indexOf(":") + 1);
}

int[] coherenceParts = splitVersion(coherenceVersion);

if (coherenceParts.length == 0) {
return false;
int[] nCoherenceParts = ServiceMemberSet.toVersionArray(coherenceVersion);
int nActual;
if (nCoherenceParts[0] > 20) {
nActual = ServiceMemberSet.encodeVersion(nCoherenceParts[0], nCoherenceParts[1], nCoherenceParts[2]);
}

int[] versionParts = splitVersion(args[0]);
int partCount = Math.min(coherenceParts.length, versionParts.length);

if (partCount > 0) {
for (int i = 0; i < partCount; i++) {
if (coherenceParts[i] == versionParts[i]) {
continue;
}
// else versions differ
return coherenceParts[i] > versionParts[i];
}
else {
nActual = ServiceMemberSet.parseVersion(coherenceVersion);
}

// versions are equal
return true;
}

private static int[] splitVersion(String version) {
Matcher matcher = PATTERN.matcher(version);
int[] count;

if (matcher.matches()) {
int groupCount = matcher.groupCount();
count = new int[groupCount];

for (int i = 1; i <= groupCount; i++) {
try {
count[i - 1] = Integer.parseInt(matcher.group(i));
}
catch (NumberFormatException e) {
count[i - 1] = 0;
}
}
int[] nParts = ServiceMemberSet.toVersionArray(args[0]);
int nRequired;
if (nParts[0] > 20) {
nRequired = ServiceMemberSet.encodeVersion(nParts[0], nParts[1], nParts[2]);
}
else {
count = new int[0];
nRequired = ServiceMemberSet.parseVersion(args[0]);
}
boolean fResult = nActual >= nRequired;

return count;
// versions are equal
System.out.println(" result=" + fResult);
return fResult;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class Main {

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

private static final String PROP_FORCE_EXIT = "coherence.k8s.operator.force.exit";

private static boolean initialised = false;

/**
Expand Down Expand Up @@ -50,6 +52,11 @@ else if (DEFAULT_MAIN.equals(args[0])) {
Class<?> clsMain = Class.forName(sMainClass);
Method method = clsMain.getMethod("main", asArgsReal.getClass());
method.invoke(null, (Object) asArgsReal);

boolean fExit = Boolean.getBoolean(PROP_FORCE_EXIT);
if (fExit) {
System.exit(0);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,16 @@ public void shouldBeLess() {
@Test
public void shouldWorkWithInterimBuild() throws Exception {
assertThat(CoherenceVersion.versionCheck("14.1.1.0.15 (101966-Int)", "14.1.1.0.0"), is(true));
assertThat(CoherenceVersion.versionCheck("14.1.1.0.15 (101966-Int)", "22.06.0"), is(false));
assertThat(CoherenceVersion.versionCheck("14.1.1.0.15 (101966-Int)", "14.1.1.0.16"), is(false));
}

@Test
public void shouldWorkWithCE() throws Exception {
assertThat(CoherenceVersion.versionCheck("14.1.1.0.15", "22.06.1"), is(false));
assertThat(CoherenceVersion.versionCheck("14.1.1.2206.5", "22.06.6"), is(false));
assertThat(CoherenceVersion.versionCheck("14.1.1.2206.7", "22.06.7"), is(true));
assertThat(CoherenceVersion.versionCheck("22.06.5", "22.06.6"), is(false));
assertThat(CoherenceVersion.versionCheck("22.06.7", "22.06.7"), is(true));
}

}
7 changes: 4 additions & 3 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ func createCommand(details *RunDetails) (string, *exec.Cmd, error) {
details.addArgFromEnvVar(v1.EnvVarCohClusterName, "-Dcoherence.cluster")
details.addArgFromEnvVar(v1.EnvVarCohCacheConfig, "-Dcoherence.cacheconfig")
details.addArgFromEnvVar(v1.EnvVarCohIdentity, "-Dcoherence.k8s.operator.identity")
details.addArgFromEnvVar(v1.EnvVarCohForceExit, "-Dcoherence.k8s.operator.force.exit")
details.setSystemPropertyFromEnvVarOrDefault(v1.EnvVarCohHealthPort, "-Dcoherence.k8s.operator.health.port", fmt.Sprintf("%d", v1.DefaultHealthPort))
details.setSystemPropertyFromEnvVarOrDefault(v1.EnvVarCohMgmtPrefix+v1.EnvVarCohPortSuffix, "-Dcoherence.management.http.port", fmt.Sprintf("%d", v1.DefaultManagementPort))
details.setSystemPropertyFromEnvVarOrDefault(v1.EnvVarCohMetricsPrefix+v1.EnvVarCohPortSuffix, "-Dcoherence.metrics.http.port", fmt.Sprintf("%d", v1.DefaultMetricsPort))
Expand Down Expand Up @@ -358,15 +359,15 @@ func createCommand(details *RunDetails) (string, *exec.Cmd, error) {
cohPre12214(details)
}

post2206 := checkCoherenceVersion("22.06.0", details)
post2206 := checkCoherenceVersion("14.1.1.2206.0", details)
if post2206 {
// at least CE 22.06
cohPost2206(details)
} else {
post2006 := checkCoherenceVersion("20.06.0", details)
post2006 := checkCoherenceVersion("14.1.1.2006.0", details)
if !post2006 {
// pre CE 20.06 - could be 14.1.1.2206
if post14112206 := checkCoherenceVersion("14.1.1.2206", details); post14112206 {
if post14112206 := checkCoherenceVersion("14.1.1.2206.0", details); post14112206 {
// at least 14.1.1.2206
cohPost2206(details)
}
Expand Down