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

[#456][#462] refactor logic determining cluster version and logic downloading oc binar #465

Merged
merged 2 commits into from
Nov 4, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,29 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<xtf.global_test_properties.path>${project.basedir}/src/test/resources/global-test.properties</xtf.global_test_properties.path>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/java/cz/xtf/core/config/OpenShiftConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public final class OpenShiftConfig {
public static final String OPENSHIFT_VERSION = "xtf.openshift.version";
public static final String OPENSHIFT_NAMESPACE = "xtf.openshift.namespace";
public static final String OPENSHIFT_BINARY_PATH = "xtf.openshift.binary.path";
public static final String OPENSHIFT_BINARY_URL_CHANNEL = "xtf.openshift.binary.url.channel";
public static final String OPENSHIFT_BINARY_CACHE_ENABLED = "xtf.openshift.binary.cache.enabled";
public static final String OPENSHIFT_BINARY_CACHE_PATH = "xtf.openshift.binary.cache.path";
public static final String OPENSHIFT_BINARY_CACHE_DEFAULT_FOLDER = "xtf-oc-cache";
Expand Down Expand Up @@ -55,6 +56,17 @@ public static String binaryPath() {
return XTFConfig.get(OPENSHIFT_BINARY_PATH);
}

/**
* Channel configuration for download of OpenShift client from
* https://mirror.openshift.com/pub/openshift-v4/x86_64/clients/ocp/
* Channels are: stable, latest, fast, candidate
*
* @return channel as configured in xtf.openshift.binary.url.channel property, or default 'stable'
*/
public static String binaryUrlChannelPath() {
return XTFConfig.get(OPENSHIFT_BINARY_URL_CHANNEL, "stable");
}

public static boolean isBinaryCacheEnabled() {
return Boolean.parseBoolean(XTFConfig.get(OPENSHIFT_BINARY_CACHE_ENABLED, "true"));
}
Expand Down
131 changes: 131 additions & 0 deletions core/src/main/java/cz/xtf/core/openshift/ClusterVersionInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package cz.xtf.core.openshift;

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

import org.apache.commons.lang3.StringUtils;
import org.jboss.dmr.ModelNode;

import cz.xtf.core.config.OpenShiftConfig;
import cz.xtf.core.http.Https;
import cz.xtf.core.http.HttpsException;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.openshift.api.model.ClusterVersion;
import io.fabric8.openshift.api.model.ClusterVersionList;
import io.fabric8.openshift.client.OpenShiftHandlers;
import lombok.extern.slf4j.Slf4j;

@Slf4j
class ClusterVersionInfo {
// version must be in format major.minor.micro (4.8.13) or major.minor (4.8)
private static final Pattern versionPattern = Pattern.compile("^(\\d+\\.\\d+)(\\.\\d+)?$");
private final String openshiftVersion;
private final Matcher versionMatcher;

ClusterVersionInfo() {
if (StringUtils.isNotEmpty(OpenShiftConfig.version())) {
// manually configured version in config
openshiftVersion = OpenShiftConfig.version();
} else {
// try to detect version from cluster
openshiftVersion = detectClusterVersionFromCluster();
}

versionMatcher = openshiftVersion != null ? validateConfiguredVersion(openshiftVersion) : null;
}

/**
* @return full version of OpenShift cluster as detected or configured or null
*/
String getOpenshiftVersion() {
return openshiftVersion;
}

/**
* @return major.minor only version of OpenShift cluster as detected or configured or null
*/
String getMajorMinorOpenshiftVersion() {
if (openshiftVersion != null) {
return versionMatcher.group(1);
}
return null;
}

/**
* @return true if version is in major.minor format only, false if not valid url
*/
boolean isMajorMinorOnly() {
if (openshiftVersion != null) {
return versionMatcher.group(2) == null;
}
return false;
}

/**
* @return true if version is in major.minor.micro format, false if not valid url
*/
boolean isMajorMinorMicro() {
if (openshiftVersion != null) {
return versionMatcher.group(2) != null;
}
return false;
}

/**
* Detects cluster version from cluster
*
* @return version of OpenShift cluster or null
*/
private String detectClusterVersionFromCluster() {
String openshiftVersion = null;
try {
// try to access version info on OpenShift 3.x, this endpoint isn't available on OpenShift 4.x
// another option might be client.getVersion() but it returns Kubernetes version, we would
// need to check whether version starts with 1.x == Kubernetes == OpenShift 4.x
//
// Response looks like this:
// {
// "major": "3",
// "minor": "11+",
// "gitVersion": "v3.11.272",
// "gitCommit": "8b0575fb48",
// "gitTreeState": "",
// "buildDate": "2020-08-18T05:38:34Z",
// "goVersion": "",
// "compiler": "",
// "platform": ""
// }
String versionInfo = Https.httpsGetContent(OpenShiftConfig.url() + "/version/openshift");

// it is OpenShift 3, parse version from gitVersion and convert it
// example: v3.11.272 -> 3.11.272
openshiftVersion = ModelNode.fromJSONString(versionInfo).get("gitVersion").asString()
.replaceAll("^v(.*)", "$1");
} catch (HttpsException he) {
// it is OpenShift 4+
// admin is required for operation
try {
NonNamespaceOperation<ClusterVersion, ClusterVersionList, Resource<ClusterVersion>> op = OpenShiftHandlers
.getOperation(ClusterVersion.class, ClusterVersionList.class, OpenShifts.admin().getHttpClient(),
OpenShifts.admin().getConfiguration());
openshiftVersion = op.withName("version").get().getStatus().getDesired().getVersion();
} catch (KubernetesClientException kce) {
log.warn("xtf.openshift.version isn't configured and automatic version detection failed.", kce);
}
}
return openshiftVersion;
}

private Matcher validateConfiguredVersion(final String version) {
Objects.requireNonNull(version);

Matcher matcher = versionPattern.matcher(version);
if (!matcher.matches()) {
log.warn("Version {} configured in xtf.openshift.version isn't in expected format 'major.minor[.micro]'.", version);
}
return matcher;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cz.xtf.core.openshift;

enum ClusterVersionInfoFactory {
INSTANCE;

private volatile ClusterVersionInfo clusterVersionInfo;

public ClusterVersionInfo getClusterVersionInfo() {
ClusterVersionInfo localRef = clusterVersionInfo;
if (localRef == null) {
synchronized (ClusterVersionInfoFactory.class) {
localRef = clusterVersionInfo;
if (localRef == null) {
clusterVersionInfo = localRef = new ClusterVersionInfo();
}
}
}
return localRef;
}
}
Loading