-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathactions.py
51 lines (42 loc) · 1.36 KB
/
actions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import json
import os.path
import yaml
from chaoslib.exceptions import ActivityFailed
from chaoslib.types import Secrets
from kubernetes import client
from chaosk8s import create_k8s_api_client
__all__ = ["create_namespace", "delete_namespace"]
def create_namespace(
name: str = None, spec_path: str = None, secrets: Secrets = None
):
"""
Create a namespace from the specified spec_path, which must be
the path to the JSON or YAML representation of the namespace.
"""
api = create_k8s_api_client(secrets)
if spec_path:
with open(spec_path) as f:
p, ext = os.path.splitext(spec_path)
if ext == ".json":
ns = json.loads(f.read())
elif ext in [".yml", ".yaml"]:
ns = yaml.safe_load(f.read())
else:
raise ActivityFailed(f"cannot process {spec_path}")
elif name:
ns = {
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {"name": name},
}
else:
raise ActivityFailed("You need to either specify name or spec_path")
v1 = client.CoreV1Api(api)
v1.create_namespace(body=ns)
def delete_namespace(name: str, secrets: Secrets = None):
"""
Remove the given namespace
"""
api = create_k8s_api_client(secrets)
v1 = client.CoreV1Api(api)
v1.delete_namespace(name)