## Input params
# Valid actions: "create", "scale and update", "delete"
{{$actionName := printf "%s objects" .actionName}}
{{$namespaces := .namespaces}}
{{$tuningSet := .tuningSet}}

# Derivative variables
{{$is_deleting := (eq .actionName "delete")}}
{{$randomScaleFactor := DefaultParam .randomScaleFactor 0}}
{{$minReplicaFactor := SubtractFloat 1 $randomScaleFactor}}
{{$maxReplicaFactor := AddFloat 1 $randomScaleFactor}}
{{$testMaxReplicaFactor := AddFloat 1 .testMaxReplicaFactor}}
{{$operationTimeout := .operationTimeout}}

# DaemonSets
{{$daemonSetImage := DefaultParam .daemonSetImage "registry.k8s.io/pause:3.9"}}
{{$daemonSetReplicas := .daemonSetReplicas}}
{{$daemonSetEnv := .daemonSetEnv}}

# Deployments
{{$deploymentImage := DefaultParam .deploymentImage "registry.k8s.io/pause:3.9"}}
{{$bigDeploymentSize := .bigDeploymentSize}}
{{$bigDeploymentsPerNamespace := .bigDeploymentsPerNamespace}}
{{$mediumDeploymentSize := .mediumDeploymentSize}}
{{$mediumDeploymentsPerNamespace := .mediumDeploymentsPerNamespace}}
{{$smallDeploymentSize := .smallDeploymentSize}}
{{$smallDeploymentsPerNamespace := .smallDeploymentsPerNamespace}}

# StatefulSets
{{$statefulSetImage := DefaultParam .statefulSetImage "registry.k8s.io/pause:3.9"}}
{{$smallStatefulSetSize := .smallStatefulSetSize}}
{{$smallStatefulSetsPerNamespace := .smallStatefulSetsPerNamespace}}
{{$mediumStatefulSetSize := .mediumStatefulSetSize}}
{{$mediumStatefulSetsPerNamespace := .mediumStatefulSetsPerNamespace}}

# Jobs
{{$jobImage := DefaultParam .jobImage "registry.k8s.io/pause:3.10"}}
{{$bigJobSize := .bigJobSize}}
{{$bigJobsPerNamespace := .bigJobsPerNamespace}}
{{$mediumJobSize := .mediumJobSize}}
{{$mediumJobsPerNamespace := .mediumJobsPerNamespace}}
{{$smallJobSize := .smallJobSize}}
{{$smallJobsPerNamespace := .smallJobsPerNamespace}}
{{$completionsFactor := MultiplyFloat 2 $testMaxReplicaFactor}}

# PV
{{$pvSmallStatefulSetSize := DefaultParam .pvSmallStatefulSetSize 0}}
{{$pvMediumStatefulSetSize := DefaultParam .pvMediumStatefulSetSize 0}}

## CL2 params
{{$CHECK_IF_PODS_ARE_UPDATED := DefaultParam .CL2_CHECK_IF_PODS_ARE_UPDATED true}}
{{$DISABLE_DAEMONSETS := DefaultParam .CL2_DISABLE_DAEMONSETS false}}
{{$ENABLE_DNSTESTS := DefaultParam .CL2_ENABLE_DNSTESTS false}}
{{$ENABLE_NETWORKPOLICIES := DefaultParam .CL2_ENABLE_NETWORKPOLICIES false}}
{{$ENABLE_PVS := DefaultParam .CL2_ENABLE_PVS true}}
{{$ENABLE_NETWORK_POLICY_ENFORCEMENT_LATENCY_TEST := DefaultParam .CL2_ENABLE_NETWORK_POLICY_ENFORCEMENT_LATENCY_TEST false}}
{{$NET_POLICY_ENFORCEMENT_LATENCY_TARGET_LABEL_KEY := DefaultParam .CL2_NET_POLICY_ENFORCEMENT_LATENCY_TARGET_LABEL_KEY "net-pol-test"}}
{{$NET_POLICY_ENFORCEMENT_LATENCY_TARGET_LABEL_VALUE := DefaultParam .CL2_NET_POLICY_ENFORCEMENT_LATENCY_TARGET_LABEL_VALUE "enforcement-latency"}}
{{$NET_POLICY_SERVER_EVERY_NTH_POD := DefaultParam .CL2_NET_POLICY_SERVER_EVERY_NTH_POD 3}}

