Skip to content

Commit c4709fb

Browse files
oumkaleispeakc0de
andauthored
Adding SDK for litmus-python and updating base code changes (#19)
* adding sdk for litmus-python Signed-off-by: Oum Kale <[email protected]> * minor changes in README Signed-off-by: Oum Kale <[email protected]> * Apply suggestions from code review Co-authored-by: Shubham Chaudhary <[email protected]>
1 parent ed3027b commit c4709fb

40 files changed

+1361
-37
lines changed

CHANGELOG.md

Lines changed: 0 additions & 13 deletions
This file was deleted.

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ RUN ./install.sh
4646
WORKDIR /litmus
4747

4848
# Copying experiment file
49-
COPY ./bin/experiment/experiment.py ./experiments
49+
COPY ./bin/experiment/experiment.py ./experiment
5050

5151
ENV PYTHONPATH /litmus
5252

__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#!/usr/bin/env python
-1.19 KB
Binary file not shown.

bin/experiment/experiment.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22

3-
import experimentList.generic.podDelete.podDelete as podDelete
3+
import experiments.generic.pod_delete.pod_delete as pod_delete
44
import argparse
55
import logging
66
import pkg.utils.client.client as client
@@ -23,10 +23,9 @@ def main():
2323

2424
# invoke the corresponding experiment based on the the (-name) flag
2525
if args.name == "pod-delete":
26-
podDelete.PodDelete(clients)
26+
pod_delete.PodDelete(clients)
2727
else:
2828
logging.error("Unsupported -name %s, please provide the correct value of -name args", args.name)
2929
return
3030
if __name__ == "__main__":
3131
main()
32-
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
## Steps to Bootstrap a Chaos Experiment
2+
3+
The artifacts associated with a chaos-experiment are summarized below:
4+
5+
- Submitted in the litmuschaos/litmus-python repository, under the experiments/*chaos_category*/*experiment_name* folder
6+
7+
- Experiment business logic in python. May involve creating new or reusing an existing chaosLib
8+
- Experiment test deployment manifest that is used for verification purposes
9+
- Experiment RBAC (holds experiment-specific ServiceAccount, Role and RoleBinding)
10+
11+
Example: [pod delete experiment in litmus-python](/experiments/generic/pod_delete)
12+
13+
- Submitted in litmuschaos/chaos-charts repository, under the *chaos_category* folder
14+
15+
- Experiment custom resource (CR) (holds experiment-specific chaos parameters & experiment entrypoint)
16+
- Experiment ChartServiceVersion (holds experiment metadata that will be rendered on [charthub](hub.litmuschaos.io))
17+
- Experiment RBAC (holds experiment-specific ServiceAccount, Role and RoleBinding)
18+
- Experiment Engine (holds experiment-specific chaosengine)
19+
20+
Example: [pod delete experiment in chaos-charts](https://github.com/litmuschaos/chaos-charts/tree/master/charts/generic/pod-delete)
21+
22+
The *generate_experiment.py* script is a simple way to bootstrap your experiment, and helps create the aforementioned artifacts in the
23+
appropriate directory (i.e., as per the chaos_category) based on an attributes file provided as input by the chart-developer. The
24+
scaffolded files consist of placeholders which can then be filled as desired.
25+
26+
### Pre-Requisites
27+
28+
- *python3* is available (`sudo apt-get install python3`)
29+
- *jinja2* & *pyYaml* python packages are available (`sudo apt-get install python3-pip`, `pip install jinja2`, `pip install pyYaml`)
30+
31+
### Steps to Generate Experiment Manifests
32+
33+
- Clone the litmus-python repository & navigate to the `contribute/developer-guide` folder
34+
35+
```
36+
$ git clone https://github.com/litmuschaos/litmus-python.git
37+
$ cd litmus-python/contribute/developer-guide
38+
```
39+
40+
- Populate the `attributes.yaml` with details of the chaos experiment (or chart). Use the [attributes.yaml.sample](/contribute/developer-guide/attributes.yaml.sample) as reference.
41+
42+
As an example, let us consider an experiment to kill one of the replicas of a nginx deployment. The attributes.yaml can be constructed like this:
43+
44+
```yaml
45+
$ cat attributes.yaml
46+
47+
---
48+
name: "sample_exec_chaos"
49+
version: "0.1.0"
50+
category: "sample_category"
51+
repository: "https://github.com/litmuschaos/litmus-python/tree/master/sample_category/sample_exec_chaos"
52+
community: "https://kubernetes.slack.com/messages/CNXNB0ZTN"
53+
description: "it execs inside target pods to run the chaos inject commands, waits for the chaos duration and reverts the chaos"
54+
keywords:
55+
- "pods"
56+
- "kubernetes"
57+
- "sample-category"
58+
- "exec"
59+
platforms:
60+
- Minikube
61+
scope: "Namespaced"
62+
auxiliaryappcheck: false
63+
permissions:
64+
- apigroups:
65+
- ""
66+
- "batch"
67+
- "apps"
68+
- "litmuschaos.io"
69+
resources:
70+
- "jobs"
71+
- "pods"
72+
- "pods/log"
73+
- "events"
74+
- "deployments"
75+
- "replicasets"
76+
- "pods/exec"
77+
- "chaosengines"
78+
- "chaosexperiments"
79+
- "chaosresults"
80+
verbs:
81+
- "create"
82+
- "list"
83+
- "get"
84+
- "patch"
85+
- "update"
86+
- "delete"
87+
- "deletecollection"
88+
maturity: "alpha"
89+
maintainers:
90+
- name: "oumkale"
91+
92+
provider:
93+
name: "ChaosNative"
94+
minkubernetesversion: "1.12.0"
95+
references:
96+
- name: Documentation
97+
url: "https://docs.litmuschaos.io/docs/getstarted/"
98+
99+
```
100+
101+
- Run the following command to generate the necessary artefacts for submitting the `sample_category` chaos chart with
102+
`sample_exec_chaos` experiment.
103+
104+
```
105+
$ python3 generate_experiment.py -f=attributes.yaml -g=<generate-type>
106+
```
107+
108+
**Note**: Replace the `-g=<generate-type>` placeholder with the appropriate value based on the usecase:
109+
- `experiment`: Chaos experiment artifacts belonging to an existing OR new experiment.
110+
- `chart`: Just the chaos-chart metadata, i.e., chartserviceversion.yaml
111+
- Provide the type of chart in the `-t=<type>` flag. It supports the following values:
112+
- `category`: It creates the chart metadata for the category i.e chartserviceversion, package manifests
113+
- `experiment`: It creates the chart for the experiment i.e chartserviceversion, engine, rbac, experiment manifests
114+
- `all`: it creates both category and experiment charts (default type)
115+
116+
- Provide the path of the attribute.yaml manifest in the `-f` flag.
117+
118+
View the generated files in `/experiments/<chaos_category>` folder.
119+
120+
```
121+
$ cd /experiments
122+
123+
$ ls -ltr
124+
125+
total 8
126+
-rw-rw-r-- 1 oumkale oumkale 0 Jul 7 16:44 __init__.py
127+
drwxrwxr-x 3 oumkale oumkale 4096 Jul 7 16:44 generic/
128+
drwxrwxr-x 3 oumkale oumkale 4096 Jul 7 16:47 sample_category/
129+
130+
$ ls -ltr sample_category/
131+
132+
total 4
133+
-rw-rw-r-- 1 oumkale oumkale 0 Jul 7 16:50 __init__.py
134+
drwxr-xr-x 5 oumkale oumkale 4096 July 7 16:51 sample_exec_chaos/
135+
136+
$ ls -ltr sample_category/sample_exec_chaos/
137+
138+
total 12
139+
-rw-rw-r-- 1 oumkale oumkale 0 Jul 7 16:47 __init__.py
140+
drwxrwxr-x 2 oumkale oumkale 4096 Jul 7 16:48 experiment/
141+
drwxrwxr-x 2 oumkale oumkale 4096 Jul 7 16:49 charts/
142+
drwxrwxr-x 2 oumkale oumkale 4096 Jul 7 16:50 test/
143+
144+
$ ls -ltr sample_category/sample_exec_chaos/experiment
145+
146+
total 8
147+
-rw-rw-r-- 1 oumkale oumkale 0 Jul 7 18:43 __init__.py
148+
-rw-rw-r-- 1 oumkale oumkale 6440 Jul 7 18:47 sample_exec_chaos.py
149+
150+
$ ls -ltr sample_category/charts
151+
152+
total 24
153+
-rw-rw-r-- 1 oumkale oumkale 144 Jul 7 18:48 sample_category.package.yaml
154+
-rw-rw-r-- 1 oumkale oumkale 848 Jul 7 18:48 sample_category.category_chartserviceversion.yaml
155+
-rw-rw-r-- 1 oumkale oumkale 989 Jul 7 18:48 sample_exec_chaos.experiment_chartserviceversion.yaml
156+
-rw-rw-r-- 1 oumkale oumkale 1540 Jul 7 18:48 experiment.yaml
157+
-rw-rw-r-- 1 oumkale oumkale 1224 Jul 7 18:48 rbac.yaml
158+
-rw-rw-r-- 1 oumkale oumkale 731 Jul 7 18:48 engine.yaml
159+
160+
$ ls -ltr sample-category/sample-exec-chaos/test
161+
162+
total 4
163+
-rw-r--r-- 1 oumkale oumkale 1039 July 7 18:52 test.yaml
164+
165+
$ ls -ltr chaosLib
166+
total 4
167+
-rw-rw-r-- 1 oumkale oumkale 0 Jul 7 16:44 __init__.py
168+
drwxrwxr-x 4 oumkale oumkale 4096 Jul 7 18:43 litmus
169+
170+
$ ls -ltr chaosLib/litmus
171+
total 8
172+
drwxrwxr-x 3 oumkale oumkale 4096 Jul 7 16:44 pod_delete
173+
-rw-rw-r-- 1 oumkale oumkale 0 Jul 7 16:44 __init__.py
174+
drwxrwxr-x 2 oumkale oumkale 4096 Jul 7 18:43 sample_exec_chaos
175+
176+
$ ls -ltr chaosLib/litmus/sample_exec_chaos
177+
total 8
178+
-rw-rw-r-- 1 oumkale oumkale 0 Jul 7 18:43 __init__.py
179+
-rw-rw-r-- 1 oumkale oumkale 5828 Jul 7 18:47 sample_exec_chaos.py
180+
181+
```
182+
183+
- Proceed with construction of business logic inside the `sample_exec_chaos.py` file, by making
184+
the appropriate modifications listed below to achieve the desired effect:
185+
186+
- variables
187+
- entry & exit criteria checks for the experiment
188+
- helper utils in either [pkg](/pkg/) or new [base chaos libraries](/chaosLib)
189+
190+
- The chaosLib is created at `chaosLib/litmus/sample_exec_chaos/lib/sample_exec_chaos.py` path. It contains some pre-defined steps which runs the `ChaosInject` command (explicitly provided as an ENV var in the experiment CR). Which will induce chaos in the target application. It will wait for the given chaos duration and finally runs the `ChaosKill` command (also provided as an ENV var) for cleanup purposes. Update this chaosLib to achieve the desired effect based on the use-case or reuse the other existing chaosLib.
191+
192+
- Create an experiment README explaining, briefly, the *what*, *why* & *how* of the experiment to aid users of this experiment.
193+
194+
### Steps to Test Experiment
195+
196+
We can use [Okteto](https://github.com/okteto/okteto) to help us in performing the dev-tests for experiment created.
197+
Follow the steps provided below to setup okteto & test the experiment execution.
198+
199+
- Install the Okteto CLI
200+
201+
```
202+
curl https://get.okteto.com -sSfL | sh
203+
```
204+
205+
- (Optional) Create a sample nginx deployment that can be used as the application under test (AUT).
206+
207+
```
208+
kubectl create deployment nginx --image=nginx
209+
```
210+
211+
- Setup the RBAC necessary for execution of this experiment by applying the generated `rbac.yaml`
212+
213+
```
214+
kubectl apply -f rbac.yaml
215+
```
216+
217+
- Modify the `test/test.yaml` with the desired values (app & chaos info) in the ENV and appropriate `chaosServiceAccount` along
218+
with any other dependencies, if applicable (configmaps, volumes etc.,) & create this deployment
219+
220+
```
221+
kubectl apply -f test/test.yml
222+
```
223+
224+
- Go to the root of this repository (litmuschaos/litmus-python) & launch the Okteto development environment in your workspace.
225+
This should take you to the bash prompt on the dev container into which the content of the litmus-python repo is loaded.
226+
227+
- Note :
228+
- Avoid use `_` in names ex: sample_category(not sample-category)
229+
- Add packages routes for all the files which are generated from sdk in `setup.py` before creating image.
230+
example :
231+
```
232+
'chaosLib/litmus/sample_exec_chaos',
233+
'pkg/sample_category',
234+
'pkg/sample_category/environment',
235+
'pkg/sample_category/types',
236+
'experiments/sample_category',
237+
'experiments/sample_category/sample_exec_chaos',
238+
'experiments/sample_category/sample_exec_chaos/experiment',
239+
```
240+
- Import main file it in bin/experiment/experiment.py and add case. example: line number 3 in experiment.py
241+
- Then go to root(litmus-python) and run `python3 setup.py install`
242+
243+
```
244+
root@test:~/okteto/litmus-python# okteto up
245+
246+
Deployment litmus-python doesn't exist in namespace litmus. Do you want to create a new one? [y/n]: y
247+
✓ Development container activated
248+
✓ Files synchronized
249+
250+
The value of /proc/sys/fs/inotify/max_user_watches in your cluster nodes is too low.
251+
This can affect file synchronization performance.
252+
Visit https://okteto.com/docs/reference/known-issues/index.html for more information.
253+
Namespace: default
254+
Name: litmus-experiment
255+
Forward: 2345 -> 2345
256+
8080 -> 8080
257+
258+
Welcome to your development container. Happy coding!
259+
```
260+
261+
This dev container inherits the env, serviceaccount & other properties specified on the test deployment & is now suitable for
262+
running the experiment.
263+
264+
- Execute the experiment against the sample app chosen & verify the steps via logs printed on the console.
265+
266+
```
267+
python3 bin/experiment/experiment.py -name=<experiment-name>
268+
```
269+
270+
- In parallel, observe the experiment execution via the changes to the pod/node state
271+
272+
```
273+
watch -n 1 kubectl get pods,nodes
274+
```
275+
276+
- If there are necessary changes to the code based on the run, make them via your favourite IDE.
277+
These changes are automatically reflected on the dev container. Re-run the experiment to confirm changes.
278+
279+
- Once the experiment code is validated, stop/remove the development environment
280+
281+
```
282+
root@test:~/okteto/litmus-python# okteto down
283+
✓ Development container deactivated
284+
i Run 'okteto push' to deploy your code changes to the cluster
285+
```
286+
287+
- (Optional) Once the experiment has been validated using the above step, it can also be tested against the standard Litmus chaos
288+
flow. This involves:
289+
The experiment created using the above steps, can be tested in the following manner:
290+
291+
- Run the `experiment.yml` with the desired values in the ENV and appropriate `chaosServiceAccount`
292+
using a custom dev image instead of `litmuschaos/litmus-python` (say, oumkale/litmus-python) that packages the
293+
business logic.
294+
- Creating a custom image built with the code validated by the previous steps
295+
- Launching the Chaos-Operator
296+
- Modifying the ChaosExperiment manifest (experiment.yaml) with right defaults (env & other attributes, as applicable) & creating
297+
this CR on the cluster (pointing the `.spec.definition.image` to the custom one just built)
298+
- Modifying the ChaosEngine manifest (engine.yaml) with right app details, run properties & creating this CR to launch the chaos pods
299+
- Verifying the experiment status via ChaosResult
300+
301+
Refer [litmus docs](https://docs.litmuschaos.io/docs/getstarted/) for more details on performing each step in this procedure.
302+
303+
### Steps to Include the Chaos Charts/Experiments into the ChartHub
304+
305+
- Send a PR to the [litmus-python](https://github.com/litmuschaos/litmus-python) repo with the modified experiment files, rbac,
306+
test deployment & README.
307+
- Send a PR to the [chaos-charts](https://github.com/litmuschaos/chaos-charts) repo with the modified experiment CR,
308+
experiment chartserviceversion, rbac, (category-level) chaos chart chartserviceversion & package.yaml (if applicable).

0 commit comments

Comments
 (0)