Beispiel #1
0
    def test_get_pod_volumes_with_specified_values(self):
        volumes, volume_mounts = get_pod_volumes(persistence_outputs='outputs2',
                                                 persistence_data=['data2'])
        assert len(volumes) == 2
        assert volumes[0].host_path.path == '/root/outputs'
        assert volumes[1].host_path.path == '/root/data'
        assert volume_mounts[0].mount_path == '/outputs/2'
        assert volume_mounts[1].mount_path == '/data/2'

        volumes, volume_mounts = get_pod_volumes(persistence_outputs='outputs1',
                                                 persistence_data=['data1', 'data2'])
        assert len(volumes) == 3
        assert volumes[0].persistent_volume_claim.claim_name == 'test-claim-outputs-1'

        data_claim_name = None
        data_host_path = None
        mount_path1 = None
        mount_path2 = None
        if volumes[1].name == 'data1':
            data_claim_name = volumes[1].persistent_volume_claim.claim_name
            data_host_path = volumes[2].host_path.path
            mount_path1 = volume_mounts[1].mount_path
            mount_path2 = volume_mounts[2].mount_path
        elif volumes[1].name == 'data2':
            data_host_path = volumes[1].host_path.path
            data_claim_name = volumes[2].persistent_volume_claim.claim_name
            mount_path2 = volume_mounts[1].mount_path
            mount_path1 = volume_mounts[2].mount_path

        assert data_claim_name == 'test-claim-data-1'
        assert data_host_path == '/root/data'
        assert mount_path1 == '/data/1'
        assert mount_path2 == '/data/2'
Beispiel #2
0
    def test_default_get_pod_volumes_with_updated_settings(self):
        volumes, volume_mounts = get_pod_volumes(persistence_outputs='outputs1',
                                                 persistence_data=None)
        assert len(volumes) == 3  # Data3 won't be included because it's a bucket
        if volumes[0].name == 'outputs1':
            assert volumes[0].persistent_volume_claim.claim_name == 'test-claim-outputs-1'
            assert volume_mounts[0].mount_path == '/outputs/1'
        elif volumes[0].name == 'outputs2':
            assert volumes[0].host_path.path == '/root/outputs'
            assert volume_mounts[0].mount_path == '/outputs/2'

        data_claim_name = None
        data_host_path = None
        mount_path1 = None
        mount_path2 = None
        if volumes[1].name == 'data1':
            data_claim_name = volumes[1].persistent_volume_claim.claim_name
            data_host_path = volumes[2].host_path.path
            mount_path1 = volume_mounts[1].mount_path
            mount_path2 = volume_mounts[2].mount_path
        elif volumes[1].name == 'data2':
            data_host_path = volumes[1].host_path.path
            data_claim_name = volumes[2].persistent_volume_claim.claim_name
            mount_path2 = volume_mounts[1].mount_path
            mount_path1 = volume_mounts[2].mount_path

        assert data_claim_name == 'test-claim-data-1'
        assert data_host_path == '/root/data'
        assert mount_path1 == '/data/1'
        assert mount_path2 == '/data/2'
