Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mlserver-mlflow rock #20

Merged
merged 4 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions mlserver-mlflow/rockcraft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Based on https://github.com/SeldonIO/MLServer/blob/1.2.0/Dockerfile
name: mlserver-mlflow
summary: An image for Seldon MLServer MLflow
description: |
This image is used as part of the Charmed Kubeflow product.
version: 1.2.0_22.04_1 # <upstream-version>_<base-version>_<Charmed-KF-version>
license: Apache-2.0
base: ubuntu:22.04
run-user: _daemon_
services:
mlserver-mlflow:
override: replace
summary: "mlserver-mlflow service"
startup: enabled
command: bash -c 'cd /opt/mlserver && export PATH=/opt/conda/bin/:/opt/mlserver/.local/bin:${PATH}:/usr/bin && export PYTHONPATH=/opt/mlserver/.local/lib/python3.8/site-packages/:${PYTHONPATH} && eval $(/opt/conda/bin/conda shell.bash hook 2> /dev/null) && mlserver start ${MLSERVER_MODELS_DIR}'
environment:
MLSERVER_ENV_TARBALL: "/mnt/models/environment.tar.gz"
MLSERVER_MODELS_DIR: "/mnt/models/"
platforms:
amd64:

parts:
mlserver-mlflow:
plugin: nil
source: https://github.com/SeldonIO/MLServer
source-type: git
source-tag: 1.2.0
build-packages:
- bash
- tar
- gzip
# TO-DO: Verify need for the packages below
# - libgomp
# - mesa-libGL
# - glib2-devel
# - shadow-utils
stage-packages:
- bash
override-build: |

mkdir -p ${CRAFT_PART_INSTALL}/opt/mlserver/dist
cp ${CRAFT_PART_SRC}/setup.py .
cp ${CRAFT_PART_SRC}/README.md .

./hack/build-wheels.sh ${CRAFT_PART_INSTALL}/opt/mlserver/dist

override-stage: |
export ROCK_RUNTIME="mlflow"
export PYTHON_VERSION="3.8.13"
export CONDA_VERSION="4.13.0"
export RUNTIMES="mlserver_${ROCK_RUNTIME}"
export MINIFORGE_VERSION="${CONDA_VERSION}-1"
export MLSERVER_PATH=opt/mlserver
export CONDA_PATH=opt/conda
export PATH=/opt/mlserver/.local/bin:/opt/conda/bin:$PATH

# Install Conda, Python 3.8 and FFmpeg
curl -L -o ~/miniforge3.sh https://github.com/conda-forge/miniforge/releases/download/${MINIFORGE_VERSION}/Miniforge3-${MINIFORGE_VERSION}-Linux-x86_64.sh
bash ~/miniforge3.sh -b -u -p ${CONDA_PATH}
rm ~/miniforge3.sh
${CONDA_PATH}/bin/conda install --yes conda=${CONDA_VERSION} python=${PYTHON_VERSION} ffmpeg
${CONDA_PATH}/bin/conda clean -tipy
mkdir -p etc/profile.d
ln -sf ${CONDA_PATH}/etc/profile.d/conda.sh etc/profile.d/conda.sh
echo ". ${CONDA_PATH}/etc/profile.d/conda.sh" >> ~/.bashrc
echo "PATH=${PATH}" >> ~/.bashrc
bash -c "${CONDA_PATH}/bin/conda init bash"
echo "conda activate base" >> ~/.bashrc
chgrp -R root opt/conda && chmod -R g+rw opt/conda

# conda writes shebangs with its path everywhere, and in crafting, that will be, for example:
# #!/root/stage/opt/conda/...
#
# Snip off the /root/stage part
bash -c "grep -R -E '/root/stage' opt/ 2>/dev/null | grep -v Bin | awk '{split(\$0,out,\":\"); print out[1]}' | uniq | xargs -I{} sed -i -e 's/\/root\/stage//' {}"

