-
Notifications
You must be signed in to change notification settings - Fork 141
Added Kubeflow Pipeline component for ModelKit packaging and OCI push #969
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
Open
TheCoder2010-create
wants to merge
1
commit into
kitops-ml:main
Choose a base branch
from
TheCoder2010-create:kfp-modelkit-component
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Dockerfile for Kubeflow ModelKit Component | ||
FROM python:3.10-slim | ||
|
||
# Install system dependencies | ||
RUN apt-get update && apt-get install -y --no-install-recommends \ | ||
git \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
# Install ModelKit CLI (replace with actual install if available via pip or other means) | ||
# Example: pip install modelkit-cli | ||
# If ModelKit is only available via binary, add COPY or curl/wget here | ||
# For now, placeholder: | ||
RUN pip install --no-cache-dir ml-metadata kfp | ||
# TODO: Add ModelKit CLI installation here | ||
|
||
# Copy component code | ||
COPY component.py /component.py | ||
|
||
ENTRYPOINT ["python", "/component.py"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import os | ||
import subprocess | ||
import json | ||
from typing import Optional | ||
from ml_metadata import metadata_store | ||
from ml_metadata.proto import metadata_store_pb2 | ||
|
||
# This script is intended to be used as a Kubeflow Pipeline component entrypoint. | ||
# It expects the following environment variables or arguments: | ||
# MODEL_DIR: Path to model artifacts | ||
# REGISTRY_URI: OCI registry URI (e.g., oci://my-registry/modelkit:tag) | ||
# PIPELINE_RUN_ID: Kubeflow pipeline run ID | ||
# EXPERIMENT_NAME: Kubeflow experiment name | ||
# KUBEFLOW_METADATA_HOST: MLMD gRPC host (default: metadata-grpc-service.kubeflow) | ||
# DOCKER_CONFIG_PATH: Path to Docker config for registry auth (default: /kaniko/.docker) | ||
|
||
def main(): | ||
model_dir = os.environ.get('MODEL_DIR') | ||
registry_uri = os.environ.get('REGISTRY_URI') | ||
pipeline_run_id = os.environ.get('PIPELINE_RUN_ID') | ||
experiment_name = os.environ.get('EXPERIMENT_NAME') | ||
kubeflow_metadata_host = os.environ.get('KUBEFLOW_METADATA_HOST', 'metadata-grpc-service.kubeflow') | ||
docker_config_path = os.environ.get('DOCKER_CONFIG_PATH', '/kaniko/.docker') | ||
|
||
if not model_dir or not registry_uri or not pipeline_run_id or not experiment_name: | ||
raise ValueError("Missing required environment variables.") | ||
|
||
# 1. Connect to ML Metadata | ||
store = metadata_store.MetadataStore( | ||
metadata_store_pb2.ConnectionConfig( | ||
host=kubeflow_metadata_host, | ||
port=8080, | ||
) | ||
) | ||
# 2. Extract run/experiment metadata | ||
runs = store.get_executions_by_type('kfp-run') | ||
run = next((r for r in runs if r.custom_properties.get('run_id', None) and r.custom_properties['run_id'].string_value == pipeline_run_id), None) | ||
metrics = run.custom_properties.get('metrics', {}).string_value if run and 'metrics' in run.custom_properties else '{}' | ||
|
||
# 3. Prepare ModelKit metadata | ||
metadata = { | ||
"pipeline_run_id": pipeline_run_id, | ||
"experiment_name": experiment_name, | ||
"metrics": metrics, | ||
} | ||
with open('modelkit-metadata.json', 'w') as f: | ||
json.dump(metadata, f) | ||
|
||
# 4. Package model as ModelKit (assumes modelkit CLI is installed) | ||
subprocess.run([ | ||
'modelkit', 'pack', | ||
'--input', model_dir, | ||
'--metadata', 'modelkit-metadata.json', | ||
'--output', 'modelkit.tar' | ||
], check=True) | ||
TheCoder2010-create marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# 5. Push to OCI registry | ||
env = os.environ.copy() | ||
env['DOCKER_CONFIG'] = docker_config_path | ||
subprocess.run([ | ||
'modelkit', 'push', | ||
'--input', 'modelkit.tar', | ||
'--destination', registry_uri | ||
], check=True, env=env) | ||
|
||
print(f"ModelKit pushed to {registry_uri}") | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
value: {inputValue: registry_uri} | ||
- name: PIPELINE_RUN_ID | ||
value: {inputValue: pipeline_run_id} | ||
- name: EXPERIMENT_NAME | ||
value: {inputValue: experiment_name} | ||
- name: KUBEFLOW_METADATA_HOST | ||
value: {inputValue: kubeflow_metadata_host} | ||
- name: DOCKER_CONFIG_PATH | ||
value: {inputValue: docker_config_path} | ||
# Add volume mounts for secrets if needed |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can potentially reuse the standard KitOps image and copy the binary out of it into this image.