From fd505e85c8534e471028cb0c9bdd52e295bdd7bc Mon Sep 17 00:00:00 2001 From: NohaIhab Date: Fri, 15 Mar 2024 09:58:04 +0000 Subject: [PATCH] feat: add agent rock --- agent/rockcraft.yaml | 59 ++++++++++++++++++++++++++++++++++++++ agent/tests/test_rock.py | 61 ++++++++++++++++++++++++++++++++++++++++ agent/tox.ini | 54 +++++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 agent/rockcraft.yaml create mode 100644 agent/tests/test_rock.py create mode 100644 agent/tox.ini diff --git a/agent/rockcraft.yaml b/agent/rockcraft.yaml new file mode 100644 index 0000000..1024973 --- /dev/null +++ b/agent/rockcraft.yaml @@ -0,0 +1,59 @@ +# Dockerfile https://github.com/kserve/kserve/blob/v0.11.2/agent.Dockerfile +name: kserve-agent +summary: KServe agent +description: "KServe model agent" +version: "0.11.2" +license: Apache-2.0 +base: ubuntu@22.04 +platforms: + amd64: +run-user: _daemon_ + +services: + kserve-agent: + override: replace + summary: "KServe agent service" + startup: enabled + command: "/ko-app/agent" + +parts: + 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 + + agent: + plugin: go + source: https://github.com/kserve/kserve + source-type: git + source-tag: v0.11.2 + build-snaps: + - go/1.20/stable + build-environment: + - CGO_ENABLED: 0 + - GOOS: linux + override-build: | + + # Remove the source from the build directory so we don't + # accidentally use the wrong inputs. + # The build steps below copy everything that is needed to the working directory + rm -rf ./* + + # Copy in the go src + cp $CRAFT_PART_SRC/go.mod $CRAFT_PART_SRC/go.sum ./ + + go mod download + + cp -r $CRAFT_PART_SRC/cmd ./cmd + cp -r $CRAFT_PART_SRC/pkg ./pkg + + # Build + go build -a -o agent ./cmd/agent + + # Copy the files to the install directory + cp -r $CRAFT_PART_SRC/third_party/ $CRAFT_PART_INSTALL/third_party/ + mkdir $CRAFT_PART_INSTALL/ko-app + cp -r agent $CRAFT_PART_INSTALL/ko-app/agent \ No newline at end of file diff --git a/agent/tests/test_rock.py b/agent/tests/test_rock.py new file mode 100644 index 0000000..a4b48e1 --- /dev/null +++ b/agent/tests/test_rock.py @@ -0,0 +1,61 @@ +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. + +import random +import pytest +import string +import subprocess + +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(rock_test_env): + """Test rock.""" + temp_dir, container_name = rock_test_env + check_rock = CheckRock("rockcraft.yaml") + rock_image = check_rock.get_name() + rock_version = check_rock.get_version() + LOCAL_ROCK_IMAGE = f"{rock_image}:{rock_version}" + + # assert the rock contains the expected files + subprocess.run( + [ + "docker", + "run", + "--entrypoint", + "/bin/bash", + LOCAL_ROCK_IMAGE, + "-c", + "ls -la /third_party", + ], + check=True, + ) + + subprocess.run( + [ + "docker", + "run", + "--entrypoint", + "/bin/bash", + LOCAL_ROCK_IMAGE, + "-c", + "ls -la /ko-app/agent", + ], + check=True, + ) diff --git a/agent/tox.ini b/agent/tox.ini new file mode 100644 index 0000000..3c73eb2 --- /dev/null +++ b/agent/tox.ini @@ -0,0 +1,54 @@ +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. +[tox] +skipsdist = True +skip_missing_interpreters = True +envlist = pack, export-to-docker, sanity, integration + +[testenv] +setenv = + PYTHONPATH={toxinidir} + PYTHONBREAKPOINT=ipdb.set_trace + CHARM_REPO=https://github.com/canonical/kserve-operators.git + CHARM_BRANCH=main + LOCAL_CHARM_DIR=charm_repo + +[testenv:pack] +passenv = * +allowlist_externals = + rockcraft +commands = + rockcraft pack + +[testenv:export-to-docker] +passenv = * +allowlist_externals = + bash + skopeo + yq +commands = + # export rock to docker + bash -c 'NAME=$(yq eval .name rockcraft.yaml) && \ + VERSION=$(yq eval .version rockcraft.yaml) && \ + ARCH=$(yq eval ".platforms | keys | .[0]" rockcraft.yaml) && \ + ROCK="$\{NAME\}_$\{VERSION\}_$\{ARCH\}.rock" && \ + DOCKER_IMAGE=$NAME:$VERSION && \ + echo "Exporting $ROCK to docker as $DOCKER_IMAGE" && \ + skopeo --insecure-policy copy oci-archive:$ROCK docker-daemon:$DOCKER_IMAGE' + +[testenv:sanity] +passenv = * +deps = + pytest + charmed-kubeflow-chisme +commands = + # run rock tests + pytest -s -v --tb native --show-capture=all --log-cli-level=INFO {posargs} {toxinidir}/tests + +[testenv:integration] +passenv = * +allowlist_externals = + echo +commands = + # TODO: Implement integration tests here + echo "WARNING: This is a placeholder test - no test is implemented here."