# install required wheels
mkdir -p ${MLSERVER_PATH}
mkdir -p ./wheels
cp -p ${CRAFT_PART_INSTALL}/opt/mlserver/dist/mlserver-*.whl ./wheels
cp -p ${CRAFT_PART_INSTALL}/opt/mlserver/dist/mlserver_${ROCK_RUNTIME}-*.whl ./wheels
pip install --prefix ${MLSERVER_PATH}/.local --upgrade pip wheel 'setuptools<65.6.0'
pip install --prefix ${MLSERVER_PATH}/.local $(ls "./wheels/mlserver-"*.whl)
pip install --prefix ${MLSERVER_PATH}/.local $(ls "./wheels/mlserver_${ROCK_RUNTIME}-"*.whl)
chown -R root:root ${MLSERVER_PATH} && chmod -R 777 ${MLSERVER_PATH}

# replace first line of mlserver script with reference to installed Conda python
export CONDA_PYTHON="#\!\/opt\/conda\/bin\/python"
sed -i "1s/.*/${CONDA_PYTHON}/" ${MLSERVER_PATH}/.local/bin/mlserver

# clean wheels
rm -rf ./wheels

override-prime: |
# copy all artifacts
cp -rp ${CRAFT_STAGE}/opt .

# copy required files
mkdir -p licenses
cp ${CRAFT_PART_SRC}/licenses/license.txt licenses/
mkdir -p hack
cp ${CRAFT_PART_SRC}/hack/build-env.sh hack/
cp ${CRAFT_PART_SRC}/hack/generate_dotenv.py hack/
cp ${CRAFT_PART_SRC}/hack/activate-env.sh hack/

security-team-requirement:
plugin: nil
override-build: |
mkdir -p ${CRAFT_PART_INSTALL}/usr/share/rocks
(echo "# os-release" && cat /etc/os-release && echo "# dpkg-query" && \
dpkg-query --root=${CRAFT_PROJECT_DIR}/../bundles/ubuntu-22.04/rootfs/ -f '${db:Status-Abbrev},${binary:Package},${Version},${source:Package},${Source:Version}\n' -W) \
> ${CRAFT_PART_INSTALL}/usr/share/rocks/dpkg.query
47 changes: 47 additions & 0 deletions mlserver-mlflow/tests/test_rock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2022 Canonical Ltd.
# See LICENSE file for licensing details.
#
#

from pathlib import Path

import os
import logging
import random
import pytest
import string
import subprocess
import yaml

from pytest_operator.plugin import OpsTest
from charmed_kubeflow_chisme.rock import CheckRock

@pytest.fixture()
def rock_test_env(tmpdir):
"""Yields a temporary directory and random docker container name, then cleans them up after."""
container_name = "".join([str(i) for i in random.choices(string.ascii_lowercase, k=8)])
yield tmpdir, container_name

try:
subprocess.run(["docker", "rm", container_name])
except Exception:
pass
# tmpdir fixture we use here should clean up the other files for us

@pytest.mark.abort_on_fail
def test_rock(ops_test: OpsTest, rock_test_env):
"""Test rock."""
temp_dir, container_name = rock_test_env
check_rock = CheckRock("rockcraft.yaml")
rock_image = check_rock.get_image_name()
rock_version = check_rock.get_version()
LOCAL_ROCK_IMAGE = f"{check_rock.get_image_name()}:{check_rock.get_version()}"

# TO-DO uncomment when updated chisme is published
#rock_services = check_rock.get_services()
#assert rock_services["mlserver-mlflow"]
#assert rock_services["mlserver-mlflow"]["startup"] == "enabled"

