Exemplo n.º 1
0
    def create_job(self, job_spec):
        proc = subprocess.Popen(job_spec.command)
        job = core.Job(proc, proc.pid)
        job.host = 'localhost'

        return job
Exemplo n.º 2
0
    def create_job(self, job_spec):
        logger.debug("[k8s]create_job: %s", job_spec)
        body = client.V1Pod()
        name = "{}-{}".format(
            job_spec.name.replace("_", "-").lower(),
            str(uuid.uuid4())[:8])
        body.metadata = client.V1ObjectMeta(namespace=self.default_namespace,
                                            name=name)

        # set environment varialbes
        # TODO(jiale) add environment variables

        image = job_spec.image if job_spec.image else self.current_image

        container = client.V1Container(
            name=name,
            image=image,
            command=job_spec.command,
            env=[],
            stdin=True,
            tty=True,
        )

        rr = self._get_resource_requirements(job_spec)
        if rr:
            logger.debug(
                "[k8s]create_job, container resource requirements: %s",
                job_spec)
            container.resources = rr

        body.spec = client.V1PodSpec(containers=[container],
                                     restart_policy="Never")

        # propagate mount points to new containers if necesary
        if job_spec.volumes:
            volumes = []
            volume_mounts = []

            for pd_name, mount_info in job_spec.volumes.items():
                #volume_name = job_spec.volume if job_spec.volume else self.current_mount
                pvc = client.V1PersistentVolumeClaimVolumeSource(
                    claim_name=pd_name)
                volume = client.V1Volume(
                    persistent_volume_claim=pvc,
                    name="volume-" + pd_name,
                )
                volumes.append(volume)
                mount = client.V1VolumeMount(
                    mount_path=mount_info["bind"],
                    name=volume.name,
                )
                if mount_info["mode"] == "r":
                    mount.read_only = True
                volume_mounts.append(mount)
            body.spec.volumes = volumes
            container.volume_mounts = volume_mounts
        elif self.mounts:
            body.spec.volumes = self.volumes
            container.volume_mounts = self.mounts

        logger.debug("[k8s]calling create_namespaced_pod: %s",
                     body.metadata.name)
        try:
            v1pod = self.core_api.create_namespaced_pod(
                self.default_namespace, body)
        except ApiException as e:
            raise e

        return core.Job(v1pod, v1pod.metadata.uid)