apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  name: argowf-chaos-node-cpu-hog
  namespace: litmus
  labels:
    subject: "{{workflow.parameters.context}}"
spec:
  entrypoint: argowf-chaos
  serviceAccountName: argo-chaos
  securityContext:
    runAsUser: 1000
    runAsNonRoot: true
  arguments:
    parameters:
      - name: adminModeNamespace
        value: "litmus"
      - name: context
        value: "node-cpu-hog_infra"
  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/node-cpu-hog.yaml
            raw:
              data: |
                apiVersion: litmuschaos.io/v1alpha1
                description:
                  message: |
                    Give a cpu spike on a node belonging to a deployment
                kind: ChaosExperiment
                metadata:
                  name: node-cpu-hog
                spec:
                  definition:
                    scope: Cluster
                    permissions:
                      - apiGroups:
                          - ""
                          - "batch"
                          - "apps"
                          - "litmuschaos.io"
                        resources:
                          - "jobs"
                          - "pods"
                          - "pods/log"
                          - "events"
                          - "chaosengines"
                          - "chaosexperiments"
                          - "chaosresults"
                        verbs:
                          - "create"
                          - "list"
                          - "get"
                          - "patch"
                          - "update"
                          - "delete"
                      - apiGroups:
                          - ""
                        resources:
                          - "nodes"
                        verbs:
                          - "get"
                          - "list"
                    image: "litmuschaos.docker.scarf.sh/litmuschaos/go-runner:latest"
                    imagePullPolicy: Always
                    args:
                    - -c
                    - ./experiments -name node-cpu-hog
                    command:
                    - /bin/bash
                    env:

                    - name: TOTAL_CHAOS_DURATION
                      value: '60'

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

                    ## ENTER THE NUMBER OF CORES OF CPU FOR CPU HOGGING
                    ## OPTIONAL VALUE IN CASE OF EMPTY VALUE IT WILL TAKE NODE CPU CAPACITY
                    - name: NODE_CPU_CORE
                      value: ''

                    # provide lib image
                    - name: LIB_IMAGE
                      value: 'litmuschaos.docker.scarf.sh/litmuschaos/go-runner:latest'

                    labels:
                      name: node-cpu-hog
      container:
        image: litmuschaos/k8s:latest
        command: [sh, -c]
        args:
          [
            "kubectl apply -f /tmp/node-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-node-cpu-hog-chaos
                  namespace: "{{workflow.parameters.adminModeNamespace}}"
                  labels:
                    context: "{{workflow.parameters.context}}"
                  annotations: {}
                spec:
                  jobCleanUpPolicy: retain
                  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.yaml", "-saveName=/tmp/engine-name"]

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