Skip to content

Commit

Permalink
Replace PlacementRule with Placement
Browse files Browse the repository at this point in the history
PlacementRule is deprecated and replaced by Placement in OpenShift for a
while. We want to test only Placement, so testing with upstream is more
likely to reveal issues with OpenShift.

This change should be merge together with this ocm-ramen-samples PR:
RamenDR/ocm-ramen-samples#39

Signed-off-by: Nir Soffer <nsoffer@redhat.com>
  • Loading branch information
nirs committed Oct 9, 2023
1 parent 5cbb0f1 commit 31fc932
Showing 1 changed file with 33 additions and 59 deletions.
92 changes: 33 additions & 59 deletions test/drenv/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
parser = None
log_format = "%(asctime)s %(levelname)-7s [%(name)s] %(message)s"

OCM_SCHEDULING_DISABLE = (
"cluster.open-cluster-management.io/experimental-scheduling-disable"
)


def start(name, file, config_file="config.yaml"):
global workdir, config, parser, log
Expand Down Expand Up @@ -134,11 +138,21 @@ def enable_dr():
"""
Enable DR for deployed application.
"""
cluster = lookup_cluster()
placement = _lookup_app_resource("placement")
if not placement:
raise RuntimeError(f"Cannot find placement for application {config['name']}")

# TODO: support placement
info("Setting placementrule scheduler to ramen")
_patch_placementrule({"spec": {"schedulerName": "ramen"}})
info("Disabling OCM scheduling for '%s'", placement)
kubectl.annotate(
placement,
{OCM_SCHEDULING_DISABLE: "true"},
namespace=config["namespace"],
context=env["hub"],
log=debug,
)

cluster = lookup_cluster()
placement_name = placement.split("/")[1]

drpc = f"""
apiVersion: ramendr.openshift.io/v1alpha1
Expand All @@ -153,8 +167,8 @@ def enable_dr():
drPolicyRef:
name: dr-policy
placementRef:
kind: PlacementRule
name: busybox-placement
kind: Placement
name: {placement_name}
pvcSelector:
matchLabels:
appname: busybox
Expand All @@ -180,24 +194,16 @@ def disable_dr():
log=debug,
)

# TODO: support placement
info("Clearing placementrule scheduler")
_patch_placementrule({"spec": {"schedulerName": None}})


def _patch_placementrule(patch):
placementrule = _lookup_app_resource("placementrule")
if not placementrule:
raise RuntimeError(
f"Cannot find placementrule for application {config['name']}"
)
placement = _lookup_app_resource("placement")
if not placement:
debug("placement already removed")
return

kubectl.patch(
placementrule,
"--patch",
json.dumps(patch),
"--type=merge",
f"--namespace={config['namespace']}",
info("Enabling OCM scheduling for '%s'", placement)
kubectl.annotate(
placement,
{OCM_SCHEDULING_DISABLE: None},
namespace=config["namespace"],
context=env["hub"],
log=debug,
)
Expand All @@ -218,25 +224,16 @@ def lookup_cluster():
"""
Return the current cluster the application is placed on.
"""
cluster = _get_cluster_from_placement_decisions()
if not cluster:
cluster = _get_cluster_from_placementrule()
if not cluster:
raise RuntimeError(f"Cannot find cluster for application {config['name']}")

return cluster


def _get_cluster_from_placement_decisions():
placement = _lookup_app_resource("placement")
if not placement:
return None
raise RuntimeError(f"Cannot find placement for application {config['name']}")

info("Waiting for '%s' decisions", placement)
kubectl.wait(
placement,
"--for=condition=PlacementSatisfied",
"--timeout=60s",
f"--namespace={config['namespace']}",
context=env["hub"],
log=debug,
)
Expand All @@ -245,32 +242,9 @@ def _get_cluster_from_placement_decisions():

return kubectl.get(
"placementdecisions",
f"--selector=cluster.open-cluster-management.io/placement={placement_name}"
f"--namespace={config['namespace']}",
"--output=jsonpath={.status.decisions[0].clusterName}",
context=env["hub"],
)


def _get_cluster_from_placementrule():
placementrule = _lookup_app_resource("placementrule")
if not placementrule:
return None

info("Waiting for '%s' decisions", placementrule)
drenv.wait_for(
placementrule,
output="jsonpath={.status.decisions}",
namespace=config["namespace"],
timeout=60,
profile=env["hub"],
log=debug,
)

return kubectl.get(
placementrule,
f"--selector=cluster.open-cluster-management.io/placement={placement_name}",
f"--namespace={config['namespace']}",
"--output=jsonpath={.status.decisions[0].clusterName}",
"--output=jsonpath={.items[0].status.decisions[0].clusterName}",
context=env["hub"],
)

Expand Down

0 comments on commit 31fc932

Please sign in to comment.