name: resource-consumer

{{$deploymentSize := DefaultParam .CL2_DEPLOYMENT_SIZE 10}}
{{$memory := DefaultParam .CL2_RESOURCE_CONSUME_MEMORY "100"}}
{{$memoryKi := DefaultParam .CL2_RESOURCE_CONSUME_MEMORY_KI "100"}}
{{$cpu := DefaultParam .CL2_RESOURCE_CONSUME_CPU 100}}
{{$repeats := DefaultParam .CL2_REPEATS 1}}

{{$steps := DefaultParam .CL2_STEPS 1}}
{{$nodePerStep := DefaultParam .CL2_NODE_PER_STEP 1}}
{{$totalNodes := MultiplyInt $nodePerStep $steps}}
{{$replicas := MultiplyInt $deploymentSize $totalNodes}}
{{$scaleReplicas := MultiplyInt $deploymentSize $nodePerStep}}
{{$scaleEnabled := DefaultParam .CL2_SCALE_ENABLED false}}

{{$operationTimeout := DefaultParam .CL2_OPERATION_TIMEOUT "5m"}}
{{$podStartupLatencyThreshold := DefaultParam .CL2_POD_STARTUP_LATENCY_THRESHOLD "15s"}}
{{$loadType := DefaultParam .CL2_LOAD_TYPE "memory"}}

{{$provider := DefaultParam .CL2_PROVIDER "aks"}}
{{$scrapeKubelets := DefaultParam .CL2_SCRAPE_KUBELETS false}}

namespace:
  number: 1
  prefix: resource-consumer
  deleteStaleNamespaces: true
  deleteAutomanagedNamespaces: true
  enableExistingNamespaces: false

tuningSets:
- name: Uniform1qps
  qpsLoad:
    qps: 1

steps:
  - name: Start measurements
    measurements:
      - Identifier: PodStartupLatency
        Method: PodStartupLatency
        Params:
          action: start
          labelSelector: group = resource-consumer
          threshold: {{$podStartupLatencyThreshold}}
      - Identifier: ResourceUsageSummary
        Method: ResourceUsageSummary
        Params:
          action: start
          labelSelector: group = resource-consumer
      - Identifier: WaitForRunningLatencyDeployments
        Method: WaitForControlledPodsRunning
        Params:
          action: start
          checkIfPodsAreUpdated: true
          apiVersion: apps/v1
          kind: Deployment
          labelSelector: group = resource-consumer
          operationTimeout: {{$operationTimeout}}

{{range $i := Loop $repeats}}

  {{if $scrapeKubelets}}
  - module:
      path: /kubelet-measurement.yaml
      params:
        action: start
  {{end}}

  {{range $j := Loop $steps}}
  - name: Create deployment {{$j}}
    phases:
    - namespaceRange:
          min: 1
          max: 1
      replicasPerNamespace: 1
      tuningSet: Uniform1qps
      objectBundle:
      - basename: resource-consumer-{{$j}}
        objectTemplatePath: deployment_template.yaml
        templateFillMap:
      {{if $scaleEnabled}}
        {{if eq $j 0}}
          Replicas: {{AddInt $scaleReplicas $deploymentSize}}
        {{else}}
          Replicas: {{$scaleReplicas}}
        {{end}}
      {{else}}
          Replicas: {{$replicas}}
      {{end}}
          Group: resource-consumer
          Memory: {{$memory}}K
          CPU: --millicores={{$cpu}}
          MemoryRequest: {{$memoryKi}}
          CPURequest: {{$cpu}}m
          LoadType: {{$loadType}}
          Provider: {{$provider}}

  - name: Waiting for latency pods to be running
    measurements:
      - Identifier: WaitForRunningLatencyDeployments
        Method: WaitForControlledPodsRunning
        Params:
          action: gather

  - name: Wait for resource consumption
    measurements:
      - Identifier: Sleep
        Method: Sleep
        Params:
          duration: 1m

  - name: Wait for nodes to be ready
    measurements:
      - Identifier: ConfirmNodeCount
        Method: WaitForNodes
        Params:
          action: start
        {{if $scaleEnabled}}
          minDesiredNodeCount: {{MultiplyInt (AddInt (MultiplyInt $nodePerStep (AddInt $j 1)) 1) 0.8}}
          maxDesiredNodeCount: {{AddInt $totalNodes 1}}
        {{else}}
          minDesiredNodeCount: {{MultiplyInt $totalNodes 0.8}}
          maxDesiredNodeCount: {{$totalNodes}}
        {{end}}
          labelSelector: cri-resource-consume = true
          timeout: 1m
          refreshInterval: 5s
  {{end}}

  {{if $scrapeKubelets}}
  - module:
      path: /kubelet-measurement.yaml
      params:
        action: gather
  {{end}}

  {{range $j := Loop $steps}}
  - name: Deleting deployments {{$j}}
    phases:
      - namespaceRange:
          min: 1
          max: 1
        replicasPerNamespace: 0
        tuningSet: Uniform1qps
        objectBundle:
          - basename: resource-consumer-{{$j}}
            objectTemplatePath: deployment_template.yaml

  - name: Waiting for latency pods to be deleted
    measurements:
      - Identifier: WaitForRunningLatencyDeployments
        Method: WaitForControlledPodsRunning
        Params:
          action: gather
  {{end}}
{{end}}

  - name: Collect measurements
    measurements:
      - Identifier: ResourceUsageSummary
        Method: ResourceUsageSummary
        Params:
          action: gather
      - Identifier: PodStartupLatency
        Method: PodStartupLatency
        Params:
          action: gather