steps:
- name: Starting measurement for '{{$actionName}}'
  measurements:
  - Method: WaitForControlledPodsRunning
    Instances:
    - Identifier: WaitForRunningDeployments
      Params:
        apiVersion: apps/v1
        kind: Deployment
    - Identifier: WaitForRunningStatefulSets
      Params:
        apiVersion: apps/v1
        kind: StatefulSet
    - Identifier: WaitForRunningDaemonSets
      Params:
        apiVersion: apps/v1
        kind: DaemonSet
    - Identifier: WaitForRunningJobs
      Params:
        apiVersion: batch/v1
        kind: Job
    Params:
      action: start
      checkIfPodsAreUpdated: {{$CHECK_IF_PODS_ARE_UPDATED}}
      labelSelector: group = load
      operationTimeout: {{$operationTimeout}}

- name: {{$actionName}}
  phases:
{{if not $DISABLE_DAEMONSETS}}
  - namespaceRange:
      min: 1
      max: 1
    replicasPerNamespace: {{$daemonSetReplicas}}
    tuningSet: {{$tuningSet}}
    objectBundle:
    - basename: daemonset
      objectTemplatePath: daemonset.yaml
      templateFillMap:
        Image: {{$daemonSetImage}}
        Env: {{$daemonSetEnv}}
{{end}}
  - namespaceRange:
      min: 1
      max: {{$namespaces}}
    replicasPerNamespace: {{$bigDeploymentsPerNamespace}}
    tuningSet: {{$tuningSet}}
    objectBundle:
{{if $ENABLE_NETWORKPOLICIES}}
    - basename: big-deployment
      objectTemplatePath: networkpolicy.yaml
{{end}}
    - basename: big-deployment
      objectTemplatePath: deployment.yaml
      templateFillMap:
        # DNS Test clients are enabled only in the medium-size deployment.
        EnableDNSTests: false
        ReplicasMin: {{MultiplyInt $bigDeploymentSize $minReplicaFactor}}
        ReplicasMax: {{MultiplyInt $bigDeploymentSize $maxReplicaFactor}}
        SvcName: big-service
        Image: {{$deploymentImage}}
  - namespaceRange:
      min: 1
      max: {{$namespaces}}
    replicasPerNamespace: {{$mediumDeploymentsPerNamespace}}
    tuningSet: {{$tuningSet}}
    objectBundle:
{{if $ENABLE_NETWORKPOLICIES}}
    - basename: medium-deployment
      objectTemplatePath: networkpolicy.yaml
{{end}}
    - basename: medium-deployment
      objectTemplatePath: deployment.yaml
      templateFillMap:
        EnableDNSTests: {{$ENABLE_DNSTESTS}}
        ReplicasMin: {{MultiplyInt $mediumDeploymentSize $minReplicaFactor}}
        ReplicasMax: {{MultiplyInt $mediumDeploymentSize $maxReplicaFactor}}
        SvcName: medium-service
        Image: {{$deploymentImage}}
  - namespaceRange:
      min: 1
      max: {{$namespaces}}
    replicasPerNamespace: {{$smallDeploymentsPerNamespace}}
    tuningSet: {{$tuningSet}}
    objectBundle:
{{if $ENABLE_NETWORKPOLICIES}}
    - basename: small-deployment
      objectTemplatePath: networkpolicy.yaml
{{end}}
    - basename: small-deployment
      objectTemplatePath: deployment.yaml
      templateFillMap:
        # DNS Test clients are enabled only in the medium-size deployment.
        EnableDNSTests: false
        ReplicasMin: {{MultiplyInt $smallDeploymentSize $minReplicaFactor}}
        ReplicasMax: {{MultiplyInt $smallDeploymentSize $maxReplicaFactor}}
        SvcName: small-service
        EnableNetworkPolicyEnforcementLatencyTest: {{$ENABLE_NETWORK_POLICY_ENFORCEMENT_LATENCY_TEST}}
        TargetLabelKey: {{$NET_POLICY_ENFORCEMENT_LATENCY_TARGET_LABEL_KEY}}
        TargetLabelValue: {{$NET_POLICY_ENFORCEMENT_LATENCY_TARGET_LABEL_VALUE}}
        NetPolServerOnEveryNthPod: {{$NET_POLICY_SERVER_EVERY_NTH_POD}}
        Image: {{$deploymentImage}}
  - namespaceRange:
      min: 1
      max: {{$namespaces}}
    replicasPerNamespace: {{$smallStatefulSetsPerNamespace}}
    tuningSet: {{$tuningSet}}
    objectBundle:
    - basename: small-statefulset
      objectTemplatePath: statefulset_service.yaml
    - basename: small-statefulset
      objectTemplatePath: statefulset.yaml
      templateFillMap:
        ReplicasMin: {{MultiplyInt $smallStatefulSetSize $minReplicaFactor}}
        ReplicasMax: {{MultiplyInt $smallStatefulSetSize $maxReplicaFactor}}
        Image: {{$statefulSetImage}}
  - namespaceRange:
      min: 1
      max: {{$namespaces}}
    replicasPerNamespace: {{$mediumStatefulSetsPerNamespace}}
    tuningSet: {{$tuningSet}}
    objectBundle:
    - basename: medium-statefulset
      objectTemplatePath: statefulset_service.yaml
    - basename: medium-statefulset
      objectTemplatePath: statefulset.yaml
      templateFillMap:
        ReplicasMin: {{MultiplyInt $mediumStatefulSetSize $minReplicaFactor}}
        ReplicasMax: {{MultiplyInt $mediumStatefulSetSize $maxReplicaFactor}}
        Image: {{$statefulSetImage}}
  - namespaceRange:
      min: 1
      max: {{$namespaces}}
    replicasPerNamespace: {{$smallJobsPerNamespace}}
    tuningSet: {{$tuningSet}}
    objectBundle:
    - basename: small-job
      objectTemplatePath: job.yaml
      templateFillMap:
        Completions: {{MultiplyInt $smallJobSize $completionsFactor}}
        ReplicasMin: {{MultiplyInt $smallJobSize $minReplicaFactor}}
        ReplicasMax: {{MultiplyInt $smallJobSize $maxReplicaFactor}}
        Image: {{$jobImage}}
  - namespaceRange:
      min: 1
      max: {{$namespaces}}
    replicasPerNamespace: {{$mediumJobsPerNamespace}}
    tuningSet: {{$tuningSet}}
    objectBundle:
    - basename: medium-job
      objectTemplatePath: job.yaml
      templateFillMap:
        Completions: {{MultiplyInt $mediumJobSize $completionsFactor}}
        ReplicasMin: {{MultiplyInt $mediumJobSize $minReplicaFactor}}
        ReplicasMax: {{MultiplyInt $mediumJobSize $maxReplicaFactor}}
        Image: {{$jobImage}}
  - namespaceRange:
      min: 1
      max: {{$namespaces}}
    replicasPerNamespace: {{$bigJobsPerNamespace}}
    tuningSet: {{$tuningSet}}
    objectBundle:
    - basename: big-job
      objectTemplatePath: job.yaml
      templateFillMap:
        Completions: {{MultiplyInt $bigJobSize $completionsFactor}}
        ReplicasMin: {{MultiplyInt $bigJobSize $minReplicaFactor}}
        ReplicasMax: {{MultiplyInt $bigJobSize $maxReplicaFactor}}
        Image: {{$jobImage}}
{{if and $is_deleting $ENABLE_PVS}}
  - namespaceRange:
      min: 1
      max: {{$namespaces}}
    replicasPerNamespace: 0
    tuningSet: {{$tuningSet}}
    objectBundle:
  {{range $ssIndex := Loop $pvSmallStatefulSetSize}}
      - basename: pv-small-statefulset-{{$ssIndex}}
        objectTemplatePath: pvc.yaml
        listUnknownObjectOptions:
          labelSelector:
            matchLabels:
              name: small-statefulset-{{$ssIndex}}
  {{end}}
  - namespaceRange:
      min: 1
      max: {{$namespaces}}
    replicasPerNamespace: 0
    tuningSet: {{$tuningSet}}
    objectBundle:
  {{range $ssIndex := Loop $pvMediumStatefulSetSize}}
      - basename: pv-medium-statefulset-{{$ssIndex}}
        objectTemplatePath: pvc.yaml
        listUnknownObjectOptions:
          labelSelector:
            matchLabels:
              name: medium-statefulset-{{$ssIndex}}
  {{end}}
{{end}}

- name: Waiting for '{{$actionName}}' to be completed
  measurements:
  - Method: WaitForControlledPodsRunning
    Instances:
    - Identifier: WaitForRunningDeployments
    - Identifier: WaitForRunningStatefulSets
    - Identifier: WaitForRunningDaemonSets
    - Identifier: WaitForRunningJobs
    Params:
      action: gather
{{if and $is_deleting $ENABLE_PVS}}
  - Identifier: WaitForPVCsToBeDeleted
    Method: WaitForBoundPVCs
    Params:
      desiredPVCCount: 0
      labelSelector: group = load
      timeout: {{$operationTimeout}}
{{end}}
