|
1 | 1 | # -*- coding: utf-8 -*-
|
2 |
| -import json |
3 |
| -import os.path |
4 |
| - |
5 |
| -import yaml |
6 |
| -from chaoslib.exceptions import ActivityFailed |
7 | 2 | from chaoslib.types import Secrets
|
8 |
| -from kubernetes import client |
9 |
| -from kubernetes.client.rest import ApiException |
10 |
| -from logzero import logger |
11 | 3 |
|
12 |
| -from chaosk8s import create_k8s_api_client |
| 4 | +from chaosk8s import _log_deprecated |
| 5 | +from chaosk8s.deployment.actions import create_deployment, delete_deployment, \ |
| 6 | + scale_deployment |
| 7 | +from chaosk8s.replicaset.actions import delete_replica_set |
| 8 | +from chaosk8s.pod.actions import delete_pods |
| 9 | +from chaosk8s.service.actions import delete_service |
13 | 10 |
|
14 | 11 | __all__ = ["start_microservice", "kill_microservice", "scale_microservice",
|
15 | 12 | "remove_service_endpoint"]
|
|
18 | 15 | def start_microservice(spec_path: str, ns: str = "default",
|
19 | 16 | secrets: Secrets = None):
|
20 | 17 | """
|
21 |
| - Start a microservice described by the deployment config, which must be the |
22 |
| - path to the JSON or YAML representation of the deployment. |
23 |
| - """ |
24 |
| - api = create_k8s_api_client(secrets) |
25 |
| - |
26 |
| - with open(spec_path) as f: |
27 |
| - p, ext = os.path.splitext(spec_path) |
28 |
| - if ext == '.json': |
29 |
| - deployment = json.loads(f.read()) |
30 |
| - elif ext in ['.yml', '.yaml']: |
31 |
| - deployment = yaml.safe_load(f.read()) |
32 |
| - else: |
33 |
| - raise ActivityFailed( |
34 |
| - "cannot process {path}".format(path=spec_path)) |
35 |
| - |
36 |
| - v1 = client.AppsV1Api(api) |
37 |
| - resp = v1.create_namespaced_deployment(ns, body=deployment) |
38 |
| - return resp |
39 |
| - |
40 |
| - |
41 |
| -def kill_microservice(name: str, ns: str = "default", |
42 |
| - label_selector: str = "name in ({name})", |
43 |
| - secrets: Secrets = None): |
44 |
| - """ |
45 |
| - Kill a microservice by `name` in the namespace `ns`. |
46 |
| -
|
47 |
| - The microservice is killed by deleting the deployment for it without |
48 |
| - a graceful period to trigger an abrupt termination. |
49 |
| -
|
50 |
| - The selected resources are matched by the given `label_selector`. |
| 18 | + !!!DEPRECATED!!! |
51 | 19 | """
|
52 |
| - label_selector = label_selector.format(name=name) |
53 |
| - api = create_k8s_api_client(secrets) |
54 |
| - |
55 |
| - v1 = client.AppsV1Api(api) |
56 |
| - if label_selector: |
57 |
| - ret = v1.list_namespaced_deployment(ns, label_selector=label_selector) |
58 |
| - else: |
59 |
| - ret = v1.list_namespaced_deployment(ns) |
60 |
| - |
61 |
| - logger.debug("Found {d} deployments named '{n}'".format( |
62 |
| - d=len(ret.items), n=name)) |
63 |
| - |
64 |
| - body = client.V1DeleteOptions() |
65 |
| - for d in ret.items: |
66 |
| - res = v1.delete_namespaced_deployment( |
67 |
| - d.metadata.name, ns, body=body) |
68 |
| - |
69 |
| - v1 = client.AppsV1Api(api) |
70 |
| - if label_selector: |
71 |
| - ret = v1.list_namespaced_replica_set(ns, label_selector=label_selector) |
72 |
| - else: |
73 |
| - ret = v1.list_namespaced_replica_set(ns) |
74 |
| - |
75 |
| - logger.debug("Found {d} replica sets named '{n}'".format( |
76 |
| - d=len(ret.items), n=name)) |
77 |
| - |
78 |
| - body = client.V1DeleteOptions() |
79 |
| - for r in ret.items: |
80 |
| - res = v1.delete_namespaced_replica_set( |
81 |
| - r.metadata.name, ns, body=body) |
82 |
| - |
83 |
| - v1 = client.CoreV1Api(api) |
84 |
| - if label_selector: |
85 |
| - ret = v1.list_namespaced_pod(ns, label_selector=label_selector) |
86 |
| - else: |
87 |
| - ret = v1.list_namespaced_pod(ns) |
88 |
| - |
89 |
| - logger.debug("Found {d} pods named '{n}'".format( |
90 |
| - d=len(ret.items), n=name)) |
91 |
| - |
92 |
| - body = client.V1DeleteOptions() |
93 |
| - for p in ret.items: |
94 |
| - res = v1.delete_namespaced_pod( |
95 |
| - p.metadata.name, ns, body=body) |
| 20 | + _log_deprecated("start_microservice", "create_deployment") |
| 21 | + create_deployment(spec_path, ns, secrets) |
96 | 22 |
|
97 | 23 |
|
98 | 24 | def remove_service_endpoint(name: str, ns: str = "default",
|
99 | 25 | secrets: Secrets = None):
|
100 | 26 | """
|
101 |
| - Remove the service endpoint that sits in front of microservices (pods). |
| 27 | + !!!DEPRECATED!!! |
102 | 28 | """
|
103 |
| - api = create_k8s_api_client(secrets) |
104 |
| - |
105 |
| - v1 = client.CoreV1Api(api) |
106 |
| - v1.delete_namespaced_service(name, namespace=ns) |
| 29 | + _log_deprecated("remove_service_endpoint", "delete_service") |
| 30 | + delete_service(name, ns, secrets) |
107 | 31 |
|
108 | 32 |
|
109 | 33 | def scale_microservice(name: str, replicas: int, ns: str = "default",
|
110 | 34 | secrets: Secrets = None):
|
111 | 35 | """
|
112 |
| - Scale a deployment up or down. The `name` is the name of the deployment. |
| 36 | + !!!DEPRECATED!!! |
113 | 37 | """
|
114 |
| - api = create_k8s_api_client(secrets) |
| 38 | + _log_deprecated("scale_microserviceal", "scale_deployment") |
| 39 | + scale_deployment(name, replicas, ns, secrets) |
| 40 | + |
115 | 41 |
|
116 |
| - v1 = client.AppsV1Api(api) |
117 |
| - body = {"spec": {"replicas": replicas}} |
118 |
| - try: |
119 |
| - v1.patch_namespaced_deployment_scale(name, namespace=ns, body=body) |
120 |
| - except ApiException as e: |
121 |
| - raise ActivityFailed( |
122 |
| - "failed to scale '{s}' to {r} replicas: {e}".format( |
123 |
| - s=name, r=replicas, e=str(e))) |
| 42 | +def kill_microservice(name: str, ns: str = "default", |
| 43 | + label_selector: str = "name in ({name})", |
| 44 | + secrets: Secrets = None): |
| 45 | + """ |
| 46 | + !!!DEPRECATED!!! |
| 47 | + """ |
| 48 | + _log_deprecated("kill_microservice", |
| 49 | + "delete_deployment/delete_replica_set/delete_pods") |
| 50 | + delete_deployment(name, ns, label_selector, secrets) |
| 51 | + delete_replica_set(name, ns, label_selector, secrets) |
| 52 | + delete_pods(name, ns, label_selector, secrets) |
0 commit comments