Beispiel #3
0
    def start_notebook(self,
                       persistence_outputs=None,
                       persistence_data=None,
                       outputs_refs_jobs=None,
                       outputs_refs_experiments=None,
                       resources=None,
                       labels=None,
                       annotations=None,
                       secret_refs=None,
                       config_map_refs=None,
                       node_selector=None,
                       affinity=None,
                       tolerations=None,
                       backend=None,
                       max_restarts=None,
                       reconcile_url=None,
                       mount_code_in_notebooks=False):
        ports = [self.request_notebook_port()]
        target_ports = [self.port]
        volumes, volume_mounts = get_pod_volumes(persistence_outputs=persistence_outputs,
                                                 persistence_data=persistence_data)
        refs_volumes, refs_volume_mounts = get_pod_refs_outputs_volumes(
            outputs_refs=outputs_refs_jobs,
            persistence_outputs=persistence_outputs)
        volumes += refs_volumes
        volume_mounts += refs_volume_mounts
        refs_volumes, refs_volume_mounts = get_pod_refs_outputs_volumes(
            outputs_refs=outputs_refs_experiments,
            persistence_outputs=persistence_outputs)
        volumes += refs_volumes
        volume_mounts += refs_volume_mounts
        shm_volumes, shm_volume_mounts = get_shm_volumes()
        volumes += shm_volumes
        volume_mounts += shm_volume_mounts

        context_volumes, context_mounts = get_auth_context_volumes()
        volumes += context_volumes
        volume_mounts += context_mounts

        if mount_code_in_notebooks:
            code_volume, code_volume_mount = self.get_notebook_code_volume()
            volumes.append(code_volume)
            volume_mounts.append(code_volume_mount)

        resource_name = self.resource_manager.get_resource_name()
        args = self.get_notebook_args(deployment_name=resource_name,
                                      mount_code_in_notebooks=mount_code_in_notebooks,
                                      backend=backend)
        command = ["/bin/sh", "-c"]
        labels = get_labels(default_labels=self.resource_manager.labels, labels=labels)
        deployment = self.resource_manager.get_deployment(
            resource_name=resource_name,
            volume_mounts=volume_mounts,
            volumes=volumes,
            labels=labels,
            env_vars=None,
            command=command,
            args=args,
            init_env_vars=self.get_init_env_vars(),
            persistence_outputs=persistence_outputs,
            persistence_data=persistence_data,
            outputs_refs_jobs=outputs_refs_jobs,
            outputs_refs_experiments=outputs_refs_experiments,
            secret_refs=secret_refs,
            config_map_refs=config_map_refs,
            resources=resources,
            annotations=annotations,
            ephemeral_token=None,
            node_selector=node_selector,
            affinity=affinity,
            tolerations=tolerations,
            ports=target_ports,
            init_context_mounts=context_mounts,
            reconcile_url=reconcile_url,
            max_restarts=max_restarts,
            restart_policy=get_deployment_restart_policy(max_restarts))
        dep_resp, _ = self.create_or_update_deployment(name=resource_name,
                                                       body=deployment,
                                                       reraise=True)
        service = services.get_service(
            namespace=self.namespace,
            name=resource_name,
            labels=self.resource_manager.get_labels(),
            ports=ports,
            target_ports=target_ports,
            service_type=self._get_service_type())
        service_resp, _ = self.create_or_update_service(name=resource_name,
                                                        body=service,
                                                        reraise=True)
        results = {'deployment': dep_resp.to_dict(), 'service': service_resp.to_dict()}

        if self._use_ingress():
            annotations = json.loads(conf.get(K8S_INGRESS_ANNOTATIONS))
            paths = [{
                'path': '/notebooks/{}'.format(self.project_name.replace('.', '/')),
                'backend': {
                    'serviceName': resource_name,
                    'servicePort': ports[0]
                }
            }]
            ingress = ingresses.get_ingress(namespace=self.namespace,
                                            name=resource_name,
                                            labels=self.resource_manager.get_labels(),
                                            annotations=annotations,
                                            paths=paths)
            self.create_or_update_ingress(name=resource_name, body=ingress, reraise=True)
        return results
