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

[release-4.15]: download OCP nightly through GA'ed version incase of failures #10079

Merged
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
109 changes: 105 additions & 4 deletions ocs_ci/utility/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,11 +1058,17 @@ def get_openshift_client(
# record current working directory and switch to BIN_DIR
previous_dir = os.getcwd()
os.chdir(bin_dir)
url = get_openshift_mirror_url("openshift-client", version)
tarball = "openshift-client.tar.gz"
download_file(url, tarball)
run_cmd(f"tar xzvf {tarball} oc kubectl")
delete_file(tarball)
try:
url = get_openshift_mirror_url("openshift-client", version)
download_file(url, tarball)
run_cmd(f"tar xzvf {tarball} oc kubectl")
delete_file(tarball)
except Exception as e:
log.error(f"Failed to download the openshift client. Exception '{e}'")
# check given version is GA'ed or not
if "nightly" in version:
get_nightly_oc_via_ga(version, tarball)
if custom_ocp_image and not skip_if_client_downloaded_from_installer:
extract_ocp_binary_from_image("oc", custom_ocp_image, bin_dir)
try:
Expand Down Expand Up @@ -1093,6 +1099,83 @@ def get_openshift_client(
return client_binary_path


def is_ocp_version_gaed(version):
"""
Checks whether given OCP version is GA'ed or not

Args:
version (str): OCP version ( eg: 4.16, 4.15 )

Returns:
bool: True if OCP is GA'ed otherwise False

"""
channel = f"stable-{version}"
total_versions_count = len(get_available_ocp_versions(channel))
if total_versions_count != 0:
return True


def get_nightly_oc_via_ga(version, tarball="openshift-client.tar.gz"):
"""
Downloads the nightly OC via GA'ed version

Args:
version (str): nightly OC version to download
tarball (str): target name of the tarfile

"""
version_major_minor = str(
version_module.get_semantic_version(version, only_major_minor=True)
)

# For GA'ed version, check for N, N-1 and N-2 versions
for current_version_count in range(3):
previous_version = version_module.get_previous_version(
version_major_minor, current_version_count
)
log.debug(
f"previous version with count {current_version_count} is {previous_version}"
)
if is_ocp_version_gaed(previous_version):
# Download GA'ed version
pull_secret_path = os.path.join(constants.DATA_DIR, "pull-secret")
log.info(
f"version {previous_version} is GA'ed, use the same version to download oc"
)
config.DEPLOYMENT["ocp_url_template"] = (
"https://mirror.openshift.com/pub/openshift-v4/clients/"
"ocp/{version}/{file_name}-{os_type}-{version}.tar.gz"
)
ga_version = expose_ocp_version(f"{previous_version}-ga")
url = get_openshift_mirror_url("openshift-client", ga_version)
download_file(url, tarball)

# extract to tmp location, since we need to download the nightly version again
tmp_oc_path = "/tmp"
run_cmd(f"tar xzvf {tarball} -C {tmp_oc_path}")

# use appropriate oc based on glibc version
glibc_version = get_glibc_version()
if version_module.get_semantic_version(
glibc_version
) < version_module.get_semantic_version("2.34"):
oc_type = "oc.rhel8"
else:
oc_type = "oc"

# extract oc
cmd = (
f"{tmp_oc_path}/oc adm release extract -a {pull_secret_path} --command={oc_type} "
f"registry.ci.openshift.org/ocp/release:{version} --to ."
)
exec_cmd(cmd)
delete_file(tarball)
break
else:
log.debug(f"version {previous_version} is not GA'ed")


def get_vault_cli(bind_dir=None, force_download=False):
"""
Download vault based on platform
Expand Down Expand Up @@ -4747,3 +4830,21 @@ def exec_nb_db_query(query):
output = output[2:-1]

return output


def get_glibc_version():
"""
Gets the GLIBC version.

Returns:
str: GLIBC version

"""
cmd = "ldd --version ldd"
res = exec_cmd(cmd)
out = res.stdout.decode("utf-8")
version_match = re.search(r"ldd \(GNU libc\) (\d+\.\d+)", out)
if version_match:
return version_match.group(1)
else:
log.warning("GLIBC version number not found")
19 changes: 19 additions & 0 deletions ocs_ci/utility/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,22 @@ def compare_versions(expression):
)
v1, op, v2 = m.groups()
return eval(f"get_semantic_version(v1, True){op}get_semantic_version(v2, True)")


def get_previous_version(version, count=1):
"""
Fetches the nth previous version

Args:
version (str): Version ( eg: 4.16, 4.16.0-0.nightly-2024-06-25-194629)
count (int): previous version count. if count is 1, it will get 1st previous version.
if count is 2, it will get 2nd previous version.

Returns:
str: Previous version ( returns only major and minor version, eg: 4.15 )

"""
version = get_semantic_version(version, only_major_minor=True)
new_minor = version.minor - count
previous_version = f"{version.major}.{new_minor}"
return previous_version
Loading