Extend Chaos Daemon Interface

    note

    It’s recommended to read Chaos Mesh architecture before you go forward.

    This document covers:

    In api/v1alpha1/helloworldchaos_type.go, you have defined HelloWorldSpec, which includes ContainerSelector:

    In Chaos Mesh, Selector is used to define the scope of a chaos experiment, the target namespace, annotation, label, etc. Selector can also be some more specific values (for example, AWSSelector in AWSChaos). Usually, each chaos experiment requires only one Selector, with exceptions such as NetworkChaos because it sometimes needs two Selectors as two objects for network partition.

    To allow Chaos Daemon to accept the requests from Chaos Controller Manager, you need to implement a new gRPC interface.

    1. Add the RPC in pkg/chaosdaemon/pb/chaosdaemon.proto:

      1. service chaosDaemon {
      2. ...
      3. rpc ExecHelloWorldChaos(ExecHelloWorldRequest) returns (google.protobuf.Empty) {}
      4. }
      5. message ExecHelloWorldRequest {
      6. string container_id = 1;
      7. }

      You need to update the Golang code generated by this proto file:

      1. make proto
    2. Implement gRPC services in Chaos Daemon.

      1. package chaosdaemon
      2. import (
      3. "context"
      4. "fmt"
      5. "github.com/golang/protobuf/ptypes/empty"
      6. "github.com/chaos-mesh/chaos-mesh/pkg/bpm"
      7. "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
      8. func (s *DaemonServer) ExecHelloWorldChaos(ctx context.Context, req *pb.ExecHelloWorldRequest) (*empty.Empty, error) {
      9. log := s.getLoggerFromContext(ctx)
      10. log.Info("ExecHelloWorldChaos", "request", req)
      11. pid, err := s.crClient.GetPidFromContainerID(ctx, req.ContainerId)
      12. if err != nil {
      13. return nil, err
      14. }
      15. cmd := bpm.DefaultProcessBuilder("sh", "-c", fmt.Sprintf("ps aux")).
      16. SetNS(pid, bpm.MountNS).
      17. SetContext(ctx).
      18. Build(ctx)
      19. out, err := cmd.Output()
      20. if err != nil {
      21. return nil, err
      22. }
      23. if len(out) != 0 {
      24. log.Info("cmd output", "output", string(out))
      25. }
      26. return &empty.Empty{}, nil
      27. }

      After chaos-daemon receives the ExecHelloWorldChaos request, you can see a list of processes in the current container.

    To verify the experiment, perform the following steps.

    1. Build the Docker image and push it to your local Registry. If the Kubernetes cluster is deployed using kind, you need to load the image to kind:

      1. make image
      2. make docker-push
      3. kind load docker-image localhost:5000/pingcap/chaos-mesh:latest
      4. kind load docker-image localhost:5000/pingcap/chaos-daemon:latest
      5. kind load docker-image localhost:5000/pingcap/chaos-dashboard:latest
    2. Update Chaos Mesh:

      1. kubectl apply -f https://raw.githubusercontent.com/chaos-mesh/apps/master/ping/busybox-statefulset.yaml
    3. Create a new YAML file with the following content:

      1. apiVersion: chaos-mesh.org/v1alpha1
      2. kind: HelloWorldChaos
      3. metadata:
      4. name: busybox-helloworld-chaos
      5. spec:
      6. selector:
      7. namespaces:
      8. - busybox
      9. mode: all
      10. duration: 1h
    4. Apply the chaos experiment:

    5. Verify the results. You can check several logs:

      • Check the logs of Chaos Controller Manager:
      1. kubectl logs chaos-controller-manager-{pod-post-fix} -n chaos-mesh

      Example output:

      1. 2021-06-25T06:02:12.754Z INFO records apply chaos {"id": "busybox/busybox-1/busybox"}
      2. 2021-06-25T06:02:12.754Z INFO helloworldchaos Apply helloworld chaos
      • Check the logs of Chaos Daemon:

        Example output:

        You can see ps aux in two separate lines, which are corresponding to two different Pods.

        Extend Chaos Daemon Interface - 图3note

        If your cluster has multiple nodes, you will find more than one Chaos Daemon Pod. Try to check logs of every Chaos Daemon Pods and find which Pod is being called.

      If you encounter any problems in this process, create an in the Chaos Mesh repository.

      Now you are ready to become a Chaos Mesh developer! You are welcome to visit the Chaos Mesh repository to find a and get started!