Skip to content

Commit

Permalink
Allow to login to internal or shared cluster
Browse files Browse the repository at this point in the history
Signed-off-by: Petr "Stone" Hracek <[email protected]>
  • Loading branch information
phracek committed Nov 12, 2024
1 parent 723c64a commit 98b5598
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
20 changes: 19 additions & 1 deletion container_ci_suite/engines/openshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
import logging
import time

from pathlib import Path
from subprocess import CalledProcessError
from typing import Dict, Any

from container_ci_suite.utils import run_oc_command
from container_ci_suite.utils import run_oc_command, get_file_content, load_shared_credentials, get_shared_variable


logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.DEBUG)
Expand All @@ -44,6 +45,23 @@ def __init__(self, pod_name_prefix: str = ""):
def set_namespace(self, namespace: str):
self.namespace = namespace

def login_to_cluster(self, shared_cluster: bool = False):
if shared_cluster:
token = load_shared_credentials("SHARED_CLUSTER_TOKEN")
url = get_shared_variable("shared_cluster_url")
if not all([token, url]):
print("Important variables 'SHARED_CLUSTER_TOKEN,shared_cluster_url' are missing.")
return None
cmd = f"login --token={token} --server={url}"
else:
url = load_shared_credentials("LOCAL_CLUSTER_URL")
password = get_file_content(filename=Path("/root/.kube/ocp-kube"))
cmd = f"login -u kubeadmin -p {password} --server={url}"
output = run_oc_command(cmd, json_output=False)
print(output)
output = run_oc_command("version", json_output=False)
print(output)

def get_pod_status(self) -> Dict:
# output = OpenShiftAPI.run_oc_command("get all", json_output=False)
# print(f"oc get all: {output}")
Expand Down
12 changes: 3 additions & 9 deletions container_ci_suite/openshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(
pod_name_prefix: str = "", create_prj: bool = True,
delete_prj: bool = True,
shared_cluster: bool = False,
version: str = ""
version: str = "",
):
self.create_prj = create_prj
self.delete_prj = delete_prj
Expand All @@ -71,6 +71,7 @@ def __init__(
def create_project(self):
print(f"Create project {self.create_prj} and {self.shared_cluster}")
if self.create_prj:
self.openshift_ops.login_to_cluster(shared_cluster=self.shared_cluster)
if self.shared_cluster:
self.shared_random_name = f"sclorg-{random.randrange(10000, 100000)}"
self.namespace = f"core-services-ocp--{self.shared_random_name}"
Expand Down Expand Up @@ -109,7 +110,6 @@ def create_egress_rules(self) -> bool:
def prepare_tenant_namespace(self):
print(f"Prepare Tenant Namespace with name: '{self.shared_random_name}'")
json_flag = False
self.login_to_shared_cluster()
if not self.create_tenant_namespace():
return False
# Let's wait 3 seconds till project is not up
Expand Down Expand Up @@ -189,13 +189,7 @@ def start_build(self, service_name: str, app_name: str = "") -> str:
return output

def login_to_shared_cluster(self):
token = utils.load_shared_credentials("SHARED_CLUSTER_TOKEN")
url = utils.get_shared_variable("shared_cluster_url")
if not all([token, url]):
print("Important variables 'SHARED_CLUSTER_TOKEN,shared_cluster_url' are missing.")
return None
output = run_oc_command(f"login --token={token} --server={url}", json_output=False)
print(output)
self.openshift_ops.login_to_cluster(shared_cluster=True)
output = run_oc_command("version", json_output=False)
print(output)
output = run_oc_command(f"project {self.config_tenant_name}", json_output=False)
Expand Down

0 comments on commit 98b5598

Please sign in to comment.