Skip to content

Commit

Permalink
fix: refactor deployment v2 client and cli (#4383)
Browse files Browse the repository at this point in the history
* fix: refactor deployment v2 client

Signed-off-by: FogDong <[email protected]>

* refactor cli

Signed-off-by: FogDong <[email protected]>

* fix lint

Signed-off-by: FogDong <[email protected]>

* resolve comments

Signed-off-by: FogDong <[email protected]>

* Update src/bentoml/_internal/cloud/deployment.py

* Update tests/unit/_internal/cloud/test_deployment.py

---------

Signed-off-by: FogDong <[email protected]>
Co-authored-by: Frost Ming <[email protected]>
  • Loading branch information
FogDong and frostming authored Jan 9, 2024
1 parent 7813cfa commit 777301d
Show file tree
Hide file tree
Showing 8 changed files with 897 additions and 784 deletions.
72 changes: 40 additions & 32 deletions src/bentoml/_internal/cloud/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,9 @@ def get_models_list(self) -> ModelWithRepositoryListSchema | None:
return schema_from_json(resp.text, ModelWithRepositoryListSchema)

def get_cluster_deployment_list(
self, cluster_name: str, **params: str | int | None
self, cluster: str, **params: str | int | None
) -> DeploymentListSchema | None:
url = urljoin(self.endpoint, f"/api/v1/clusters/{cluster_name}/deployments")
url = urljoin(self.endpoint, f"/api/v1/clusters/{cluster}/deployments")
resp = self.session.get(url, params=params)
if self._is_not_found(resp):
return None
Expand All @@ -441,19 +441,19 @@ def get_organization_deployment_list(
return schema_from_json(resp.text, DeploymentListSchema)

def create_deployment(
self, cluster_name: str, create_schema: CreateDeploymentSchemaV1
self, cluster: str, create_schema: CreateDeploymentSchemaV1
) -> DeploymentFullSchema | None:
url = urljoin(self.endpoint, f"/api/v1/clusters/{cluster_name}/deployments")
url = urljoin(self.endpoint, f"/api/v1/clusters/{cluster}/deployments")
resp = self.session.post(url, content=schema_to_json(create_schema))
self._check_resp(resp)
return schema_from_json(resp.text, DeploymentFullSchema)

def get_deployment(
self, cluster_name: str, kube_namespace: str, deployment_name: str
self, cluster: str, kube_namespace: str, name: str
) -> DeploymentFullSchema | None:
url = urljoin(
self.endpoint,
f"/api/v1/clusters/{cluster_name}/namespaces/{kube_namespace}/deployments/{deployment_name}",
f"/api/v1/clusters/{cluster}/namespaces/{kube_namespace}/deployments/{name}",
)
resp = self.session.get(url)
if self._is_not_found(resp):
Expand All @@ -463,14 +463,14 @@ def get_deployment(

def update_deployment(
self,
cluster_name: str,
cluster: str,
kube_namespace: str,
deployment_name: str,
name: str,
update_schema: UpdateDeploymentSchema,
) -> DeploymentFullSchema | None:
url = urljoin(
self.endpoint,
f"/api/v1/clusters/{cluster_name}/namespaces/{kube_namespace}/deployments/{deployment_name}",
f"/api/v1/clusters/{cluster}/namespaces/{kube_namespace}/deployments/{name}",
)
resp = self.session.patch(url, content=schema_to_json(update_schema))
if self._is_not_found(resp):
Expand All @@ -479,11 +479,11 @@ def update_deployment(
return schema_from_json(resp.text, DeploymentFullSchema)

def terminate_deployment(
self, cluster_name: str, kube_namespace: str, deployment_name: str
self, cluster: str, kube_namespace: str, name: str
) -> DeploymentFullSchema | None:
url = urljoin(
self.endpoint,
f"/api/v1/clusters/{cluster_name}/namespaces/{kube_namespace}/deployments/{deployment_name}/terminate",
f"/api/v1/clusters/{cluster}/namespaces/{kube_namespace}/deployments/{name}/terminate",
)
resp = self.session.post(url)
if self._is_not_found(resp):
Expand All @@ -492,11 +492,11 @@ def terminate_deployment(
return schema_from_json(resp.text, DeploymentFullSchema)

def delete_deployment(
self, cluster_name: str, kube_namespace: str, deployment_name: str
self, cluster: str, kube_namespace: str, name: str
) -> DeploymentFullSchema | None:
url = urljoin(
self.endpoint,
f"/api/v1/clusters/{cluster_name}/namespaces/{kube_namespace}/deployments/{deployment_name}",
f"/api/v1/clusters/{cluster}/namespaces/{kube_namespace}/deployments/{name}",
)
resp = self.session.delete(url)
if self._is_not_found(resp):
Expand All @@ -514,8 +514,8 @@ def get_cluster_list(
self._check_resp(resp)
return schema_from_json(resp.text, ClusterListSchema)

def get_cluster(self, cluster_name: str) -> ClusterFullSchema | None:
url = urljoin(self.endpoint, f"/api/v1/clusters/{cluster_name}")
def get_cluster(self, cluster: str) -> ClusterFullSchema | None:
url = urljoin(self.endpoint, f"/api/v1/clusters/{cluster}")
resp = self.session.get(url)
if self._is_not_found(resp):
return None
Expand All @@ -540,48 +540,52 @@ def get_latest_model(

class RestApiClientV2(BaseRestApiClient):
def create_deployment(
self, create_schema: CreateDeploymentSchemaV2, cluster_name: str
self,
create_schema: CreateDeploymentSchemaV2,
cluster: str | None = None,
) -> DeploymentFullSchemaV2:
url = urljoin(self.endpoint, "/api/v2/deployments")
resp = self.session.post(
url, content=schema_to_json(create_schema), params={"cluster": cluster_name}
url, content=schema_to_json(create_schema), params={"cluster": cluster}
)
self._check_resp(resp)
return schema_from_json(resp.text, DeploymentFullSchemaV2)

def update_deployment(
self,
name: str,
update_schema: UpdateDeploymentSchemaV2,
cluster_name: str,
deployment_name: str,
cluster: str | None = None,
) -> DeploymentFullSchemaV2 | None:
url = urljoin(
self.endpoint,
f"/api/v2/deployments/{deployment_name}",
f"/api/v2/deployments/{name}",
)
data = schema_to_json(update_schema)
resp = self.session.put(url, content=data, params={"cluster": cluster_name})
resp = self.session.put(url, content=data, params={"cluster": cluster})
if self._is_not_found(resp):
return None
self._check_resp(resp)
return schema_from_json(resp.text, DeploymentFullSchemaV2)

def get_deployment(
self, cluster_name: str, deployment_name: str
self,
name: str,
cluster: str | None = None,
) -> DeploymentFullSchemaV2 | None:
url = urljoin(
self.endpoint,
f"/api/v2/deployments/{deployment_name}",
f"/api/v2/deployments/{name}",
)
resp = self.session.get(url, params={"cluster": cluster_name})
resp = self.session.get(url, params={"cluster": cluster})
if self._is_not_found(resp):
return None
self._check_resp(resp)
return schema_from_json(resp.text, DeploymentFullSchemaV2)

def list_deployment(
self,
cluster_name: str | None = None,
cluster: str | None = None,
all: bool | None = None,
# if both of the above is none, list default cluster's deployments
count: int | None = None,
Expand All @@ -593,7 +597,7 @@ def list_deployment(
resp = self.session.get(
url,
params={
"cluster": cluster_name,
"cluster": cluster,
"all": all,
"count": count,
"q": q,
Expand All @@ -607,26 +611,30 @@ def list_deployment(
return schema_from_json(resp.text, DeploymentListSchemaV2)

def terminate_deployment(
self, cluster_name: str, deployment_name: str
self,
name: str,
cluster: str | None = None,
) -> DeploymentFullSchemaV2 | None:
url = urljoin(
self.endpoint,
f"/api/v2/deployments/{deployment_name}/terminate",
f"/api/v2/deployments/{name}/terminate",
)
resp = self.session.post(url, params={"cluster": cluster_name})
resp = self.session.post(url, params={"cluster": cluster})
if self._is_not_found(resp):
return None
self._check_resp(resp)
return schema_from_json(resp.text, DeploymentFullSchemaV2)

def delete_deployment(
self, cluster_name: str, deployment_name: str
self,
name: str,
cluster: str | None = None,
) -> DeploymentFullSchemaV2 | None:
url = urljoin(
self.endpoint,
f"/api/v2/deployments/{deployment_name}",
f"/api/v2/deployments/{name}",
)
resp = self.session.delete(url, params={"cluster": cluster_name})
resp = self.session.delete(url, params={"cluster": cluster})
if self._is_not_found(resp):
return None
self._check_resp(resp)
Expand Down
Loading

0 comments on commit 777301d

Please sign in to comment.