Develop a New Chaos
After preparing the development environment, let's develop a new type of chaos, HelloWorldChaos, which only prints a "Hello World!" message to the log. Generally, to add a new chaos type for Chaos Mesh, you need to take the following steps:
- Define the schema type
- Register the CRD
- Register the handler for this chaos object
- Make the Docker image
- Run chaos
#
Define the schema typeTo define the schema type for the new chaos object, add helloworldchaos_types.go
in the api directory api/v1alpha1
and fill it with the following content:
With this file added, the HelloWorldChaos schema type is defined. The structure of it can be described as the YAML file below:
make generate
will generate boilerplate functions for it, which is needed to integrate the resource in the Chaos Mesh.
#
Register the CRDThe HelloWorldChaos object is a custom resource object in Kubernetes. This means you need to register the corresponding CRD in the Kubernetes API. Run make yaml
, then the CRD will be generated in config/crd/bases/chaos-mesh.org_helloworldchaos.yaml
. In order to combine all these YAML file into manifests/crd.yaml
, modify kustomization.yaml by adding the corresponding line as shown below:
Run make yaml
again, and the definition of HelloWorldChaos will show in manifests/crd.yaml
. You can check it through git diff
#
Register the handler for this chaos objectCreate file controllers/helloworldchaos/types.go
and fill it with following codes:
We should also import github.com/chaos-mesh/chaos-mesh/controllers/helloworldchaos
in the cmd/controller-manager/main.go
, then it will register on the route table when the controller starts up.
#
Make the Docker imageHaving the object successfully added, you can make a Docker image:
Then push it to your registry:
If your Kubernetes cluster is deployed by Kind, you need to load images to Kind:
#
Run chaosYou are almost there. In this step, you will pull the image and apply it for testing.
Before you pull any image for Chaos Mesh (using helm install
or helm upgrade
), modify values.yaml of the helm template to replace the default image with what you just pushed to your local registry.
In this case, the template uses pingcap/chaos-mesh:latest
as the default target registry, so you need to replace it with the environment variable DOCKER_REGISTRY
's value(default localhost:5000
), as shown below:
Now take the following steps to run chaos:
Create namespace
chaos-testing
Get the related custom resource type for Chaos Mesh:
You can see CRD
helloworldchaos
is created:Now you can get the CRD using the command below:
Install Chaos Mesh:
For helm 3.X
For helm 2.X
To verify your installation, get pods from the
chaos-testing
namespace:Note:
Arguments
--set chaosDaemon.runtime=containerd --set chaosDaemon.socketPath=/run/containerd/containerd.sock
are used to to support network chaos on kind.Create
chaos.yaml
in any location with the lines below:Apply the chaos:
Now you should be able to check the
Hello World!
result in the log of ofchaos-controller-manager
:The log is as follows:
Note:
{pod-post-fix}
is a random string generated by Kubernetes. You can check it by executingkubectl get pod -n chaos-testing
.
#
Next stepsCongratulations! You have just added a chaos type for Chaos Mesh successfully. Let us know if you run into any issues during the process. If you feel like doing other types of contributions, refer to Add facilities to chaos daemon.