-
Notifications
You must be signed in to change notification settings - Fork 786
Move generated Python models into kubeflow_trainer_api package #2632
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
Move generated Python models into kubeflow_trainer_api package #2632
Conversation
b97d73e
to
ea46447
Compare
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.
Thanks @kramaranya for the PR
can we use the swagger_config.json
to do type mapping of the k8s native types to k8s native python client?
type mapping will reduce this pr massively and will make our models very lite. something like {
"packageName": "kubeflow.trainer",
"typeMappings": {
"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": "V1ObjectMeta",
"io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta": "V1ListMeta",
"io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector": "V1LabelSelector",
"io.k8s.apimachinery.pkg.apis.meta.v1.DeleteOptions": "V1DeleteOptions",
"io.k8s.apimachinery.pkg.apis.meta.v1.OwnerReference": "V1OwnerReference",
"io.k8s.apimachinery.pkg.apis.meta.v1.Status": "V1Status",
"io.k8s.apimachinery.pkg.apis.meta.v1.WatchEvent": "V1WatchEvent",
"io.k8s.api.core.v1.Pod": "V1Pod",
"io.k8s.api.core.v1.PodSpec": "V1PodSpec",
"io.k8s.api.core.v1.PodStatus": "V1PodStatus",
"io.k8s.api.core.v1.Container": "V1Container",
"io.k8s.api.core.v1.ContainerPort": "V1ContainerPort",
"io.k8s.api.core.v1.EnvVar": "V1EnvVar",
"io.k8s.api.core.v1.EnvVarSource": "V1EnvVarSource",
"io.k8s.api.core.v1.Volume": "V1Volume",
"io.k8s.api.core.v1.VolumeMount": "V1VolumeMount",
"io.k8s.api.core.v1.ResourceRequirements": "V1ResourceRequirements",
"io.k8s.api.core.v1.Service": "V1Service",
"io.k8s.api.core.v1.ServiceSpec": "V1ServiceSpec",
"io.k8s.api.core.v1.ServiceStatus": "V1ServiceStatus",
"io.k8s.api.apps.v1.Deployment": "V1Deployment",
"io.k8s.api.apps.v1.DeploymentSpec": "V1DeploymentSpec",
"io.k8s.api.apps.v1.DeploymentStatus": "V1DeploymentStatus",
"io.k8s.api.apps.v1.Scale": "V1Scale",
"io.k8s.api.apps.v1.RollingUpdateDeployment": "V1RollingUpdateDeployment"
},
"importMappings": {
"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": "kubernetes.client.models.v1_object_meta.V1ObjectMeta",
"io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta": "kubernetes.client.models.v1_list_meta.V1ListMeta",
"io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector": "kubernetes.client.models.v1_label_selector.V1LabelSelector",
"io.k8s.apimachinery.pkg.apis.meta.v1.DeleteOptions": "kubernetes.client.models.v1_delete_options.V1DeleteOptions",
it should generate only the models required by the trainer and leave the k8s native CRDs to its official python client https://github.com/kubernetes-client/python/tree/master/kubernetes/client/models |
api/pyproject.toml
Outdated
@@ -0,0 +1,42 @@ | |||
[build-system] |
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.
Can you move the pyproject and README to the subdirectory (e.g. /api/python_models
or /api/python_api
)?
Does this name sound good @kramaranya @szaher @astefanutti @Electronic-Waste @tenzen-y ?
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.
@andreyvelich sounds great to me! I've updated with /api/python_api
, unless anyone else has objections
ea46447
to
f283219
Compare
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.
Thanks, I left a few comments.
/assign @astefanutti @Electronic-Waste @szaher @kubeflow/wg-training-leads
hack/python-api/gen-api.sh
Outdated
# Remove empty api directory that shouldn't be generated | ||
if [ -d "${PKG_ROOT}/api" ]; then | ||
rm -rf "${PKG_ROOT}/api" | ||
echo "Removing API directory" | ||
fi |
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.
Why do we need it ?
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.
same as here #2632 (comment)
echo "Removing unused files for the Python API" | ||
git clean -f ${API_OUTPUT_PATH}/.openapi-generator | ||
git clean -f ${API_OUTPUT_PATH}/.github | ||
git clean -f ${API_OUTPUT_PATH}/test |
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.
Do we need it if we set this property for OpenAPI generator ?
modelTests=false
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.
modelTests=false
stops generating test files, but some directories and files are still created by OpenAPI generator:
tree ./api/python_api -a -L 2
├── .github
│ └── workflows
├── .openapi-generator
│ ├── FILES
│ └── VERSION
├── README.md
├── kubeflow_trainer_api
│ ├── __init__.py
│ ├── __pycache__
│ ├── api
│ └── models
├── pyproject.toml
└── test
└── __init__.py
so we clean those up afterward, but I’m fine leaving the directories in place if you prefer
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.
Oh, so even with modelTest=false
OpenAPI generator creates these dirs.
Yes, let's remove them.
packages = ["kubeflow_trainer_api"] | ||
|
||
[tool.hatch.version] | ||
path = "kubeflow_trainer_api/__init__.py" |
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.
Shall we add direct reference to allow this packages be direct referenced via git ?
[tool.hatch.metadata]
allow-direct-references = true
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.
To my understanding, allow-direct-references = true
allows your project to install dependencies via git, but it doesn't affect whether other projects can install this package from git, see https://hatch.pypa.io/1.13/config/metadata/#metadata-options
https://stackoverflow.com/questions/73316644/usage-of-direct-references-in-pyproject-toml-with-hatchling-backend
So since we don't have any direct references in our dependencies, I think we don't need to add it. Please correct me if I'm wrong
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.
Oh, I see! Thanks for letting me know.
.pre-commit-config.yaml
Outdated
@@ -29,5 +29,7 @@ exclude: | | |||
sdk/kubeflow/trainer/__init__.py| | |||
sdk/kubeflow/trainer/api/__init__.py| | |||
sdk/kubeflow/trainer/models/.*| | |||
api/python_api/kubeflow_trainer_api/__init__.py| |
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.
Why we should exclude manually created init.py file from the pre-commit ?
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.
missed this one, thanks
Signed-off-by: kramaranya <kramaranya15@gmail.com>
f283219
to
26436a7
Compare
@szaher I've tried using type mappings with the k8s client models, but they're incompatible with our OpenAPI-generated pydantic models, causing errors during serialization/deserialization |
Is there anything else needed from my side to get this merged? I'd appreciate your reviews! |
git clean -f ${API_OUTPUT_PATH}/.openapi-generator | ||
git clean -f ${API_OUTPUT_PATH}/.github | ||
git clean -f ${API_OUTPUT_PATH}/test | ||
git clean -f ${PKG_ROOT}/api |
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.
@kramaranya Since the output for generator is api/python-api
, what files are generated under api
folder ?
trainer/hack/python-api/gen-api.sh
Line 39 in 26436a7
-o "local/${API_OUTPUT_PATH}" \ |
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.
It generates an empty __init__.py
:
tree ./api/python_api/kubeflow_trainer_api/api -a -L 2
./api/python_api/kubeflow_trainer_api/api
└── __init__.py
1 directory, 1 file
and It's the same as this one:
trainer/sdk/kubeflow/trainer/api/__init__.py
Lines 1 to 3 in 26436a7
# flake8: noqa | |
# import apis into api package |
Since trainer_client.py should remain part of sdk (am I right saying that?), we don't need an
api
folder, therefore I clean it up.
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.
Oh, I missed that you use the subpath for the PKG_ROOT.
Lgtm
Thank you for this @kramaranya! |
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.
LGTM, thanks @kramaranya!
"models" has really become an overloaded term :)
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.
@kramaranya Thanks for this great contribution. LGTM!
/lgtm
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.
/approve
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: andreyvelich The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
…low/trainer#2632) Signed-off-by: kramaranya <kramaranya15@gmail.com>
…low/trainer#2632) Signed-off-by: kramaranya <kramaranya15@gmail.com>
…low/trainer#2632) Signed-off-by: kramaranya <kramaranya15@gmail.com>
…low/trainer#2632) Signed-off-by: kramaranya <kramaranya15@gmail.com>
What this PR does / why we need it:
Move generated Python models from
sdk/kubeflow/trainer/models
into a newapi/kubeflow_trainer_api/models
package.Which issue(s) this PR fixes :
Fixes #2629
Checklist: