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

[Projects] Add annotations field #615

Merged
merged 6 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions mlrun/api/schemas/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ProjectMetadata(pydantic.BaseModel):
name: str
created: typing.Optional[datetime.datetime] = None
labels: typing.Optional[dict]
annotations: typing.Optional[dict]

class Config:
extra = pydantic.Extra.allow
Expand Down
7 changes: 7 additions & 0 deletions mlrun/api/utils/clients/nuclio.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def patch_project(
response_body.setdefault("metadata", {}).setdefault("labels", {}).update(
project["metadata"]["labels"]
)
if project.get("metadata").get("annotations") is not None:
response_body.setdefault("metadata", {}).setdefault(
"annotations", {}
).update(project["metadata"]["annotations"])
if project.get("spec").get("description") is not None:
response_body.setdefault("spec", {})["description"] = project["spec"][
"description"
Expand Down Expand Up @@ -159,6 +163,8 @@ def _generate_request_body(project: mlrun.api.schemas.Project):
}
if project.metadata.labels:
body["metadata"]["labels"] = project.metadata.labels
if project.metadata.annotations:
body["metadata"]["annotations"] = project.metadata.annotations
if project.spec.description:
body["spec"] = {"description": project.spec.description}
return body
Expand All @@ -169,6 +175,7 @@ def _transform_nuclio_project_to_schema(nuclio_project):
metadata=mlrun.api.schemas.ProjectMetadata(
name=nuclio_project["metadata"]["name"],
labels=nuclio_project["metadata"].get("labels"),
annotations=nuclio_project["metadata"].get("annotations"),
),
spec=mlrun.api.schemas.ProjectSpec(
description=nuclio_project["spec"].get("description")
Expand Down
3 changes: 2 additions & 1 deletion mlrun/projects/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,11 @@ def _project_instance_from_struct(struct, name):


class ProjectMetadata(ModelObj):
def __init__(self, name=None, created=None, labels=None):
def __init__(self, name=None, created=None, labels=None, annotations=None):
self.name = name
self.created = created
self.labels = labels or {}
self.annotations = annotations or {}

@property
def name(self) -> str:
Expand Down
80 changes: 69 additions & 11 deletions tests/api/utils/clients/test_nuclio.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@ def test_get_project(
project_labels = {
"some-label": "some-label-value",
}
project_annotations = {
"some-annotation": "some-annotation-value",
}
response_body = _generate_project_body(
project_name, project_description, project_labels, with_spec=True,
project_name,
project_description,
project_labels,
project_annotations,
with_spec=True,
)
requests_mock.get(f"{api_url}/api/projects/{project_name}", json=response_body)
project = nuclio_client.get_project(None, project_name)
Expand All @@ -45,14 +52,21 @@ def test_get_project(
deepdiff.DeepDiff(project_labels, project.metadata.labels, ignore_order=True,)
== {}
)
assert (
deepdiff.DeepDiff(
project_annotations, project.metadata.annotations, ignore_order=True,
)
== {}
)

# now without description and labels
# now without description, labels and annotations
response_body = _generate_project_body(project_name, with_spec=True)
requests_mock.get(f"{api_url}/api/projects/{project_name}", json=response_body)
project = nuclio_client.get_project(None, project_name)
assert project.metadata.name == project_name
assert project.spec.description is None
assert project.metadata.labels is None
assert project.metadata.annotations is None


def test_list_project(
Expand All @@ -66,15 +80,21 @@ def test_list_project(
{"name": "project-name-3", "labels": {"key": "value"}},
{
"name": "project-name-4",
"annotations": {"annotation-key": "annotation-value"},
},
{
"name": "project-name-5",
"description": "project-description-4",
"labels": {"key2": "value2"},
"annotations": {"annotation-key2": "annotation-value2"},
},
]
response_body = {
mock_project["name"]: _generate_project_body(
mock_project["name"],
mock_project.get("description"),
mock_project.get("labels"),
mock_project.get("annotations"),
with_spec=True,
)
for mock_project in mock_projects
Expand All @@ -92,6 +112,14 @@ def test_list_project(
)
== {}
)
assert (
deepdiff.DeepDiff(
mock_projects[index].get("annotations"),
project.metadata.annotations,
ignore_order=True,
)
== {}
)


def test_create_project(
Expand All @@ -104,6 +132,9 @@ def test_create_project(
project_labels = {
"some-label": "some-label-value",
}
project_annotations = {
"some-annotation": "some-annotation-value",
}

def verify_creation(request, context):
assert (
Expand All @@ -112,6 +143,7 @@ def verify_creation(request, context):
project_name,
project_description,
project_labels,
project_annotations,
with_namespace=False,
),
request.json(),
Expand All @@ -126,7 +158,9 @@ def verify_creation(request, context):
None,
mlrun.api.schemas.Project(
metadata=mlrun.api.schemas.ProjectMetadata(
name=project_name, labels=project_labels
name=project_name,
labels=project_labels,
annotations=project_annotations,
),
spec=mlrun.api.schemas.ProjectSpec(description=project_description),
),
Expand All @@ -143,6 +177,9 @@ def test_store_project_creation(
project_labels = {
"some-label": "some-label-value",
}
project_annotations = {
"some-annotation": "some-annotation-value",
}

def verify_store_creation(request, context):
assert (
Expand All @@ -151,6 +188,7 @@ def verify_store_creation(request, context):
project_name,
project_description,
project_labels,
project_annotations,
with_namespace=False,
),
request.json(),
Expand All @@ -171,7 +209,9 @@ def verify_store_creation(request, context):
project_name,
mlrun.api.schemas.Project(
metadata=mlrun.api.schemas.ProjectMetadata(
name=project_name, labels=project_labels
name=project_name,
labels=project_labels,
annotations=project_annotations,
),
spec=mlrun.api.schemas.ProjectSpec(description=project_description),
),
Expand All @@ -188,9 +228,10 @@ def test_store_project_update(
project_labels = {
"some-label": "some-label-value",
}
mocked_project_body = _generate_project_body(
project_name, labels={"label-key": "label-value"}, with_spec=True
)
project_annotations = {
"some-annotation": "some-annotation-value",
}
mocked_project_body = _generate_project_body(project_name, with_spec=True)

def verify_store_update(request, context):
assert (
Expand All @@ -199,6 +240,7 @@ def verify_store_update(request, context):
project_name,
project_description,
project_labels,
project_annotations,
with_namespace=False,
),
request.json(),
Expand All @@ -218,7 +260,9 @@ def verify_store_update(request, context):
project_name,
mlrun.api.schemas.Project(
metadata=mlrun.api.schemas.ProjectMetadata(
name=project_name, labels=project_labels
name=project_name,
labels=project_labels,
annotations=project_annotations,
),
spec=mlrun.api.schemas.ProjectSpec(description=project_description),
),
Expand All @@ -235,15 +279,22 @@ def test_patch_project(
project_labels = {
"some-label": "some-label-value",
}
project_annotations = {
"some-annotation": "some-annotation-value",
}
mocked_project_body = _generate_project_body(
project_name, labels={"label-key": "label-value"}, with_spec=True
project_name,
labels={"label-key": "label-value"},
annotations={"annotation-key": "annotation-value"},
with_spec=True,
)

def verify_patch(request, context):
# verifying the patch kept the old labels, patched the description, and added the new label
expected_body = mocked_project_body
expected_body["spec"]["description"] = project_description
expected_body["metadata"]["labels"].update(project_labels)
expected_body["metadata"]["annotations"].update(project_annotations)
assert (
deepdiff.DeepDiff(expected_body, request.json(), ignore_order=True,) == {}
)
Expand All @@ -257,7 +308,7 @@ def verify_patch(request, context):
None,
project_name,
{
"metadata": {"labels": project_labels},
"metadata": {"labels": project_labels, "annotations": project_annotations},
"spec": {"description": project_description},
},
)
Expand Down Expand Up @@ -286,7 +337,12 @@ def verify_deletion(request, context):


def _generate_project_body(
name=None, description=None, labels=None, with_namespace=True, with_spec=False
name=None,
description=None,
labels=None,
annotations=None,
with_namespace=True,
with_spec=False,
):
body = {
"metadata": {"name": name},
Expand All @@ -299,4 +355,6 @@ def _generate_project_body(
body["metadata"]["namespace"] = "default-tenant"
if labels:
body["metadata"]["labels"] = labels
if annotations:
body["metadata"]["annotations"] = annotations
return body
3 changes: 2 additions & 1 deletion tests/rundb/test_httpdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,9 @@ def test_project_file_db_roundtrip(create_server):
subpath = "subpath"
origin_url = "origin_url"
labels = {"key": "value"}
annotations = {"annotation-key": "annotation-value"}
project_metadata = mlrun.projects.project.ProjectMetadata(
project_name, labels=labels
project_name, labels=labels, annotations=annotations,
)
project_spec = mlrun.projects.project.ProjectSpec(
description,
Expand Down