Skip to content

Commit

Permalink
add: abstraction to remove appwrapper yaml visibility from the NB con…
Browse files Browse the repository at this point in the history
…sole.
  • Loading branch information
VanillaSpoon authored and openshift-merge-bot[bot] committed Nov 13, 2023
1 parent 53177f2 commit c212bd8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/codeflare_sdk/cluster/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ def __init__(self, config: ClusterConfiguration):
"""
self.config = config
self.app_wrapper_yaml = self.create_app_wrapper()
self.app_wrapper_name = self.app_wrapper_yaml.split(".")[0]
self._job_submission_client = None
self.app_wrapper_name = self.app_wrapper_yaml.replace(".yaml", "").split("/")[
-1
]

@property
def _client_headers(self):
Expand Down
10 changes: 9 additions & 1 deletion src/codeflare_sdk/utils/generate_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import yaml
import sys
import os
import argparse
import uuid
from kubernetes import client, config
Expand Down Expand Up @@ -506,8 +507,14 @@ def disable_raycluster_tls(resources):


def write_user_appwrapper(user_yaml, output_file_name):
# Create the directory if it doesn't exist
directory_path = os.path.dirname(output_file_name)
if not os.path.exists(directory_path):
os.makedirs(directory_path)

with open(output_file_name, "w") as outfile:
yaml.dump(user_yaml, outfile, default_flow_style=False)

print(f"Written to: {output_file_name}")


Expand Down Expand Up @@ -675,7 +682,8 @@ def generate_appwrapper(
if openshift_oauth:
enable_openshift_oauth(user_yaml, cluster_name, namespace)

outfile = appwrapper_name + ".yaml"
directory_path = os.path.expanduser("~/.codeflare/appwrapper/")
outfile = os.path.join(directory_path, appwrapper_name + ".yaml")
if not mcad:
write_components(user_yaml, outfile)
else:
Expand Down
44 changes: 24 additions & 20 deletions tests/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from codeflare_sdk.cluster import cluster

parent = Path(__file__).resolve().parents[1]
aw_dir = os.path.expanduser("~/.codeflare/appwrapper/")
sys.path.append(str(parent) + "/src")

from kubernetes import client, config
Expand Down Expand Up @@ -261,10 +262,12 @@ def test_config_creation():

def test_cluster_creation(mocker):
cluster = createClusterWithConfig(mocker)
assert cluster.app_wrapper_yaml == "unit-test-cluster.yaml"
assert cluster.app_wrapper_yaml == f"{aw_dir}unit-test-cluster.yaml"
assert cluster.app_wrapper_name == "unit-test-cluster"
assert filecmp.cmp(
"unit-test-cluster.yaml", f"{parent}/tests/test-case.yaml", shallow=True
f"{aw_dir}unit-test-cluster.yaml",
f"{parent}/tests/test-case.yaml",
shallow=True,
)


Expand All @@ -290,10 +293,10 @@ def test_cluster_creation_no_mcad(mocker):
config.name = "unit-test-cluster-ray"
config.mcad = False
cluster = Cluster(config)
assert cluster.app_wrapper_yaml == "unit-test-cluster-ray.yaml"
assert cluster.app_wrapper_yaml == f"{aw_dir}unit-test-cluster-ray.yaml"
assert cluster.app_wrapper_name == "unit-test-cluster-ray"
assert filecmp.cmp(
"unit-test-cluster-ray.yaml",
f"{aw_dir}unit-test-cluster-ray.yaml",
f"{parent}/tests/test-case-no-mcad.yamls",
shallow=True,
)
Expand All @@ -313,10 +316,12 @@ def test_cluster_creation_priority(mocker):
return_value={"spec": {"domain": "apps.cluster.awsroute.org"}},
)
cluster = Cluster(config)
assert cluster.app_wrapper_yaml == "prio-test-cluster.yaml"
assert cluster.app_wrapper_yaml == f"{aw_dir}prio-test-cluster.yaml"
assert cluster.app_wrapper_name == "prio-test-cluster"
assert filecmp.cmp(
"prio-test-cluster.yaml", f"{parent}/tests/test-case-prio.yaml", shallow=True
f"{aw_dir}prio-test-cluster.yaml",
f"{parent}/tests/test-case-prio.yaml",
shallow=True,
)


Expand All @@ -335,7 +340,7 @@ def test_default_cluster_creation(mocker):
)
cluster = Cluster(default_config)

assert cluster.app_wrapper_yaml == "unit-test-default-cluster.yaml"
assert cluster.app_wrapper_yaml == f"{aw_dir}unit-test-default-cluster.yaml"
assert cluster.app_wrapper_name == "unit-test-default-cluster"
assert cluster.config.namespace == "opendatahub"

Expand Down Expand Up @@ -365,21 +370,21 @@ def arg_check_apply_effect(group, version, namespace, plural, body, *args):
if plural == "appwrappers":
assert group == "workload.codeflare.dev"
assert version == "v1beta1"
with open("unit-test-cluster.yaml") as f:
with open(f"{aw_dir}unit-test-cluster.yaml") as f:
aw = yaml.load(f, Loader=yaml.FullLoader)
assert body == aw
elif plural == "rayclusters":
assert group == "ray.io"
assert version == "v1alpha1"
with open("unit-test-cluster-ray.yaml") as f:
with open(f"{aw_dir}unit-test-cluster-ray.yaml") as f:
yamls = yaml.load_all(f, Loader=yaml.FullLoader)
for resource in yamls:
if resource["kind"] == "RayCluster":
assert body == resource
elif plural == "routes":
assert group == "route.openshift.io"
assert version == "v1"
with open("unit-test-cluster-ray.yaml") as f:
with open(f"{aw_dir}unit-test-cluster-ray.yaml") as f:
yamls = yaml.load_all(f, Loader=yaml.FullLoader)
for resource in yamls:
if resource["kind"] == "Route":
Expand Down Expand Up @@ -2408,7 +2413,7 @@ def parse_j(cmd):


def test_AWManager_creation():
testaw = AWManager("test.yaml")
testaw = AWManager(f"{aw_dir}test.yaml")
assert testaw.name == "test"
assert testaw.namespace == "ns"
assert testaw.submitted == False
Expand All @@ -2432,7 +2437,7 @@ def arg_check_aw_apply_effect(group, version, namespace, plural, body, *args):
assert version == "v1beta1"
assert namespace == "ns"
assert plural == "appwrappers"
with open("test.yaml") as f:
with open(f"{aw_dir}test.yaml") as f:
aw = yaml.load(f, Loader=yaml.FullLoader)
assert body == aw
assert args == tuple()
Expand All @@ -2448,7 +2453,7 @@ def arg_check_aw_del_effect(group, version, namespace, plural, name, *args):


def test_AWManager_submit_remove(mocker, capsys):
testaw = AWManager("test.yaml")
testaw = AWManager(f"{aw_dir}test.yaml")
testaw.remove()
captured = capsys.readouterr()
assert (
Expand Down Expand Up @@ -2876,13 +2881,12 @@ def test_gen_app_wrapper_with_oauth(mocker: MockerFixture):

# Make sure to always keep this function last
def test_cleanup():
os.remove("unit-test-cluster.yaml")
os.remove("prio-test-cluster.yaml")
os.remove("unit-test-default-cluster.yaml")
os.remove("unit-test-cluster-ray.yaml")
os.remove("test.yaml")
os.remove("raytest2.yaml")
os.remove("quicktest.yaml")
os.remove(f"{aw_dir}unit-test-cluster.yaml")
os.remove(f"{aw_dir}prio-test-cluster.yaml")
os.remove(f"{aw_dir}unit-test-default-cluster.yaml")
os.remove(f"{aw_dir}test.yaml")
os.remove(f"{aw_dir}raytest2.yaml")
os.remove(f"{aw_dir}quicktest.yaml")
os.remove("tls-cluster-namespace/ca.crt")
os.remove("tls-cluster-namespace/tls.crt")
os.remove("tls-cluster-namespace/tls.key")
Expand Down

0 comments on commit c212bd8

Please sign in to comment.