# This test can be executed only in Chaos namespace
# this will launch the argo and chaos in chaos namespace
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: argowf-chaos-
spec:
  entrypoint: pdbcreate
  poddisruptionbudget:
    minavailable: 100%
  # must complete in 1m
  activeDeadlineSeconds: 86400
  # keep workflows for 3m
  ttlStrategy:
    secondsAfterCompletion: 3600
  # delete all pods as soon as they complete
  podGC:
    strategy: OnPodCompletion
  serviceAccountName: argowf-svcacc
  arguments:
    parameters:
      - name: appNamespace
        value: "kube-system"
      - name: appCurrentNamespace
        value: "default"
      - name: appLabel
        value: "k8s-app=wavefront-collector"
      - name: appEndpoint
        value: "localhost"
      - name: fileName
        value: "pod-custom-kill-health.json"
      - name: chaosServiceAccount
        value: chaos-admin
      - name: reportEndpoint
        value: none
  templates:
    - name: argowf-chaos
      steps:
        - - name: pdbcreate
            template: pdbcreate 
        - - name: run-chaos
            template: run-chaos
        - - name: revert-chaos
            template: revert-chaos

    - name: pdbcreate
      container:
        image: alpine:latest
        command: [sh, -c]
        args: [sleep 10]

    - name: run-chaos
      inputs:
        artifacts:
        - name: run-chaos
          path: /tmp/createChaosEngine.yaml
          raw:
            data: |
              # chaosengine.yaml
              apiVersion: litmuschaos.io/v1alpha1
              kind: ChaosEngine
              metadata:
                name: k8-pod-delete
                namespace: {{workflow.parameters.appCurrentNamespace}}
              spec:
                #ex. values: ns1:name=percona,ns2:run=nginx
                appinfo:
                  appns: {{workflow.parameters.appNamespace}}
                  # FYI, To see app label, apply kubectl get pods --show-labels
                  #applabel: "app=nginx"
                  applabel: "app={{workflow.parameters.appLabel}}"
                  appkind: deployment
                jobCleanUpPolicy: delete
                annotationCheck: 'false'
                engineState: 'active'
                chaosServiceAccount: {{workflow.parameters.chaosServiceAccount}}
                experiments:
                  - name: k8-pod-delete
                    spec:
                      components:
                        env:
                          - name: NAME_SPACE
                            value: {{workflow.parameters.appNamespace}}
                          - name: LABEL_NAME
                            value: {{workflow.parameters.appLabel}}
                          - name: APP_ENDPOINT
                            value: {{workflow.parameters.appEndpoint}}
                          - name: FILE
                            value: {{workflow.parameters.fileName}}
                          - name: REPORT
                            value: 'false'
                          - name: REPORT_ENDPOINT
                            value: '{{workflow.parameters.reportEndpoint}}'
                          - name: TEST_NAMESPACE
                            value: {{workflow.parameters.appCurrentNamespace}}
      container:
        image: litmuschaos/k8s:latest
        command: [sh, -c]
        args: ['kubectl apply -f /tmp/createChaosEngine.yaml -n {{workflow.parameters.appCurrentNamespace}} | echo "sleeping for 60s" | sleep 60 ']

    - name: revert-chaos
      inputs:
        artifacts:
        - name: revert-chaos
          path: /tmp/deleteChaosEngine.yaml
          raw:
            data: |
              # chaosengine.yaml
              apiVersion: litmuschaos.io/v1alpha1
              kind: ChaosEngine
              metadata:
                name: k8-pod-delete
                namespace: {{workflow.parameters.appCurrentNamespace}}
              spec:
                #ex. values: ns1:name=percona,ns2:run=nginx
                appinfo:
                  appns: {{workflow.parameters.appNamespace}}
                  # FYI, To see app label, apply kubectl get pods --show-labels
                  #applabel: "app=nginx"
                  applabel: "app={{workflow.parameters.appLabel}}"
                  appkind: deployment
                jobCleanUpPolicy: delete
                annotationCheck: 'false'
                engineState: 'active'
                chaosServiceAccount: {{workflow.parameters.chaosServiceAccount}}
                experiments:
                  - name: k8-pod-delete
                    spec:
                      components:
                        env:
                          - name: NAME_SPACE
                            value: {{workflow.parameters.appNamespace}}
                          - name: LABEL_NAME
                            value: {{workflow.parameters.appLabel}}
                          - name: APP_ENDPOINT
                            value: {{workflow.parameters.appEndpoint}}
                          - name: FILE
                            value: {{workflow.parameters.fileName}}
                          - name: REPORT
                            value: 'false'
                          - name: REPORT_ENDPOINT
                            value: '{{workflow.parameters.reportEndpoint}}'
                          - name: TEST_NAMESPACE
                            value: {{workflow.parameters.appCurrentNamespace}}
      container:
        image: litmuschaos/k8s:latest
        command: [sh, -c]
        args: [' sleep 20 | kubectl delete  -f /tmp/deleteChaosEngine.yaml -n {{workflow.parameters.appCurrentNamespace}} | echo "sleeping for 60s" | sleep 60 ']
