Skip to content

Commit

Permalink
moved hypershift metadata collection in to respective class
Browse files Browse the repository at this point in the history
Signed-off-by: Murali Krishnasamy <[email protected]>
  • Loading branch information
mukrishn committed Aug 21, 2024
1 parent e9d6a06 commit 3c3e47c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
3 changes: 3 additions & 0 deletions libs/platforms/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ def create_cluster(self, platform, cluster_name):
def delete_cluster(self, platform, cluster_name):
pass

def get_metadata(self, platform, cluster_name):
pass

def platform_cleanup(self):
pass

Expand Down
31 changes: 26 additions & 5 deletions libs/platforms/rosa/hypershift/hypershift.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,35 @@ def _verify_provision_shard(self):
self.logging.error(f"No Provision Shard found for Service Cluster {self.environment['service_cluster']} on {self.environment['aws']['region']}")
return None

def get_mc(self, cluster_id):
def _get_mc(self, cluster_id):
self.logging.debug(f"Get the mgmt cluster of cluster {cluster_id}")
resp_code, resp_out, resp_err = self.utils.subprocess_exec(
"ocm get /api/clusters_mgmt/v1/clusters/" + cluster_id + "/hypershift",
extra_params={"universal_newlines": True},
)
return json.loads(resp_out).get("management_cluster", None) if resp_code == 0 else None

# Get Hypershift cluster metadata and set required platform environment variables
def get_metadata(self, platform, cluster_name):
metadata = super().get_metadata(platform, cluster_name)
self.logging.info(f"Getting information for cluster {cluster_name}")
metadata_code, metadata_out, metadata_err = self.utils.subprocess_exec(
"rosa describe cluster -c " + cluster_name + " -o json",
extra_params={"universal_newlines": True}
)
try:
status = json.loads(metadata_out)["state"]
except Exception as err:
self.logging.error(f"Cannot load metadata for cluster {cluster_name}")
self.logging.error(err)

if status == "ready":
cluster_mc = self._get_mc(self.get_cluster_id(cluster_name))
metadata["mgmt_cluster_name"] = cluster_mc
platform.environment["mc_kubeconfig"] = platform.environment["path"] + "/kubeconfig_" + cluster_mc

return metadata

def platform_cleanup(self):
super().platform_cleanup()
self.logging.info("Cleaning resources")
Expand Down Expand Up @@ -268,7 +289,7 @@ def delete_cluster(self, platform, cluster_name):
cluster_start_time = int(datetime.datetime.utcnow().timestamp())
cluster_info["uuid"] = self.environment["uuid"]
cluster_info["install_method"] = "rosa"
cluster_info["mgmt_cluster_name"] = self.get_mc(cluster_info["metadata"]["cluster_id"])
cluster_info["mgmt_cluster_name"] = self._get_mc(cluster_info["metadata"]["cluster_id"])
self.logging.info(f"Deleting cluster {cluster_name} on Hypershift Platform")
cleanup_code, cleanup_out, cleanup_err = self.utils.subprocess_exec("rosa delete cluster -c " + cluster_name + " -y --watch", cluster_info["path"] + "/cleanup.log", {'preexec_fn': self.utils.disable_signals})
cluster_delete_end_time = int(datetime.datetime.utcnow().timestamp())
Expand Down Expand Up @@ -473,15 +494,15 @@ def create_cluster(self, platform, cluster_name):

cluster_info['status'] = "Installing"
self.logging.info(f"Cluster {cluster_name} installation started on the {trying} try")
cluster_info["metadata"] = self.get_metadata(cluster_name)
cluster_info["metadata"] = self.get_metadata(platform, cluster_name)
cluster_info["install_try"] = trying
with concurrent.futures.ThreadPoolExecutor() as executor:
preflight_ch = executor.submit(self._preflight_wait, cluster_info["metadata"]["cluster_id"], cluster_name)
sc_namespace = executor.submit(self._namespace_wait, platform.environment["sc_kubeconfig"], cluster_info["metadata"]["cluster_id"], cluster_name, "Service") if platform.environment["sc_kubeconfig"] != "" else 0
cluster_info["preflight_checks"] = preflight_ch.result()
cluster_info["sc_namespace_timing"] = sc_namespace.result() - cluster_start_time if platform.environment["sc_kubeconfig"] != "" else None

