Skip to content
Permalink
Browse files

Improving CLI - implement label filter for list deployments (#521)

* implement label filter for list deployment

* update base on comment
  • Loading branch information
yubozhao committed Feb 10, 2020
1 parent 40849b2 commit 5c771cad874fefb474d3732441d71cadc89a9892
@@ -23,6 +23,8 @@
CLI_COLOR_ERROR,
_echo,
CLI_COLOR_SUCCESS,
parse_labels_callback,
validate_labels_query_callback,
)
from bentoml.cli.deployment import (
_print_deployment_info,
@@ -71,9 +73,10 @@ def aws_lambda():
'-l',
'--labels',
type=click.STRING,
callback=parse_labels_callback,
help='Key:value pairs that are attached to deployments and intended to be used'
'to specify identifying attributes of the deployments that are meaningful to '
'users',
'users. Multiple labels are separated with `,`',
)
@click.option('--region', help='AWS region name for deployment')
@click.option(
@@ -370,6 +373,7 @@ def get(name, namespace, output):
'-l',
'--labels',
type=click.STRING,
callback=validate_labels_query_callback,
help='List deployments matching the giving labels',
)
@click.option(
@@ -389,7 +393,7 @@ def list_deployments(namespace, limit, labels, order_by, asc, output):
try:
list_result = yatai_client.deployment.list_lambda_deployments(
limit=limit,
labels=labels,
labels_query=labels,
namespace=namespace,
order_by=order_by,
ascending_order=asc,
@@ -22,6 +22,8 @@
CLI_COLOR_ERROR,
_echo,
CLI_COLOR_SUCCESS,
parse_labels_callback,
validate_labels_query_callback,
)
from bentoml.cli.deployment import (
_print_deployment_info,
@@ -73,9 +75,10 @@ def aws_sagemaker():
'-l',
'--labels',
type=click.STRING,
callback=parse_labels_callback,
help='Key:value pairs that are attached to deployments and intended to be used'
'to specify identifying attributes of the deployments that are meaningful to '
'users',
'users. Multiple labels are separated with `,`',
)
@click.option('--region', help='AWS region name for deployment')
@click.option(
@@ -385,6 +388,7 @@ def get(name, namespace, output):
'-l',
'--labels',
type=click.STRING,
callback=validate_labels_query_callback,
help='List deployments matching the giving labels',
)
@click.option(
@@ -404,7 +408,7 @@ def list_deployment(namespace, limit, labels, order_by, asc, output):
try:
list_result = yatai_client.deployment.list_sagemaker_deployments(
limit=limit,
labels=labels,
labels_query=labels,
namespace=namespace,
order_by=order_by,
ascending_order=asc,
@@ -133,6 +133,40 @@ def parse_bento_tag_callback(ctx, param, value): # pylint: disable=unused-argum
return value


def parse_labels_callback(ctx, param, value): # pylint: disable=unused-argument
if not value:
return value

parsed_labels = {}
label_list = value.split(',')
for label in label_list:
if ':' not in label:
raise click.BadParameter(
f'Bad formatting for label {label}. '
f'Please present label in key:value format'
)
label_key, label_value = label.split(':')
parsed_labels[label_key] = label_value

return parsed_labels


def validate_labels_query_callback(
ctx, param, value
): # pylint: disable=unused-argument
if not value:
return value

labels = value.split(',')
for label in labels:
if '=' not in label:
raise click.BadParameter(
f'Bad formatting for label {label}. '
f'Please present labels query in key=value format'
)
return value


def parse_yaml_file_callback(ctx, param, value): # pylint: disable=unused-argument
yaml = YAML()
yml_content = value.read()
@@ -26,6 +26,7 @@
CLI_COLOR_ERROR,
CLI_COLOR_SUCCESS,
parse_yaml_file_callback,
validate_labels_query_callback,
)
from bentoml.yatai.client import YataiClient
from bentoml.deployment.store import ALL_NAMESPACE_TAG
@@ -273,6 +274,7 @@ def get(name, namespace, output):
'-l',
'--labels',
type=click.STRING,
callback=validate_labels_query_callback,
help='List deployments matching the giving labels',
)
@click.option(
@@ -292,7 +294,7 @@ def list_deployments(namespace, platform, limit, labels, order_by, asc, output):
try:
list_result = yatai_client.deployment.list(
limit=limit,
labels=labels,
labels_query=labels,
namespace=namespace,
operator=platform,
order_by=order_by,
@@ -32,7 +32,7 @@
from sqlalchemy.orm.exc import NoResultFound
from google.protobuf.json_format import ParseDict

from bentoml.exceptions import YataiDeploymentException
from bentoml.exceptions import YataiDeploymentException, BadInput
from bentoml.db import Base, create_session
from bentoml.proto import deployment_pb2
from bentoml.proto.deployment_pb2 import DeploymentSpec, ListDeploymentsRequest
@@ -159,7 +159,7 @@ def list(
self,
namespace,
operator=None,
labels=None,
labels_query=None,
offset=None,
limit=None,
order_by=ListDeploymentsRequest.created_at,
@@ -180,8 +180,20 @@ def list(
query = query.filter(
Deployment.spec['operator'].contains(operator_name)
)
if labels:
raise NotImplementedError("Listing by labels is not yet implemented")
if labels_query:
# We only handle key=value query at the moment, the more advanced query
# such as `in` or `notin` are not handled.
labels_list = labels_query.split(',')
for label in labels_list:
if '=' not in label:
raise BadInput(
'Invalid label format. Please present query in '
'key=value format'
)
label_key, label_value = label.split('=')
query = query.filter(
Deployment.labels[label_key].contains(label_value)
)

# We are not defaulting limit to 200 in the signature,
# because protobuf will pass 0 as value

0 comments on commit 5c771ca

Please sign in to comment.
You can’t perform that action at this time.