Skip to content

Commit 7bf1aeb

Browse files
SDK: change list apis to return objects as default (#1630)
* SDK: change list apis to return objects as default - change list_trials, list_experiments to return list of objects as a default - also, give 'in_short' parameter for who wants only name and status as before * [enh]: change return type from List[dict] to List[V1beta1Experiment] * [enh]: deserialize dict to katib's custom class * [docs]: refactor KatibClient docs * change deserialize method location to utils * remove useless import * Add objects necessary to deserilization in swagger * use fakeresponse rather than duplicating codes * Update sdk/python/v1beta1/kubeflow/katib/utils/utils.py Co-authored-by: Andrey Velichkevich <[email protected]> * Update sdk/python/v1beta1/kubeflow/katib/utils/utils.py Co-authored-by: Andrey Velichkevich <[email protected]> Co-authored-by: Andrey Velichkevich <[email protected]>
1 parent 21580f6 commit 7bf1aeb

File tree

6 files changed

+40
-28
lines changed

6 files changed

+40
-28
lines changed

hack/gen-python-sdk/post_gen.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def _rewrite_helper(input_file, output_file, rewrite_rules):
5050
lines.append("from kubernetes.client import V1ListMeta\n")
5151
lines.append("from kubernetes.client import V1Container\n")
5252
lines.append("from kubernetes.client import V1HTTPGetAction\n")
53+
lines.append("from kubernetes.client import V1ManagedFieldsEntry\n")
54+
lines.append("from kubernetes.client import V1OwnerReference\n")
5355

5456
with open(output_file, 'w') as f:
5557
f.writelines(lines)

hack/gen-python-sdk/swagger_config.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"V1Container": "from kubernetes.client import V1Container",
77
"V1ListMeta": "from kubernetes.client import V1ListMeta",
88
"V1ObjectMeta": "from kubernetes.client import V1ObjectMeta",
9-
"V1HTTPGetAction": "from kubernetes.client import V1HTTPGetAction"
9+
"V1HTTPGetAction": "from kubernetes.client import V1HTTPGetAction",
10+
"V1ManagedFieldsEntry": "from kubernetes.client import V1ManagedFieldsEntry",
11+
"V1OwnerReference": "from kubernetes.client import V1OwnerReference"
1012
},
1113
"typeMappings": {
1214
"V1Time": "datetime",

sdk/python/v1beta1/docs/KatibClient.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ dict
116116
List all Katib Experiments.
117117
If the namespace is `None`, it takes "default" namespace.
118118

119-
Return list of Experiment names with the statuses.
119+
Return list of Experiment objects.
120120

121121
### Parameters
122122

@@ -126,7 +126,7 @@ Return list of Experiment names with the statuses.
126126

127127
### Return type
128128

129-
list[dict]
129+
list[V1beta1Experiment]
130130

131131
## get_experiment_status
132132

@@ -175,7 +175,7 @@ bool
175175
List all Experiment's Trials.
176176
If the namespace is `None`, it takes "default" namespace.
177177

178-
Return list of Trial names with the statuses.
178+
Return list of Trial objects
179179

180180
### Parameters
181181

@@ -186,7 +186,7 @@ Return list of Trial names with the statuses.
186186

187187
### Return type
188188

189-
list[dict]
189+
list[V1beta1Trial]
190190

191191
## get_success_trial_details
192192

sdk/python/v1beta1/kubeflow/katib/api/katib_client.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414

1515
import multiprocessing
1616

17-
from kubernetes import client, config
18-
17+
from kubeflow.katib import V1beta1Experiment
18+
from kubeflow.katib import V1beta1Trial
19+
from kubeflow.katib.api_client import ApiClient
1920
from kubeflow.katib.constants import constants
2021
from kubeflow.katib.utils import utils
22+
from kubernetes import client, config
2123

2224

2325
class KatibClient(object):
@@ -45,6 +47,7 @@ def __init__(self, config_file=None, context=None,
4547
self.in_cluster = True
4648

4749
self.api_instance = client.CustomObjectsApi()
50+
self.api_client = ApiClient()
4851

4952
def _is_ipython(self):
5053
"""Returns whether we are running in notebook."""
@@ -251,8 +254,8 @@ def list_experiments(self, namespace=None):
251254
:param namespace: Experiments namespace.
252255
If the namespace is None, it takes "default" namespace.
253256
254-
:return: List of Experiment names with the statuses.
255-
:rtype: list[dict]
257+
:return: List of Experiment objects.
258+
:rtype: list[V1beta1Experiment]
256259
"""
257260
if namespace is None:
258261
namespace = utils.get_default_target_namespace()
@@ -267,13 +270,11 @@ def list_experiments(self, namespace=None):
267270
katibexp = None
268271
try:
269272
katibexp = thread.get(constants.APISERVER_TIMEOUT)
270-
result = []
271-
for i in katibexp.get("items"):
272-
output = {}
273-
output["name"] = i.get("metadata", {}).get("name")
274-
output["status"] = i.get("status", {}).get(
275-
"conditions", [])[-1].get("type")
276-
result.append(output)
273+
result = [
274+
self.api_client.deserialize(utils.FakeResponse(item), V1beta1Experiment)
275+
for item in katibexp.get("items")
276+
]
277+
277278
except multiprocessing.TimeoutError:
278279
raise RuntimeError("Timeout trying to get katib experiment.")
279280
except client.rest.ApiException as e:
@@ -282,8 +283,8 @@ def list_experiments(self, namespace=None):
282283
%s\n" % e)
283284
except Exception as e:
284285
raise RuntimeError(
285-
"There was a problem to get experiments in namespace {1}. Exception: \
286-
{2} ".format(namespace, e))
286+
"There was a problem to get experiments in namespace {0}. Exception: \
287+
{1} ".format(namespace, e))
287288
return result
288289

289290
def get_experiment_status(self, name, namespace=None):
@@ -324,8 +325,8 @@ def list_trials(self, name=None, namespace=None):
324325
:param namespace: Experiments namespace.
325326
If the namespace is None, it takes "default" namespace.
326327
327-
:return: List of Trial names with the statuses.
328-
:rtype: list[dict]
328+
:return: List of Trial objects
329+
:rtype: list[V1beta1Trial]
329330
"""
330331
if namespace is None:
331332
namespace = utils.get_default_target_namespace()
@@ -340,14 +341,10 @@ def list_trials(self, name=None, namespace=None):
340341
katibtrial = None
341342
try:
342343
katibtrial = thread.get(constants.APISERVER_TIMEOUT)
343-
result = []
344-
for i in katibtrial.get("items"):
345-
output = {}
346-
if i.get("metadata", {}).get("ownerReferences")[0].get("name") == name:
347-
output["name"] = i.get("metadata", {}).get("name")
348-
output["status"] = i.get("status", {}).get(
349-
"conditions", [])[-1].get("type")
350-
result.append(output)
344+
result = [
345+
self.api_client.deserialize(utils.FakeResponse(item), V1beta1Trial)
346+
for item in katibtrial.get("items")
347+
]
351348
except multiprocessing.TimeoutError:
352349
raise RuntimeError("Timeout trying to getkatib experiment.")
353350
except client.rest.ApiException as e:

sdk/python/v1beta1/kubeflow/katib/models/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@
6161
from kubernetes.client import V1ListMeta
6262
from kubernetes.client import V1Container
6363
from kubernetes.client import V1HTTPGetAction
64+
from kubernetes.client import V1ManagedFieldsEntry
65+
from kubernetes.client import V1OwnerReference

sdk/python/v1beta1/kubeflow/katib/utils/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import json
1516
import os
1617

1718

@@ -34,3 +35,11 @@ def set_katib_namespace(katib):
3435
katib_namespace = katib.metadata.namespace
3536
namespace = katib_namespace or get_default_target_namespace()
3637
return namespace
38+
39+
class FakeResponse:
40+
"""Fake object of RESTResponse to deserialize
41+
Ref) https://github.com/kubeflow/katib/pull/1630#discussion_r697877815
42+
Ref) https://github.com/kubernetes-client/python/issues/977#issuecomment-592030030
43+
"""
44+
def __init__(self, obj):
45+
self.data = json.dumps(obj)

0 commit comments

Comments
 (0)