# config allows for tweaking how the policy should be run
config:
  runStrategy:
    strategy: round-robin
    runs: 1000
    minSecondsBetweenRuns: 77
    maxSecondsBetweenRuns: 78

  exitStrategy:
    strategy: fail-fast

# scenarios is an array of chaos experiment descriptions
scenarios:

# example of a policy using al the filters available
- name: Example scenario
  description: >
    A longer description of the scenario.

    This one, for instances, tries to illustrate everything that you can do
    with a PowerfulSeal policy, and make sure it validates against the schema.
  steps:

  # wait lets you wait X amount of time
  - wait:
      seconds: 1.5

  # kubectl allows you to apply or delete an arbitrary kubernetes config
  # you can use this to setup the
  - kubectl:
      action: apply
      autoDelete: true
      payload: |
        ---
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: sealpolicy
        data:
          policy.yml: |-
            scenarios:
            - name: "delete a random pod in default namespace"
              steps:
              - podAction:
                  matches:
                  - namespace: "default"
                  filters:
                  - randomSample:
                      size: 1
                  actions:
                  - kill:
                      probability: 0.77
                      force: true
        ---
        apiVersion: apps/v1
        kind: Deployment
        metadata:
          name: powerfulseal
        spec:
          replicas: 1
          template:
            metadata:
              labels:
                name: powerfulseal
            spec:
              serviceAccountName: powerfulseal
              containers:
                - name: powerfulseal
                  image: bloomberg/powerfulseal:3.0.1
                  args:
                  - autonomous
                  - --policy-file=/policy.yml
                  volumeMounts:
                    - name: policyfile
                      mountPath: /policy.yml
                      subPath: policy.yml
              volumes:
                - name: policyfile
                  configMap:
                    name: sealpolicy
  - kubectl:
      action: delete
      payload: |
        ---

  - kubectl:
      action: apply
      proxy: someproxy.com:8080
      kubectlBinary: /some/path/binary
      payload: |
        ---

  # nodeAction performs actions on specific nodes in the cluster
  - nodeAction:
      # Choose the initial set of nodes to operate on.
      # Note that this will be an union of all the notes you match (logical OR)
      matches:
        - property:
            name: "name"
            value: "minion-*"
        - property:
            name: "ip"
            value: "127.0.0.1"
        - property:
            name: "group"
            value: "minion"
        - property:
            name: "az"
            value: "AZ1|AZ2"
        - property:
            name: "state"
            value: "UP"

      # The filters are executed in the order specified and can be
      # used mutiple times, and piped from one to the next.
      filters:
        # property filters (all the property filters support regexp)
        - property:
            name: "name"
            value: "minion-*"
        - property:
            name: "ip"
            value: "127.0.0.1"
        - property:
            name: "group"
            value: "minion"
        - property:
            name: "az"
            value: "AZ1|AZ2"
        - property:
            name: "state"
            value: "UP"
        # time of execution filters
        # to restrict the actions to work days, you can do
        - dayTime:
            onlyDays:
            - "monday"
            - "tuesday"
            - "wednesday"
            - "thursday"
            - "friday"
            startTime:
              hour: 10
              minute: 0
              second: 0
            endTime:
              hour: 17
              minute: 30
              second: 0
        # to pick a random sample of nodes/pods, you can specify either a size
        - randomSample:
            size: 5
        # or a ratio (will be rounded down to an integer)
        - randomSample:
            ratio: 0.2
        # this will pass all the nodes with the given probability,
        # or none otherwise
        - probability: 0.5

      # The actions will be executed in the order specified
      actions:
        - stop:
            force: false
        - stop:
            autoRestart: false
        - wait:
            seconds: 30
        - start:
        - execute:
            cmd: "sudo service docker restart"

  # podAction performs actions on specific pods in the cluster
  - podAction:
      # Match the intial set of pods. The set of pods will be a union of all matches.
      matches:
        # you can pick a whole namespace
        - namespace: "doomsday"
        # you can pick a particular deployment
        - deployment:
            name: "doomsday"
            namespace: "example"
        # throw in another one to the union
        - deployment:
            name: "dangerous"
            namespace: "moyk"
        # you can also select labels in a namespace
        # (note that for the labels are always strings)
        - labels:
            namespace: "something"
            selector: "app=true,something=1"

      filters:
        # property filters (all the property filters support regexp)
        - property:
            name: "name"
            value: "application-X-*"
        - property:
            name: "state"
            value: "Running"
        # time of execution filters
        # to restrict the actions to work days, you can do
        - dayTime:
            onlyDays:
              - "monday"
              - "tuesday"
              - "wednesday"
              - "thursday"
              - "friday"
            startTime:
              hour: 10
              minute: 0
              second: 0
            endTime:
              hour: 17
              minute: 30
              second: 0
        # to pick a random sample of nodes/pods, you can specify either a size
        - randomSample:
            size: 5
        # or a ratio (will be rounded down to an integer)
        - randomSample:
            ratio: 0.2
        # this will pass all the nodes with the given probability,
        # or none otherwise
        - probability: 0.5

      # The actions will be executed in the order specified
      actions:
        - kill:
            probability: 0.5
            force: true
        - wait:
            seconds: 5
        - kill:
            probability: 1
            force: true

        # stops the hosts the pods are running on
        - stopHost:
            autoRestart: true


  # check that there are two pods, and both running in luckywinner namespace
  - podAction:
      matches:
        - namespace: "luckywinner"
      actions:
        - checkPodCount:
            count: 2
        - checkPodState:
            state: Running

  # probeHTTP allows for verifying that the HTTP server responds
  - probeHTTP:
      target:
        url: "http://somee.url.example.com"
      retries: 10
      delay: 2000
      timeout: 100
      code: 404
      endpoint: /metrics
      # make a hundred requests to guarantee we hit every pod in the service
      count: 100
      method: post
      body: |
        {
          "payload": "nope"
        }
      headers:
      - name: TEST_HEADER
        value: some value
      - name: Host
        value: some other value
      insecure: true

  - probeHTTP:
      target:
        service:
          name: some-service
          namespace: default
          port: 9090
          protocol: http
