Skip to content

Commit

Permalink
Added changes for workload
Browse files Browse the repository at this point in the history
Signed-off-by: Avdhoot <[email protected]>
  • Loading branch information
avd-sagare committed Jun 26, 2024
1 parent 52e890b commit 1f09fc1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
19 changes: 16 additions & 3 deletions ocs_ci/helpers/dr_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import logging
import tempfile
import time

from ocs_ci.framework import config
from ocs_ci.ocs.acm.acm import (
Expand Down Expand Up @@ -1126,9 +1127,11 @@ def disable_dr_from_app(secondary_cluster_name):
cmd="oc get drpc -A",
expected_output_lst="No resources found",
)
if not sample.wait_for_func_status(result=True):
if not sample.wait_for_func_status(result=False):
raise Exception("All drpcs are not deleted")

time.sleep(10)

# Remove annotation from placements
for placement in placements:
name = placement["metadata"]["name"]
Expand Down Expand Up @@ -1184,11 +1187,21 @@ def replace_cluster(workload, primary_cluster_name, secondary_cluster_name):
run_cmd(cmd=f"chmod +x {constants.REMOVE_DR_EACH_MANAGED_CLUSTER}")
run_cmd(cmd=f"sh {constants.REMOVE_DR_EACH_MANAGED_CLUSTER}")

sample = TimeoutSampler(
timeout=300,
sleep=5,
func=run_cmd_verify_cli_output,
cmd="oc get namespace openshift-operators",
expected_output_lst={"openshift-operators", "Active"},
)
if not sample.wait_for_func_status(result=True):
raise Exception("Namespace openshift-operators is not created")

# add label to openshift-opeartors namespace
ocp_obj = ocp.OCP(kind="namespace")
ocp_obj = ocp.OCP(kind="Namespace")
label = "openshift.io/cluster-monitoring='true'"
ocp_obj.add_label(
kind="namespace", resource_name=constants.OPENSHIFT_OPERATORS, label=label
kind="Namespace", resource_name=constants.OPENSHIFT_OPERATORS, label=label
)

# Detach old primary
Expand Down
52 changes: 52 additions & 0 deletions ocs_ci/ocs/dr/dr_workload.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,14 @@ def __init__(self, **kwargs):
self.drpc_yaml_file = os.path.join(
self.workload_subscription_dir, self.workload_name, "drpc.yaml"
)
self.drpc_yaml_file_placement = os.path.join(constants.DRPC_PATH)
self.channel_yaml_file = os.path.join(
self.workload_subscription_dir, "channel.yaml"
)
self.placement_yaml_file = os.path.join(
self.workload_subscription_dir, self.workload_name, "placement.yaml"
)
self.workload_pvc_selector = kwargs.get("workload_pvc_selector")

def deploy_workload(self):
"""
Expand All @@ -168,6 +173,33 @@ def deploy_workload(self):
drpc_yaml_data["spec"]["drPolicyRef"]["name"] = self.dr_policy_name
templating.dump_data_to_temp_yaml(drpc_yaml_data, self.drpc_yaml_file)

# load placement.yaml
placement_yaml_data = templating.load_yaml(self.placement_yaml_file)
placement_yaml_data["spec"]["predicates"][0]["requiredClusterSelector"][
"labelSelector"
]["matchExpressions"][0]["values"][0] = self.preferred_primary_cluster
self.sub_placement_name = placement_yaml_data["metadata"]["name"]
templating.dump_data_to_temp_yaml(placement_yaml_data, self.placement_yaml_file)

if placement_yaml_data["kind"] == "Placement":
drpc_yaml_data = templating.load_yaml(self.drpc_yaml_file_placement)
drpc_yaml_data["metadata"]["name"] = f"{self.sub_placement_name}-drpc"
drpc_yaml_data["spec"]["preferredCluster"] = self.preferred_primary_cluster
drpc_yaml_data["spec"]["drPolicyRef"]["name"] = self.dr_policy_name
drpc_yaml_data["spec"]["placementRef"]["name"] = self.sub_placement_name

drpc_yaml_data["metadata"]["namespace"] = self.workload_namespace
drpc_yaml_data["spec"]["placementRef"][
"namespace"
] = self.workload_namespace
drpc_yaml_data["spec"]["pvcSelector"][
"matchLabels"
] = self.workload_pvc_selector
drcp_data_yaml = tempfile.NamedTemporaryFile(
mode="w+", prefix="drpc", delete=False
)
templating.dump_data_to_temp_yaml(drpc_yaml_data, drcp_data_yaml.name)

# TODO
# drpc_yaml_file needs to be committed back to the repo
# because ACM would refetch from repo directly
Expand All @@ -178,9 +210,13 @@ def deploy_workload(self):
templating.dump_data_to_temp_yaml(channel_yaml_data, self.channel_yaml_file)

# Create the resources on Hub cluster

config.switch_acm_ctx()
run_cmd(f"oc create -k {self.workload_subscription_dir}")
run_cmd(f"oc create -k {self.workload_subscription_dir}/{self.workload_name}")
if placement_yaml_data["kind"] == "Placement":
self.add_annotation_to_placement()
run_cmd(f"oc create -f {drcp_data_yaml.name}")

self.verify_workload_deployment()

Expand Down Expand Up @@ -208,6 +244,22 @@ def _get_workload_namespace(self):
namespace_yaml_data = templating.load_yaml(namespace_yaml_file)
return namespace_yaml_data["metadata"]["name"]

def add_annotation_to_placement(self):
"""
Add annotation to sub placements
"""

config.switch_acm_ctx()
placement_obj = ocp.OCP(
kind=constants.PLACEMENT,
resource_name=self.sub_placement_name,
namespace=self.workload_namespace,
)
placement_obj.annotate(
annotation="cluster.open-cluster-management.io/experimental-scheduling-disable='true'"
)

def verify_workload_deployment(self):
"""
Verify busybox workload
Expand Down

0 comments on commit 1f09fc1

Please sign in to comment.