def test_get_job_custom_resource(self): termination = V1Termination(max_retries=5, ttl=10, timeout=10) environment = V1Environment( labels={"foo": "bar"}, annotations={"foo": "bar"}, node_selector={"foo": "bar"}, node_name="foo", restart_policy="never", ) notifications = [ V1Notification(connections=["test"], trigger=V1Statuses.DONE) ] launcher, launcher_replica_template = self.get_replica(environment) worker, worker_replica_template = self.get_replica(environment) template_spec = { "cleanPodPolicy": "Running", "slotsPerWorker": 12, "replicaSpecs": { "Launcher": launcher_replica_template, "Worker": worker_replica_template, }, } custom_object = { "mpiJobSpec": template_spec, "termination": { "backoffLimit": termination.max_retries, "activeDeadlineSeconds": termination.timeout, "ttlSecondsAfterFinished": termination.ttl, }, "collectLogs": True, "syncStatuses": True, "notifications": [n.to_operator() for n in notifications], } expected_crd = get_custom_object( namespace="default", resource_name="foo", kind="Operation", api_version="core.polyaxon.com/v1", labels={"foo": "bar"}, annotations={"foo": "bar"}, custom_object=custom_object, ) crd = get_mpi_job_custom_resource( namespace="default", resource_name="foo", launcher=launcher, worker=worker, slots_per_worker=12, clean_pod_policy="Running", termination=termination, collect_logs=True, sync_statuses=True, notifications=notifications, labels=environment.labels, annotations={"foo": "bar"}, ) assert crd == expected_crd
def test_get_service_custom_resource(self): main_container = k8s_schemas.V1Container(name="main") sidecar_containers = [k8s_schemas.V1Container(name="sidecar")] init_containers = [k8s_schemas.V1Container(name="init")] termination = V1Termination(timeout=10) environment = V1Environment( labels={"foo": "bar"}, annotations={"foo": "bar"}, node_selector={"foo": "bar"}, node_name="foo", restart_policy="never", ) metadata, pod_spec = get_pod_spec( namespace="default", main_container=main_container, sidecar_containers=sidecar_containers, init_containers=init_containers, resource_name="foo", volumes=[], environment=environment, labels=environment.labels, ) custom_object = { "serviceSpec": { "template": get_pod_template_spec(metadata=metadata, pod_spec=pod_spec), }, "termination": { "activeDeadlineSeconds": termination.timeout }, "collectLogs": True, "syncStatuses": True, "notifications": [], } expected_crd = get_custom_object( namespace="default", resource_name="foo", kind="Operation", api_version="core.polyaxon.com/v1", labels={"foo": "bar"}, custom_object=custom_object, ) crd = get_service_custom_resource( namespace="default", resource_name="foo", main_container=main_container, sidecar_containers=sidecar_containers, init_containers=init_containers, volumes=[], termination=termination, environment=environment, labels=environment.labels, collect_logs=True, sync_statuses=True, notifications=None, ports=[], ) assert crd == expected_crd
def test_get_tf_job_custom_resource_with_no_workers(self): termination = V1Termination(max_retries=5, ttl=10, timeout=10) environment = V1Environment( labels={"foo": "bar"}, annotations={"foo": "bar"}, node_selector={"foo": "bar"}, node_name="foo", restart_policy="never", ) custom_object = { "tfJobSpec": { "cleanPodPolicy": "All", "replicaSpecs": {} }, "termination": { "backoffLimit": termination.max_retries, "activeDeadlineSeconds": termination.timeout, "ttlSecondsAfterFinished": termination.ttl, }, "collectLogs": False, "syncStatuses": False, "notifications": [], } expected_crd = get_custom_object( namespace="default", resource_name="foo", kind="Operation", api_version="core.polyaxon.com/v1", labels={"foo": "bar"}, annotations={"foo": "long-foo-bar" * 300}, custom_object=custom_object, ) crd = get_tf_job_custom_resource( namespace="default", resource_name="foo", chief=None, worker=None, ps=None, evaluator=None, clean_pod_policy=None, termination=termination, collect_logs=False, sync_statuses=False, notifications=None, labels=environment.labels, annotations={"foo": "long-foo-bar" * 300}, ) assert crd == expected_crd
def test_get_pod_spec(self): init_container = k8s_schemas.V1Container(name="init") main_container = k8s_schemas.V1Container(name="main") sidecar_container = k8s_schemas.V1Container(name="sidecar") volumes = [k8s_schemas.V1Volume(name="vol")] labels = {"key": "labels"} annotations = {"key": "annotations"} node_selector = {"key": "selector"} affinity = [{"key": "affinity"}] tolerations = {"key": "tolerations"} security_context = {"uid": 222, "gid": 222} restart_policy = "never" with self.assertRaises(PolypodException): get_pod_spec( namespace="default", main_container=None, sidecar_containers=None, init_containers=None, resource_name="foo", volumes=None, environment=V1Environment(), labels={}, annotations={}, ) environment = V1Environment( service_account_name="sa", labels=labels, annotations=annotations, node_selector=node_selector, affinity=affinity, tolerations=tolerations, security_context=security_context, image_pull_secrets=[], restart_policy=restart_policy, ) metadata, pod_spec = get_pod_spec( namespace="default", resource_name="foo", main_container=main_container, sidecar_containers=None, init_containers=None, volumes=None, environment=environment, labels=environment.labels, annotations=environment.annotations, ) assert metadata.name == "foo" assert metadata.labels == labels assert metadata.namespace == "default" assert metadata.annotations == annotations assert isinstance(pod_spec, k8s_schemas.V1PodSpec) assert pod_spec.security_context == security_context assert pod_spec.restart_policy == "never" assert pod_spec.service_account_name == "sa" assert pod_spec.init_containers == [] assert pod_spec.containers == [main_container] assert pod_spec.volumes is None assert pod_spec.node_selector == node_selector assert pod_spec.tolerations == tolerations assert pod_spec.affinity == affinity environment = V1Environment( service_account_name="sa", labels=labels, annotations=annotations, node_selector=node_selector, affinity=affinity, tolerations=tolerations, security_context=security_context, image_pull_secrets=[], restart_policy=restart_policy, ) metadata, pod_spec = get_pod_spec( namespace="default", main_container=main_container, sidecar_containers=[sidecar_container], init_containers=[init_container], resource_name="foo", volumes=volumes, environment=environment, labels={}, annotations={}, ) assert pod_spec.init_containers == [init_container] assert pod_spec.containers == [main_container, sidecar_container] assert pod_spec.volumes == volumes assert metadata.annotations == {}
def test_environment_config(self): # Resources config_dict = {"labels": {"foo": "bar"}} config = V1Environment.from_dict(config_dict) assert_equal_dict(config_dict, config.to_dict()) # Add node selectors config_dict["nodeSelector"] = {"polyaxon.com": "master"} config = V1Environment.from_dict(config_dict) assert_equal_dict(config_dict, config.to_dict()) # Add affinity config_dict["affinity"] = { "nodeAffinity": {"requiredDuringSchedulingIgnoredDuringExecution": {}} } config = V1Environment.from_dict(config_dict) assert config.affinity.node_affinity == { "requiredDuringSchedulingIgnoredDuringExecution": {} } assert_equal_dict(config_dict, config.to_dict()) # Add labels config_dict["labels"] = {"foo": "bar"} config = V1Environment.from_dict(config_dict) assert_equal_dict(config_dict, config.to_dict()) # Add annotations config_dict["annotations"] = {"foo": "bar"} config = V1Environment.from_dict(config_dict) assert_equal_dict(config_dict, config.to_dict()) # Add toleration config_dict["tolerations"] = [{"key": "key", "operator": "Exists"}] config = V1Environment.from_dict(config_dict) assert_equal_dict(config_dict, config.to_dict()) # Add service_account config_dict["serviceAccountName"] = "service_account_name" config = V1Environment.from_dict(config_dict) assert_equal_dict(config_dict, config.to_dict()) # Add image_pull_secrets config_dict["imagePullSecrets"] = ["secret1", "secret2"] config = V1Environment.from_dict(config_dict) assert_equal_dict(config_dict, config.to_dict()) # Add security context per job config_dict["securityContext"] = {"runAsUser": 1000, "runAsGroup": 3000} config = V1Environment.from_dict(config_dict) assert config.security_context.run_as_user == 1000 assert config.security_context.run_as_group == 3000 assert_equal_dict(config_dict, config.to_dict()) # Add restart_policy config_dict["restartPolicy"] = "never" config = V1Environment.from_dict(config_dict) assert_equal_dict(config_dict, config.to_dict())
def test_get_job_custom_resource(self): main_container = k8s_schemas.V1Container(name="main") sidecar_containers = [k8s_schemas.V1Container(name="sidecar")] init_containers = [k8s_schemas.V1Container(name="init")] termination = V1Termination(max_retries=5, ttl=10, timeout=10) environment = V1Environment( labels={"foo": "bar"}, annotations={"foo": "bar"}, node_selector={"foo": "bar"}, node_name="foo", restart_policy="never", ) notifications = [ V1Notification(connections=["test"], trigger=V1NotificationTrigger.DONE) ] metadata, pod_spec = get_pod_spec( namespace="default", main_container=main_container, sidecar_containers=sidecar_containers, init_containers=init_containers, resource_name="foo", volumes=[], environment=environment, labels=environment.labels, ) custom_object = { "batchJobSpec": { "template": get_pod_template_spec(metadata=metadata, pod_spec=pod_spec), }, "termination": { "backoffLimit": termination.max_retries, "activeDeadlineSeconds": termination.timeout, "ttlSecondsAfterFinished": termination.ttl, }, "collectLogs": True, "syncStatuses": True, "notifications": [n.to_operator() for n in notifications], } expected_crd = get_custom_object( namespace="default", resource_name="foo", kind="Operation", api_version="core.polyaxon.com/v1", labels={"foo": "bar"}, custom_object=custom_object, annotations={"foo": "long-foo-bar" * 300}, ) crd = get_job_custom_resource( namespace="default", resource_name="foo", main_container=main_container, sidecar_containers=sidecar_containers, init_containers=init_containers, volumes=[], termination=termination, environment=environment, collect_logs=True, sync_statuses=True, notifications=notifications, labels=environment.labels, annotations={"foo": "long-foo-bar" * 300}, ) assert crd == expected_crd