apiVersion: argoproj.io/v1alpha1
kind: CronWorkflow
metadata:
  name: argo-chaos-pod-cpu-cron-wf
  namespace: litmus
  labels:
    subject: "{{workflow.parameters.appNamespace}}_kube-proxy"
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"
        - name: appNamespace
          value: "kube-system"
    templates:
      - name: argowf-chaos
        steps:
          - - name: install-chaos-faults
              template: install-chaos-faults
          - - name: run-chaos
              template: run-chaos
          - - name: cleanup-chaos-resources
              template: cleanup-chaos-resources

      - name: install-chaos-faults
        inputs:
          artifacts:
            - name: install-chaos-faults
              path: /tmp/pod-cpu-hog.yaml
              raw:
                data: |
                  apiVersion: litmuschaos.io/v1alpha1
                  description:
                    message: |
                      Injects cpu consumption on pods belonging to an app deployment
                  kind: ChaosExperiment
                  metadata:
                    name: pod-cpu-hog
                  spec:
                    definition:
                      scope: Namespaced
                      permissions:
                        - apiGroups:
                            - ""
                            - "batch"
                            - "litmuschaos.io"
                          resources:
                            - "jobs"
                            - "pods"
                            - "pods/log"
                            - "events"
                            - "chaosengines"
                            - "chaosexperiments"
                            - "chaosresults"
                          verbs:
                            - "create"
                            - "list"
                            - "get"
                            - "patch"
                            - "update"
                            - "delete"
                      image: "litmuschaos.docker.scarf.sh/litmuschaos/go-runner:latest"
                      imagePullPolicy: Always
                      args:
                      - -c
                      - ./experiments -name pod-cpu-hog
                      command:
                      - /bin/bash
                      env:
                      - name: TOTAL_CHAOS_DURATION
                        value: '30'

                      - name: CHAOS_INTERVAL
                        value: '10'

                      ## Number of CPU cores to stress
                      - name: CPU_CORES
                        value: '1'

                      ## Percentage of total pods to target
                      - name: PODS_AFFECTED_PERC
                        value: ''

                      ## Period to wait before and after injection of chaos in sec
                      - name: RAMP_TIME
                        value: ''

                      - name: TARGET_POD
                        value: ''

                      labels:
                        name: pod-cpu-hog
        container:
          image: litmuschaos/k8s:latest
          command: [sh, -c]
          args:
            [
              "kubectl apply -f /tmp/pod-cpu-hog.yaml -n {{workflow.parameters.adminModeNamespace}}",
            ]

      - name: run-chaos
        inputs:
          artifacts:
            - name: run-chaos
              path: /tmp/chaosengine.yaml
              raw:
                data: |
                  apiVersion: litmuschaos.io/v1alpha1
                  kind: ChaosEngine
                  metadata:
                    name: kube-proxy-pod-cpu-hog-chaos
                    namespace: "{{workflow.parameters.adminModeNamespace}}"
                    labels:
                      context: "{{workflow.parameters.appNamespace}}_kube-proxy"
                    annotations: {}
                  spec:
                    appinfo:
                      appns: kube-system
                      applabel: "k8s-app=kube-proxy"
                      appkind: daemonset
                    jobCleanUpPolicy: retain
                    engineState: 'active'
                    chaosServiceAccount: litmus-admin
                    experiments:
                      - name: pod-cpu-hog
                        spec:
                          components:
                            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 -9 $(ps afx | grep \"[md5sum] /dev/zero\" | awk '{print$1}' | tr '\n' ' ')"
        container:
          image: litmuschaos/litmus-checker:latest
          args: ["-file=/tmp/chaosengine.yaml", "-saveName=/tmp/engine-name"]

      - name: cleanup-chaos-resources
        container:
          image: litmuschaos/k8s:latest
          command: [sh, -c]
          args:
            [
              "kubectl delete chaosengine kube-proxy-pod-cpu-hog-chaos -n {{workflow.parameters.adminModeNamespace}}",
            ]