# create ROCK filesystem
subprocess.run(["docker", "run", LOCAL_ROCK_IMAGE, "exec", "ls", "-la", "/opt/mlserver/.local/lib/python3.8/site-packages/mlserver"], check=True)
subprocess.run(["docker", "run", LOCAL_ROCK_IMAGE, "exec", "ls", "-la", "/opt/mlserver/.local/bin/mlserver"], check=True)
76 changes: 76 additions & 0 deletions mlserver-mlflow/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright 2022 Canonical Ltd.
# See LICENSE file for licensing details.
[tox]
skipsdist = True
skip_missing_interpreters = True

[testenv]
setenv =
PYTHONPATH={toxinidir}
PYTHONBREAKPOINT=ipdb.set_trace
CHARM_REPO=https://github.com/canonical/seldon-core-operator.git
CHARM_BRANCH=main
LOCAL_CHARM_DIR=charm_repo

[testenv:unit]
passenv = *
allowlist_externals =
bash
tox
rockcraft
deps =
juju~=2.9.0
pytest
pytest-operator
ops
charmed_kubeflow_chisme
commands =
# build and pack rock
rockcraft pack
bash -c 'NAME=$(yq eval .name rockcraft.yaml) && \
VERSION=$(yq eval .version rockcraft.yaml) && \
ARCH=$(yq eval ".platforms | keys" rockcraft.yaml | awk -F " " '\''{ print $2 }'\'') && \
ROCK="$\{NAME\}_$\{VERSION\}_$\{ARCH\}" && \
sudo skopeo --insecure-policy copy oci-archive:$ROCK.rock docker-daemon:$ROCK:$VERSION'

# run rock tests
pytest -vvv --tb native --show-capture=all --log-cli-level=INFO {posargs} {toxinidir}/tests

[testenv:integration]
passenv = *
allowlist_externals =
bash
git
rm
tox
rockcraft
sed
deps =
juju~=2.9.0
pytest
pytest-operator
ops
commands =
# build and pack rock
rockcraft pack
# clone related charm
rm -rf {env:LOCAL_CHARM_DIR}
git clone --branch {env:CHARM_BRANCH} {env:CHARM_REPO} {env:LOCAL_CHARM_DIR}
# replace jinja2 templated value with yq safe placeholder
sed -i "s/namespace: {{ namespace }}/namespace: YQ_SAFE/" {env:LOCAL_CHARM_DIR}/src/templates/configmap.yaml.j2
# upload rock to docker and microk8s cache, replace charm's container with local rock reference
bash -c 'NAME=$(yq eval .name rockcraft.yaml) && \
VERSION=$(yq eval .version rockcraft.yaml) && \
ARCH=$(yq eval ".platforms | keys" rockcraft.yaml | awk -F " " '\''{ print $2 }'\'') && \
ROCK="$\{NAME\}_$\{VERSION\}_$\{ARCH\}" && \
sudo skopeo --insecure-policy copy oci-archive:$ROCK.rock docker-daemon:$ROCK:$VERSION && \
docker save $ROCK > $ROCK.tar && \
microk8s ctr image import $ROCK.tar --digests=true && \
predictor_servers=$(yq e ".data.predictor_servers" {env:LOCAL_CHARM_DIR}/src/templates/configmap.yaml.j2) && \
predictor_servers=$(jq --arg jq_rock $ROCK -r '\''.MLFLOW_SERVER.protocols.v2.image=$jq_rock'\'' <<< $predictor_servers) && \
predictor_servers=$(jq --arg jq_version $VERSION -r '\''.MLFLOW_SERVER.protocols.v2.defaultImageVersion=$jq_version'\'' <<< $predictor_servers) yq e -i ".data.predictor_servers=strenv(predictor_servers)" {env:LOCAL_CHARM_DIR}/src/templates/configmap.yaml.j2'
# replace yq safe placeholder with original value
i-chvets marked this conversation as resolved.
Show resolved Hide resolved
sed -i "s/namespace: YQ_SAFE/namespace: {{ namespace }}/" {env:LOCAL_CHARM_DIR}/src/templates/configmap.yaml.j2
# run charm integration test with rock
tox -c {env:LOCAL_CHARM_DIR} -e integration