Skip to content

Commit

Permalink
[SDK] change mount_v3io to multi-mount (/User and /v3io) by default (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Hedingber committed Dec 25, 2020
1 parent 9df76c5 commit a4d25e2
Show file tree
Hide file tree
Showing 4 changed files with 335 additions and 33 deletions.
8 changes: 7 additions & 1 deletion mlrun/__init__.py
Expand Up @@ -30,7 +30,13 @@
from .db import get_run_db
from .execution import MLClientCtx
from .model import RunTemplate, NewTask, new_task, RunObject
from .platforms import mount_v3io, v3io_cred, mount_v3io_extended, VolumeMount
from .platforms import (
mount_v3io,
v3io_cred,
mount_v3io_extended,
VolumeMount,
mount_v3io_legacy,
)
from .projects import load_project, new_project
from .run import (
get_or_create_ctx,
Expand Down
1 change: 1 addition & 0 deletions mlrun/platforms/__init__.py
Expand Up @@ -19,6 +19,7 @@
mount_v3io,
add_or_refresh_credentials,
mount_v3io_extended,
mount_v3io_legacy,
VolumeMount,
)
from .other import mount_pvc, auto_mount
95 changes: 91 additions & 4 deletions mlrun/platforms/iguazio.py
Expand Up @@ -14,11 +14,14 @@
import json
import os
import requests
import warnings
import urllib3
from http import HTTPStatus
from datetime import datetime
from collections import namedtuple

import mlrun.errors


_cached_control_session = None

Expand Down Expand Up @@ -61,12 +64,16 @@ def mount_v3io_extended(
:param user: the username used to auth against v3io. if not given V3IO_USERNAME env var will be used
:param secret: k8s secret name which would be used to get the username and access key to auth against v3io.
"""
if remote and not mounts:
raise mlrun.errors.MLRunInvalidArgumentError(
"mounts must be specified when remote is given"
)

# Empty remote & mounts defaults are mounts of /v3io and /User
if not remote and not mounts:
user = os.environ.get("V3IO_USERNAME", user)
user = _resolve_mount_user(user)
if not user:
raise ValueError(
raise mlrun.errors.MLRunInvalidArgumentError(
"user name/env must be specified when using empty remote and mounts"
)
mounts = [
Expand Down Expand Up @@ -98,11 +105,89 @@ def _mount_v3io_extended(task):
return _mount_v3io_extended


def _resolve_mount_user(user=None):
return os.environ.get("V3IO_USERNAME", user)


def mount_v3io(
name="v3io", remote="~/", mount_path="/User", access_key="", user="", secret=None
name="v3io",
remote="",
mount_path="",
access_key="",
user="",
secret=None,
volume_mounts=None,
):
"""Modifier function to apply to a Container Op to volume mount a v3io path
:param name: the volume name
:param remote: the v3io path to use for the volume. ~/ prefix will be replaced with /users/<username>/
:param mount_path: the volume mount path (deprecated, exists for backwards compatibility, prefer to
use mounts instead)
:param access_key: the access key used to auth against v3io. if not given V3IO_ACCESS_KEY env var will be used
:param user: the username used to auth against v3io. if not given V3IO_USERNAME env var will be used
:param secret: k8s secret name which would be used to get the username and access key to auth against v3io.
:param volume_mounts: list of VolumeMount. empty volume mounts & remote will default to mount /v3io & /User.
"""
if mount_path and volume_mounts:
raise mlrun.errors.MLRunInvalidArgumentError(
"mount_path and mounts can not be given toegther"
)

if mount_path:
warnings.warn(
"mount_path is pending deprecation, use mounts instead"
"This will be deprecated in 0.8.0, and will be removed in 0.10.0",
# TODO: In 0.8.0 do changes in examples & demos In 0.10.0 remove
PendingDeprecationWarning,
)

# For backwards compatibility with version<0.6.0 when multi mount wasn't an option (there was no mounts)
if not volume_mounts:
if mount_path:
if remote:
# If both remote and mount_path given, no default behavior is expected, we can't assume anything
# therefore we don't add the v3io volume mount and default to legacy behavior
return mount_v3io_legacy(
name, remote, mount_path, access_key, user, secret
)
else:
# If mount path but no remote, it means the user "counted" on the default remote
# Back then remote default was ~/ which is /users/<username>, but since we now use multi mount, we're
# using subpath instead
user = _resolve_mount_user(user)
if not user:
raise mlrun.errors.MLRunInvalidArgumentError(
"user name/env must be specified when using empty remote and mount_path"
)
volume_mounts = [
VolumeMount(path="/v3io", sub_path=""),
VolumeMount(path=mount_path, sub_path="users/" + user),
]
else:
if remote:
# If remote but no mount path, it means the user "counted" on the default mount path
# Back then mount_path default was /User, but since the remote was given we can't assume anything
# therefore we don't add the v3io volume mount and default to legacy behavior
return mount_v3io_legacy(
name, remote, access_key=access_key, user=user, secret=secret
)
# not remote and not mounts (and not mount_path) handled by the extended handler

return mount_v3io_extended(
name=name,
remote=remote,
mounts=volume_mounts,
access_key=access_key,
user=user,
secret=secret,
)


def mount_v3io_legacy(
name="v3io", remote="~/", mount_path="/User", access_key="", user="", secret=None
):
"""Modifier function to apply to a Container Op to volume mount a v3io path
:param name: the volume name
:param remote: the v3io path to use for the volume. ~/ prefix will be replaced with /users/<username>/
:param mount_path: the volume mount path
Expand Down Expand Up @@ -245,7 +330,9 @@ def v3io_to_vol(name, remote="~/", access_key="", user="", secret=None):
if remote.startswith("~/"):
user = environ.get("V3IO_USERNAME", user)
if not user:
raise ValueError('user name/env must be specified when using "~" in path')
raise mlrun.errors.MLRunInvalidArgumentError(
'user name/env must be specified when using "~" in path'
)
if remote == "~/":
remote = "users/" + user
else:
Expand Down

0 comments on commit a4d25e2

Please sign in to comment.