Project: Litmus (Observability and Analysis / Chaos Engineering)
File: spring-boot-exceptions.md

It can target random pods with a Spring Boot application and allows configuring the assaults to inject exceptions at runtime when the method is used. It tests the resiliency of the system when some applications are having unexpected faulty behavior. !!! tip "Scenario: Inject exceptions to Spring Boot Application" ??? info "View the uses of the experiment" coming soon ??? info "Verify the prerequisites" <ul> <li> Ensure that Kubernetes Version > 1.16 </li> <li>Ensure that the Litmus Chaos Operator is running by executing <code>kubectl get pods</code> in operator namespace (typically, <code>litmus</code>).If not, install from <a href="https://v1-docs.litmuschaos.io/docs/getstarted/#install-litmus">here</a> </li> <li> Ensure that the <code> spring-boot-exceptions </code> experiment resource is available in the cluster by executing <code>kubectl get chaosexperiments</code> in the desired namespace. If not, install from <a href="https://hub.litmuschaos.io/api/chaos/master?file=faults/spring-boot/spring-boot-exceptions/fault.yaml">here</a></li> <li>Chaos Monkey Spring Boot dependency should be present in application. It can be enabled by two ways: <ol> <li>Add internal dependency inside the spring boot application <ol> <li>Add Chaos Monkey for Spring Boot as dependency for your project ```maven <dependency> <groupId>de.codecentric</groupId> <artifactId>chaos-monkey-spring-boot</artifactId> <version>2.6.1</version> </dependency> ``` </li> <li> Start your Spring Boot App with the chaos-monkey spring profile enabled ```bash java -jar your-app.jar --spring.profiles.active=chaos-monkey --chaos.monkey.enabled=true ``` </li> </ol> </li> <li> Add as external dependency <ol> <li>You can extend your existing application with the chaos-monkey and add it as an external dependency at startup, for this it is necessary to use the PropertiesLauncher of Spring Boot ```maven <dependency> <groupId>de.codecentric</groupId> <artifactId>chaos-monkey-spring-boot</artifactId> <classifier>jar-with-dependencies</classifier> <version>2.6.1</version> </dependency> ``` </li> <li>Start your Spring Boot application, add Chaos Monkey for Spring Boot JAR and properties ```bash java -cp your-app.jar -Dloader.path=chaos-monkey-spring-boot-2.6.1-jar-with-dependencies.jar org.springframework.boot.loader.PropertiesLauncher --spring.profiles.active=chaos-monkey --spring.config.location=file:./chaos-monkey.properties ``` </li> </ol> </li> </ol> </li> </ul> ??? info "View the default validations" Spring boot pods are healthy before and after chaos injection !!! tip "NOTE" If you are using this experiment as part of a litmus workflow scheduled constructed & executed from chaos-center, then you may be making use of the RBAC, which is pre-installed in the cluster as part of the agent setup. ??? note "View the Minimal RBAC permissions" ```yaml apiVersion: v1 kind: ServiceAccount metadata: name: spring-boot-exceptions-sa namespace: default labels: name: spring-boot-exceptions-sa app.kubernetes.io/part-of: litmus apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: spring-boot-exceptions-sa namespace: default labels: name: spring-boot-exceptions-sa app.kubernetes.io/part-of: litmus rules: apiGroups: [""] resources: ["pods"] verbs: ["create","delete","get","list","patch","update", "deletecollection"] apiGroups: [""] resources: ["events"] verbs: ["create","get","list","patch","update"] apiGroups: [""] resources: ["pods/log"] verbs: ["get","list","watch"] apiGroups: [""] resources: ["pods/exec"] verbs: ["get","list","create"] apiGroups: ["batch"] resources: ["jobs"] verbs: ["create","list","get","delete","deletecollection"] apiGroups: ["litmuschaos.io"] resources: ["chaosengines","chaosexperiments","chaosresults"] verbs: ["create","list","get","patch","update","delete"] apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: spring-boot-exceptions-sa namespace: default labels: name: spring-boot-exceptions-sa app.kubernetes.io/part-of: litmus roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: spring-boot-exceptions-sa subjects: kind: ServiceAccount name: spring-boot-exceptions-sa namespace: default ``` Use this sample RBAC manifest to create a chaosServiceAccount in the desired (app)