Beispiel #4
0
    def start_job(self,
                  container_cmd_callback,
                  persistence_outputs=None,
                  persistence_data=None,
                  outputs_refs_jobs=None,
                  outputs_refs_experiments=None,
                  resources=None,
                  labels=None,
                  annotations=None,
                  secret_refs=None,
                  config_map_refs=None,
                  node_selector=None,
                  affinity=None,
                  tolerations=None,
                  reconcile_url=None,
                  max_restarts=None):
        # Set and validate volumes
        volumes, volume_mounts = get_pod_volumes(
            persistence_outputs=persistence_outputs,
            persistence_data=persistence_data)
        refs_volumes, refs_volume_mounts = get_pod_refs_outputs_volumes(
            outputs_refs=outputs_refs_jobs,
            persistence_outputs=persistence_outputs)
        volumes += refs_volumes
        volume_mounts += refs_volume_mounts
        refs_volumes, refs_volume_mounts = get_pod_refs_outputs_volumes(
            outputs_refs=outputs_refs_experiments,
            persistence_outputs=persistence_outputs)
        volumes += refs_volumes
        volume_mounts += refs_volume_mounts
        shm_volumes, shm_volume_mounts = get_shm_volumes()
        volumes += shm_volumes
        volume_mounts += shm_volume_mounts

        context_volumes, context_mounts = get_auth_context_volumes()
        volumes += context_volumes
        volume_mounts += context_mounts

        command, args = container_cmd_callback()
        resource_name = self.resource_manager.get_resource_name()
        labels = get_labels(default_labels=self.resource_manager.labels,
                            labels=labels)
        pod = self.resource_manager.get_pod(
            resource_name=resource_name,
            volume_mounts=volume_mounts,
            volumes=volumes,
            labels=labels,
            env_vars=None,
            command=command,
            args=args,
            init_env_vars=self.get_init_env_vars(),
            persistence_outputs=persistence_outputs,
            persistence_data=persistence_data,
            outputs_refs_jobs=outputs_refs_jobs,
            outputs_refs_experiments=outputs_refs_experiments,
            secret_refs=secret_refs,
            config_map_refs=config_map_refs,
            resources=resources,
            annotations=annotations,
            ephemeral_token=None,
            node_selector=node_selector,
            affinity=affinity,
            tolerations=tolerations,
            init_context_mounts=context_mounts,
            reconcile_url=reconcile_url,
            max_restarts=max_restarts,
            restart_policy=get_pod_restart_policy(max_restarts))
        pod_resp, _ = self.create_or_update_pod(name=resource_name,
                                                body=pod,
                                                reraise=True)

        return pod_resp.to_dict()
Beispiel #5
0
    def _create_job(self,
                    task_type,
                    task_idx,
                    add_service,
                    command=None,
                    args=None,
                    env_vars=None,
                    resources=None,
                    annotations=None,
                    node_selector=None,
                    affinity=None,
                    tolerations=None,
                    max_restarts=None):
        ephemeral_token = None
        if self.token_scope:
            ephemeral_token = RedisEphemeralTokens.generate_header_token(scope=self.token_scope)
        resource_name = self.resource_manager.get_resource_name(task_type=task_type,
                                                                task_idx=task_idx)
        job_uuid = self.get_job_uuids(task_type=task_type, task_idx=task_idx)
        reconcile_url = get_experiment_reconcile_url(self.experiment_name, job_uuid)
        labels = self.get_labels(task_type=task_type,
                                 task_idx=task_idx,
                                 job_uuid=job_uuid)

        # Set and validate volumes
        volumes, volume_mounts = get_pod_volumes(
            persistence_outputs=self.persistence_config.outputs,
            persistence_data=self.persistence_config.data)
        refs_volumes, refs_volume_mounts = get_pod_refs_outputs_volumes(
            outputs_refs=self.outputs_refs_jobs,
            persistence_outputs=self.persistence_config.outputs)
        volumes += refs_volumes
        volume_mounts += refs_volume_mounts
        refs_volumes, refs_volume_mounts = get_pod_refs_outputs_volumes(
            outputs_refs=self.outputs_refs_experiments,
            persistence_outputs=self.persistence_config.outputs)
        volumes += refs_volumes
        volume_mounts += refs_volume_mounts
        shm_volumes, shm_volume_mounts = get_shm_volumes()
        volumes += shm_volumes
        volume_mounts += shm_volume_mounts

        context_volumes, context_mounts = get_auth_context_volumes()
        volumes += context_volumes
        volume_mounts += context_mounts

        pod = self.resource_manager.get_task_pod(
            task_type=task_type,
            task_idx=task_idx,
            volume_mounts=volume_mounts,
            volumes=volumes,
            labels=labels,
            env_vars=env_vars,
            command=command,
            args=args,
            ports=self.ports,
            init_env_vars=self.get_init_env_vars(),
            persistence_outputs=self.persistence_config.outputs,
            persistence_data=self.persistence_config.data,
            outputs_refs_jobs=self.outputs_refs_jobs,
            outputs_refs_experiments=self.outputs_refs_experiments,
            secret_refs=self.spec.secret_refs,
            config_map_refs=self.spec.config_map_refs,
            resources=resources,
            ephemeral_token=ephemeral_token,
            node_selector=node_selector,
            affinity=affinity,
            tolerations=tolerations,
            init_context_mounts=context_mounts,
            reconcile_url=reconcile_url,
            max_restarts=max_restarts,
            restart_policy=get_pod_restart_policy(max_restarts))
        pod_resp, _ = self.create_or_update_pod(name=resource_name, body=pod, reraise=True)
        results = {'pod': pod_resp.to_dict()}
        if add_service:
            service = services.get_service(namespace=self.namespace,
                                           name=resource_name,
                                           labels=labels,
                                           ports=self.ports,
                                           target_ports=self.ports)
            service_resp, _ = self.create_or_update_service(name=resource_name,
                                                            body=service,
                                                            reraise=True)
            results['service'] = service_resp.to_dict()
        return results
