def test_push_image_to_registry_handling_errors(): image_uri = "dockerhub_account/mlflow-kubernetes-example" with pytest.raises( ExecutionException, match="Error while pushing to docker registry: An image does not exist locally", ): kb.push_image_to_registry(image_uri)
def test_push_image_to_registry(): image_uri = "dockerhub_account/mlflow-kubernetes-example" with mock.patch("docker.from_env") as docker_mock: client = mock.MagicMock() docker_mock.return_value = client kb.push_image_to_registry(image_uri) assert client.images.push.call_count == 1 args = client.images.push.call_args_list assert args[0][1]['repository'] == image_uri
def _run(uri, experiment_id, entry_point="main", version=None, parameters=None, backend=None, backend_config=None, use_conda=True, storage_dir=None, synchronous=True, run_id=None): """ Helper that delegates to the project-running method corresponding to the passed-in backend. Returns a ``SubmittedRun`` corresponding to the project run. """ parameters = parameters or {} work_dir = _fetch_project(uri=uri, force_tempdir=False, version=version) project = _project_spec.load_project(work_dir) _validate_execution_environment(project, backend) project.get_entry_point(entry_point)._validate_parameters(parameters) if run_id: active_run = tracking.MlflowClient().get_run(run_id) else: active_run = _create_run(uri, experiment_id, work_dir, entry_point) # Consolidate parameters for logging. # `storage_dir` is `None` since we want to log actual path not downloaded local path entry_point_obj = project.get_entry_point(entry_point) final_params, extra_params = entry_point_obj.compute_parameters(parameters, storage_dir=None) for key, value in (list(final_params.items()) + list(extra_params.items())): tracking.MlflowClient().log_param(active_run.info.run_id, key, value) repo_url = _get_git_repo_url(work_dir) if repo_url is not None: for tag in [MLFLOW_GIT_REPO_URL, LEGACY_MLFLOW_GIT_REPO_URL]: tracking.MlflowClient().set_tag(active_run.info.run_id, tag, repo_url) # Add branch name tag if a branch is specified through -version if _is_valid_branch_name(work_dir, version): for tag in [MLFLOW_GIT_BRANCH, LEGACY_MLFLOW_GIT_BRANCH_NAME]: tracking.MlflowClient().set_tag(active_run.info.run_id, tag, version) if backend == "databricks": tracking.MlflowClient().set_tag(active_run.info.run_id, MLFLOW_PROJECT_BACKEND, "databricks") from mlflow.projects.databricks import run_databricks return run_databricks( remote_run=active_run, uri=uri, entry_point=entry_point, work_dir=work_dir, parameters=parameters, experiment_id=experiment_id, cluster_spec=backend_config) elif backend == "local" or backend is None: command = [] command_separator = " " # If a docker_env attribute is defined in MLproject then it takes precedence over conda yaml # environments, so the project will be executed inside a docker container. if project.docker_env: tracking.MlflowClient().set_tag(active_run.info.run_id, MLFLOW_PROJECT_ENV, "docker") tracking.MlflowClient().set_tag(active_run.info.run_id, MLFLOW_PROJECT_BACKEND, "local") _validate_docker_env(project) _validate_docker_installation() image = _build_docker_image(work_dir=work_dir, repository_uri=project.name, base_image=project.docker_env.get('image'), run_id=active_run.info.run_id) command += _get_docker_command(image=image, active_run=active_run) # Synchronously create a conda environment (even though this may take some time) # to avoid failures due to multiple concurrent attempts to create the same conda env. elif use_conda: tracking.MlflowClient().set_tag(active_run.info.run_id, MLFLOW_PROJECT_ENV, "conda") tracking.MlflowClient().set_tag(active_run.info.run_id, MLFLOW_PROJECT_BACKEND, "local") command_separator = " && " conda_env_name = _get_or_create_conda_env(project.conda_env_path) command += _get_conda_command(conda_env_name) # In synchronous mode, run the entry point command in a blocking fashion, sending status # updates to the tracking server when finished. Note that the run state may not be # persisted to the tracking server if interrupted if synchronous: command += _get_entry_point_command(project, entry_point, parameters, storage_dir) command = command_separator.join(command) return _run_entry_point(command, work_dir, experiment_id, run_id=active_run.info.run_id) # Otherwise, invoke `mlflow run` in a subprocess return _invoke_mlflow_run_subprocess( work_dir=work_dir, entry_point=entry_point, parameters=parameters, experiment_id=experiment_id, use_conda=use_conda, storage_dir=storage_dir, run_id=active_run.info.run_id) elif backend == "kubernetes": from mlflow.projects import kubernetes as kb tracking.MlflowClient().set_tag(active_run.info.run_id, MLFLOW_PROJECT_ENV, "docker") tracking.MlflowClient().set_tag(active_run.info.run_id, MLFLOW_PROJECT_BACKEND, "kubernetes") _validate_docker_env(project) _validate_docker_installation() kube_config = _parse_kubernetes_config(backend_config) image = _build_docker_image(work_dir=work_dir, repository_uri=kube_config["repository-uri"], base_image=project.docker_env.get('image'), run_id=active_run.info.run_id) image_digest = kb.push_image_to_registry(image.tags[0]) submitted_run = kb.run_kubernetes_job(project.name, active_run, image.tags[0], image_digest, _get_entry_point_command(project, entry_point, parameters, storage_dir), _get_run_env_vars( run_id=active_run.info.run_uuid, experiment_id=active_run.info.experiment_id), kube_config['kube-context'], kube_config['kube-job-template']) return submitted_run supported_backends = ["local", "databricks", "kubernetes"] raise ExecutionException("Got unsupported execution mode %s. Supported " "values: %s" % (backend, supported_backends))
def test_push_image_to_registry_handling_errors(): image_uri = "dockerhub_account/mlflow-kubernetes-example" with pytest.raises(ExecutionException): kb.push_image_to_registry(image_uri)
def _run(uri, experiment_id, entry_point="main", version=None, parameters=None, docker_args=None, backend_name=None, backend_config=None, use_conda=True, storage_dir=None, synchronous=True): """ Helper that delegates to the project-running method corresponding to the passed-in backend. Returns a ``SubmittedRun`` corresponding to the project run. """ tracking_store_uri = tracking.get_tracking_uri() if backend_name: backend = loader.load_backend(backend_name) if backend: submitted_run = backend.run(uri, entry_point, parameters, version, backend_config, experiment_id, tracking_store_uri) tracking.MlflowClient().set_tag(submitted_run.run_id, MLFLOW_PROJECT_BACKEND, backend_name) return submitted_run work_dir = fetch_and_validate_project(uri, version, entry_point, parameters) project = load_project(work_dir) _validate_execution_environment(project, backend_name) existing_run_id = None if backend_name == "local" and _MLFLOW_LOCAL_BACKEND_RUN_ID_CONFIG in backend_config: existing_run_id = backend_config[_MLFLOW_LOCAL_BACKEND_RUN_ID_CONFIG] active_run = get_or_create_run(existing_run_id, uri, experiment_id, work_dir, version, entry_point, parameters) if backend_name == "databricks": tracking.MlflowClient().set_tag(active_run.info.run_id, MLFLOW_PROJECT_BACKEND, "databricks") from mlflow.projects.databricks import run_databricks return run_databricks(remote_run=active_run, uri=uri, entry_point=entry_point, work_dir=work_dir, parameters=parameters, experiment_id=experiment_id, cluster_spec=backend_config) elif backend_name == "local" or backend_name is None: tracking.MlflowClient().set_tag(active_run.info.run_id, MLFLOW_PROJECT_BACKEND, "local") command_args = [] command_separator = " " # If a docker_env attribute is defined in MLproject then it takes precedence over conda yaml # environments, so the project will be executed inside a docker container. if project.docker_env: tracking.MlflowClient().set_tag(active_run.info.run_id, MLFLOW_PROJECT_ENV, "docker") _validate_docker_env(project) _validate_docker_installation() image = _build_docker_image( work_dir=work_dir, repository_uri=project.name, base_image=project.docker_env.get('image'), run_id=active_run.info.run_id) command_args += _get_docker_command( image=image, active_run=active_run, docker_args=docker_args, volumes=project.docker_env.get("volumes"), user_env_vars=project.docker_env.get("environment")) # Synchronously create a conda environment (even though this may take some time) # to avoid failures due to multiple concurrent attempts to create the same conda env. elif use_conda: tracking.MlflowClient().set_tag(active_run.info.run_id, MLFLOW_PROJECT_ENV, "conda") command_separator = " && " conda_env_name = _get_or_create_conda_env(project.conda_env_path) command_args += _get_conda_command(conda_env_name) # In synchronous mode, run the entry point command in a blocking fashion, sending status # updates to the tracking server when finished. Note that the run state may not be # persisted to the tracking server if interrupted if synchronous: command_args += _get_entry_point_command(project, entry_point, parameters, storage_dir) command_str = command_separator.join(command_args) return _run_entry_point(command_str, work_dir, experiment_id, run_id=active_run.info.run_id) # Otherwise, invoke `mlflow run` in a subprocess return _invoke_mlflow_run_subprocess(work_dir=work_dir, entry_point=entry_point, parameters=parameters, experiment_id=experiment_id, use_conda=use_conda, storage_dir=storage_dir, run_id=active_run.info.run_id) elif backend_name == "kubernetes": from mlflow.projects import kubernetes as kb tracking.MlflowClient().set_tag(active_run.info.run_id, MLFLOW_PROJECT_ENV, "docker") tracking.MlflowClient().set_tag(active_run.info.run_id, MLFLOW_PROJECT_BACKEND, "kubernetes") _validate_docker_env(project) _validate_docker_installation() kube_config = _parse_kubernetes_config(backend_config) image = _build_docker_image( work_dir=work_dir, repository_uri=kube_config["repository-uri"], base_image=project.docker_env.get('image'), run_id=active_run.info.run_id) image_digest = kb.push_image_to_registry(image.tags[0]) submitted_run = kb.run_kubernetes_job( project.name, active_run, image.tags[0], image_digest, _get_entry_point_command(project, entry_point, parameters, storage_dir), _get_run_env_vars(run_id=active_run.info.run_uuid, experiment_id=active_run.info.experiment_id), kube_config.get('kube-context', None), kube_config['kube-job-template']) return submitted_run supported_backends = ["local", "databricks", "kubernetes"] raise ExecutionException("Got unsupported execution mode %s. Supported " "values: %s" % (backend_name, supported_backends))
def _run( uri, experiment_id, entry_point, version, parameters, docker_args, backend_name, backend_config, use_conda, storage_dir, synchronous, ): """ Helper that delegates to the project-running method corresponding to the passed-in backend. Returns a ``SubmittedRun`` corresponding to the project run. """ tracking_store_uri = tracking.get_tracking_uri() backend_config[PROJECT_USE_CONDA] = use_conda backend_config[PROJECT_SYNCHRONOUS] = synchronous backend_config[PROJECT_DOCKER_ARGS] = docker_args backend_config[PROJECT_STORAGE_DIR] = storage_dir # TODO: remove this check once kubernetes execution has been refactored if backend_name not in {"databricks", "kubernetes"}: backend = loader.load_backend(backend_name) if backend: submitted_run = backend.run( uri, entry_point, parameters, version, backend_config, tracking_store_uri, experiment_id, ) tracking.MlflowClient().set_tag(submitted_run.run_id, MLFLOW_PROJECT_BACKEND, backend_name) return submitted_run work_dir = fetch_and_validate_project(uri, version, entry_point, parameters) project = load_project(work_dir) _validate_execution_environment(project, backend_name) active_run = get_or_create_run(None, uri, experiment_id, work_dir, version, entry_point, parameters) if backend_name == "databricks": tracking.MlflowClient().set_tag(active_run.info.run_id, MLFLOW_PROJECT_BACKEND, "databricks") from mlflow.projects.databricks import run_databricks return run_databricks( remote_run=active_run, uri=uri, entry_point=entry_point, work_dir=work_dir, parameters=parameters, experiment_id=experiment_id, cluster_spec=backend_config, ) elif backend_name == "kubernetes": from mlflow.projects.docker import ( build_docker_image, validate_docker_env, validate_docker_installation, ) from mlflow.projects import kubernetes as kb tracking.MlflowClient().set_tag(active_run.info.run_id, MLFLOW_PROJECT_ENV, "docker") tracking.MlflowClient().set_tag(active_run.info.run_id, MLFLOW_PROJECT_BACKEND, "kubernetes") validate_docker_env(project) validate_docker_installation() kube_config = _parse_kubernetes_config(backend_config) image = build_docker_image( work_dir=work_dir, repository_uri=kube_config["repository-uri"], base_image=project.docker_env.get("image"), run_id=active_run.info.run_id, ) image_digest = kb.push_image_to_registry(image.tags[0]) submitted_run = kb.run_kubernetes_job( project.name, active_run, image.tags[0], image_digest, get_entry_point_command(project, entry_point, parameters, storage_dir), get_run_env_vars(run_id=active_run.info.run_uuid, experiment_id=active_run.info.experiment_id), kube_config.get("kube-context", None), kube_config["kube-job-template"], ) return submitted_run supported_backends = ["databricks", "kubernetes"] + list( loader.MLFLOW_BACKENDS.keys()) raise ExecutionException("Got unsupported execution mode %s. Supported " "values: %s" % (backend_name, supported_backends))