This example consists of the minimum necessary role permissions to execute the experiment. ??? info "check the experiment tunables" <h2>Mandatory Fields</h2> <table> <tr> <th> Variables </th> <th> Description </th> <th> Notes </th> </tr> <tr> <td> CM_PORT </td> <td> It contains port of the spring boot application </td> <td> </td> </tr> </table> <h2>Optional Fields</h2> <table> <tr> <th> Variables </th> <th> Description </th> <th> Notes </th> </tr> <tr> <td> CMEXCEPTIONSTYPE </td> <td> It contains type of raised exception </td> <td> Defaults value: <code> java.lang.IllegalArgumentException </code> </td> </tr> <tr> <td> CMEXCEPTIONSARGUMENTS </td> <td> It contains argument of raised exception </td> <td> Defaults value: <code> java.lang.String:custom illegal argument exception </code> </td> </tr> <tr> <td> CM_LEVEL </td> <td> It contains number of requests are to be attacked, n value means nth request will be affected </td> <td> Defaults value: 1, it lies in [1,10000] range </td> </tr> <tr> <td> CMWATCHEDCUSTOM_SERVICES </td> <td> It limits watched packages/classes/methods, it contains comma seperated list of fully qualified packages(class and/or method names)</td> <td> ByDefault it is empty list, which means it target all services </td> </tr> <tr> <td> CM_WATCHERS </td> <td> It contains comma separated list of watchers from the following watchers list [controller, restController, service, repository, component, webClient] </td> <td> ByDefault it is <code> restController </code></td> </tr> <tr> <td> TOTALCHAOSDURATION </td> <td> The time duration for chaos injection (seconds) </td> <td> Defaults to 30 </td> </tr> <tr> <td> SEQUENCE </td> <td> It defines sequence of chaos execution for multiple target pods </td> <td> Default value: parallel. Supported: serial, parallel </td> </tr> <tr> <td> PODSAFFECTEDPERC </td> <td> The Percentage of total pods to target </td> <td> Defaults to 0% (corresponds to 1 replica) </td> </tr> <tr> <td> LIB </td> <td> The chaos lib used to inject the chaos </td> <td> Defaults to <code>litmus</code>. Supported <code>litmus</code> only </td> </tr> <tr> <td> RAMP_TIME </td> <td> Period to wait before and after injection of chaos in sec </td> <td> </td> </tr> </table> Refer the and to tune the common tunables for all experiments and spring-boot specific tunables. It tunes the spring-boot application port via `CM_PORT` ENV Use the following example to tune this: ```yaml apiVersion: litmuschaos.io/v1alpha1 kind: ChaosEngine metadata: name: spring-boot-chaos namespace: default spec: appinfo: appns: 'default' applabel: 'app=spring-boot' appkind: 'deployment' engineState: 'active' chaosServiceAccount: spring-boot-exceptions-sa experiments: name: spring-boot-exceptions spec: components: env: name: CM_PORT value: '8080' ``` Spring boot exception type and arguments can be tuned via `CMEXCEPTIONSTYPE` and `CMEXCEPTIONSARGUMENTS` ENV Use the following example to tune this: ```yaml apiVersion: litmuschaos.io/v1alpha1 kind: ChaosEngine metadata: name: spring-boot-chaos namespace: default spec: appinfo: appns: 'default' applabel: 'app=spring-boot' appkind: 'deployment' engineState: 'active' chaosServiceAccount: spring-boot-exceptions-sa experiments: name: spring-boot-exceptions spec: components: env: name: CMEXCEPTIONSTYPE value: 'java.lang.IllegalArgumentException' name: CMEXCEPTIONSARGUMENTS value: 'java.lang.String:custom illegal argument exception' name: CM_PORT value: '8080' ```