-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathactions.py
227 lines (195 loc) · 6.39 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import json
import os.path
from typing import Any, Dict
import yaml
from chaoslib.exceptions import ActivityFailed
from chaoslib.types import Secrets
from kubernetes import client
from kubernetes.client.rest import ApiException
from chaosk8s import create_k8s_api_client
__all__ = [
"create_ingress",
"delete_ingress",
"update_ingress",
"create_network_policy",
"remove_network_policy",
"deny_all_ingress",
"remove_deny_all_ingress",
"deny_all_egress",
"remove_deny_all_egress",
"allow_dns_access",
"remove_allow_dns_access",
]
def create_ingress(
spec_path: str, ns: str = "default", secrets: Secrets = None
):
"""
Create an ingress object from the specified spec_path, which must be
the path to the JSON or YAML representation of the ingress.
"""
api = create_k8s_api_client(secrets)
with open(spec_path) as f:
ext = spec_path.split(".")[-1]
if ext == "json":
body = json.loads(f.read())
elif ext in ["yml", "yaml"]:
body = yaml.safe_load(f.read())
else:
raise ActivityFailed(f"cannot process {spec_path}")
v1 = client.NetworkingV1Api(api)
v1.create_namespaced_ingress(namespace=ns, body=body)
def delete_ingress(name: str, ns: str = "default", secrets: Secrets = None):
"""
Remove the given ingress
"""
api = create_k8s_api_client(secrets)
v1 = client.NetworkingV1Api(api)
v1.delete_namespaced_ingress(name, namespace=ns)
def update_ingress(
name: str, spec: dict, ns: str = "default", secrets: Secrets = None
):
"""
Update the specification of the targeted ingress according to spec.
"""
api = create_k8s_api_client(secrets)
v1 = client.NetworkingV1Api(api)
try:
v1.patch_namespaced_ingress(name=name, namespace=ns, body=spec)
except ApiException as e:
raise ActivityFailed(f"failed to update daemon set '{name}': {str(e)}")
def create_network_policy(
spec: Dict[str, Any] = None,
spec_path: str = None,
ns: str = "default",
secrets: Secrets = None,
):
"""
Create a network policy in the given namespace eitehr from the definition
as `spec` or from a file containing the definition at `spec_path`.
"""
api = create_k8s_api_client(secrets)
if spec_path and os.path.isfile(spec_path):
with open(spec_path) as f:
p, ext = os.path.splitext(spec_path)
if ext == ".json":
spec = json.loads(f.read())
elif ext in [".yml", ".yaml"]:
spec = yaml.safe_load(f.read())
else:
raise ActivityFailed(f"cannot process {spec_path}")
v1 = client.NetworkingV1Api(api)
v1.create_namespaced_network_policy(ns, body=spec)
def remove_network_policy(
name: str, ns: str = "default", secrets: Secrets = None
):
"""
Create a network policy in the given namespace eitehr from the definition
as `spec` or from a file containing the definition at `spec_path`.
"""
api = create_k8s_api_client(secrets)
v1 = client.NetworkingV1Api(api)
v1.delete_namespaced_network_policy(name, ns)
def deny_all_ingress(
label_selectors: Dict[str, Any] = None,
ns: str = "default",
secrets: Secrets = None,
):
"""
Convenient helper policy to deny ingress network to all pods in a
namespace, unless `label_selectors, in which case, only matching pods will
be impacted.
"""
pod_selector = {}
if label_selectors:
pod_selector["matchLabels"] = label_selectors
create_network_policy(
spec={
"apiVersion": "networking.k8s.io/v1",
"kind": "NetworkPolicy",
"metadata": {"name": "chaostoolkit-deny-all-ingress"},
"spec": {
"podSelector": pod_selector,
"policyTypes": ["Ingress"],
"ingress": [],
},
},
ns=ns,
secrets=secrets,
)
def remove_deny_all_ingress(ns: str = "default", secrets: Secrets = None):
"""
Remove the rule set by the `deny_all_ingress` action.
"""
remove_network_policy(
"chaostoolkit-deny-all-ingress", ns=ns, secrets=secrets
)
def deny_all_egress(
label_selectors: Dict[str, Any] = None,
ns: str = "default",
secrets: Secrets = None,
):
"""
Convenient helper rule to deny all egress network from all pods in a
namespace, unless `label_selectors, in which case, only matching pods will
be impacted.
"""
pod_selector = {}
if label_selectors:
pod_selector["matchLabels"] = label_selectors
create_network_policy(
{
"apiVersion": "networking.k8s.io/v1",
"kind": "NetworkPolicy",
"metadata": {"name": "chaostoolkit-deny-all-egress"},
"spec": {"podSelector": pod_selector, "policyTypes": ["Egress"]},
},
ns=ns,
secrets=secrets,
)
def remove_deny_all_egress(ns: str = "default", secrets: Secrets = None):
"""
Remove the rule set by the `deny_all_egress` action.
"""
remove_network_policy(
"chaostoolkit-deny-all-egress", ns=ns, secrets=secrets
)
def allow_dns_access(
label_selectors: Dict[str, Any] = None,
ns: str = "default",
secrets: Secrets = None,
):
"""
Convenient helper rule to DNS access from all pods
in a namespace, unless `label_selectors, in which case, only matching pods
will be impacted.
"""
pod_selector = {}
if label_selectors:
pod_selector["matchLabels"] = label_selectors
create_network_policy(
{
"apiVersion": "networking.k8s.io/v1",
"kind": "NetworkPolicy",
"metadata": {"name": "chaostoolkit-allow-dns"},
"spec": {
"podSelector": pod_selector,
"policyTypes": ["Egress"],
"egress": [
{
"to": [{"namespaceSelector": {}}],
"ports": [
{"port": 53, "protocol": "UDP"},
{"port": 53, "protocol": "TCP"},
],
}
],
},
},
ns=ns,
secrets=secrets,
)
def remove_allow_dns_access(ns: str = "default", secrets: Secrets = None):
"""
Remove the rule set by the `allow_dns_access` action.
"""
remove_network_policy("chaostoolkit-allow-dns", ns=ns, secrets=secrets)