apiVersion: argoproj.io/v1alpha1
kind: CronWorkflow
metadata:
  name: argo-chaos-kube-proxy-cron-wf
  namespace: litmus
spec:
  schedule: "0 * * * *"
  concurrencyPolicy: "Forbid"
  startingDeadlineSeconds: 0
  workflowSpec:
    entrypoint: argowf-chaos
    serviceAccountName: argo-chaos
    securityContext:
      runAsUser: 1000
      runAsNonRoot: true
    arguments:
      parameters:
        - name: adminModeNamespace
          value: "litmus"
    templates:
      - name: argowf-chaos
        steps:
          - - name: install-chaos-experiments
              template: install-chaos-experiments
          - - name: node-cpu-hog
              template: node-cpu-hog
            - name: pod-memory-hog
              template: pod-memory-hog
          - - name: pod-cpu-hog
              template: pod-cpu-hog
            - name: node-memory-hog
              template: node-memory-hog
          - - name: pod-delete
              template: pod-delete
          - - name: revert-kube-proxy-chaos
              template: revert-kube-proxy-chaos

      - name: install-chaos-experiments
        container:
          image: lachlanevenson/k8s-kubectl
          command: [sh, -c]
          args:
            - "kubectl apply -f https://hub.litmuschaos.io/api/chaos/1.9.0?file=charts/generic/experiments.yaml -n
              {{workflow.parameters.adminModeNamespace}} | sleep 30"

      - name: node-cpu-hog
        inputs:
          artifacts:
            - name: node-cpu-hog
              path: /tmp/chaosengine-node-cpu-hog.yaml
              raw:
                data: |
                  apiVersion: litmuschaos.io/v1alpha1
                  kind: ChaosEngine
                  metadata:
                    name: kube-proxy-node-cpu-hog
                    namespace: {{workflow.parameters.adminModeNamespace}}
                  spec:
                    appinfo:
                      appns: kube-system
                      applabel: "k8s-app=kube-proxy"
                      appkind: daemonset
                    jobCleanUpPolicy: retain
                    monitoring: false
                    annotationCheck: 'false'
                    engineState: 'active'
                    chaosServiceAccount: litmus-admin
                    experiments:
                      - name: node-cpu-hog
                        spec:
                          components:
                            env:
                              - name: NODE_CPU_CORE
                                value: '1'

                              - name: TOTAL_CHAOS_DURATION
                                value: '60' # in seconds
        container:
          image: litmuschaos/litmus-checker:latest
          args: ["-file=/tmp/chaosengine-node-cpu-hog.yaml","-saveName=/tmp/engine-name"]

      - name: pod-memory-hog
        inputs:
          artifacts:
            - name: pod-memory-hog
              path: /tmp/chaosengine-pod-memory-hog.yaml
              raw:
                data: |
                  apiVersion: litmuschaos.io/v1alpha1
                  kind: ChaosEngine
                  metadata:
                    name: kube-proxy-pod-memory-hog-chaos
                    namespace: {{workflow.parameters.adminModeNamespace}}
                  spec:
                    appinfo:
                      appns: kube-system
                      applabel: "k8s-app=kube-proxy"
                      appkind: daemonset
                    jobCleanUpPolicy: retain
                    monitoring: false
                    annotationCheck: 'false'
                    engineState: 'active'
                    chaosServiceAccount: litmus-admin
                    experiments:
                      - name: pod-memory-hog
                        spec:
                          components:
                            experimentImage: "litmuschaos/go-runner:ci"
                            env:
                              - name: TARGET_CONTAINER
                                value: 'kube-proxy'
                              - name: MEMORY_CONSUMPTION
                                value: '500'
                              - name: TOTAL_CHAOS_DURATION
                                value: '60' # in seconds
                              - name: CHAOS_KILL_COMMAND
                                value: "kill $(find /proc -name exe -lname '*/dd' 2>&1 | grep -v 'Permission denied' | awk -F/ '{print $(NF-1)}' |  head -n 1)"
        container:
          image: litmuschaos/litmus-checker:latest
          args: ["-file=/tmp/chaosengine-pod-memory-hog.yaml","-saveName=/tmp/engine-name"]

      - name: pod-cpu-hog
        inputs:
          artifacts:
            - name: pod-cpu-hog
              path: /tmp/chaosengine-pod-cpu-hog.yaml
              raw:
                data: |
                  apiVersion: litmuschaos.io/v1alpha1
                  kind: ChaosEngine
                  metadata:
                    name: kube-proxy-pod-cpu-hog-chaos
                    namespace: {{workflow.parameters.adminModeNamespace}}
                  spec:
                    appinfo:
                      appns: kube-system
                      applabel: "k8s-app=kube-proxy"
                      appkind: daemonset
                    jobCleanUpPolicy: retain
                    monitoring: false
                    annotationCheck: 'false'
                    engineState: 'active'
                    chaosServiceAccount: litmus-admin
                    experiments:
                      - name: pod-cpu-hog
                        spec:
                          components:
                            experimentImage: "litmuschaos/go-runner:ci"
                            env:
                              - name: TARGET_CONTAINER
                                value: 'kube-proxy'
                              - name: CPU_CORES
                                value: '1'
                              - name: TOTAL_CHAOS_DURATION
                                value: '60' # in seconds
                              - name: CHAOS_KILL_COMMAND
                                value: "kill $(find /proc -name exe -lname '*/md5sum' 2>&1 | grep -v 'Permission denied' | awk -F/ '{print $(NF-1)}' |  head -n 1)"
        container:
          image: litmuschaos/litmus-checker:latest
          args: ["-file=/tmp/chaosengine-pod-cpu-hog.yaml","-saveName=/tmp/engine-name"]

      - name: node-memory-hog
        inputs:
          artifacts:
            - name: node-memory-hog
              path: /tmp/chaosengine-node-memory-hog.yaml
              raw:
                data: |
                  apiVersion: litmuschaos.io/v1alpha1
                  kind: ChaosEngine
                  metadata:
                    name: kube-proxy-node-memory-hog-chaos
                    namespace: {{workflow.parameters.adminModeNamespace}}
                  spec:
                    appinfo:
                      appns: kube-system
                      applabel: "k8s-app=kube-proxy"
                      appkind: daemonset
                    jobCleanUpPolicy: retain
                    monitoring: false
                    annotationCheck: 'false'
                    engineState: 'active'
                    chaosServiceAccount: litmus-admin
                    experiments:
                      - name: node-memory-hog
                        spec:
                          components:
                            env:
                              - name: MEMORY_PERCENTAGE
                                value: '50'
                              - name: TOTAL_CHAOS_DURATION
                                value: '60' # in seconds
        container:
          image: litmuschaos/litmus-checker:latest
          args: ["-file=/tmp/chaosengine-node-memory-hog.yaml","-saveName=/tmp/engine-name"]

      - name: pod-delete
        inputs:
          artifacts:
            - name: pod-delete
              path: /tmp/chaosengine-pod-delete.yaml
              raw:
                data: |
                  apiVersion: litmuschaos.io/v1alpha1
                  kind: ChaosEngine
                  metadata:
                    name: kube-proxy-pod-delete-chaos
                    namespace: {{workflow.parameters.adminModeNamespace}}
                  spec:
                    appinfo:
                      appns: kube-system
                      applabel: "k8s-app=kube-proxy"
                      appkind: daemonset
                    jobCleanUpPolicy: retain
                    monitoring: false
                    annotationCheck: 'false'
                    engineState: 'active'
                    chaosServiceAccount: litmus-admin
                    experiments:
                      - name: pod-delete
                        spec:
                          components:
                            env:
                              - name: TOTAL_CHAOS_DURATION
                                value: "60"
                              - name: CHAOS_INTERVAL
                                value: "10"
                              - name: FORCE
                                value: "false"
        container:
          image: litmuschaos/litmus-checker:latest
          args: ["-file=/tmp/chaosengine-pod-delete.yaml","-saveName=/tmp/engine-name"]

      - name: revert-kube-proxy-chaos
        container:
          image: lachlanevenson/k8s-kubectl
          command: [sh, -c]
          args:
            - "kubectl delete chaosengines --all -n
              {{workflow.parameters.adminModeNamespace}}"
