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:

  1. Add the chaos object in controller
  2. Register the CRD
  3. Implement the schema type
  4. Make the Docker image
  5. Run chaos

Add the chaos object in controller

In Chaos Mesh, all chaos types are managed by the controller manager. To add a new chaos type, you need to start from adding the corresponding reconciler type in the controller, as instructed in the following steps:

  1. Add the HelloWorldChaos object in the controller manager main.go.

    You will notice existing chaos types such as PodChaos, NetworkChaos and IOChaos. Add the new type below them:

    1. if err = (&controllers.HelloWorldChaosReconciler{
    2. Client: mgr.GetClient(),
    3. Log: ctrl.Log.WithName("controllers").WithName("HelloWorldChaos"),
    4. }).SetupWithManager(mgr); err != nil {
    5. setupLog.Error(err, "unable to create controller", "controller", "HelloWorldChaos")
    6. os.Exit(1)
    7. }
  2. Under controllers, create a helloworldchaos_controller.go file and edit it as below:

    ``` package controllers

  1. import (
  2. "github.com/go-logr/logr"
  3. chaosmeshv1alpha1 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
  4. ctrl "sigs.k8s.io/controller-runtime"
  5. "sigs.k8s.io/controller-runtime/pkg/client"
  6. )
  7. // HelloWorldChaosReconciler reconciles a HelloWorldChaos object
  8. type HelloWorldChaosReconciler struct {
  9. client.Client
  10. Log logr.Logger
  11. }
  12. // +kubebuilder:rbac:groups=chaos-mesh.org,resources=helloworldchaos,verbs=get;list;watch;create;update;patch;delete
  13. // +kubebuilder:rbac:groups=chaos-mesh.org,resources=helloworldchaos/status,verbs=get;update;patch
  14. func (r *HelloWorldChaosReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
  15. logger := r.Log.WithValues("reconciler", "helloworldchaos")
  16. // the main logic of `HelloWorldChaos`, it prints a log `Hello World!` and returns nothing.
  17. logger.Info("Hello World!")
  18. return ctrl.Result{}, nil
  19. }
  20. func (r *HelloWorldChaosReconciler) SetupWithManager(mgr ctrl.Manager) error {
  21. // exports `HelloWorldChaos` object, which represents the yaml schema content the user applies.
  22. return ctrl.NewControllerManagedBy(mgr).
  23. For(&chaosmeshv1alpha1.HelloWorldChaos{}).
  24. Complete(r)
  25. }
  26. ```

Note:

The comment // +kubebuilder:rbac:groups=chaos-mesh.org... is an authority control mechanism that decides which account can access this reconciler. To make it accessible by the dashboard and chaos-controller-manager, you need to modify controller-manager-rbac.yaml accordingly:

  1. - apiGroups: ['chaos-mesh.org']
  2. resources:
  3. - podchaos
  4. - networkchaos
  5. - iochaos
  6. - helloworldchaos # Add this line in all chaos-mesh.org group
  7. verbs: ['*']

Register the CRD

The HelloWorldChaos object is a custom resource object in Kubernetes. This means you need to register the corresponding CRD in the Kubernetes API. To do this, modify kustomization.yaml by adding the corresponding line as shown below:

  1. resources:
  2. - bases/chaos-mesh.org_podchaos.yaml
  3. - bases/chaos-mesh.org_networkchaos.yaml
  4. - bases/chaos-mesh.org_iochaos.yaml
  5. - bases/chaos-mesh.org_helloworldchaos.yaml # this is the new line

Implement the schema type

To implement the schema type for the new chaos object, add helloworldchaos_types.go in the api directory and modify it as below:

  1. package v1alpha1
  2. import (
  3. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  4. )
  5. // +kubebuilder:object:root=true
  6. // HelloWorldChaos is the Schema for the helloworldchaos API
  7. type HelloWorldChaos struct {
  8. metav1.TypeMeta `json:",inline"`
  9. metav1.ObjectMeta `json:"metadata,omitempty"`
  10. }
  11. // +kubebuilder:object:root=true
  12. // HelloWorldChaosList contains a list of HelloWorldChaos
  13. type HelloWorldChaosList struct {
  14. metav1.TypeMeta `json:",inline"`
  15. metav1.ListMeta `json:"metadata,omitempty"`
  16. Items []HelloWorldChaos `json:"items"`
  17. }
  18. func init() {
  19. SchemeBuilder.Register(&HelloWorldChaos{}, &HelloWorldChaosList{})
  20. }

With this file added, the HelloWorldChaos schema type is defined and can be called by the following YAML lines:

  1. apiVersion: chaos-mesh.org/v1alpha1
  2. kind: HelloWorldChaos
  3. metadata:
  4. name: <name-of-this-resource>
  5. namespace: <ns-of-this-resource>

Make the Docker image

Having the object successfully added, you can make a Docker image and push it to your registry:

  1. make
  2. make docker-push

Note:

The default DOCKER_REGISTRY is localhost:5000, which is preset in hack/kind-cluster-build.sh. You can overwrite it to any registry to which you have access permission.

Run chaos

You 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 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 localhost:5000, as shown below:

  1. clusterScoped: true
  2. # Also see clusterScoped and controllerManager.serviceAccount
  3. rbac:
  4. create: true
  5. controllerManager:
  6. serviceAccount: chaos-controller-manager
  7. ...
  8. image: localhost:5000/pingcap/chaos-mesh:latest
  9. ...
  10. chaosDaemon:
  11. image: localhost:5000/pingcap/chaos-daemon:latest
  12. ...
  13. dashboard:
  14. image: localhost:5000/pingcap/chaos-dashboard:latest
  15. ...

Now take the following steps to run chaos:

  1. Get the related custom resource type for Chaos Mesh:

    1. kubectl apply -f manifests/
    2. kubectl get crd podchaos.chaos-mesh.org
  2. Install Chaos Mesh:

    1. helm install helm/chaos-mesh --name=chaos-mesh --namespace=chaos-testing --set chaosDaemon.runtime=containerd --set chaosDaemon.socketPath=/run/containerd/containerd.sock
    2. kubectl get pods --namespace chaos-testing -l app.kubernetes.io/instance=chaos-mesh

    The arguments --set chaosDaemon.runtime=containerd --set chaosDaemon.socketPath=/run/containerd/containerd.sock is used to to support network chaos on kind.

  3. Create chaos.yaml in any location with the lines below:

    1. apiVersion: chaos-mesh.org/v1alpha1
    2. kind: HelloWorldChaos
    3. metadata:
    4. name: hello-world
    5. namespace: chaos-testing
  4. Apply the chaos:

    1. kubectl apply -f /path/to/chaos.yaml
    2. kubectl get HelloWorldChaos -n chaos-testing

    Now you should be able to check the Hello World! result in the log:

    1. kubectl logs chaos-controller-manager-{pod-post-fix} -n chaos-testing

    Note:

    {pod-post-fix} is a random string generated by Kubernetes, you can check it by executing kubectl get po -n chaos-testing.

Next steps

Congratulations! 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 (WIP).