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

mdr+cnv unique namespaces for cnv workloads #10110

Merged
merged 2 commits into from
Jul 17, 2024
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
6 changes: 3 additions & 3 deletions conf/ocsci/mdr_workload.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ ENV_DATA:
},
]
dr_cnv_workload_appset_push: [
{ name: "vm-appset-push-1", destination_namespace: "vm-appset-push-1", appset_model: "push",
{ name: "vm-appset-push-1", appset_model: "push",
workload_dir: "mdr/cnv-workload/appset/vm-appset-push-1",
dr_workload_app_placement_name: "vm-appset-push-1-placement", vm_name: "vm-workload-1",
vm_secret: "vm-secret-1", vm_username: "cirros",
dr_workload_app_pvc_selector: { 'appname': 'kubevirt' }, pod_count: 1, pvc_count: 1
},
]
dr_cnv_workload_appset_pull: [
{ name: "vm-appset-pull-1", destination_namespace: "vm-appset-pull-1", appset_model: "pull",
{ name: "vm-appset-pull-1", appset_model: "pull",
workload_dir: "mdr/cnv-workload/appset/vm-appset-pull-1",
dr_workload_app_placement_name: "vm-appset-pull-1-placement", vm_name: "vm-workload-1",
vm_secret: "vm-secret-1", vm_username: "cirros",
dr_workload_app_pvc_selector: { 'appname': 'kubevirt' }, pod_count: 1, pvc_count: 1
},
]
dr_cnv_workload_sub: [
{ name: "vm-sub-1", destination_namespace: "vm-sub-1", workload_dir: "mdr/cnv-workload/subscription/vm-sub-1",
{ name: "vm-subscription-1", workload_dir: "mdr/cnv-workload/subscription/vm-subscription-1",
dr_workload_app_placement_name: "vm-sub-1-placement", vm_name: "vm-workload-1",
vm_secret: "vm-secret-1", vm_username: "cirros",
dr_workload_app_pvc_selector: { 'appname': 'kubevirt' }, pod_count: 1, pvc_count: 1
Expand Down
84 changes: 53 additions & 31 deletions ocs_ci/ocs/dr/dr_workload.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
delete_volume_in_backend,
verify_volume_deleted_in_backend,
create_project,
create_unique_resource_name,
)
from ocs_ci.ocs import constants, ocp
from ocs_ci.ocs.cnv.virtual_machine import VirtualMachine
Expand Down Expand Up @@ -545,6 +546,8 @@ class CnvWorkload(DRWorkload):

"""

_repo_cloned = False

def __init__(self, **kwargs):
"""
Initialize CnvWorkload instance
Expand All @@ -561,7 +564,7 @@ def __init__(self, **kwargs):
self.vm_obj = None
self.vm_username = kwargs.get("vm_username")
self.workload_type = kwargs.get("workload_type")
self.workload_namespace = kwargs.get("workload_namespace", None)
self.workload_namespace = create_unique_resource_name("ns", "vm")
self.workload_pod_count = kwargs.get("workload_pod_count")
self.workload_pvc_count = kwargs.get("workload_pvc_count")
self.dr_policy_name = kwargs.get(
Expand All @@ -576,6 +579,12 @@ def __init__(self, **kwargs):
self.cnv_workload_dir = os.path.join(
self.target_clone_dir, kwargs.get("workload_dir")
)
if self.workload_type == constants.SUBSCRIPTION:
self.channel_name = ""
self.channel_namespace = create_unique_resource_name("ns", "channel")
self.channel_yaml_file = os.path.join(
self.target_clone_dir, kwargs.get("workload_dir"), "channel.yaml"
)
self.cnv_workload_yaml_file = os.path.join(
self.cnv_workload_dir, self.workload_name + ".yaml"
)
Expand Down Expand Up @@ -619,12 +628,40 @@ def deploy_workload(self):
cnv_workload_yaml_data_load = list(
templating.load_yaml(self.cnv_workload_yaml_file, multi_document=True)
)
log.info(cnv_workload_yaml_data_load)
if self.workload_type == constants.SUBSCRIPTION:
# load channel.yaml
channel_yaml_data_load = list(
templating.load_yaml(self.channel_yaml_file, multi_document=True)
)
for channel_yaml_data in channel_yaml_data_load:
if channel_yaml_data["kind"] == "Namespace":
channel_yaml_data["metadata"]["name"] = self.channel_namespace
elif channel_yaml_data["kind"] == "Channel":
self.channel_name = channel_yaml_data["metadata"]["name"]
channel_yaml_data["spec"]["pathname"] = self.workload_repo_url
channel_yaml_data["metadata"]["namespace"] = self.channel_namespace
templating.dump_data_to_temp_yaml(
channel_yaml_data_load, self.channel_yaml_file
)
for cnv_workload_yaml_data in cnv_workload_yaml_data_load:
if self.workload_type == constants.SUBSCRIPTION:
# Update channel for Subscription apps
if cnv_workload_yaml_data["kind"] == "Channel":
cnv_workload_yaml_data["spec"]["pathname"] = self.workload_repo_url
if cnv_workload_yaml_data["kind"] == "Namespace":
cnv_workload_yaml_data["metadata"]["name"] = self.workload_namespace
elif cnv_workload_yaml_data["kind"] == "Application":
cnv_workload_yaml_data["metadata"][
"namespace"
] = self.workload_namespace
elif cnv_workload_yaml_data["kind"] == "Subscription":
cnv_workload_yaml_data["metadata"][
"namespace"
] = self.workload_namespace
cnv_workload_yaml_data["spec"][
"channel"
] = f"{self.channel_namespace}/{self.channel_name}"
elif cnv_workload_yaml_data["kind"] == "ManagedClusterSetBinding":
cnv_workload_yaml_data["metadata"][
"namespace"
] = self.workload_namespace
elif cnv_workload_yaml_data["kind"] == "ApplicationSet":
cnv_workload_yaml_data["metadata"]["name"] = self.workload_name
# Change the destination namespace for AppSet workload
Expand Down Expand Up @@ -685,6 +722,8 @@ def deploy_workload(self):
cnv_workload_yaml_data_load, self.cnv_workload_yaml_file
)
config.switch_acm_ctx()
if self.workload_type == constants.SUBSCRIPTION:
run_cmd(f"oc create -f {self.channel_yaml_file}")
run_cmd(f"oc create -f {self.cnv_workload_yaml_file}")
self.add_annotation_to_placement()
run_cmd(f"oc create -f {drcp_data_yaml.name}")
Expand All @@ -696,11 +735,13 @@ def _deploy_prereqs(self):

"""
# Clone workload repo
clone_repo(
url=self.workload_repo_url,
location=self.target_clone_dir,
branch=self.workload_repo_branch,
)
if not CnvWorkload._repo_cloned:
CnvWorkload._repo_cloned = True
clone_repo(
url=self.workload_repo_url,
location=self.target_clone_dir,
branch=self.workload_repo_branch,
)

def add_annotation_to_placement(self):
"""
Expand All @@ -722,26 +763,6 @@ def add_annotation_to_placement(self):
annotation="cluster.open-cluster-management.io/experimental-scheduling-disable='true'"
)

def _get_workload_namespace(self):
"""
Get the workload namespace

"""

cnv_workload_data = list(
templating.load_yaml(self.cnv_workload_yaml_file, multi_document=True)
)

for _wl_data in cnv_workload_data:
if self.workload_type == constants.APPLICATION_SET:
if _wl_data["kind"] == constants.APPLICATION_SET:
return _wl_data["spec"]["template"]["spec"]["destination"][
"namespace"
]
else:
if _wl_data["kind"] == constants.SUBSCRIPTION:
return _wl_data["metadata"]["namespace"]

def _get_workload_name(self):
"""
Get cnv workload name
Expand Down Expand Up @@ -792,7 +813,8 @@ def delete_workload(self, force=False):
try:
config.switch_acm_ctx()
run_cmd(cmd=f"oc delete -f {self.cnv_workload_yaml_file}", timeout=900)

if self.workload_type == constants.SUBSCRIPTION:
run_cmd(f"oc delete -f {self.channel_yaml_file}")
for cluster, secret_obj in zip(
get_non_acm_cluster_config(), self.vm_secret_obj
):
Expand Down
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6691,7 +6691,6 @@ def factory(
vm_secret=workload_details["vm_secret"],
vm_username=workload_details["vm_username"],
workload_name=workload_details["name"],
workload_namespace=workload_details["destination_namespace"],
workload_pod_count=workload_details["pod_count"],
workload_pvc_count=workload_details["pvc_count"],
workload_placement_name=workload_details[
Expand Down
Loading