Skip to content
Draft
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
2 changes: 0 additions & 2 deletions ocs_ci/deployment/helpers/hypershift_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
)
from ocs_ci.utility.decorators import switch_to_orig_index_at_last
from ocs_ci.ocs.utils import get_namespce_name_by_pattern
from ocs_ci.deployment.ocp import OCPDeployment as BaseOCPDeployment

"""
This module contains the base class for HyperShift hosted cluster management.
Expand Down Expand Up @@ -379,7 +378,6 @@ class HyperShiftBase:

def __init__(self):
super().__init__()
BaseOCPDeployment(skip_download_installer=True).test_cluster()

bin_dir_rel_path = os.path.expanduser(config.RUN["bin_dir"])
self.bin_dir = os.path.abspath(bin_dir_rel_path)
Expand Down
6 changes: 6 additions & 0 deletions ocs_ci/framework/pytest_customization/marks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import pytest
from funcy import compose

from ocs_ci.framework.pytest_customization.ocscilib import (
generate_kubeconfig_using_creds,
)
from ocs_ci.ocs.exceptions import ClusterNotFoundException
from ocs_ci.framework import config
from ocs_ci.ocs.constants import (
Expand Down Expand Up @@ -459,6 +462,9 @@ def setup_multicluster_marker(marker_base, push_missing_configs=False):
and config.default_cluster_ctx.ENV_DATA["platform"].lower()
in HCI_PROVIDER_CLIENT_PLATFORMS
) and test_stage:
generate_kubeconfig_using_creds(
config.default_cluster_ctx.ENV_DATA["cluster_path"], config
)
hypershift_cluster_factory(
duty=DUTY_USE_EXISTING_HOSTED_CLUSTERS_PUSH_MISSING_CONFIG,
)
Expand Down
118 changes: 70 additions & 48 deletions ocs_ci/framework/pytest_customization/ocscilib.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,54 +610,7 @@ def process_cluster_cli_params(config):
)
set_cli_param(config, f"cluster_path{suffix}", cluster_path)

if not cluster_path:
raise ClusterPathNotProvidedError()
cluster_path = os.path.expanduser(cluster_path)
if not os.path.exists(cluster_path):
os.makedirs(cluster_path)

# create kubeconfig if doesn't exist and OCP url and kubeadmin password is provided
kubeconfig_path = os.path.join(
cluster_path, ocsci_config.RUN["kubeconfig_location"]
)
if not os.path.isfile(kubeconfig_path) and not (
get_cli_param(config, "deploy", default=False)
or get_cli_param(config, "teardown", default=False)
or get_cli_param(config, "kubeconfig")
):
if ocsci_config.RUN.get("kubeadmin_password") and ocsci_config.RUN.get(
"ocp_url"
):
log.info(
"Generating kubeconfig file from provided kubeadmin password and OCP URL"
)
# check and correct OCP URL (change it to API url if console url provided and add port if needed
ocp_api_url = ocsci_config.RUN.get("ocp_url").replace(
"console-openshift-console.apps", "api"
)
if ":6443" not in ocp_api_url:
ocp_api_url = ocp_api_url.rstrip("/") + ":6443"

cmd = (
f"oc login --username {ocsci_config.RUN['username']} "
f"--password {ocsci_config.RUN['kubeadmin_password']} "
f"{ocp_api_url} "
f"--kubeconfig {kubeconfig_path} "
"--insecure-skip-tls-verify=true"
)
result = exec_cmd(cmd, secrets=(ocsci_config.RUN["kubeadmin_password"],))
if result.returncode:
log.warning(f"executed command: {cmd}")
log.warning(f"returncode: {result.returncode}")
log.warning(f"stdout: {result.stdout}")
log.warning(f"stderr: {result.stderr}")
else:
log.warning(f"Kubeconfig file were created: {kubeconfig_path}.")
else:
raise ConfigurationError(
"Kubeconfig doesn't exists and RUN['kubeadmin_password'] and RUN['ocp_url'] "
"environment variables were not provided."
)
cluster_path = generate_kubeconfig_using_creds(cluster_path, config)

# Importing here cause once the function is invoked we have already config
# loaded, so this is OK to import once you sure that config is loaded.
Expand Down Expand Up @@ -833,6 +786,75 @@ def process_cluster_cli_params(config):
ocsci_config.RUN["skip_rpm_go_version_collection"] = skip_rpm_go_version_collection


def generate_kubeconfig_using_creds(cluster_path, config):
"""
Generate kubeconfig file from provided kubeadmin password and OCP URL

Args:
cluster_path (str): Path to cluster directory
config (pytest.config): Pytest config object

Raises:
ClusterPathNotProvidedError: If a cluster path is missing
ConfigurationError: If kubeconfig doesn't exist and RUN['kubeadmin_password'] and
RUN['ocp_url'] environment variables were not provided.

Returns:
str: Path to cluster directory

"""

if not cluster_path:
raise ClusterPathNotProvidedError()
cluster_path = os.path.expanduser(cluster_path)
if not os.path.exists(cluster_path):
os.makedirs(cluster_path)

# create kubeconfig if doesn't exist and OCP url and kubeadmin password is provided
kubeconfig_path = os.path.join(
cluster_path, ocsci_config.RUN["kubeconfig_location"]
)
if not os.path.isfile(kubeconfig_path) and not (
get_cli_param(config, "deploy", default=False)
or get_cli_param(config, "teardown", default=False)
or get_cli_param(config, "kubeconfig")
):
if ocsci_config.RUN.get("kubeadmin_password") and ocsci_config.RUN.get(
"ocp_url"
):
log.info(
"Generating kubeconfig file from provided kubeadmin password and OCP URL"
)
# check and correct OCP URL (change it to API url if console url provided and add port if needed
ocp_api_url = ocsci_config.RUN.get("ocp_url").replace(
"console-openshift-console.apps", "api"
)
if ":6443" not in ocp_api_url:
ocp_api_url = ocp_api_url.rstrip("/") + ":6443"

cmd = (
f"oc login --username {ocsci_config.RUN['username']} "
f"--password {ocsci_config.RUN['kubeadmin_password']} "
f"{ocp_api_url} "
f"--kubeconfig {kubeconfig_path} "
"--insecure-skip-tls-verify=true"
)
result = exec_cmd(cmd, secrets=[ocsci_config.RUN["kubeadmin_password"]])
if result.returncode:
log.warning(f"executed command: {cmd}")
log.warning(f"returncode: {result.returncode}")
log.warning(f"stdout: {result.stdout}")
log.warning(f"stderr: {result.stderr}")
else:
log.warning(f"Kubeconfig file were created: {kubeconfig_path}.")
else:
raise ConfigurationError(
"Kubeconfig doesn't exists and RUN['kubeadmin_password'] and RUN['ocp_url'] "
"environment variables were not provided."
)
return cluster_path


def pytest_collection_modifyitems(session, config, items):
"""
Add Polarion ID property to test cases that are marked with one.
Expand Down
Loading