Beispiel #6
0
    def _create_job(
            self,  # pylint:disable=arguments-differ
            task_type,
            command=None,
            args=None,
            env_vars=None,
            resources=None,
            annotations=None,
            node_selector=None,
            affinity=None,
            tolerations=None,
            replicas=1,
            restart_policy='Never'):
        ephemeral_token = None
        if self.token_scope:
            ephemeral_token = RedisEphemeralTokens.generate_header_token(
                scope=self.token_scope)
        resource_name = self.resource_manager.get_kf_resource_name(
            task_type=task_type)
        labels = self.resource_manager.get_labels(task_type=task_type)

        # Set and validate volumes
        volumes, volume_mounts = get_pod_volumes(
            persistence_outputs=self.persistence_config.outputs,
            persistence_data=self.persistence_config.data)
        refs_volumes, refs_volume_mounts = get_pod_refs_outputs_volumes(
            outputs_refs=self.outputs_refs_jobs,
            persistence_outputs=self.persistence_config.outputs)
        volumes += refs_volumes
        volume_mounts += refs_volume_mounts
        refs_volumes, refs_volume_mounts = get_pod_refs_outputs_volumes(
            outputs_refs=self.outputs_refs_experiments,
            persistence_outputs=self.persistence_config.outputs)
        volumes += refs_volumes
        volume_mounts += refs_volume_mounts
        shm_volumes, shm_volume_mounts = get_shm_volumes()
        volumes += shm_volumes
        volume_mounts += shm_volume_mounts

        context_volumes, context_mounts = get_auth_context_volumes()
        volumes += context_volumes
        volume_mounts += context_mounts

        pod_template_spec = self.resource_manager.get_pod_template_spec(
            resource_name=resource_name,
            volume_mounts=volume_mounts,
            volumes=volumes,
            labels=labels,
            env_vars=env_vars,
            command=command,
            args=args,
            ports=self.ports,
            init_env_vars=self.get_init_env_vars(),
            persistence_outputs=self.persistence_config.outputs,
            persistence_data=self.persistence_config.data,
            outputs_refs_jobs=self.outputs_refs_jobs,
            outputs_refs_experiments=self.outputs_refs_experiments,
            secret_refs=self.spec.secret_refs,
            config_map_refs=self.spec.config_map_refs,
            resources=resources,
            annotations=annotations,
            ephemeral_token=ephemeral_token,
            node_selector=node_selector,
            affinity=affinity,
            tolerations=tolerations,
            init_context_mounts=context_mounts,
            restart_policy=restart_policy)
        return {
            'replicas': replicas,
            'restartPolicy': restart_policy,
            'template': pod_template_spec
        }
Beispiel #7
0
 def test_default_get_pod_volumes(self):
     volumes, _ = get_pod_volumes(persistence_outputs=None, persistence_data=None)
     assert len(volumes) == 2
     assert volumes[0].persistent_volume_claim.claim_name == 'test-claim-outputs'
     assert volumes[1].persistent_volume_claim.claim_name == 'test-claim-data'
Beispiel #8
0
 def test_get_pod_volumes_with_buckets_values_only(self):
     volumes, _ = get_pod_volumes(persistence_outputs='outputs3', persistence_data=['data3'])
     self.assertEqual(len(volumes), 0)
Beispiel #9
0
    def test_get_pod_volumes_raises_for_wrong_values(self):
        with self.assertRaises(StoreNotFoundError):
            get_pod_volumes(persistence_outputs='foo', persistence_data=None)

        with self.assertRaises(StoreNotFoundError):
            get_pod_volumes(persistence_outputs=None, persistence_data='foo')