mgmt_cluster_name = self.get_mc(cluster_info["metadata"]["cluster_id"])
mgmt_cluster_name = self._get_mc(cluster_info["metadata"]["cluster_id"])
self.environment["mc_kubeconfig"] = self.download_kubeconfig(mgmt_cluster_name, self.environment["path"])
mc_namespace = executor.submit(self._namespace_wait, platform.environment["mc_kubeconfig"], cluster_info["metadata"]["cluster_id"], cluster_name, "Management") if platform.environment["mc_kubeconfig"] != "" else 0
cluster_info["mc_namespace_timing"] = mc_namespace.result() - cluster_start_time if platform.environment["mc_kubeconfig"] != "" else None
Expand All @@ -495,7 +516,7 @@ def create_cluster(self, platform, cluster_name):
cluster_end_time = int(datetime.datetime.utcnow().timestamp())
index_time = datetime.datetime.utcnow().isoformat()
# Getting againg metadata to update the cluster status
cluster_info["metadata"] = self.get_metadata(cluster_name)
cluster_info["metadata"] = self.get_metadata(platform, cluster_name)
cluster_info["install_duration"] = cluster_end_time - cluster_start_time
access_timers = self.get_cluster_admin_access(cluster_name, cluster_info["path"])
cluster_info["kubeconfig"] = access_timers.get("kubeconfig", None)
Expand Down
3 changes: 2 additions & 1 deletion libs/platforms/rosa/rosa.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ def get_workers_ready(self, kubeconfig, cluster_name):
super().get_workers_ready(kubeconfig, cluster_name)
return Platform.get_workers_ready(self, kubeconfig, cluster_name)

def get_metadata(self, cluster_name):
def get_metadata(self, platform, cluster_name):
super().get_metadata(platform, cluster_name)
metadata = {}
self.logging.info(f"Getting information for cluster {cluster_name}")
metadata_code, metadata_out, metadata_err = self.utils.subprocess_exec("rosa describe cluster -c " + cluster_name + " -o json", extra_params={"universal_newlines": True})
Expand Down
2 changes: 1 addition & 1 deletion libs/platforms/rosa/terraform/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def create_cluster(self, platform, cluster_name):

cluster_info['status'] = "installed"
self.logging.info(f"Cluster {cluster_name} installation finished on the {trying} try")
cluster_info["metadata"] = self.get_metadata(cluster_name)
cluster_info["metadata"] = self.get_metadata(platform, cluster_name)
cluster_info["install_try"] = trying
cluster_info["install_duration"] = cluster_end_time - cluster_start_time
access_timers = self.get_cluster_admin_access(cluster_name, cluster_info["path"])
Expand Down
4 changes: 1 addition & 3 deletions libs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,11 @@ def get_cluster_info(self, platform):
loop_counter += 1
cluster_name = platform.environment["cluster_name_seed"] + "-" + str(loop_counter).zfill(4)
platform.environment["clusters"][cluster_name] = {}
platform.environment["clusters"][cluster_name]["metadata"] = platform.get_metadata(cluster_name)
platform.environment["clusters"][cluster_name]["metadata"] = platform.get_metadata(platform, cluster_name)
platform.environment["clusters"][cluster_name]["status"] = platform.environment["clusters"][cluster_name]["metadata"]["status"]
platform.environment["clusters"][cluster_name]["path"] = platform.environment["path"] + "/" + cluster_name
platform.environment["clusters"][cluster_name]["kubeconfig"] = platform.environment["clusters"][cluster_name]["path"] + "/kubeconfig"
platform.environment['clusters'][cluster_name]['workers'] = int(platform.environment["workers"].split(",")[(loop_counter - 1) % len(platform.environment["workers"].split(","))])
cluster_mc = platform.get_mc(platform.get_cluster_id(cluster_name))
platform.environment["mc_kubeconfig"] = platform.environment["path"] + "/kubeconfig_" + cluster_mc
return platform

def load_scheduler(self, platform):
Expand Down

0 comments on commit 3c3e47c

Please sign in to comment.