Skip to content

Commit 96d33ca

Browse files
Merge pull request #227 from dataiku/task/dss10-sc-82511-k8s-cluster-actions
Add k8s cluster actions
2 parents 1e4d6d8 + d4c0e29 commit 96d33ca

File tree

1 file changed

+76
-3
lines changed

1 file changed

+76
-3
lines changed

dataikuapi/dss/admin.py

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,22 +1217,94 @@ def start(self):
12171217
raise Exception('Cluster operation failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
12181218
return resp
12191219

1220-
def stop(self, terminate=True):
1220+
def stop(self, terminate=True, force_stop=False):
12211221
"""
12221222
Stops or detaches the cluster
12231223
12241224
This operation is only valid for a managed cluster.
1225-
:param boolean terminate: whether to delete the cluster after stopping it
1225+
1226+
:param bool terminate: whether to delete the cluster after stopping it
1227+
:param bool force_stop: whether to try to force stop the cluster,
1228+
useful if DSS expects the cluster to already be stopped
12261229
"""
12271230
resp = self.client._perform_json(
12281231
"POST", "/admin/clusters/%s/actions/stop" % (self.cluster_id),
1229-
params = {'terminate':terminate})
1232+
params={'terminate': terminate, 'forceStop': force_stop})
12301233
if resp is None:
12311234
raise Exception('Env update returned no data')
12321235
if resp.get('messages', {}).get('error', False):
12331236
raise Exception('Cluster operation failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
12341237
return resp
12351238

1239+
def run_kubectl(self, args):
1240+
"""
1241+
Runs an arbitrary kubectl command on the cluster.
1242+
1243+
This operation is only valid for a Kubernetes cluster.
1244+
1245+
Note: this call requires an API key with DSS instance admin rights
1246+
1247+
:param str args: the arguments to pass to kubectl (without the "kubectl")
1248+
:return: a dict containing the return value, standard output, and standard error of the command
1249+
:rtype: dict
1250+
"""
1251+
return self.client._perform_json(
1252+
"POST", "/admin/clusters/%s/k8s/actions/run-kubectl" % self.cluster_id,
1253+
body={'args': args})
1254+
1255+
def delete_finished_jobs(self, delete_failed=False, namespace=None, label_filter=None, dry_run=False):
1256+
"""
1257+
Runs a kubectl command to delete finished jobs.
1258+
1259+
This operation is only valid for a Kubernetes cluster.
1260+
1261+
:param bool delete_failed: if True, delete both completed and failed jobs, otherwise only delete completed jobs
1262+
:param str namespace: the namespace in which to delete the jobs, if None, uses the namespace set in kubectl's current context
1263+
:param str label_filter: delete only jobs matching a label filter
1264+
:param bool dry_run: if True, execute the command as a "dry run"
1265+
:return: a dict containing whether the deletion succeeded, a list of deleted job names, and
1266+
debug info for the underlying kubectl command
1267+
:rtype: dict
1268+
"""
1269+
return self.client._perform_json(
1270+
"POST", "/admin/clusters/%s/k8s/jobs/actions/delete-finished" % self.cluster_id,
1271+
params={'deleteFailed': delete_failed, 'namespace': namespace, 'labelFilter': label_filter, 'dryRun': dry_run})
1272+
1273+
def delete_finished_pods(self, namespace=None, label_filter=None, dry_run=False):
1274+
"""
1275+
Runs a kubectl command to delete finished (succeeded and failed) pods.
1276+
1277+
This operation is only valid for a Kubernetes cluster.
1278+
1279+
:param str namespace: the namespace in which to delete the pods, if None, uses the namespace set in kubectl's current context
1280+
:param str label_filter: delete only pods matching a label filter
1281+
:param bool dry_run: if True, execute the command as a "dry run"
1282+
:return: a dict containing whether the deletion succeeded, a list of deleted pod names, and
1283+
debug info for the underlying kubectl command
1284+
:rtype: dict
1285+
"""
1286+
return self.client._perform_json(
1287+
"POST", "/admin/clusters/%s/k8s/pods/actions/delete-finished" % self.cluster_id,
1288+
params={'namespace': namespace, 'labelFilter': label_filter, 'dryRun': dry_run})
1289+
1290+
def delete_all_pods(self, namespace=None, label_filter=None, dry_run=False):
1291+
"""
1292+
Runs a kubectl command to delete all pods.
1293+
1294+
This operation is only valid for a Kubernetes cluster.
1295+
1296+
:param str namespace: the namespace in which to delete the pods, if None, uses the namespace set in kubectl's current context
1297+
:param str label_filter: delete only pods matching a label filter
1298+
:param bool dry_run: if True, execute the command as a "dry run"
1299+
:return: a dict containing whether the deletion succeeded, a list of deleted pod names, and
1300+
debug info for the underlying kubectl command
1301+
:rtype: dict
1302+
"""
1303+
return self.client._perform_json(
1304+
"POST", "/admin/clusters/%s/k8s/pods/actions/delete-all" % self.cluster_id,
1305+
params={'namespace': namespace, 'labelFilter': label_filter, 'dryRun': dry_run})
1306+
1307+
12361308
class DSSClusterSettings(object):
12371309
"""
12381310
The settings of a cluster
@@ -1268,6 +1340,7 @@ def save(self):
12681340
return self.client._perform_json(
12691341
"PUT", "/admin/clusters/%s" % (self.cluster_id), body=self.settings)
12701342

1343+
12711344
class DSSClusterStatus(object):
12721345
"""
12731346
The status of a cluster

0 commit comments

Comments
 (0)